Files
2nd/10_Wiki/Topics/Domain_General/Game_Design/State-Machine-and-Phase-Transition-Events.md
T
Antigravity Agent c24165b8bc refactor(topics): 멀티 에이전트용 지식 재편 — _Common(공통 기본기) + Domain_* 구조
에이전트 8종(대화형/프로그래머 C·S/디자이너/설계자/기획자/QA/PD/PM)에게
[공통 기본 능력 + 롤별 Specialty] 2층으로 지식을 주입하기 위한 재분류.
문서 내용·포맷은 무수정, 폴더 이동만 (6,372개 문서 수 보존 확인).

- Topic_Programming → Domain_Programming (내부 구조 보존)
- Topic_Graphic → Domain_Design
- Topic_Business → Domain_Product
- Topic_General → Domain_General
- _Common 신설: Math(구 Topic_Math_Specialty), Reasoning(구 General/From_Thinking & Reasoning),
  Reasoning_Creativity(구 General/From_창의성), Communication(Poetic_Blog_Writing + From_writing)
- 타 도메인의 From_* 폴더는 유지 (출처 표기일 뿐, 이미 도메인에 맞게 분류된 문서)
- 빈 폴더 정리 (memory/procedures)
- 에이전트→폴더 매핑은 workspace의 .astra/agent-knowledge-map.json (9개 에이전트)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 11:05:56 +09:00

6.2 KiB
Raw Blame History

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-state-machine-and-phase-transiti State Machine and Phase Transition Events 10_Wiki/Topics verified self
FSM
Game State Machine
Phase Events
none A 0.9 applied
game-design
fsm
simulation
architecture
2026-05-10 pending
language framework
typescript xstate

State Machine and Phase Transition Events

매 한 줄

"매 game logic 의 FSM-first 의 architecture". 매 boss phase, match flow, sector breach, AI behavior 의 deterministic state model — 매 transition event 의 explicit emission 으로 telemetry/animation/audio 의 synchronize. 매 2026 standard: XState v5 + actor model + visualizer.

매 핵심

매 정의

  • State: 매 finite labeled status (idle, charging, attacking, dead).
  • Event: external trigger (input, timer, hp_threshold).
  • Transition: state × event → next_state + side_effects.
  • Phase event: cross-cutting broadcast (e.g. "phase_2_started") for multiple subsystems.

매 핵심 패턴

  • Hierarchical states: parent/child (combat → charging → release).
  • Parallel states: 매 동시 sub-FSM (movement + weapon + status).
  • Guards: 매 transition 의 conditional gate.
  • Actions: entry/exit/transition side effects.

매 응용

  1. Boss fight phase logic.
  2. Multiplayer match flow (lobby → loading → playing → results).
  3. Quest/dialogue branching.
  4. Network connection lifecycle.

💻 패턴

XState v5 boss FSM

import { setup, createActor } from 'xstate';

const bossMachine = setup({
  types: { context: {} as { hp: number }, events: {} as { type: 'damage'; amount: number } | { type: 'tick' } },
  actions: {
    broadcastPhase: ({ context }) => emit('phase_event', { phase: context.hp > 5000 ? 1 : context.hp > 2000 ? 2 : 3 }),
  },
  guards: {
    phase2: ({ context }) => context.hp <= 5000 && context.hp > 2000,
    phase3: ({ context }) => context.hp <= 2000 && context.hp > 0,
    dead:   ({ context }) => context.hp <= 0,
  },
}).createMachine({
  id: 'boss',
  initial: 'phase1',
  context: { hp: 10_000 },
  states: {
    phase1: { on: { damage: { actions: 'broadcastPhase', guard: 'phase2', target: 'phase2' } } },
    phase2: { on: { damage: { actions: 'broadcastPhase', guard: 'phase3', target: 'phase3' } } },
    phase3: { on: { damage: { guard: 'dead', target: 'dead' } } },
    dead:   { type: 'final' },
  },
});

export const bossActor = createActor(bossMachine);

Hand-rolled FSM (no deps)

type State = 'idle' | 'charging' | 'attacking' | 'cooldown';
type Event = 'see_target' | 'charge_done' | 'attack_done' | 'lose_target';

const TABLE: Record<State, Partial<Record<Event, State>>> = {
  idle:      { see_target: 'charging' },
  charging:  { charge_done: 'attacking', lose_target: 'idle' },
  attacking: { attack_done: 'cooldown' },
  cooldown:  { charge_done: 'idle' },
};

export class FSM {
  state: State = 'idle';
  send(e: Event) {
    const next = TABLE[this.state][e];
    if (next) { this.exit(this.state); this.state = next; this.enter(next); }
  }
  enter(s: State) { /* hooks */ }
  exit(s: State) { /* hooks */ }
}

Phase event bus

type PhaseEvent =
  | { kind: 'phase_changed'; from: number; to: number; entityId: string }
  | { kind: 'sector_state_changed'; sectorId: string; state: string }
  | { kind: 'match_state_changed'; matchId: string; state: string };

class EventBus {
  private listeners = new Map<string, Array<(e: PhaseEvent) => void>>();
  emit(e: PhaseEvent) { for (const fn of this.listeners.get(e.kind) ?? []) fn(e); }
  on(kind: PhaseEvent['kind'], fn: (e: PhaseEvent) => void) {
    const arr = this.listeners.get(kind) ?? [];
    arr.push(fn); this.listeners.set(kind, arr);
  }
}
export const bus = new EventBus();

Guard with cooldown

export function withCooldown(ms: number) {
  let last = 0;
  return () => {
    const now = Date.now();
    if (now - last < ms) return false;
    last = now;
    return true;
  };
}

Parallel states (movement + weapon)

import { setup, createMachine } from 'xstate';

const player = createMachine({
  id: 'player',
  type: 'parallel',
  states: {
    movement: {
      initial: 'idle',
      states: {
        idle: { on: { move: 'running' } },
        running: { on: { stop: 'idle' } },
      },
    },
    weapon: {
      initial: 'unarmed',
      states: {
        unarmed: { on: { equip: 'armed' } },
        armed: { on: { fire: 'cooldown' } },
        cooldown: { after: { 500: 'armed' } },
      },
    },
  },
});

FSM 의 telemetry

export function logTransition(from: string, to: string, event: string) {
  console.log(JSON.stringify({ ts: Date.now(), kind: 'fsm.transition', from, to, event }));
}

매 결정 기준

상황 Approach
매 simple 3-state Hand-rolled enum + switch.
매 hierarchical / parallel XState v5 + visualizer.
매 networked sync Authoritative server FSM + delta replication.
매 hot path AI Behavior Tree (FSM 의 not 적합).

기본값: XState v5 for non-trivial; enum FSM for simple.

🔗 Graph

🤖 LLM 활용

언제: FSM 의 design review, unreachable state 의 detect, transition coverage 의 audit. 언제 X: 매 hot loop transition (deterministic code).

안티패턴

  • Boolean flag soup: 매 implicit state — combinatorial bug surface.
  • Missing exit action: 매 leak 의 timer/listener.
  • Unbounded events: 매 event queue 의 backlog — frame stall.

🧪 검증 / 중복

  • Verified: XState v5 docs (2025), GDC AI Summit talks, Unreal Behavior Tree docs.
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — XState v5 boss + parallel state + bus 추가