From d29b4f07eae7eee1caab8cb8e952db0fe01a7300 Mon Sep 17 00:00:00 2001 From: codegirl-007 Date: Sun, 28 Jun 2026 00:42:50 -0700 Subject: [PATCH] clean up get_archtype for towers and build_controls_layout function, replacing with constants --- game/controls.odin | 50 ++++++++++++++++++++------------------- game/tower.odin | 12 +++------- game/tower_placement.odin | 12 +++++----- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/game/controls.odin b/game/controls.odin index d4a995a..bcc0876 100644 --- a/game/controls.odin +++ b/game/controls.odin @@ -3,20 +3,26 @@ package game import "core:fmt" import rl "vendor:raylib" -bottom_bar_y :: proc() -> i32 { - return SCREEN_H - CONTROL_BAR_H +BOTTOM_BAR_Y :: SCREEN_H - CONTROL_BAR_H +ARCHER_BUTTON_W :: 120 +START_WAVE_BUTTON_W :: 110 +CONTROLS_BUTTON_GAP :: 12 +CONTROLS_BUTTON_H :: 24 +CONTROLS_BUTTON_Y :: BOTTOM_BAR_Y + 8 +CONTROLS_BUTTON_X :: (SCREEN_W - (ARCHER_BUTTON_W + CONTROLS_BUTTON_GAP + START_WAVE_BUTTON_W)) / 2 + +ARCHER_BUTTON_RECT :: rl.Rectangle { + f32(CONTROLS_BUTTON_X), + f32(CONTROLS_BUTTON_Y), + ARCHER_BUTTON_W, + CONTROLS_BUTTON_H, } -build_controls_layout :: proc() -> (archer, start_wave_btn: rl.Rectangle) { - bar_y := f32(bottom_bar_y()) - arch_w: f32 = 120 - wave_w: f32 = 110 - gap: f32 = 12 - - x := f32((SCREEN_W - int(arch_w + gap + wave_w)) / 2) - archer = {x, bar_y + 8, arch_w, 24} - start_wave_btn = {x + arch_w + gap, bar_y + 8, wave_w, 24} - return +START_WAVE_BUTTON_RECT :: rl.Rectangle { + f32(CONTROLS_BUTTON_X + ARCHER_BUTTON_W + CONTROLS_BUTTON_GAP), + f32(CONTROLS_BUTTON_Y), + START_WAVE_BUTTON_W, + CONTROLS_BUTTON_H, } poll_controls_command :: proc(world: ^World) -> Command { @@ -24,11 +30,10 @@ poll_controls_command :: proc(world: ^World) -> Command { mouse := game_mouse() if rl.IsMouseButtonPressed(.LEFT) { - archer, start_wave := build_controls_layout() - if rl.CheckCollisionPointRec(mouse, archer) { + if rl.CheckCollisionPointRec(mouse, ARCHER_BUTTON_RECT) { world.selected_tower = .Archer } - if rl.CheckCollisionPointRec(mouse, start_wave) { + if rl.CheckCollisionPointRec(mouse, START_WAVE_BUTTON_RECT) { return Command{kind = .Start_Wave} } } @@ -37,7 +42,7 @@ poll_controls_command :: proc(world: ^World) -> Command { } draw_archer_button :: proc(world: ^World, archer: rl.Rectangle) { - arch := get_archetype(.Archer) + arch := TOWER_ARCHETYPES[.Archer] mouse := rl.GetMousePosition() arch_bg := CONTROLS_SHOP_IDLE @@ -59,12 +64,10 @@ draw_build_controls :: proc(world: ^World) { if world.phase != .Build do return mouse := game_mouse() - bar_y := bottom_bar_y() - rl.DrawRectangle(0, bar_y, SCREEN_W, CONTROL_BAR_H, CONTROL_BAR_BG) + rl.DrawRectangle(0, BOTTOM_BAR_Y, SCREEN_W, CONTROL_BAR_H, CONTROL_BAR_BG) - archer, start_wave_rect := build_controls_layout() - draw_archer_button(world, archer) - draw_start_wave_button(start_wave_rect, mouse) + draw_archer_button(world, ARCHER_BUTTON_RECT) + draw_start_wave_button(START_WAVE_BUTTON_RECT, mouse) } render_controls :: proc(world: ^World) { @@ -78,8 +81,7 @@ render_controls :: proc(world: ^World) { } draw_combat_banner :: proc() { - bar_y := bottom_bar_y() - rl.DrawRectangle(0, bar_y, SCREEN_W, CONTROL_BAR_H, rl.Color{120, 40, 40, 200}) + rl.DrawRectangle(0, BOTTOM_BAR_Y, SCREEN_W, CONTROL_BAR_H, rl.Color{120, 40, 40, 200}) text: cstring = "Combat!" font: i32 = 16 @@ -87,7 +89,7 @@ draw_combat_banner :: proc() { rl.DrawText( text, (SCREEN_W - tw) / 2, - bar_y + (CONTROL_BAR_H - font) / 2, + BOTTOM_BAR_Y + (CONTROL_BAR_H - font) / 2, font, rl.Color{255, 220, 220, 255}, ) diff --git a/game/tower.odin b/game/tower.odin index f8764da..8dcc33c 100644 --- a/game/tower.odin +++ b/game/tower.odin @@ -79,18 +79,12 @@ TOWER_ARCHETYPES: [Tower_Kind]Tower_Archetype = { } tower_stats_at_level :: proc(tower: Tower) -> (damage: int, range: f32) { - arch := get_archetype(tower.kind) + arch := TOWER_ARCHETYPES[tower.kind] damage = arch.damage + tower.upgrade_level * arch.damage_per_level range = arch.range + f32(tower.upgrade_level) * arch.range_per_level return } - -get_archetype :: proc(kind: Tower_Kind) -> Tower_Archetype { - return TOWER_ARCHETYPES[kind] -} - - find_target_slot :: proc(tower: Tower, world: ^World) -> int { best_slot: int = -1 best_dist: f32 = 999999 @@ -120,7 +114,7 @@ update_towers :: proc(world: ^World, dt: f32) { slot := find_target_slot(t^, world) if slot < 0 do continue - arch := get_archetype(t.kind) + arch := TOWER_ARCHETYPES[t.kind] damage, _ := tower_stats_at_level(t^) switch t.kind { @@ -169,7 +163,7 @@ enemy_at_slot :: proc(world: ^World, slot: int) -> ^Enemy { render_towers :: proc(world: ^World) { for t in world.towers { - archetype := get_archetype(t.kind) + archetype := TOWER_ARCHETYPES[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} diff --git a/game/tower_placement.odin b/game/tower_placement.odin index cbcdc4f..dbd65aa 100644 --- a/game/tower_placement.odin +++ b/game/tower_placement.odin @@ -4,7 +4,7 @@ import rl "vendor:raylib" can_place_tower :: proc(world: ^World, gx, gy: int, kind: Tower_Kind) -> bool { if world.phase != .Build do return false - arch := get_archetype(kind) + arch := TOWER_ARCHETYPES[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 @@ -48,7 +48,7 @@ footprint_fits_map :: proc(world_map: ^Map, gx, gy, footprint_w, footprint_h: in 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) + archetype := TOWER_ARCHETYPES[placed_tower.kind] if rects_overlap( gx, gy, @@ -69,7 +69,7 @@ footprint_blocked :: proc(world: ^World, gx, gy, footprint_w, footprint_h: int) 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) + arch := TOWER_ARCHETYPES[kind] world.gold -= arch.cost append( &world.towers, @@ -87,7 +87,7 @@ try_place_tower :: proc(world: ^World, gx, gy: int, kind: Tower_Kind) -> bool { 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) + arch := TOWER_ARCHETYPES[t.kind] if gx >= t.anchor_gx && gx < t.anchor_gx + arch.footprint_w && gy >= t.anchor_gy && @@ -103,7 +103,7 @@ try_upgrade_tower :: proc(world: ^World, gx, gy: int) -> bool { t := tower_at_grid(world, gx, gy) if t == nil do return false - arch := get_archetype(t.kind) + arch := TOWER_ARCHETYPES[t.kind] if t.upgrade_level >= arch.max_upgrade do return false cost := arch.upgrade_cost + t.upgrade_level * 25 @@ -119,7 +119,7 @@ render_placement_preview :: proc(world: ^World) { mouse := game_mouse() gx, gy := screen_to_grid(mouse.x, mouse.y) - arch := get_archetype(world.selected_tower) + arch := TOWER_ARCHETYPES[world.selected_tower] ok := can_place_tower(world, gx, gy, world.selected_tower) center := footprint_center(gx, gy, arch.footprint_w, arch.footprint_h)