Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ let state = createInitialGameState(loadResult.ok ? loadResult.snapshot ?? undefi
const renderer = createCanvasRenderer(canvas)
const hud = createHudController(commandRail, {
onQuickSave() {
if (state.loopPhase !== 'preparation') {
return
}

const saveResult = storage.save(createGameSnapshot(state))
if (!saveResult.ok) {
reportStorageFailure('save', saveResult)
}
},
onReset() {
state = createInitialGameState()
startSortie(state, defaultSimulationConfig.fixedDeltaMs)
resizeArena(state)
},
})
Expand All @@ -79,8 +82,8 @@ bindInput(canvas, inputState, () => state.arena)
resizeArena(state)
window.addEventListener('resize', () => resizeArena(state))

// M2 bootstrap: start sortie once after initialisation (AC12)
startSortie(state, defaultSimulationConfig.fixedDeltaMs)
maybeAutoStartRuntime()


// E2E compile-time fixture overrides — only active in VITE_E2E_MODE builds.
// These are injected via page.addInitScript() in Playwright tests before the
Expand All @@ -91,7 +94,7 @@ if (import.meta.env.VITE_E2E_MODE === 'true') {
__E2E_PLAYER_HP_OVERRIDE__?: number
}
// Short sortie: override targetTicks to ~0.5s for deterministic timeout E2E
if (e2eFlags.__E2E_SHORT_SORTIE__ === true && state.sortie.status === 'running') {
if (e2eFlags.__E2E_SHORT_SORTIE__ === true && state.loopPhase === 'running' && state.sortie.status === 'running') {
state.sortie.targetTicks = Math.ceil(500 / defaultSimulationConfig.fixedDeltaMs)
}
// Player HP override: set hp/maxHp for deterministic defeat E2E
Expand Down Expand Up @@ -128,6 +131,16 @@ function stepSimulation(fixedDeltaMs: number): void {
runSortieSimulationStep(state, commands, fixedDeltaMs)
}

function maybeAutoStartRuntime(): void {
if (import.meta.env.VITE_E2E_MODE !== 'true') {
return
}

if (state.loopPhase === 'preparation' && state.sortie.status === 'idle') {
startSortie(state, defaultSimulationConfig.fixedDeltaMs)
}
}

function resizeArena(currentState: typeof state): void {
const safeSidebar = window.innerWidth > 980 ? 380 : 32
const width = Math.min(960, Math.max(640, window.innerWidth - safeSidebar))
Expand Down Expand Up @@ -158,6 +171,7 @@ function reportStorageFailure(
interface LoopE2ESnapshot {
tick: number
elapsedMs: number
loopPhase: 'preparation' | 'running' | 'debrief_pending_reward' | 'debrief_reward_claimed'
player: {
x: number
y: number
Expand Down Expand Up @@ -199,14 +213,18 @@ if (import.meta.env.VITE_E2E_MODE === 'true') {
;(
window as Window &
typeof globalThis & {
__LOOP_E2E__: { getState: () => LoopE2ESnapshot }
__LOOP_E2E__: { getState: () => LoopE2ESnapshot; startSortie: () => void }
}
).__LOOP_E2E__ = {
startSortie() {
startSortie(state, defaultSimulationConfig.fixedDeltaMs)
},
/** Returns a shallow snapshot copy of the current game + input state. Read-only. */
getState(): LoopE2ESnapshot {
return {
tick: state.tick,
elapsedMs: state.elapsedMs,
loopPhase: state.loopPhase,
player: {
x: state.player.x,
y: state.player.y,
Expand Down
7 changes: 7 additions & 0 deletions src/state/GameState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export interface TelemetryState {
// ---------------------------------------------------------------------------

export type SortieStatus = 'idle' | 'running' | 'victory' | 'defeat' | 'timeout' | 'ended'
export type LoopPhase = 'preparation' | 'running' | 'debrief_pending_reward' | 'debrief_reward_claimed'

/**
* Reason why a sortie ended. Discriminates the three terminal conditions.
Expand Down Expand Up @@ -169,6 +170,9 @@ export type SortieState =
export interface GameState {
tick: number
elapsedMs: number
loopPhase: LoopPhase
pendingRewardApplicationId: RewardApplicationId | null
nextRewardApplicationSequence: number
arena: ArenaState
player: PlayerState
projectiles: ProjectileState[]
Expand Down Expand Up @@ -199,6 +203,9 @@ export function createInitialGameState(
return {
tick: 0,
elapsedMs: 0,
loopPhase: 'preparation',
pendingRewardApplicationId: null,
nextRewardApplicationSequence: 1,
arena: {
width: 960,
height: 540,
Expand Down
81 changes: 75 additions & 6 deletions src/systems/SortieSystem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GameState, SortieResult, SortieState } from '../state/GameState'
import type { GameState, RewardApplicationId, SortieResult, SortieState } from '../state/GameState'
import { mapInputToCommands } from '../input'
import { runMovementSystem } from './MovementSystem'
import { runEnemySpawnSystem } from './EnemySpawnSystem'
Expand All @@ -7,24 +7,89 @@ import { runCombatSystem } from './CombatSystem'
import { runProjectileSystem } from './ProjectileSystem'
import { runCollisionSystem } from './CollisionSystem'
import { resolveCombatCollisions } from './CombatSystem'
import { RewardSystem } from './RewardSystem'

/** Total sortie duration in milliseconds (30 seconds). */
export const SORTIE_DURATION_MS = 30_000
function buildRewardApplicationId(state: GameState): RewardApplicationId {
let nextSequence = Math.max(1, state.nextRewardApplicationSequence)
let applicationId: RewardApplicationId = `sortie-reward-${nextSequence}`

while (Object.prototype.hasOwnProperty.call(state.rewardClaims.claimedApplicationIds, applicationId)) {
nextSequence += 1
applicationId = `sortie-reward-${nextSequence}`
}

state.nextRewardApplicationSequence = nextSequence + 1
return applicationId
}

function resetCombatRuntime(state: GameState): void {
state.tick = 0
state.elapsedMs = 0
state.player.x = 240
state.player.y = 270
state.player.hp = state.player.maxHp
state.player.aimX = 540
state.player.aimY = 270
state.player.weaponCooldownMs = 0
state.player.shotsFired = 0
state.player.lastAimDirectionX = 1
state.player.lastAimDirectionY = 0
state.projectiles = []
state.nextProjectileId = 1
state.enemies = []
state.nextEnemyId = 1
}

export function claimPendingReward(
state: GameState,
): ReturnType<typeof RewardSystem.claim> | { ok: false; reason: 'no-pending-reward' | 'claimed-phase-ledger-miss' } {
const applicationId = state.pendingRewardApplicationId
if (applicationId === null || state.sortie.result === null) {
return { ok: false, reason: 'no-pending-reward' }
}

if (state.loopPhase === 'debrief_reward_claimed') {
if (Object.prototype.hasOwnProperty.call(state.rewardClaims.claimedApplicationIds, applicationId)) {
return { ok: false, reason: 'already-claimed' }
}

return { ok: false, reason: 'claimed-phase-ledger-miss' }
}

if (state.loopPhase !== 'debrief_pending_reward') {
return { ok: false, reason: 'no-pending-reward' }
}

const claimResult = RewardSystem.claim(state, applicationId, state.sortie.result)
if (!claimResult.ok && claimResult.reason !== 'already-claimed') {
return claimResult
}

state.loopPhase = 'debrief_reward_claimed'
return claimResult
}


/**
* Initialises the sortie state machine from `idle` to `running`.
* Must be called exactly once after `createInitialGameState()`.
* Initialises the sortie state machine from `preparation` or claimed debrief to `running`.
*
* @param state Mutable game state
* @param fixedDeltaMs Fixed timestep in milliseconds (used to compute targetTicks)
*/
export function startSortie(state: GameState, fixedDeltaMs: number): void {
if (state.sortie.status !== 'idle') {
const canStart =
state.loopPhase === 'preparation' || state.loopPhase === 'debrief_reward_claimed'
if (!canStart) {
return
}

resetCombatRuntime(state)
const targetTicks = Math.ceil(SORTIE_DURATION_MS / fixedDeltaMs)

state.loopPhase = 'running'
state.pendingRewardApplicationId = null
state.sortie = {
status: 'running',
elapsedTicks: 0,
Expand All @@ -50,7 +115,7 @@ export function startSortie(state: GameState, fixedDeltaMs: number): void {
*/
export function runSortieSystem(state: GameState, fixedDeltaMs: number): void {
// AC13: terminal gate — only advance if running
if (state.sortie.status !== 'running') {
if (state.loopPhase !== 'running' || state.sortie.status !== 'running') {
return
}

Expand Down Expand Up @@ -130,6 +195,10 @@ export function runSortieSystem(state: GameState, fixedDeltaMs: number): void {
}

state.sortie = terminalState
state.loopPhase = 'debrief_pending_reward'
if (state.pendingRewardApplicationId === null) {
state.pendingRewardApplicationId = buildRewardApplicationId(state)
}
}

/**
Expand All @@ -145,7 +214,7 @@ export function runSortieSimulationStep(
commands: ReturnType<typeof mapInputToCommands>,
fixedDeltaMs: number,
): void {
if (state.sortie.status !== 'running') return
if (state.loopPhase !== 'running' || state.sortie.status !== 'running') return
runMovementSystem(state, commands, fixedDeltaMs)
runEnemySpawnSystem(state)
runEnemyAISystem(state, fixedDeltaMs)
Expand Down
Loading
Loading