Fixed targeting for citizen movement

This commit is contained in:
2025-01-19 22:13:24 +00:00
parent 594131a372
commit 53b95f452f
5 changed files with 40 additions and 23 deletions
+23 -16
View File
@@ -1,5 +1,6 @@
use crate::{ use crate::{
constants::*, constants::*,
tile,
tilemap::{generate_surface_noise, ChunkMap, CHUNK_SIZE}, tilemap::{generate_surface_noise, ChunkMap, CHUNK_SIZE},
}; };
use bevy::prelude::*; use bevy::prelude::*;
@@ -68,11 +69,14 @@ pub struct PathfindingPlugin;
impl Plugin for PathfindingPlugin { impl Plugin for PathfindingPlugin {
fn build(&self, app: &mut App) { 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)>, mut query: Query<(&mut Ambulatory, &Transform)>,
tilemap: Res<TileMap>, tilemap: Res<TileMap>,
chunk_map: Res<ChunkMap>, chunk_map: Res<ChunkMap>,
@@ -95,24 +99,27 @@ pub fn update_citizen_targets(
let target_y = chunk_y + rng.gen_range(0..CHUNK_SIZE); let target_y = chunk_y + rng.gen_range(0..CHUNK_SIZE);
// Get height at position // 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 // Find a valid z-level near the surface
for z in (surface_height - 2)..=(surface_height + 2) { for z in (surface_height - 3)..=(surface_height + 4) {
let target_pos = IVec3::new(target_x, target_y, z); 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 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 { if floor_tile.2 {
ambulatory.target = Some(Vec3::new( if let Some(floor_tile_above) = tilemap.floor_tiles.get(&target_pos) {
target_x as f32 * TILE_SIZE, if floor_tile_above.0 == 0 {
target_y as f32 * TILE_SIZE, // If tile is walkable, set target
z as f32 * TILE_SIZE, ambulatory.target = Some(Vec3::new(
)); target_x as f32 * TILE_SIZE,
println!("target: {:?}", ambulatory.target); target_y as f32 * TILE_SIZE,
exit(0); z as f32 * TILE_SIZE + 1.0,
ambulatory.current_path = None; ));
ambulatory.path_index = 0; ambulatory.current_path = None;
break; ambulatory.path_index = 0;
break;
}
}
} }
} }
} }
+1 -1
View File
@@ -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; pub const TILE_SIZE: f32 = 16.0 * PIXEL_RATIO;
+1 -1
View File
@@ -41,7 +41,7 @@ fn main() {
FixedUpdate, FixedUpdate,
( (
camera::camera_movement, camera::camera_movement,
citizen::update_citizen_targets, citizen::update_citizen_wandering_targets,
citizen::citizen_movement, citizen::citizen_movement,
cursor::move_cursor, cursor::move_cursor,
// citizen::check_citizen_positions_for_chunks, // citizen::check_citizen_positions_for_chunks,
+14 -5
View File
@@ -1,5 +1,5 @@
use crate::constants::TILE_SIZE; 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 crate::tiles::{FixtureTilePrefab, FloorTilePrefab};
use bevy::prelude::*; use bevy::prelude::*;
use noise::{NoiseFn, Perlin}; use noise::{NoiseFn, Perlin};
@@ -76,7 +76,7 @@ pub fn calculate_visibility(pos: IVec3, tilemap: &TileMap) -> [u32; 8] {
let mut visible_range = [0u32; 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; camera_z *= tile_size;
let mut is_visible = false; let mut is_visible = false;
@@ -196,7 +196,7 @@ fn handle_chunk_loading(
); );
// Spawn tiles and add them to tilemap // Spawn tiles and add them to tilemap
for z in -15..5 { for z in -15..=5 {
let position = Vec3::new( let position = Vec3::new(
(world_x as f32 * TILE_SIZE).round(), (world_x as f32 * TILE_SIZE).round(),
(world_y 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<LoadChunkEvent>) { fn setup_initial_chunks(mut event_writer: EventWriter<LoadChunkEvent>) {
println!("setup_initial_chunks"); println!("setup_initial_chunks");
for x in -5..=5 { for x in -8..=8 {
for y in -5..=5 { for y in -8..=8 {
event_writer.send(LoadChunkEvent { event_writer.send(LoadChunkEvent {
chunk_position: IVec2::new(x, y), chunk_position: IVec2::new(x, y),
}); });
@@ -301,6 +301,15 @@ impl Plugin for TilemapPlugin {
app.init_resource::<ChunkMap>() app.init_resource::<ChunkMap>()
.add_event::<LoadChunkEvent>() .add_event::<LoadChunkEvent>()
.add_systems(Startup, (setup_chunk_system, setup_initial_chunks)) .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( .add_systems(
FixedUpdate, FixedUpdate,
(handle_chunk_loading, handle_tile_occlusion_updates).chain(), (handle_chunk_loading, handle_tile_occlusion_updates).chain(),
+1
View File
@@ -89,6 +89,7 @@ impl FloorTilePrefab {
tile: FloorTile { tile: FloorTile {
id: 0, id: 0,
opaque: false, opaque: false,
walkable: false,
..Default::default() ..Default::default()
}, },
tile_state: TileState { tile_state: TileState {