game-developer
You are a game development specialist with expertise in Unity, Unreal Engine, game mechanics design, physics systems, and multiplayer. Use when: game engine...
Documentation & Preview
Game Developer — Code Examples
Example 1
// GameCore.cs - Unity Core Game Architecture
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Unity.Netcode;
using Unity.Collections;
using Unity.Jobs;
using Unity.Burst;
using Unity.Mathematics;
namespace GameFramework
{
// Game State Management
public enum GameState
{
MainMenu,
Loading,
Playing,
Paused,
GameOver,
Victory
}
public class GameManager : NetworkBehaviour
{
private static GameManager _instance;
public static GameManager Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<GameManager>();
if (_instance == null)
{
GameObject go = new GameObject("GameManager");
_instance = go.AddComponent<GameManager>();
DontDestroyOnLoad(go);
}
}
return _instance;
}
}
[Header("Game Configuration")]
[SerializeField] private GameConfig gameConfig;
[SerializeField] private GameState currentState = GameState.MainMenu;
[Header("Events")]
public UnityEvent<GameState> OnGameStateChanged;
public UnityEvent<int> OnScoreChanged;
public UnityEvent<float> OnHealthChanged;
private Dictionary<string, object> gameData = new Dictionary<string, object>();
private SaveSystem saveSystem;
private InputManager inputManager;
private AudioManager audioManager;
private PoolManager poolManager;
protected override void Awake()
{
base.Awake();
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
_instance = this;
DontDestroyOnLoad(gameObject);
InitializeSystems();
}
private void InitializeSystems()
{
saveSystem = new SaveSystem();
inputManager = GetComponent<InputManager>() ?? gameObject.AddComponent<InputManager>();
audioManager = GetComponent<AudioManager>() ?? gameObject.AddComponent<AudioManager>();
poolManager = GetComponent<PoolManager>() ?? gameObject.AddComponent<PoolManager>();
// Initialize networking if needed
if (gameConfig.isMultiplayer)
{
InitializeNetworking();
}
}
private void InitializeNetworking()
{
NetworkManager networkManager = GetComponent<NetworkManager>();
if (networkManager == null)
{
networkManager = gameObject.AddComponent<NetworkManager>();
}
// Configure network settings
networkManager.NetworkConfig.PlayerPrefab = gameConfig.playerPrefab;
networkManager.NetworkConfig.NetworkTransport = gameConfig.networkTransport;
}
public void ChangeGameState(GameState newState)
{
if (currentState == newState) return;
// Exit current state
ExitState(currentState);
// Enter new state
currentState = newState;
EnterState(newState);
// Notify listeners
OnGameStateChanged?.Invoke(newState);
// Sync state across network
if (IsServer && gameConfig.isMultiplayer)
{
SyncGameStateClientRpc(newState);
}
}
[ClientRpc]
private void SyncGameStateClientRpc(GameState newState)
{
if (!IsServer)
{
currentState = newState;
OnGameStateChanged?.Invoke(newState);
}
}
private void EnterState(GameState state)
{
switch (state)
{
case GameState.MainMenu:
Time.timeScale = 1f;
inputManager.SetInputMode(InputMode.Menu);
audioManager.PlayMusic("MainMenu");
break;
case GameState.Loading:
StartCoroutine(LoadGameResources());
break;
case GameState.Playing:
Time.timeScale = 1f;
inputManager.SetInputMode(InputMode.Gameplay);
audioManager.PlayMusic("Gameplay");
break;
case GameState.Paused:
Time.timeScale = 0f;
inputManager.SetInputMode(InputMode.Menu);
break;
case GameState.GameOver:
Time.timeScale = 0.5f;
SaveHighScore();
ShowGameOverUI();
break;
}
}
private void ExitState(GameState state)
{
switch (state)
{
case GameState.Paused:
Time.timeScale = 1f;
break;
}
}
private System.Collections.IEnumerator LoadGameResources()
{
float progress = 0f;
// Load essential resources
yield return LoadResourceAsync("Prefabs", p => progress = p * 0.3f);
yield return LoadResourceAsync("Materials", p => progress = 0.3f + p * 0.2f);
yield return LoadResourceAsync("Audio", p => progress = 0.5f + p * 0.2f);
yield return LoadResourceAsync("UI", p => progress = 0.7f + p * 0.3f);
// Initialize game world
yield return InitializeGameWorld();
ChangeGameState(GameState.Playing);
}
private System.Collections.IEnumerator LoadResourceAsync(string path, Action<float> onProgress)
{
var request = Resources.LoadAsync(path);
while (!request.isDone)
{
onProgress?.Invoke(request.progress);
yield return null;
}
}
private System.Collections.IEnumerator InitializeGameWorld()
{
// Spawn environment
SpawnEnvironment();
yield return null;
// Spawn players
if (gameConfig.isMultiplayer)
{
SpawnNetworkPlayers();
}
else
{
SpawnLocalPlayer();
}
yield return null;
// Initialize AI
InitializeAI();
yield return null;
}
private void SaveHighScore()
{
int currentScore = (int)gameData.GetValueOrDefault("Score", 0);
int highScore = PlayerPrefs.GetInt("HighScore", 0);
if (currentScore > highScore)
{
PlayerPrefs.SetInt("HighScore", currentScore);
PlayerPrefs.Save();
}
}
private void ShowGameOverUI()
{
// Implementation for game over UI
}
private void SpawnEnvironment()
{
// Implementation for environment spawning
}
private void SpawnNetworkPlayers()
{
// Implementation for network player spawning
}
private void SpawnLocalPlayer()
{
GameObject player = Instantiate(gameConfig.playerPrefab);
player.transform.position = gameConfig.playerSpawnPoint;
}
private void InitializeAI()
{
// Implementation for AI initialization
}
}
// Advanced Character Controller
[RequireComponent(typeof(CharacterController))]
public class AdvancedCharacterController : NetworkBehaviour
{
[Header("Movement Settings")]
[SerializeField] private float walkSpeed = 5f;
[SerializeField] private float runSpeed = 10f;
[SerializeField] private float jumpHeight = 2f;
[SerializeField] private float gravity = -9.81f;
[SerializeField] private float airControl = 0.3f;
[Header("Advanced Movement")]
[SerializeField] private float dashDistance = 5f;
[SerializeField] private float dashCooldown = 1f;
[SerializeField] private float wallJumpForce = 7f;
[SerializeField] private float slideSpeed = 15f;
[SerializeField] private float climbSpeed = 3f;
[Header("Ground Check")]
[SerializeField] private Transform groundCheck;
[SerializeField] private float groundDistance = 0.4f;
[SerializeField] private LayerMask groundMask;
private CharacterController controller;
private Vector3 velocity;
private bool isGrounded;
private bool isWallRunning;
private bool isSliding;
private bool isClimbing;
private float lastDashTime;
private Vector3 wallNormal;
// Network variables
private NetworkVariable<Vector3> networkPosition = new NetworkVariable<Vector3>();
private NetworkVariable<Quaternion> networkRotation = new NetworkVariable<Quaternion>();
// Input buffer for smoother controls
private Queue<InputCommand> inputBuffer = new Queue<InputCommand>();
private const int MaxInputBufferSize = 10;
private void Awake()
{
controller = GetComponent<CharacterController>();
}
public override void OnNetworkSpawn()
{
if (IsOwner)
{
// Setup local player controls
InputManager.Instance.OnMoveInput += HandleMoveInput;
InputManager.Instance.OnJumpInput += HandleJumpInput;
InputManager.Instance.OnDashInput += HandleDashInput;
}
}
private void Update()
{
if (!IsOwner && gameConfig.isMultiplayer)
{
// Interpolate position for non-owners
transform.position = Vector3.Lerp(transform.position, networkPosition.Value, Time.deltaTime * 10f);
transform.rotation = Quaternion.Lerp(transform.rotation, networkRotation.Value, Time.deltaTime * 10f);
return;
}
// Ground check
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
// Process input buffer
ProcessInputBuffer();
// Apply movement
ApplyMovement();
// Sync position across network
if (IsOwner && gameConfig.isMultiplayer)
{
UpdateNetworkPositionServerRpc(transform.position, transform.rotation);
}
}
private void ProcessInputBuffer()
{
while (inputBuffer.Count > 0 && inputBuffer.Count <= MaxInputBufferSize)
{
InputCommand command = inputBuffer.Dequeue();
ExecuteCommand(command);
}
}
private void ExecuteCommand(InputCommand command)
{
switch (command.type)
{
case InputType.Move:
ApplyMovementInput(command.moveDirection);
break;
case InputType.Jump:
PerformJump();
break;
case InputType.Dash:
PerformDash(command.dashDirection);
break;
}