refactor map
This commit is contained in:
+1
-1
@@ -11,7 +11,7 @@ run :: proc() {
|
||||
|
||||
world := init_world()
|
||||
defer {
|
||||
delete(world.path)
|
||||
delete(world.world_map.path)
|
||||
delete(world.combat.towers)
|
||||
delete(world.events)
|
||||
delete(world.wave.spawner.recipe)
|
||||
|
||||
+5
-5
@@ -68,14 +68,14 @@ acquire_enemy :: proc(world: ^World) -> ^Enemy {
|
||||
}
|
||||
|
||||
spawn_enemy :: proc(world: ^World, kind: Enemy_Kind) {
|
||||
if len(world.path) == 0 do return // don't spawn enemies if no paths exist
|
||||
if len(world.world_map.path) == 0 do return // don't spawn enemies if no paths exist
|
||||
|
||||
e := acquire_enemy(world) // grab a free slot
|
||||
if e == nil do return // if there are no slots, skil spawning
|
||||
|
||||
arch := get_enemy_archetype(kind, world.wave.current_wave)
|
||||
e^ = Enemy {
|
||||
position = world.path[0],
|
||||
position = world.world_map.path[0],
|
||||
health = arch.health,
|
||||
max_health = arch.health,
|
||||
base_speed = arch.speed,
|
||||
@@ -87,19 +87,19 @@ spawn_enemy :: proc(world: ^World, kind: Enemy_Kind) {
|
||||
}
|
||||
|
||||
update_enemies :: proc(world: ^World, dt: f32) {
|
||||
if len(world.path) < 2 do return
|
||||
if len(world.world_map.path) < 2 do return
|
||||
|
||||
for i in 0 ..< MAX_ENEMIES {
|
||||
e := &world.combat.enemies[i]
|
||||
if !e.active do continue
|
||||
|
||||
if e.path_index >= len(world.path) {
|
||||
if e.path_index >= len(world.world_map.path) {
|
||||
world.base_health -= 1
|
||||
e.active = false
|
||||
continue
|
||||
}
|
||||
|
||||
target := world.path[e.path_index]
|
||||
target := world.world_map.path[e.path_index]
|
||||
move_speed := e.base_speed
|
||||
if e.slow_timer > 0 {
|
||||
move_speed *= ICE_SLOW_FACTOR
|
||||
|
||||
+5
-5
@@ -7,7 +7,7 @@ Tile_Kind :: enum {
|
||||
Build,
|
||||
}
|
||||
|
||||
Map :: struct {
|
||||
World_Map :: struct {
|
||||
tiles: [MAP_H][MAP_W]Tile_Kind,
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ tile_color :: proc(kind: Tile_Kind) -> rl.Color {
|
||||
return rl.MAGENTA
|
||||
}
|
||||
|
||||
init_map :: proc(world_map: ^Map) {
|
||||
init_map :: proc(world_map: ^World_Map) {
|
||||
for y in 0 ..< MAP_H {
|
||||
for x in 0 ..< MAP_W {
|
||||
world_map.tiles[y][x] = .Blocked
|
||||
@@ -48,16 +48,16 @@ init_map :: proc(world_map: ^Map) {
|
||||
}
|
||||
}
|
||||
|
||||
can_build_at :: proc(world_map: ^Map, gx, gy: int) -> bool {
|
||||
can_build_at :: proc(world_map: ^World_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
|
||||
}
|
||||
|
||||
render_map :: proc(world: ^World) {
|
||||
render_map :: proc(world_map: ^World_Map) {
|
||||
for y in 0 ..< MAP_H {
|
||||
for x in 0 ..< MAP_W {
|
||||
c := tile_color(world.world_map.tiles[y][x])
|
||||
c := tile_color(world_map.tiles[y][x])
|
||||
rl.DrawRectangle(i32(x * TILE_SIZE), i32(y * TILE_SIZE), TILE_SIZE, TILE_SIZE, c)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
package game
|
||||
|
||||
build_path :: proc(world_map: ^Map, path: ^[dynamic]Vec2) {
|
||||
build_path :: proc(world_map: ^World_Map, path: ^[dynamic]Vec2) {
|
||||
clear(path)
|
||||
for y in 0 ..< MAP_H {
|
||||
for x in 0 ..< MAP_W {
|
||||
|
||||
@@ -32,8 +32,8 @@ enemy_still_on_map :: proc(world: ^World, slot: int) -> (^Enemy, bool) {
|
||||
if slot < 0 || slot >= MAX_ENEMIES do return nil, false
|
||||
e := &world.combat.enemies[slot]
|
||||
if !e.active do return nil, false
|
||||
if len(world.path) == 0 do return nil, false
|
||||
if e.path_index >= len(world.path) do return nil, false
|
||||
if len(world.world_map.path) == 0 do return nil, false
|
||||
if e.path_index >= len(world.world_map.path) do return nil, false
|
||||
return e, true
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
package game
|
||||
|
||||
render_world :: proc(world: ^World) {
|
||||
render_map(world)
|
||||
render_map(&world.world_map.current_map)
|
||||
render_enemies(world)
|
||||
render_towers(world)
|
||||
render_placement_preview(world)
|
||||
|
||||
+3
-3
@@ -89,7 +89,7 @@ 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.economy.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_fits_map(&world.world_map.current_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
|
||||
}
|
||||
@@ -113,7 +113,7 @@ footprint_center :: proc(gx, gy, footprint_w, footprint_h: int) -> Vec2 {
|
||||
}
|
||||
}
|
||||
|
||||
footprint_fits_map :: proc(world_map: ^Map, gx, gy, footprint_w, footprint_h: int) -> bool {
|
||||
footprint_fits_map :: proc(world_map: ^World_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
|
||||
@@ -208,7 +208,7 @@ find_target_slot :: proc(tower: Tower, world: ^World) -> int {
|
||||
for i in 0 ..< MAX_ENEMIES {
|
||||
e := &world.combat.enemies[i]
|
||||
if !e.active do continue
|
||||
if e.path_index >= len(world.path) do continue
|
||||
if e.path_index >= len(world.world_map.path) do continue
|
||||
|
||||
d := distance(tower.position, e.position)
|
||||
_, range := tower_stats_at_level(tower)
|
||||
|
||||
+7
-3
@@ -5,13 +5,17 @@ World :: struct {
|
||||
combat: Combat,
|
||||
base_health: int,
|
||||
world_map: Map,
|
||||
path: [dynamic]Vec2,
|
||||
events: [dynamic]Event,
|
||||
phase: Game_Phase,
|
||||
wave: Wave,
|
||||
selected_tower: Tower_Kind,
|
||||
}
|
||||
|
||||
Map :: struct {
|
||||
current_map: World_Map,
|
||||
path: [dynamic]Vec2,
|
||||
}
|
||||
|
||||
Economy :: struct {
|
||||
gold: int,
|
||||
}
|
||||
@@ -37,8 +41,8 @@ init_world :: proc() -> World {
|
||||
phase = .Build,
|
||||
selected_tower = .Archer,
|
||||
}
|
||||
init_map(&world.world_map)
|
||||
build_path(&world.world_map, &world.path)
|
||||
init_map(&world.world_map.current_map)
|
||||
build_path(&world.world_map.current_map, &world.world_map.path)
|
||||
return world
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user