Repository navigation
Closed
Labels
agent/implementerAI implementer agentenhancementNew feature or requestphase/implementationImplementation phase
Description
Machine-Readable Contract
contract_schema_version: v1
issue_kind: implementation
parent_issue: "#483"
goal_ref: "M2: Combat MVP Gate — EnemyAISystem で enemy が player へ直線接近する最小 AI を fixed timestep system として実装する"
change_kind: codeParent Issue
Parent Goal Ref
- Goal: M2: Combat MVP Gate (v0.2.x) として 1 sortie を開始→操作→戦闘結果まで通す
- Desired Destination:
src/systems/EnemyAISystem.tsに enemy が player へ直線接近する最小 AI を実装する
Current Validated Scope
src/systems/EnemyAISystem.tsが存在しない(新規作成が必要)src/systems/index.tsに EnemyAISystem の export が未追加tests/enemy-ai-system.test.tsが存在しない(新規作成が必要)- enemy spawn は別 child issue(実装: enemy definition / enemy state / spawn を最小実装する #486)で完了済み
EnemyState.speedPxPerSec/EnemyState.defeated/EnemyState.defeatedAtTickはsrc/state/GameState.tsに定義済み(feat(#486): add EnemyDefinition / EnemyState / EnemySpawnSystem #504)- 既存 systems(MovementSystem / ProjectileSystem)は
fixedDeltaMs(ms 単位)を受け取りdeltaSec = fixedDeltaMs / 1000で変換する設計 - runtime wiring(stepSimulation への EnemyAISystem 接続)は本 Issue のスコープ外
Remaining Parent Gaps
- hitbox / collision / damage(別 child issue)
- sortie state machine / result object(別 child issue)
- runtime wiring:
stepSimulationへの EnemyAISystem 接続(実装: sortie start-end state machine と result object を実装する #489/実装: M2 playable sortie の自動または手動 playtest 証跡を追加する #490 で扱う) - playtest 証跡(別 child issue)
Outcome
runEnemyAISystem(state, fixedDeltaMs) が fixedDeltaMs(ms 単位)を受け取り、fixed timestep delta で各 non-defeated enemy を player center へ向けて直線移動させる。overshoot clamp と defeated skip を実装し、tests/enemy-ai-system.test.ts が PASS し pnpm typecheck / pnpm lint / pnpm test / pnpm build が通っている。
In Scope
src/systems/EnemyAISystem.ts新規作成src/systems/index.tsへの export 追加tests/enemy-ai-system.test.ts新規作成
Out of Scope
- flocking / obstacle avoidance / pathfinding
- target selection(常に player を追う)
- attack animation
- collision 検知(別 Issue)
stepSimulationへの runtime wiring(実装: sortie start-end state machine と result object を実装する #489/実装: M2 playable sortie の自動または手動 playtest 証跡を追加する #490 で扱う)
Acceptance Criteria
- AC1:
runEnemyAISystem(state, fixedDeltaMs)は固定タイムステップをミリ秒単位で受け取る - AC2: 移動量は
deltaSec = fixedDeltaMs / 1000を用いてenemy.speedPxPerSec * deltaSecで計算される - AC3:
defeated === trueの敵は座標を変更しない - AC4: enemy は player center に向かって正規化方向ベクトル
(dx / distance, dy / distance)で移動する - AC5:
distance <= ENEMY_AI_EPSILON_PXのとき(同座標または極近傍)enemy は移動しない(NaN 発生なし) - AC6: 移動量は
Math.min(speedPxPerSec * deltaSec, distance)で clamp され、enemy は player center を overshoot しない - AC7:
EnemyAISystemは DOM / Canvas API /window/document/Date.now/performance.nowに依存しない - AC8:
EnemyAISystemは player / projectile / sortie / result / render state を変更しない - AC9:
tests/enemy-ai-system.test.tsで cardinal 接近・diagonal 接近・overshoot clamp・zero-distance no-op・defeated skip・multi-enemy deterministic update をtoBeCloseToで検証する - AC10:
pnpm typecheck/pnpm lint/pnpm test/pnpm buildが通る
Verification Commands
# AC1
$ test -f src/systems/EnemyAISystem.ts
# AC2
$ grep -n "fixedDeltaMs" src/systems/EnemyAISystem.ts
# AC3
$ grep -n "defeated" src/systems/EnemyAISystem.ts
# AC4
$ grep -n "Math.hypot" src/systems/EnemyAISystem.ts
# AC5
$ grep -n "ENEMY_AI_EPSILON" src/systems/EnemyAISystem.ts
# AC6
$ grep -n "Math.min" src/systems/EnemyAISystem.ts
# AC7
$ grep -rL "Canvas\|document\." src/systems/EnemyAISystem.ts
# AC8
$ test -f tests/enemy-ai-system.test.ts
# AC9
$ pnpm test
# AC10
$ pnpm typecheck
# AC10
$ pnpm lint
# AC10
$ pnpm buildAllowed Paths
src/systems/EnemyAISystem.tssrc/systems/index.tstests/enemy-ai-system.test.ts
Runtime Verification Applicability
decision: not_applicable
reason: "AC coverage は pnpm test で決定論的に検証可能。DOM/Canvas 非依存は grep -rL で確認可能。ブラウザ起動は不要。"Stop Conditions
- Allowed Paths 外の変更が必要と判明した場合
- In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
- 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
- 後続 Phase / 別スコープへの波及が判明した場合
- nested SubAgent delegation が必要になった場合
- 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合
Required Skills
- TypeScript / ECS(state/systems 分離)
- Vitest BDD(
toBeCloseTo使用) - 2D vector math(正規化・zero-length fallback・overshoot clamp)
Scope Delta(任意)
N/A
Metadata
Metadata
Assignees
Labels
agent/implementerAI implementer agentenhancementNew feature or requestphase/implementationImplementation phase
Activity
squne121 commentedon May 30, 2026
判定: REQUEST CHANGES
#486 完了前提に更新すると、#487 はこのまま実装に流すには契約が粗いです。#487 は
EnemyAISystemを作り、敵を player center へ fixed timestep で直線接近させるIssueですが、現状のACは「正規化してspeedPxPerSec * fixedDeltaで移動」「zero-length fallback」「DOM/Canvas禁止」程度に留まっています。#486/#504 でEnemyDefinition/EnemyState/EnemySpawnSystemは追加済みで、EnemyState側にはdefeated系フィールドも入っています。したがって #487 は単なる移動関数ではなく、#488 の collision/damage、#489 の sortie 結果生成に接続される前提で仕様を固めるべきです。([GitHub]1)親 #483 は「60Hz fixed timestep」「systems は DOM/Canvas 非依存」「enemy approach AI は straight only」を明示しており、#487 は #488/#489 の直前工程です。ここで挙動が曖昧だと、後続の命中判定、接触ダメージ、敗北条件が全部ブレます。([GitHub]2)
ブロッカー
1.
fixedDeltaの単位が曖昧で、1000倍バグを誘発する#487 のACは
enemy.speedPxPerSec * fixedDeltaと書いていますが、既存のMovementSystem/ProjectileSystemはdeltaMs / 1000に変換して速度へ掛ける設計です。speedPxPerSecという名前なら、掛けるべき値は秒単位のdeltaSecです。fixedDeltaMsをそのまま掛けると、60Hz想定で約16.666倍ではなく、本来の1000倍進みます。([GitHub]1)修正要求:
Issue本文も
fixedDeltaではなくfixedDeltaMsに統一してください。Unity のfixedDeltaTimeも fixed frame-rate update の秒間隔として扱われる概念なので、単位の明示は必須です。([Unity Docs]3)2. overshoot 防止が未定義
現在のACだと、敵が player に近い場合でも常に
speed * dtだけ進む実装になり、player center を通り過ぎて反対側へ出ます。次tickで逆方向へ戻るため、敵がプレイヤー中心の周囲で振動します。これは collision/contact damage の前提を壊します。Gaffer の fixed timestep 解説でも、シミュレーションは固定dtで進めること自体が重要ですが、移動量の上限や積分挙動を明示しないと挙動は安定しません。#487 では「直線接近」の数式として、距離以下に移動量を clamp する必要があります。([Gaffer On Games]4)
修正要求:
ACに「敵は target を越えて移動しない」「距離が
speed * dt未満なら player center に snap する」を追加してください。3. zero-length fallback が曖昧すぎる
#487 は「zero-length fallback による NaN 防止」を要求していますが、fallback の意味が不明です。敵AIでは、プレイヤーと敵の中心が一致しているなら 移動しない のが自然です。CombatSystem の aim fallback のように last direction や固定方向へ逃がすと、中心一致状態から敵が勝手にずれます。([GitHub]1)
Math.hypotは全要素ゼロなら0を返し、引数にNaNが含まれればNaNを返します。したがって「NaNを出さない」だけでなく、「距離ゼロなら座標不変」「入力が有限値である限り出力も有限値」をテストで固定するべきです。([MDNウェブドキュメント]5)追加テスト:
4.
defeatedenemy を動かすかどうかが未定義#486/#504 で
EnemyStateにdefeated/defeatedAtTickが入った前提なら、#487 は defeated enemy を動かしてはいけません。combat-core spec でも defeated enemy は collision/damage 対象外と整理されています。動き続ける死体は、#488 の hitbox 判定や #489 の kill/result 集計と矛盾します。([GitHub]6)修正要求:
ACに「
defeated === trueの敵は座標を変更しない」を追加してください。5. 実行ループへの統合責務が欠落している
#487 の Allowed Paths は
EnemyAISystem.ts、src/systems/index.ts、テストのみです。しかし既存のstepSimulationはmain.ts側で movement / combat / projectile / collision を呼んでおり、ここにAIを接続しなければ新システムは dead code です。([GitHub]1)どちらかに決めてください。
src/main.tsまたは simulation orchestration の対象ファイルを追加する。敵対的に見ると、現状の #487 は「テストだけ通る未使用モジュール」を量産できる契約です。これは M2 Combat MVP Gate の playable sortie 目標に対して危険です。([GitHub]2)
追加すべきAcceptance Criteria
#487 のACは以下のように差し替えるのが妥当です。
テスト要求
Vitest では浮動小数点の比較に
toBeではなくtoBeCloseToを使うべきです。公式ドキュメントも floating point にはtoBeCloseToを使うよう示しています。([Vitest]7)最低限、このテストを足してください。
Issueコメント案
最終結論
#487 は「直線接近AI」ではなく、「後続の collision/damage/sortie 判定の入力座標を決める中核システム」です。
fixedDeltaMs、overshoot clamp、zero-distance no-op、defeated skip、runtime wiring の扱いをIssue本文に入れてから実装に回してください。squne121 commentedon May 30, 2026
NotebookLM草案
ご提示いただいたIssue #487および「LOOP PROTOCOL」の開発運用設計資料に基づき、実装設計のレビューと草案を作成しました。
提供された資料から直接確認できる既存のソースコードは存在しないため、ゲームループの固定タイムステップ(60Hz)方針 や、GameStateとロジック(System)の分離原則 などのアーキテクチャルールを「事実」とし、具体的なフィールド名や型定義については「既存コードからの推測・確認事項」として分けて記述しています。
1. 実装方針の要約
本Issueの目的は、敵ユニット(enemy)がプレイヤー(player)の中心座標へ向かって直線移動する最小限のAIを、純粋なシミュレーションシステム(System)として実装することです。
ECS(Entity Component System)風の分離アーキテクチャの原則に則り、DOMやCanvasには一切依存せず、固定タイムステップのデルタ時間(
fixedDelta)に基づいて確定的な状態遷移を行う純粋関数(またはそれに準ずる更新処理)として実装します。障害物回避や複雑な経路探索(A*やフローフィールド等)はM2 Combat MVPの範囲外であるため、今回は意図的に実装から除外します。2. 既存コードとの接続点
実際のコードに依存するため、実装前に以下の状態が既存コードに存在するかを確認(推測)してください。
GameState:player(PlayerState),enemies(EnemyStateの配列 または bitECSのコンポーネント定義)が存在すること。PlayerState/EnemyState: 空間座標を示すフィールド(例:x,yまたはposition: { x, y })。EnemyState:speedPxPerSec(1秒あたりの移動ピクセル数)が定義されていること。MovementSystemやSimulationLoopにおけるfixedDeltaの単位が 秒(s: 約0.0166) なのか、ミリ秒(ms: 約16.66) なのかを確認し、speedPxPerSecに掛ける際の単位変換(msの場合は/ 1000)を既存の慣習に必ず合わせてください。3.
EnemyAISystem.tsの推奨API案副作用を持たず、テストが容易な関数として設計します。
updateEnemyAISystem(state: GameState, fixedDelta: number): void(状態を直接ミューテーションする場合。イミュータブルな設計の場合は戻り値をGameStateに変更)state: 現在のゲーム状態全体、またはSystemが要求する状態の一部fixedDelta: 固定タイムステップの経過時間px,py) を取得。ex,ey) について、距離ベクトルdx = px - ex,dy = py - eyを計算。dist = Math.hypot(dx, dy)を算出。dist === 0(完全同座標)の場合は、移動ベクトルを(0, 0)として処理をスキップ(NaNの絶対回避)。dist > 0の場合、正規化ベクトル(dx / dist, dy / dist)に、フレーム移動量(enemy.speedPxPerSec * deltaInSeconds)を掛けて座標を加算する。4. テスト草案(
tests/enemy-ai-system.test.ts)Vitestを用いた純粋ロジックとしての単体テスト を実装します。
pnpm testで完結し、DOMモックを必要としない構成にします。enemyがplayerの座標に向けて1フレーム(例: 16.66ms)で正しいピクセル数分(speedPxPerSecに基づく)接近していること。enemyとplayerの座標が完全に一致している初期状態を与えた場合、エラーやNaNが発生せず、座標が維持されること。window,document,CanvasRenderingContext2DなどのDOM要素が一切使われておらず、純粋なステートのIN/OUTのみでテストがパスすること。5. 失敗しやすい実装パターン(実装者が陥りやすい罠)
以下のパターンに陥らないよう、実装時に注意を払ってください。
speedPxPerSecにミリ秒(16.66)をそのまま掛けてしまい、敵が想定の1000倍の速度でワープするバグ。distが0の時にdx / distを計算し、座標がNaNになる(ゲームがクラッシュ・描画消失する)。dxとdyそれぞれの方向に固定値の速度を足してしまい、斜め移動時の速度が6. 受け入れ条件に対するチェックリスト
GameState.enemiesやspeedPxPerSecが利用可能であるか。src/systems/EnemyAISystem.ts,src/systems/index.ts,tests/enemy-ai-system.test.tsに限定されているか。EnemyAISystem.ts内でCanvas APIやDOM操作が行われていないか。pnpm typecheck,pnpm lint,pnpm test,pnpm buildがすべて成功するか。7. 実装者向けの最小パッチ方針
Allowed Paths のみを対象に、以下の観点で最小限の実装を追加してください。
src/systems/EnemyAISystem.tsupdateEnemyAISystemを定義・エクスポートする。src/systems/index.tsEnemyAISystemのインポートと、パイプライン(メインループ)へのシステム登録を行う。tests/enemy-ai-system.test.ts8. BLOCKED 条件(作業停止・差し戻し基準)
以下のいずれかの条件に該当する場合、実装を進めずに「BLOCKED」とし、親Issueや設計の調整を要求してください。
EnemyStateやspeedPxPerSecフィールドが現在のブランチに存在せず、コンパイルエラーになる場合。GameState.tsなど、状態定義そのものの型やインターフェースを変更しなければ本AIが実装できないと判明した場合(Issueの分割が必要です)。実装・設計の境界定義
EnemyStateの座標を、PlayerStateの座標に向かって、固定タイムステップ毎に速度ベースで直線更新すること。ゼロ距離時の安全処理。Vitestによるテスト作成。squne121 commentedon May 30, 2026
Contract Snapshot (2026-05-30T08:24:12Z)
runEnemyAISystem(state, fixedDeltaMs)が ms 単位の fixed timestep で non-defeated enemy を player center へ直線移動させ、overshoot clamp と defeated skip を実装するsrc/systems/EnemyAISystem.ts/src/systems/index.ts/tests/enemy-ai-system.test.ts.claude/worktrees/issue-487-enemy-approach-ai-fixed-timestep-systemworktree-issue-487-enemy-approach-ai-fixed-timestep-systemsqune121 commentedon May 30, 2026
implement-issue: 実装完了 (2026-05-30T08:28:10Z)
.claude/worktrees/issue-487-enemy-approach-ai-fixed-timestep-systemworktree-issue-487-enemy-approach-ai-fixed-timestep-systemfeat(#487): add EnemyAISystem — fixed-timestep enemy approach AI
fix(#487): add NaN/Infinity guard to EnemyAISystem; strengthen AC8 test
feat(#487): add EnemyAISystem — fixed-timestep enemy approach AI (#508)
squne121 commentedon May 30, 2026
post-merge-cleanup: 完了 (2026-05-30T09:17:13Z)
実行内容
worktree-issue-487-enemy-approach-ai-fixed-timestep-system)