When this skill is activated, always start your first response with the 🧢 emoji.
Game Design Patterns
Game design patterns solve recurring problems in game development where standard enterprise patterns fall short. Games face unique constraints: real-time frame budgets (16ms at 60fps), thousands of dynamic entities, complex state transitions for AI and player characters, and the need for deterministic replay and undo. This skill covers four foundational patterns - state machines, object pooling, event systems, and the command pattern - that form the backbone of well-architected gameplay code.
When to use this skill
Trigger this skill when the user:
- Needs to model character states, AI behavior, or game phases with a state machine
- Wants to implement object pooling for bullets, particles, enemies, or other frequently spawned entities
- Asks about event systems, message buses, or observer patterns in a game context
- Needs the command pattern for input handling, undo/redo, or action replays
- Is building a game loop and needs architectural guidance on entity management
- Wants to decouple game systems (audio, UI, physics) from gameplay logic
- Asks about managing game state transitions (menus, gameplay, pause, cutscenes)
Do NOT trigger this skill for:
- Rendering, shaders, or graphics programming (not a design pattern concern)
- General software design patterns unrelated to games (use clean-architecture instead)
Key principles
-
Frame budget is law - Every pattern choice must respect the ~16ms frame budget. Allocations during gameplay cause GC spikes. Indirection has cache costs. Always profile before adding abstraction.
-
Decouple, but not infinitely - Game systems should communicate through events and commands rather than direct references, but over-decoupling creates debugging nightmares. One level of indirection is usually enough.
-
State is explicit - Implicit state (nested boolean flags, mode integers) leads to impossible combinations and subtle bugs. Make every valid state a first-class object with defined transitions.
-
Pool what you spawn - Any entity created and destroyed more than once per second should be pooled. The cost of allocation is not the constructor - it is the garbage collector pause 3 seconds later.
-
Commands are data - When input actions are objects rather than direct method calls, you get undo, replay, networking, and AI "for free." The command pattern is the single highest-leverage pattern in gameplay code.
Core concepts
State machines model entities that have distinct behavioral modes. A character can be Idle, Running, Jumping, or Attacking - but never Jumping and Idle at the same time. Each state encapsulates its own update logic, entry/exit behavior, and valid transitions. Hierarchical state machines (HFSM) add nested sub-states for complex AI.
Object pooling pre-allocates a fixed set of objects and recycles them instead of creating and destroying instances at runtime. The pool maintains an "available" list and hands out pre-initialized objects on request, reclaiming them when they are "killed." This eliminates allocation pressure during gameplay.
Event systems (also called observer, pub/sub, or message bus) let game systems
communicate without direct references. When a player takes damage, the health system
fires a DamageTaken event. The UI, audio, camera shake, and analytics systems each
subscribe independently. Adding a new reaction requires zero changes to the damage code.
The command pattern encapsulates an action as an object with execute() and
optionally undo(). Player input becomes a stream of command objects. This enables
input rebinding, replay recording, undo/redo in editors, and sending commands over
the network for multiplayer.
Common tasks
Implement a finite state machine for character behavior
Each state is a class with enter(), update(), exit(), and a transition check.
The machine holds the current state and delegates to it.
interface State {
enter(): void;
update(dt: number): void;
exit(): void;
}
class IdleState implements State {
constructor(private character: Character) {}
enter() { this.character.playAnimation("idle"); }
update(dt: number) {
if (this.character.input.jump) {
this.character.fsm.transition(new JumpState(this.character));
}
}
exit() {}
}
class StateMachine {
private current: State;
transition(next: State) {
this.current.exit();
this.current = next;
this.current.enter();
}
update(dt: number) {
this.current.update(dt);
}
}
Avoid string-based state names. Use typed state classes so the compiler catches invalid transitions.
Build an object pool
Pre-allocate objects at startup. acquire() returns a recycled instance; release()
returns it to the pool. Never allocate during gameplay.
class ObjectPool<T> {
private available: T[] = [];
private active: Set<T> = new Set();
constructor(
private factory: () => T,
private reset: (obj: T) => void,
initialSize: number
) {
for (let i = 0; i < initialSize; i++) {
this.available.push(this.factory());
}
}
acquire(): T | null {
if (this.available.length === 0) return null;
const obj = this.available.pop()!;
this.active.add(obj);
return obj;
}
release(obj: T): void {
if (!this.active.has(obj)) return;
this.active.delete(obj);
this.reset(obj);
this.available.push(obj);
}
}
// Usage: bullet pool
const bulletPool = new ObjectPool(
() => new Bullet(),
(b) => { b.active = false; b.position.set(0, 0); },
200
);
Size the pool to your worst-case burst. If
acquire()returns null, either grow the pool (with a warning log) or skip the spawn - never allocate inline.
Set up a typed event system
Use a type-safe event bus so subscribers know exactly what payload to expect.
type EventMap = {
"damage-taken": { target: Entity; amount: number; source: Entity };
"enemy-killed": { enemy: Entity; killer: Entity; score: number };
"level-complete": { level: number; time: number };
};
class EventBus {
private listeners = new Map<string, Set<Function>>();
on<K extends keyof EventMap>(event: K, handler: (data: EventMap[K]) => void) {
if (!this.listeners.has(event)) this.listeners.set(event, new Set());
this.listeners.get(event)!.add(handler);
return () => this.listeners.get(event)!.delete(handler); // unsubscribe
}
emit<K extends keyof EventMap>(event: K, data: EventMap[K]) {
this.listeners.get(event)?.forEach(fn => fn(data));
}
}
// Usage
const bus = new EventBus();
const unsub = bus.on("damage-taken", ({ target, amount }) => {
healthBar.update(target.id, amount);
});
Always return an unsubscribe function. Leaked subscriptions from destroyed entities are the #1 event system bug in games.
Implement the command pattern for input with undo
Each player action is a command object. Store a history stack for undo.
interface Command {
execute(): void;
undo(): void;
}
class MoveCommand implements Command {
private previousPosition: Vector2;
constructor(private entity: Entity, private direction: Vector2) {}
execute() {
this.previousPosition = this.entity.position.clone();
this.entity.position.add(this.direction);
}
undo() {
this.entity.position.copy(this.previousPosition);
}
}
class CommandHistory {
private history: Command[] = [];
private pointer = -1;
execute(cmd: Command) {
// Discard any redo history
this.history.length = this.pointer + 1;
cmd.execute();
this.history.push(cmd);
this.pointer++;
}
undo() {
if (this.pointer < 0) return;
this.history[this.pointer].undo();
this.pointer--;
}
redo() {
if (this.pointer >= this.history.length - 1) return;
this.pointer++;
this.his