Chunking 3
This commit is contained in:
+2
-46
@@ -29,10 +29,9 @@ fn main() {
|
||||
.add_systems(
|
||||
Startup,
|
||||
(
|
||||
setup_initial_chunks,
|
||||
camera::spawn_panning_camera,
|
||||
cursor::setup_cursor,
|
||||
citizen::spawn_citizens, // New citizen spawning system
|
||||
citizen::spawn_citizens,
|
||||
),
|
||||
)
|
||||
.add_systems(
|
||||
@@ -41,8 +40,7 @@ fn main() {
|
||||
camera::camera_movement,
|
||||
citizen::citizen_movement,
|
||||
cursor::move_cursor,
|
||||
check_camera_position_for_chunks,
|
||||
citizen::check_citizen_positions_for_chunks, // New citizen-based chunk loading system
|
||||
// citizen::check_citizen_positions_for_chunks,
|
||||
),
|
||||
)
|
||||
.init_resource::<tile::CameraMoved>()
|
||||
@@ -56,45 +54,3 @@ fn main() {
|
||||
)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn setup_initial_chunks(mut event_writer: EventWriter<tilemap::LoadChunkEvent>) {
|
||||
println!("setup_initial_chunks");
|
||||
// // Spawn a 3x3 grid of chunks around the origin
|
||||
// for x in -5..=5 {
|
||||
// for y in -5..=5 {
|
||||
// event_writer.send(tilemap::LoadChunkEvent {
|
||||
// chunk_position: IVec2::new(x, y),
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
// New system to check camera position and load nearby chunks
|
||||
fn check_camera_position_for_chunks(
|
||||
camera_query: Query<&Transform, With<Camera>>,
|
||||
chunk_map: Res<tilemap::ChunkMap>,
|
||||
mut event_writer: EventWriter<tilemap::LoadChunkEvent>,
|
||||
) {
|
||||
let camera_transform = camera_query.single();
|
||||
let camera_pos = camera_transform.translation;
|
||||
|
||||
// Convert camera position to chunk coordinates
|
||||
let chunk_x =
|
||||
(camera_pos.x / (tilemap::CHUNK_SIZE as f32 * constants::TILE_SIZE)).floor() as i32;
|
||||
let chunk_y =
|
||||
(camera_pos.y / (tilemap::CHUNK_SIZE as f32 * constants::TILE_SIZE)).floor() as i32;
|
||||
|
||||
// Check a 3x3 area around the camera's current chunk
|
||||
for x in (chunk_x - 2)..=(chunk_x + 2) {
|
||||
for y in (chunk_y - 2)..=(chunk_y + 2) {
|
||||
let chunk_pos = IVec2::new(x, y);
|
||||
|
||||
// Only spawn new chunks if they haven't been loaded yet
|
||||
if !chunk_map.loaded_chunks.contains_key(&chunk_pos) {
|
||||
event_writer.send(tilemap::LoadChunkEvent {
|
||||
chunk_position: chunk_pos,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user