Files
tower-defense-prototype/game/tower.odin
T
2026-06-26 18:01:06 -07:00

322 lines
7.6 KiB
Odin

package game
import "core:fmt"
import rl "vendor:raylib"
Tower :: struct {
upgrade_level: int,
position: Vec2,
kind: Tower_Kind,
cooldown: f32,
anchor_gx: int,
anchor_gy: int,
}
Tower_Kind :: enum {
Archer,
Cannon,
Ice,
}
Tower_Archetype :: struct {
kind: Tower_Kind,
range: f32,
damage: int,
fire_rate: f32,
cost: int,
footprint_w: int, // 1 column wide
footprint_h: int, // two rows tall
max_upgrade: int,
upgrade_cost: int,
damage_per_level: int,
range_per_level: f32,
splash_radius: f32,
}
TOWER_ARCHETYPES: [Tower_Kind]Tower_Archetype = {
.Archer = {
kind = .Archer,
range = 50,
damage = 12,
fire_rate = 0.45,
cost = TOWER_COST,
footprint_w = 1,
footprint_h = 2,
max_upgrade = 1,
upgrade_cost = 40,
damage_per_level = 3,
range_per_level = 8,
splash_radius = 0,
},
.Cannon = {
kind = .Cannon,
range = 110,
damage = 35,
fire_rate = 1.2,
cost = 80,
footprint_w = 2,
footprint_h = 2,
upgrade_cost = 40,
damage_per_level = 3,
range_per_level = 8,
splash_radius = 60,
},
.Ice = {
kind = .Ice,
range = 85,
damage = 6,
fire_rate = 0.55,
cost = 65,
footprint_w = 1,
footprint_h = 1,
upgrade_cost = 40,
damage_per_level = 3,
range_per_level = 8,
splash_radius = 0,
},
}
tower_stats_at_level :: proc(tower: Tower) -> (damage: int, range: f32) {
arch := get_archetype(tower.kind)
damage = arch.damage + tower.upgrade_level * arch.damage_per_level
range = arch.range + f32(tower.upgrade_level) * arch.range_per_level
return
}
can_place_tower :: proc(world: ^World, gx, gy: int, kind: Tower_Kind) -> bool {
if world.phase != .Build do return false
arch := get_archetype(kind)
if world.gold < arch.cost do return false
if !footprint_fits_map(&world.world_map, gx, gy, arch.footprint_w, arch.footprint_h) do return false
if footprint_blocked(world, gx, gy, arch.footprint_w, arch.footprint_h) do return false
return true
}
rects_overlap :: proc(
left_a, top_a, width_a, height_a: int,
left_b, top_b, width_b, height_b: int,
) -> bool {
return(
left_a < left_b + width_b &&
left_a + width_a > left_b &&
top_a < top_b + height_b &&
top_a + height_a > top_b \
)
}
footprint_center :: proc(gx, gy, footprint_w, footprint_h: int) -> Vec2 {
return Vec2 {
f32(gx * TILE_SIZE + (footprint_w * TILE_SIZE) / 2),
f32(gy * TILE_SIZE + (footprint_h * TILE_SIZE) / 2),
}
}
footprint_fits_map :: proc(world_map: ^Map, gx, gy, footprint_w, footprint_h: int) -> bool {
if gx < 0 || gy < 0 do return false
if gx + footprint_w > MAP_W do return false
if gy + footprint_h > MAP_H do return false
for column in 0 ..< footprint_h {
for row in 0 ..< footprint_w {
cell_column := gx + column
cell_row := gy + row
if !can_build_at(world_map, cell_column, cell_row) do return false
}
}
return true
}
footprint_blocked :: proc(world: ^World, gx, gy, footprint_w, footprint_h: int) -> bool {
for placed_tower in world.towers {
archetype := get_archetype(placed_tower.kind)
if rects_overlap(
gx,
gy,
footprint_w,
footprint_h,
placed_tower.anchor_gx,
placed_tower.anchor_gy,
archetype.footprint_w,
archetype.footprint_h,
) {
return true
}
}
return false
}
get_archetype :: proc(kind: Tower_Kind) -> Tower_Archetype {
return TOWER_ARCHETYPES[kind]
}
try_upgrade_tower :: proc(world: ^World, gx, gy: int) -> bool {
if world.phase != .Build do return false
t := tower_at_grid(world, gx, gy)
if t == nil do return false
arch := get_archetype(t.kind)
if t.upgrade_level >= arch.max_upgrade do return false
cost := arch.upgrade_cost + t.upgrade_level * 25
if world.gold < cost do return false
world.gold -= cost
t.upgrade_level += 1
return true
}
try_place_tower :: proc(world: ^World, gx, gy: int, kind: Tower_Kind) -> bool {
if !can_place_tower(world, gx, gy, kind) do return false
arch := get_archetype(kind)
world.gold -= arch.cost
append(
&world.towers,
Tower {
position = footprint_center(gx, gy, arch.footprint_w, arch.footprint_h),
anchor_gx = gx,
anchor_gy = gy,
kind = kind,
cooldown = 0,
},
)
return true
}
tower_at_grid :: proc(world: ^World, gx, gy: int) -> ^Tower {
for i in 0 ..< len(world.towers) {
t := &world.towers[i]
arch := get_archetype(t.kind)
if gx >= t.anchor_gx &&
gx < t.anchor_gx + arch.footprint_w &&
gy >= t.anchor_gy &&
gy < t.anchor_gy + arch.footprint_h {
return t
}
}
return nil
}
find_target_slot :: proc(tower: Tower, world: ^World) -> int {
best_slot: int = -1
best_dist: f32 = 999999
for i in 0 ..< MAX_ENEMIES {
e := &world.enemies[i]
if !e.active do continue
if e.path_index >= len(world.path) do continue
d := distance(tower.position, e.position)
_, range := tower_stats_at_level(tower)
if d <= range && d < best_dist {
best_slot = i
best_dist = d
}
}
return best_slot
}
update_towers :: proc(world: ^World, dt: f32) {
for i in 0 ..< len(world.towers) {
t := &world.towers[i]
t.cooldown -= dt
if t.cooldown > 0 do continue
slot := find_target_slot(t^, world)
if slot < 0 do continue
arch := get_archetype(t.kind)
damage, _ := tower_stats_at_level(t^)
switch t.kind {
case .Archer:
spawn_projectile(world, t.position, slot, damage)
case .Cannon:
if target := enemy_at_slot(world, slot); target != nil {
len := distance(t.position, target.position)
if (len > 0.001) {
dir := Vec2 {
(target.position.x - t.position.x) / len,
(target.position.y - t.position.y) / len,
}
spawn_ballistic(world, t.position, dir, damage, arch.splash_radius)
}
}
case .Ice:
if target := enemy_at_slot(world, slot); target != nil {
apply_ice_hit(world, target, damage)
}
}
t.cooldown = arch.fire_rate
}
}
apply_tower_hit :: proc(world: ^World, target: ^Enemy, damage: int) {
target.health -= damage
if target.health <= 0 {
target.active = false
push_event(world, .Enemy_Killed, gold_reward = 5)
}
}
apply_ice_hit :: proc(world: ^World, target: ^Enemy, damage: int) {
apply_tower_hit(world, target, damage)
if target.active do target.slow_timer = ICE_SLOW_DURATION
}
enemy_at_slot :: proc(world: ^World, slot: int) -> ^Enemy {
if slot < 0 || slot >= MAX_ENEMIES do return nil
e := &world.enemies[slot]
if !e.active do return nil
return e
}
render_towers :: proc(world: ^World) {
for t in world.towers {
archetype := get_archetype(t.kind)
pixel_width := f32(archetype.footprint_w * TILE_SIZE)
pixel_height := f32(archetype.footprint_h * TILE_SIZE)
color := rl.Color{255, 203, 0, 255}
if t.kind == .Cannon do color = rl.DARKGRAY
rl.DrawRectangle(
i32(t.position.x - pixel_width * 0.5),
i32(t.position.y - pixel_height * 0.5),
i32(pixel_width),
i32(pixel_height),
color,
)
if t.upgrade_level > 0 {
label := fmt.ctprintf("Lv%d", t.upgrade_level + 1) // or just t.upgrade_level
rl.DrawText(label, i32(t.position.x), i32(t.position.y), 14, rl.WHITE)
}
}
}
render_placement_preview :: proc(world: ^World) {
if world.phase != .Build do return
mouse := game_mouse()
gx, gy := screen_to_grid(mouse.x, mouse.y)
arch := get_archetype(world.selected_tower)
ok := can_place_tower(world, gx, gy, world.selected_tower)
center := footprint_center(gx, gy, arch.footprint_w, arch.footprint_h)
rl.DrawCircleLines(i32(center.x), i32(center.y), arch.range, rl.Fade(rl.SKYBLUE, 0.4))
tint := rl.Fade(rl.GREEN, 0.3)
if !ok do tint = rl.Fade(rl.RED, 0.35)
for row in 0 ..< arch.footprint_h {
for col in 0 ..< arch.footprint_w {
px := (gx + col) * TILE_SIZE
py := (gy + row) * TILE_SIZE
rl.DrawRectangle(i32(px), i32(py), TILE_SIZE, TILE_SIZE, tint)
}
}
}