Saving and loading chunks actually works now

This commit is contained in:
2025-02-27 13:14:29 -06:00
parent c6c8864d67
commit cddba06c25
31 changed files with 115 additions and 208 deletions

View File

@@ -28,32 +28,35 @@ handle_player_camera :: proc(p:^Player) {
player_update :: proc(p : ^Player, w: ^World) {
handle_player_input(p,w)
handle_player_camera(p)
if rl.IsKeyPressed(.SPACE) {
set_tile(w, tree_tile, vec2_to_vec2i(get_player_grid_position(p)))
}
}
load_nearby_chunks :: proc(w:^World, player_pos:rl.Vector2) {
player_chunk_pos := world_pos_to_chunk_pos(player_pos)
// player_chunk_pos := world_pos_to_chunk_pos(player_pos)
chunk_radius := 2 // Adjust based on the camera size
// chunk_radius := 2 // Adjust based on the camera size
for x := -chunk_radius; x <= chunk_radius; x += 1 {
for y := -chunk_radius; y <= chunk_radius; y += 1 {
chunk_pos := Vec2i{player_chunk_pos.x + x, player_chunk_pos.y + y}
get_chunk(w, chunk_pos) // Ensures chunk is loaded or generated
}
}
// for x := -chunk_radius; x <= chunk_radius; x += 1 {
// for y := -chunk_radius; y <= chunk_radius; y += 1 {
// chunk_pos := Vec2i{player_chunk_pos.x + x, player_chunk_pos.y + y}
// get_chunk(w, chunk_pos) // Ensures chunk is loaded or generated
// }
// }
}
unload_far_chunks :: proc(w: ^World, player_pos: Vec2i) {
for chunk_pos in w.chunks {
dist_x := abs(chunk_pos.x - player_pos.x)
dist_y := abs(chunk_pos.y - player_pos.y)
// for chunk_pos in w.chunks {
// dist_x := abs(chunk_pos.x - player_pos.x)
// dist_y := abs(chunk_pos.y - player_pos.y)
if dist_x > CHUNK_UNLOAD_DISTANCE || dist_y > CHUNK_UNLOAD_DISTANCE {
unload_chunk(chunk_pos, w)
}
}
// if dist_x > CHUNK_UNLOAD_DISTANCE || dist_y > CHUNK_UNLOAD_DISTANCE {
// unload_chunk(chunk_pos, w)
// }
// }
}
handle_player_input :: proc(p:^Player, w:^World) {