commands, towers, etc

This commit is contained in:
2026-06-10 02:32:01 -07:00
parent 2e09dbe65d
commit 114c298219
5 changed files with 65 additions and 4 deletions
+32
View File
@@ -0,0 +1,32 @@
package game
import rl "vendor:raylib"
Command_Kind :: enum {
None,
Place_Tower,
}
Command :: struct {
kind: Command_Kind,
gx: int,
gy: int,
}
gather_commands :: proc(world: ^World) -> Command {
if rl.IsMouseButtonPressed(.LEFT) {
mouse := rl.GetMousePosition()
gx, gy := screen_to_grid(mouse.x, mouse.y)
return Command{kind = .Place_Tower, gx = gx, gy = gy}
}
return Command{kind = .None}
}
execute_command :: proc(world: ^World, cmd: Command) {
switch cmd.kind {
case .None:
case .Place_Tower:
try_place_tower(world, cmd.gx, cmd.gy)
}
}
+6 -1
View File
@@ -46,6 +46,11 @@ init_map :: proc(world_map: ^Map) {
}
}
}
}
can_build_at :: proc(world_map: ^Map, gx, gy: int) -> bool {
if gx < 0 || gx >= MAP_W do return false
if gy < 0 || gy >= MAP_H do return false
return world_map.tiles[gy][gx] == .Build
}
+4
View File
@@ -28,6 +28,10 @@ render_world :: proc(world: ^World) {
)
}
for t in world.towers {
rl.DrawRectangle(i32(t.position.x - 14), i32(t.position.y - 14), 28, 28, rl.BLUE)
}
if (world.base_health == 0) {
rl.DrawText("**GAME OVER**", 20, 20, 20, rl.RED)
}
+20
View File
@@ -0,0 +1,20 @@
package game
Tower :: struct {
position: Vec2,
}
try_place_tower :: proc(world: ^World, gx, gy: int) -> bool {
if !can_build_at(&world.world_map, gx, gy) do return false
if world.gold < TOWER_COST do return false
pos := grid_to_center(gx, gy)
for t in world.towers {
if distance(t.position, pos) < 1 do return false
}
world.gold -= TOWER_COST
append(&world.towers, Tower{position = pos})
return true
}
+3 -3
View File
@@ -8,6 +8,7 @@ World :: struct {
world_map: Map,
path: [dynamic]Vec2,
enemies: [MAX_ENEMIES]Enemy,
towers: [dynamic]Tower,
}
init_world :: proc() -> World {
@@ -22,9 +23,8 @@ init_world :: proc() -> World {
// update world, never draw
update_world :: proc(world: ^World, dt: f32) {
if rl.IsKeyPressed(.G) {
world.gold += 10
}
cmd := gather_commands(world)
execute_command(world, cmd)
if rl.IsKeyPressed(.SPACE) {
spawn_group(world, 3)