diff --git a/src/citizen.rs b/src/citizen.rs index 3e4f038..374c387 100644 --- a/src/citizen.rs +++ b/src/citizen.rs @@ -1,5 +1,6 @@ use crate::{ constants::*, + tile, tilemap::{generate_surface_noise, ChunkMap, CHUNK_SIZE}, }; use bevy::prelude::*; @@ -68,11 +69,14 @@ pub struct PathfindingPlugin; impl Plugin for PathfindingPlugin { fn build(&self, app: &mut App) { - app.add_systems(FixedUpdate, (update_citizen_targets, citizen_movement)); + app.add_systems( + FixedUpdate, + (update_citizen_wandering_targets, citizen_movement), + ); } } -pub fn update_citizen_targets( +pub fn update_citizen_wandering_targets( mut query: Query<(&mut Ambulatory, &Transform)>, tilemap: Res, chunk_map: Res, @@ -95,24 +99,27 @@ pub fn update_citizen_targets( let target_y = chunk_y + rng.gen_range(0..CHUNK_SIZE); // Get height at position - let surface_height = generate_surface_noise(target_x, target_y).round() as i32; + let surface_height = 0; // Find a valid z-level near the surface - for z in (surface_height - 2)..=(surface_height + 2) { - let target_pos = IVec3::new(target_x, target_y, z); + for z in (surface_height - 3)..=(surface_height + 4) { + let mut target_pos = IVec3::new(target_x, target_y, z) * TILE_SIZE as i32; if let Some(floor_tile) = tilemap.floor_tiles.get(&target_pos) { - // If tile is walkable, set target + target_pos.z += TILE_SIZE as i32; if floor_tile.2 { - ambulatory.target = Some(Vec3::new( - target_x as f32 * TILE_SIZE, - target_y as f32 * TILE_SIZE, - z as f32 * TILE_SIZE, - )); - println!("target: {:?}", ambulatory.target); - exit(0); - ambulatory.current_path = None; - ambulatory.path_index = 0; - break; + if let Some(floor_tile_above) = tilemap.floor_tiles.get(&target_pos) { + if floor_tile_above.0 == 0 { + // If tile is walkable, set target + ambulatory.target = Some(Vec3::new( + target_x as f32 * TILE_SIZE, + target_y as f32 * TILE_SIZE, + z as f32 * TILE_SIZE + 1.0, + )); + ambulatory.current_path = None; + ambulatory.path_index = 0; + break; + } + } } } } diff --git a/src/constants.rs b/src/constants.rs index 8f68a1b..8246d63 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,2 +1,2 @@ -pub const PIXEL_RATIO: f32 = 2.; +pub const PIXEL_RATIO: f32 = 2.0; pub const TILE_SIZE: f32 = 16.0 * PIXEL_RATIO; diff --git a/src/main.rs b/src/main.rs index 4d312d5..02049ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,7 @@ fn main() { FixedUpdate, ( camera::camera_movement, - citizen::update_citizen_targets, + citizen::update_citizen_wandering_targets, citizen::citizen_movement, cursor::move_cursor, // citizen::check_citizen_positions_for_chunks, diff --git a/src/tilemap.rs b/src/tilemap.rs index 7649847..cb29bac 100644 --- a/src/tilemap.rs +++ b/src/tilemap.rs @@ -1,5 +1,5 @@ use crate::constants::TILE_SIZE; -use crate::tile::{FixtureTile, FloorTile, NeedsOccluded, TileMap}; +use crate::tile::{self, FixtureTile, FloorTile, NeedsOccluded, TileMap}; use crate::tiles::{FixtureTilePrefab, FloorTilePrefab}; use bevy::prelude::*; use noise::{NoiseFn, Perlin}; @@ -76,7 +76,7 @@ pub fn calculate_visibility(pos: IVec3, tilemap: &TileMap) -> [u32; 8] { let mut visible_range = [0u32; 8]; - for mut camera_z in -15..5 { + for mut camera_z in -15..=5 { camera_z *= tile_size; let mut is_visible = false; @@ -196,7 +196,7 @@ fn handle_chunk_loading( ); // Spawn tiles and add them to tilemap - for z in -15..5 { + for z in -15..=5 { let position = Vec3::new( (world_x as f32 * TILE_SIZE).round(), (world_y as f32 * TILE_SIZE).round(), @@ -285,8 +285,8 @@ fn handle_chunk_loading( fn setup_initial_chunks(mut event_writer: EventWriter) { println!("setup_initial_chunks"); - for x in -5..=5 { - for y in -5..=5 { + for x in -8..=8 { + for y in -8..=8 { event_writer.send(LoadChunkEvent { chunk_position: IVec2::new(x, y), }); @@ -301,6 +301,15 @@ impl Plugin for TilemapPlugin { app.init_resource::() .add_event::() .add_systems(Startup, (setup_chunk_system, setup_initial_chunks)) + .add_systems( + PostStartup, + ( + handle_chunk_loading, + handle_tile_occlusion_updates, + tile::update_tile_visibility, + ) + .chain(), + ) .add_systems( FixedUpdate, (handle_chunk_loading, handle_tile_occlusion_updates).chain(), diff --git a/src/tiles.rs b/src/tiles.rs index 84f707f..7249af5 100644 --- a/src/tiles.rs +++ b/src/tiles.rs @@ -89,6 +89,7 @@ impl FloorTilePrefab { tile: FloorTile { id: 0, opaque: false, + walkable: false, ..Default::default() }, tile_state: TileState {