Repository navigation
Expand file tree
/
Copy pathCombatSystem.ts
More file actions
148 lines (125 loc) · 5.25 KB
/
Copy pathCombatSystem.ts
File metadata and controls
148 lines (125 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import type { CollisionPair, GameState } from '../state'
import type { InputCommand } from '../input'
import { compareCollisionPair } from './CollisionSystem'
const AIM_EPSILON_PX = 0.001
const PROJECTILE_SPEED_PX_PER_SEC = 520
const PROJECTILE_LIFETIME_MS = 1200
const PROJECTILE_RADIUS_PX = 4
export function runCombatSystem(
state: GameState,
commands: InputCommand[],
deltaMs: number,
): void {
const aimCommand = commands.find((command) => command.type === 'aim')
const fireRequested = commands.some((command) => command.type === 'fire')
if (aimCommand && aimCommand.type === 'aim') {
state.player.aimX = aimCommand.x
state.player.aimY = aimCommand.y
}
state.player.weaponCooldownMs = Math.max(
0,
state.player.weaponCooldownMs - deltaMs,
)
if (!fireRequested || state.player.weaponCooldownMs > 0) {
return
}
state.player.weaponCooldownMs = state.player.weaponIntervalMs
state.player.shotsFired += 1
// Compute direction from player center to aim position
const dx = state.player.aimX - state.player.x
const dy = state.player.aimY - state.player.y
const dist = Math.hypot(dx, dy)
let directionX: number
let directionY: number
if (dist < AIM_EPSILON_PX) {
// Same-position fallback: use last saved aim direction (initial default is +X).
directionX = state.player.lastAimDirectionX
directionY = state.player.lastAimDirectionY
} else {
directionX = dx / dist
directionY = dy / dist
// Persist non-zero direction for future same-position fallback.
state.player.lastAimDirectionX = directionX
state.player.lastAimDirectionY = directionY
}
state.projectiles.push({
id: state.nextProjectileId,
x: state.player.x,
y: state.player.y,
radius: PROJECTILE_RADIUS_PX,
directionX,
directionY,
speedPxPerSec: PROJECTILE_SPEED_PX_PER_SEC,
ageMs: 0,
lifetimeMs: PROJECTILE_LIFETIME_MS,
// AC4: snapshot weaponPower at fire time so later changes do not affect existing projectiles.
damage: state.progress.weaponPower,
})
state.nextProjectileId += 1
state.telemetry.lastCommandSummary = `Volley ${state.player.shotsFired} confirmed`
}
/**
* Apply damage and defeat from collision pairs produced by runCollisionSystem.
*
* AC3: New function; existing runCombatSystem signature is unchanged.
* AC7: pairs are already ordered (projectile-enemy first, player-enemy second)
* by the numeric tuple comparator in CollisionSystem.
* AC8: enemy hp = Math.max(0, hp - projectile.damage).
* AC9: defeated = true, defeatedAtTick = state.tick (before tick increment).
* AC10: hit projectiles are removed from state.projectiles (set active=false equivalent).
* AC11: enemies defeated by projectile this tick are skipped in player-enemy processing.
* AC12: player-enemy contact damage applied in enemy id ASC order.
* AC13: player.hp clamped to >= 0.
*/
export function resolveCombatCollisions(
state: GameState,
pairs: readonly CollisionPair[],
): void {
// Collect projectile ids that scored a hit this tick.
const hitProjectileIds = new Set<number>()
// Collect enemy ids defeated by projectile this tick (for AC11).
const projectileDefeatedEnemyIds = new Set<number>()
// --- Pass 1: projectile-enemy (AC7 ordering guaranteed by CollisionSystem) ---
// Sort projectile-enemy pairs by (projectileId ASC) then (enemyId ASC) for determinism.
// Within the same tick, process projectile-enemy before player-enemy.
const projEnemyPairs = pairs
.filter((p): p is Extract<CollisionPair, { kind: 'projectile-enemy' }> => p.kind === 'projectile-enemy')
.sort(compareCollisionPair)
for (const pair of projEnemyPairs) {
// Skip if this projectile already used up on another enemy this tick.
if (hitProjectileIds.has(pair.projectileId)) continue
const enemy = state.enemies.find((e) => e.id === pair.enemyId)
const projectile = state.projectiles.find((p) => p.id === pair.projectileId)
// This tick's collision pair projectiles are always consumed (BLOCKER2).
hitProjectileIds.add(pair.projectileId)
// Defeated enemy: consume projectile but do not apply damage.
if (!enemy || enemy.defeated || !projectile) continue
// AC8: reduce hp
enemy.hp = Math.max(0, enemy.hp - projectile.damage)
// AC9: defeat check
if (enemy.hp === 0) {
enemy.defeated = true
enemy.defeatedAtTick = state.tick
projectileDefeatedEnemyIds.add(enemy.id)
}
}
// AC10: remove hit projectiles from state
state.projectiles = state.projectiles.filter(
(p) => !hitProjectileIds.has(p.id),
)
// --- Pass 2: player-enemy contact damage ---
// AC11: skip enemies defeated by projectile this same tick.
// AC12: process undefeated enemies in id ASC order, accumulate totalDamage.
const playerEnemyPairs = pairs
.filter((p): p is Extract<CollisionPair, { kind: 'player-enemy' }> => p.kind === 'player-enemy')
.filter((p) => !projectileDefeatedEnemyIds.has(p.enemyId))
.sort((a, b) => a.enemyId - b.enemyId)
let totalDamage = 0
for (const pair of playerEnemyPairs) {
const enemy = state.enemies.find((e) => e.id === pair.enemyId)
if (!enemy || enemy.defeated) continue
totalDamage += enemy.contactDamage
}
// AC12, AC13: clamp player hp to >= 0.
state.player.hp = Math.max(0, state.player.hp - totalDamage)
}