preview towers before placement
This commit is contained in:
@@ -4,6 +4,7 @@ render_world :: proc(world: ^World) {
|
||||
render_map(world)
|
||||
render_enemies(world)
|
||||
render_towers(world)
|
||||
render_placement_preview(world)
|
||||
render_projectiles(world)
|
||||
}
|
||||
|
||||
|
||||
+39
-12
@@ -56,6 +56,15 @@ TOWER_ARCHETYPES: [Tower_Kind]Tower_Archetype = {
|
||||
},
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -80,9 +89,11 @@ footprint_fits_map :: proc(world_map: ^Map, gx, gy, footprint_w, footprint_h: in
|
||||
if gx + footprint_w > MAP_W do return false
|
||||
if gy + footprint_h > MAP_H do return false
|
||||
|
||||
for row in gy ..< gy + footprint_h {
|
||||
for column in gx ..< gx + footprint_w {
|
||||
if !can_build_at(world_map, column, row) do return false
|
||||
for column in gy ..< footprint_h {
|
||||
for row in gx ..< footprint_w {
|
||||
cell_column := gx + column
|
||||
cell_row := gy + row
|
||||
if !can_build_at(world_map, cell_column, cell_row) do return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,21 +125,14 @@ get_archetype :: proc(kind: Tower_Kind) -> Tower_Archetype {
|
||||
}
|
||||
|
||||
try_place_tower :: proc(world: ^World, gx, gy: int, kind: Tower_Kind) -> bool {
|
||||
if world.phase != .Build do return false
|
||||
if !can_place_tower(world, gx, gy, kind) do return false
|
||||
|
||||
arch := get_archetype(kind)
|
||||
footprint_w := arch.footprint_w
|
||||
footprint_h := arch.footprint_h
|
||||
|
||||
if !footprint_fits_map(&world.world_map, gx, gy, footprint_w, footprint_h) do return false
|
||||
if footprint_blocked(world, gx, gy, footprint_w, footprint_h) do return false
|
||||
if world.gold < arch.cost do return false
|
||||
|
||||
world.gold -= arch.cost
|
||||
append(
|
||||
&world.towers,
|
||||
Tower {
|
||||
position = footprint_center(gx, gy, footprint_w, footprint_h),
|
||||
position = footprint_center(gx, gy, arch.footprint_w, arch.footprint_h),
|
||||
anchor_gx = gx,
|
||||
anchor_gy = gy,
|
||||
kind = kind,
|
||||
@@ -223,3 +227,26 @@ render_towers :: proc(world: ^World) {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user