refactor enemy to remove redundancy

This commit is contained in:
2026-06-28 23:36:37 -07:00
parent 2c4527ae8b
commit f2a24a37b8
2 changed files with 43 additions and 48 deletions
+41 -47
View File
@@ -21,39 +21,40 @@ Enemy_Kind :: enum {
Tank,
}
Enemy_Archetype :: struct {
kind: Enemy_Kind,
health: int,
speed: f32,
color: rl.Color,
Enemy_Def :: struct {
base_health: int,
health_per_wave: int,
base_speed: f32,
speed_per_wave: f32,
color: rl.Color,
draw_radius: f32,
}
get_enemy_archetype :: proc(kind: Enemy_Kind, wave: int) -> Enemy_Archetype {
switch kind {
case .Grunt:
return {
kind = .Grunt,
health = 15 + wave * 5,
speed = 55 + f32(wave) * 5,
color = rl.MAROON,
}
case .Runner:
return {
kind = .Runner,
health = 10 + wave * 2,
speed = 95 + f32(wave) * 3,
color = rl.ORANGE,
}
case .Tank:
return {
kind = .Tank,
health = 50 + wave * 12,
speed = 35 + f32(wave) * 2,
color = rl.DARKGREEN,
}
}
return {}
ENEMY_DEF: [Enemy_Kind]Enemy_Def = {
.Grunt = {
base_health = 15,
health_per_wave = 5,
base_speed = 55,
speed_per_wave = 5,
color = rl.MAROON,
draw_radius = 11,
},
.Runner = {
base_health = 10,
health_per_wave = 2,
base_speed = 95,
speed_per_wave = 3,
color = rl.ORANGE,
draw_radius = 11,
},
.Tank = {
base_health = 50,
health_per_wave = 12,
base_speed = 35,
speed_per_wave = 2,
color = rl.DARKGREEN,
draw_radius = 14,
},
}
acquire_enemy :: proc(world: ^World) -> ^Enemy {
@@ -73,12 +74,14 @@ spawn_enemy :: proc(world: ^World, kind: Enemy_Kind) {
e := acquire_enemy(world) // grab a free slot
if e == nil do return // if there are no slots, skil spawning
arch := get_enemy_archetype(kind, world.wave)
def := ENEMY_DEF[kind]
health := def.base_health + world.wave * def.health_per_wave
speed := def.base_speed + f32(world.wave) * def.speed_per_wave
e^ = Enemy {
position = world.path[0],
health = arch.health,
max_health = arch.health,
base_speed = arch.speed,
health = health,
max_health = health,
base_speed = speed,
slow_timer = 0,
path_index = 1,
kind = kind,
@@ -128,18 +131,9 @@ render_enemies :: proc(world: ^World) {
for i in 0 ..< MAX_ENEMIES {
e := world.enemies[i]
if !e.active do continue
color := rl.MAROON
size := 11
switch e.kind {
case .Grunt:
color = rl.MAROON
case .Runner:
color = rl.ORANGE
case .Tank:
color = rl.DARKGREEN
size = 14
}
rl.DrawCircle(i32(e.position.x), i32(e.position.y), f32(size), color)
def := ENEMY_DEF[e.kind]
rl.DrawCircle(i32(e.position.x), i32(e.position.y), def.draw_radius, def.color)
if e.slow_timer > 0 {
rl.DrawCircleLines(i32(e.position.x), i32(e.position.y), 14, rl.SKYBLUE)
}
+2 -1
View File
@@ -124,8 +124,9 @@ test_update_wave_spawns_and_resets_timer :: proc(t: ^testing.T) {
@(test)
test_update_phase_from_build_phase :: proc(t: ^testing.T) {
world := World{}
world.phase = .Build
defer delete(world.events)
world.phase = .Build
update_phase(&world)
testing.expect(t, world.phase == .Build)