able to spawn enemy when pressing space

This commit is contained in:
2026-06-10 02:32:01 -07:00
parent 4032a5ea76
commit d7ac123359
8 changed files with 100 additions and 7 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ package game
import rl "vendor:raylib"
run :: proc() {
rl.InitWindow(640, 480, "Tower Defense")
rl.InitWindow(SCREEN_W, SCREEN_H, "Tower Defense")
defer rl.CloseWindow()
rl.SetTargetFPS(60)
+3
View File
@@ -4,3 +4,6 @@ MAP_W :: 16 // map width in tiles
MAP_H :: 12 // map height in tiles
TILE_SIZE :: 32 // size of tiles in pixels
SCREEN_W :: MAP_W * TILE_SIZE
SCREEN_H :: MAP_H * TILE_SIZE
+18
View File
@@ -0,0 +1,18 @@
package game
Enemy :: struct {
position: Vec2,
health: int,
active: bool,
}
spawn_enemy :: proc(world: ^World) {
if len(world.path) == 0 do return // don't spawn enemies if no paths exist
world.enemy = Enemy {
position = world.path[0],
health = 20,
active = true,
}
}
+17
View File
@@ -30,5 +30,22 @@ init_map :: proc(world_map: ^Map) {
world_map.tiles[y][x] = .Blocked
}
}
for x in 1 ..< 13 {
world_map.tiles[6][x] = .Path
}
for y in 7 ..< 8 {
world_map.tiles[y][12] = .Path
}
for y in 4 ..< 9 {
for x in 3 ..< 12 {
if world_map.tiles[y][x] != .Path {
world_map.tiles[y][x] = .Build
}
}
}
}
+6
View File
@@ -0,0 +1,6 @@
package game
Vec2 :: struct {
x, y: f32,
}
+16
View File
@@ -0,0 +1,16 @@
package game
build_path :: proc(world_map: ^Map, path: ^[dynamic]Vec2) {
clear(path)
for y in 0 ..< MAP_H {
for x in 0 ..< MAP_W {
if world_map.tiles[y][x] == .Path {
append(
path,
Vec2{f32(x * TILE_SIZE + TILE_SIZE / 2), f32(y * TILE_SIZE + TILE_SIZE / 2)},
)
}
}
}
}
+25
View File
@@ -0,0 +1,25 @@
package game
import "core:fmt"
import rl "vendor:raylib"
render_world :: proc(world: ^World) {
for y in 0 ..< MAP_H {
for x in 0 ..< MAP_W {
c := tile_color(world.world_map.tiles[y][x])
rl.DrawRectangle(i32(x * TILE_SIZE), i32(y * TILE_SIZE), TILE_SIZE, TILE_SIZE, c)
}
}
for p in world.path {
rl.DrawCircle(i32(p.x), i32(p.y), 3, rl.GOLD)
}
if world.enemy.active {
e := world.enemy
rl.DrawCircle(i32(e.position.x), i32(e.position.y), 11, rl.MAROON)
}
rl.DrawText(fmt.ctprintf("Gold: %d", world.gold), 10, 10, 20, rl.BLACK) // Gold HUD
rl.DrawText(fmt.ctprintf("Base HP: %d", world.base_health), 10, 32, 20, rl.BLACK) // HP HUD
}
+14 -6
View File
@@ -5,10 +5,19 @@ import rl "vendor:raylib"
World :: struct {
gold: int,
base_health: int,
world_map: Map,
path: [dynamic]Vec2,
enemy: Enemy,
}
init_world :: proc() -> World {
return World{gold = 10, base_health = 20}
world := World {
gold = 100,
base_health = 20,
}
init_map(&world.world_map)
build_path(&world.world_map, &world.path)
return world
}
// update world, never draw
@@ -18,10 +27,9 @@ update_world :: proc(world: ^World, dt: f32) {
if rl.IsKeyPressed(.G) {
world.gold += 10
}
}
render_world :: proc(world: ^World) {
rl.DrawText(fmt.caprintf("gold: %d", world.gold), 10, 10, 22, rl.BLACK)
rl.DrawText(fmt.caprintf("health: %d", world.base_health), 10, 38, 22, rl.BLACK)
if rl.IsKeyPressed(.SPACE) {
spawn_enemy(world)
}
}