diff --git a/src/citizen.rs b/src/citizen.rs index ca7cd0f..313746f 100644 --- a/src/citizen.rs +++ b/src/citizen.rs @@ -110,21 +110,19 @@ pub fn update_citizen_wandering_targets( // Find a valid z-level near the surface for z in (surface_height - 3)..=(surface_height + 4) { let mut target_pos = IVec3::new(target_x, target_y, z) * ITILE_SIZE; - if let Some(floor_tile) = tilemap.floor_tiles.get(&target_pos) { + if let Some(_) = tilemap.floor_tiles.get(&target_pos) { target_pos.z += ITILE_SIZE; - if floor_tile.2 { - 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 + TILE_SIZE + 1.0, - )); - ambulatory.current_path = None; - ambulatory.path_index = 0; - break; - } + if let Some(_base_texture) = tilemap.floor_tiles.get(&target_pos) { + if is_standable_tile(&tilemap, target_pos) { + // If tile is walkable, set target + ambulatory.target = Some(Vec3::new( + target_pos.x as f32, + target_pos.y as f32, + target_pos.z as f32 + 1.0, + )); + ambulatory.current_path = None; + ambulatory.path_index = 0; + break; } } } diff --git a/src/tilemap.rs b/src/tilemap.rs index 38835f3..da4b14d 100644 --- a/src/tilemap.rs +++ b/src/tilemap.rs @@ -4,7 +4,8 @@ use std::time::Instant; use crate::constants::{ITILE_SIZE, TILE_SIZE}; use crate::tile::{FixtureTile, FloorTile, NeedsOccluded, TileMap}; use crate::tiles::{ - CurrentWorldSpriteState, FixtureTilePrefab, FloorTilePrefab, TerrainSpriteState, + CurrentWorldSpriteState, FixtureTilePrefab, FloorTilePrefab, TerrainSpriteState, TextureIDs, + Textures, }; use bevy::prelude::*; use bevy_platform::collections::hash_map::HashMap; @@ -12,10 +13,12 @@ use noise::{NoiseFn, Perlin}; pub const CHUNK_SIZE: i32 = 8; -pub const Z_BELOW: f32 = 20.0; -pub const Z_ABOVE: f32 = 4.0; +pub const Z_BELOW: f32 = 6.0; +pub const Z_ABOVE: f32 = 6.0; pub const Z_TOTAL: f32 = Z_ABOVE + Z_BELOW; // MAX 255 DO NOT EXCEED +pub const SEED: u32 = 420; + #[derive(Resource)] pub struct ChunkMap { pub loaded_chunks: HashMap, @@ -166,7 +169,7 @@ pub fn calculate_visibility(pos: IVec3, tilemap: &TileMap) -> [u32; 8] { } pub fn generate_surface_terrain(x: i32, y: i32) -> f32 { - let noise = Perlin::new(0); + let noise = Perlin::new(SEED); let mut noise_value = 0.0; let mut amplitude = 1.0; let mut frequency = 0.008; @@ -215,7 +218,7 @@ fn generate_chunk_terrain( let start = Instant::now(); let count = events.len(); - let cave_noise = Perlin::new(0); + let cave_noise = Perlin::new(SEED); // Create mutexes for our shared resources let chunk_map_updates = Mutex::new(HashMap::new()); @@ -365,6 +368,8 @@ fn generate_chunk_forrestry( commands: ParallelCommands<'_, '_>, // Use ParallelCommands for parallel spawning mut events: EventReader, mut tilemap: ResMut, + texture_ids: Res, + textures: Res, ) { let start = Instant::now(); let count = events.len(); @@ -373,16 +378,38 @@ fn generate_chunk_forrestry( events.par_read().for_each(|event| { let floor_positions = &event.floor_tiles; + let mut tree_positions: Vec = Vec::new(); + let min_distance = 7.0 * TILE_SIZE; for (position, floor_type) in floor_positions.iter() { let above_pos = *position + Vec3::new(0.0, 0.0, TILE_SIZE); let above_ivec = above_pos.as_ivec3(); match floor_type.as_str() { - // TODO Fix rendering "grass" => { commands.command_scope(|mut commands| { - FixtureTilePrefab::log(above_pos).spawn(&mut commands); + // Check if position is far enough from existing trees + let is_far_enough = tree_positions + .iter() + .all(|&tree_pos| above_pos.distance(tree_pos) > min_distance); + + // 1 in 25 chance if far enough from other trees + if is_far_enough && rand::random::() % 45 == 0 { + FixtureTilePrefab::log(above_pos).spawn(&mut commands); + if let Some(texture_id) = texture_ids.refs.get(&500004) { + if let Some(texture) = textures.handles.get(texture_id) { + let sprite = Sprite { + image: texture.clone(), + ..Default::default() + }; + commands.spawn(( + sprite, + Transform::from_xyz(above_pos.x, above_pos.y, above_pos.z), + )); + tree_positions.push(above_pos); + } + } + } }); collected_tilemap_updates diff --git a/src/tiles.rs b/src/tiles.rs index 2b3b174..c5db2c4 100644 --- a/src/tiles.rs +++ b/src/tiles.rs @@ -581,12 +581,14 @@ impl FixtureTilePrefab { } } - pub fn spawn(self, commands: &mut Commands) { - commands.spawn(( - self.tile, - self.transform, - self.visibility, - self.needs_occluded, - )); + pub fn spawn(self, commands: &mut Commands) -> Entity { + commands + .spawn(( + self.tile, + self.transform, + self.visibility, + self.needs_occluded, + )) + .id() } }