programming-architecture
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGame Programming Architecture
游戏编程架构
Design Patterns for Games
游戏设计模式
1. State Machine
1. 状态机
Best for: Character states, AI, game flow
csharp
// ✅ Production-Ready State Machine
public abstract class State<T> where T : class
{
protected T Context { get; private set; }
public void SetContext(T context) => Context = context;
public virtual void Enter() { }
public virtual void Update() { }
public virtual void Exit() { }
}
public class StateMachine<T> where T : class
{
private State<T> _current;
private readonly T _context;
public StateMachine(T context) => _context = context;
public void ChangeState(State<T> newState)
{
_current?.Exit();
_current = newState;
_current.SetContext(_context);
_current.Enter();
}
public void Update() => _current?.Update();
}
// Usage
public class PlayerIdleState : State<Player>
{
public override void Enter() => Context.Animator.Play("Idle");
public override void Update()
{
if (Context.Input.magnitude > 0.1f)
Context.StateMachine.ChangeState(new PlayerMoveState());
}
}适用场景:角色状态、AI、游戏流程
csharp
// ✅ Production-Ready State Machine
public abstract class State<T> where T : class
{
protected T Context { get; private set; }
public void SetContext(T context) => Context = context;
public virtual void Enter() { }
public virtual void Update() { }
public virtual void Exit() { }
}
public class StateMachine<T> where T : class
{
private State<T> _current;
private readonly T _context;
public StateMachine(T context) => _context = context;
public void ChangeState(State<T> newState)
{
_current?.Exit();
_current = newState;
_current.SetContext(_context);
_current.Enter();
}
public void Update() => _current?.Update();
}
// Usage
public class PlayerIdleState : State<Player>
{
public override void Enter() => Context.Animator.Play("Idle");
public override void Update()
{
if (Context.Input.magnitude > 0.1f)
Context.StateMachine.ChangeState(new PlayerMoveState());
}
}2. Object Pool
2. 对象池
Best for: Bullets, particles, enemies
csharp
// ✅ Production-Ready Object Pool
public class ObjectPool<T> where T : Component
{
private readonly Queue<T> _pool = new();
private readonly T _prefab;
private readonly Transform _parent;
public ObjectPool(T prefab, int initialSize, Transform parent = null)
{
_prefab = prefab;
_parent = parent;
for (int i = 0; i < initialSize; i++)
_pool.Enqueue(CreateInstance());
}
public T Get(Vector3 position)
{
var obj = _pool.Count > 0 ? _pool.Dequeue() : CreateInstance();
obj.transform.position = position;
obj.gameObject.SetActive(true);
return obj;
}
public void Return(T obj)
{
obj.gameObject.SetActive(false);
_pool.Enqueue(obj);
}
private T CreateInstance() => Object.Instantiate(_prefab, _parent);
}适用场景:子弹、粒子特效、敌人
csharp
// ✅ Production-Ready Object Pool
public class ObjectPool<T> where T : Component
{
private readonly Queue<T> _pool = new();
private readonly T _prefab;
private readonly Transform _parent;
public ObjectPool(T prefab, int initialSize, Transform parent = null)
{
_prefab = prefab;
_parent = parent;
for (int i = 0; i < initialSize; i++)
_pool.Enqueue(CreateInstance());
}
public T Get(Vector3 position)
{
var obj = _pool.Count > 0 ? _pool.Dequeue() : CreateInstance();
obj.transform.position = position;
obj.gameObject.SetActive(true);
return obj;
}
public void Return(T obj)
{
obj.gameObject.SetActive(false);
_pool.Enqueue(obj);
}
private T CreateInstance() => Object.Instantiate(_prefab, _parent);
}3. Observer Pattern (Events)
3. 观察者模式(事件)
Best for: UI updates, achievements, damage notifications
csharp
// ✅ Production-Ready Event System
public static class GameEvents
{
public static event Action<int> OnScoreChanged;
public static event Action<float> OnHealthChanged;
public static event Action OnPlayerDied;
public static void ScoreChanged(int score) => OnScoreChanged?.Invoke(score);
public static void HealthChanged(float health) => OnHealthChanged?.Invoke(health);
public static void PlayerDied() => OnPlayerDied?.Invoke();
}
// Subscribe
GameEvents.OnScoreChanged += UpdateScoreUI;
// Always unsubscribe in OnDestroy
private void OnDestroy() => GameEvents.OnScoreChanged -= UpdateScoreUI;适用场景:UI更新、成就解锁、伤害通知
csharp
// ✅ Production-Ready Event System
public static class GameEvents
{
public static event Action<int> OnScoreChanged;
public static event Action<float> OnHealthChanged;
public static event Action OnPlayerDied;
public static void ScoreChanged(int score) => OnScoreChanged?.Invoke(score);
public static void HealthChanged(float health) => OnHealthChanged?.Invoke(health);
public static void PlayerDied() => OnPlayerDied?.Invoke();
}
// Subscribe
GameEvents.OnScoreChanged += UpdateScoreUI;
// Always unsubscribe in OnDestroy
private void OnDestroy() => GameEvents.OnScoreChanged -= UpdateScoreUI;4. Command Pattern
4. 命令模式
Best for: Undo/redo, input replay, networking
csharp
public interface ICommand
{
void Execute();
void Undo();
}
public class MoveCommand : ICommand
{
private readonly Transform _target;
private readonly Vector3 _direction;
private Vector3 _previousPosition;
public MoveCommand(Transform target, Vector3 direction)
{
_target = target;
_direction = direction;
}
public void Execute()
{
_previousPosition = _target.position;
_target.position += _direction;
}
public void Undo() => _target.position = _previousPosition;
}适用场景:撤销/重做、输入重放、网络同步
csharp
public interface ICommand
{
void Execute();
void Undo();
}
public class MoveCommand : ICommand
{
private readonly Transform _target;
private readonly Vector3 _direction;
private Vector3 _previousPosition;
public MoveCommand(Transform target, Vector3 direction)
{
_target = target;
_direction = direction;
}
public void Execute()
{
_previousPosition = _target.position;
_target.position += _direction;
}
public void Undo() => _target.position = _previousPosition;
}Architecture Layers
架构分层
┌─────────────────────────────────────────────────────────────┐
│ GAME LAYER │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Managers (GameManager, UIManager, AudioManager) │ │
│ └─────────────────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Systems (Combat, Movement, Inventory, Quest) │ │
│ └─────────────────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Components (Health, Weapon, CharacterController) │ │
│ └─────────────────────────────────────────────────────┘ │
├─────────────────────────────────────────────────────────────┤
│ ENGINE LAYER │
│ Physics │ Rendering │ Audio │ Input │ Networking │
└─────────────────────────────────────────────────────────────┘┌─────────────────────────────────────────────────────────────┐
│ 游戏层 │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ 管理器(GameManager、UIManager、AudioManager) │ │
│ └─────────────────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ 系统(战斗、移动、背包、任务) │ │
│ └─────────────────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ 组件(生命值、武器、CharacterController) │ │
│ └─────────────────────────────────────────────────────┘ │
├─────────────────────────────────────────────────────────────┤
│ 引擎层 │
│ 物理系统 | 渲染系统 | 音频系统 | 输入系统 | 网络系统 │
└─────────────────────────────────────────────────────────────┘🔧 Troubleshooting
🔧 问题排查
┌─────────────────────────────────────────────────────────────┐
│ PROBLEM: Spaghetti code / tight coupling │
├─────────────────────────────────────────────────────────────┤
│ SOLUTIONS: │
│ → Use dependency injection │
│ → Communicate via events, not direct references │
│ → Follow single responsibility principle │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ PROBLEM: Hard to test game systems │
├─────────────────────────────────────────────────────────────┤
│ SOLUTIONS: │
│ → Separate logic from MonoBehaviour │
│ → Use interfaces for dependencies │
│ → Create testable pure functions │
└─────────────────────────────────────────────────────────────┘┌─────────────────────────────────────────────────────────────┐
│ 问题:面条代码 / 紧耦合 │
├─────────────────────────────────────────────────────────────┤
│ 解决方案: │
│ → 使用依赖注入 │
│ → 通过事件通信,而非直接引用 │
│ → 遵循单一职责原则 │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ 问题:游戏系统难以测试 │
├─────────────────────────────────────────────────────────────┤
│ 解决方案: │
│ → 将逻辑与MonoBehaviour分离 │
│ → 为依赖项使用接口 │
│ → 创建可测试的纯函数 │
└─────────────────────────────────────────────────────────────┘Best Practices
最佳实践
| Practice | Benefit |
|---|---|
| Loose coupling | Systems can change independently |
| Data-driven design | Balance without recompiling |
| Interface abstractions | Easy mocking and testing |
| Single responsibility | Clear, focused classes |
Use this skill: When architecting systems, improving code structure, or optimizing performance.
| 实践方法 | 优势 |
|---|---|
| 松耦合 | 系统可独立修改 |
| 数据驱动设计 | 无需重新编译即可调整参数 |
| 接口抽象 | 便于模拟和测试 |
| 单一职责 | 类职责清晰、聚焦 |
使用场景:在架构系统、优化代码结构或提升性能时使用。