Fixed targeting for citizen movement
This commit is contained in:
+16
-9
@@ -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,21 +99,22 @@ 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 {
|
||||||
|
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(
|
ambulatory.target = Some(Vec3::new(
|
||||||
target_x as f32 * TILE_SIZE,
|
target_x as f32 * TILE_SIZE,
|
||||||
target_y as f32 * TILE_SIZE,
|
target_y as f32 * TILE_SIZE,
|
||||||
z as f32 * TILE_SIZE,
|
z as f32 * TILE_SIZE + 1.0,
|
||||||
));
|
));
|
||||||
println!("target: {:?}", ambulatory.target);
|
|
||||||
exit(0);
|
|
||||||
ambulatory.current_path = None;
|
ambulatory.current_path = None;
|
||||||
ambulatory.path_index = 0;
|
ambulatory.path_index = 0;
|
||||||
break;
|
break;
|
||||||
@@ -119,6 +124,8 @@ pub fn update_citizen_targets(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn citizen_movement(
|
pub fn citizen_movement(
|
||||||
|
|||||||
+1
-1
@@ -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
@@ -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
@@ -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(),
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user