Remove collision indices.
This commit is contained in:
parent
b6fe9d420e
commit
c4533c7024
|
@ -14,14 +14,6 @@ use crate::{
|
||||||
pathfinding::Destination,
|
pathfinding::Destination,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default, Reflect)]
|
|
||||||
#[reflect(Component)]
|
|
||||||
pub struct BlocksMotion;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deref, DerefMut, Reflect)]
|
|
||||||
#[reflect(Component)]
|
|
||||||
pub struct CollisionsMonitored(pub Vec<bool>);
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Deref, DerefMut, Reflect)]
|
#[derive(Clone, Copy, Debug, Deref, DerefMut, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct MaxSpeed(pub f32);
|
pub struct MaxSpeed(pub f32);
|
||||||
|
@ -32,14 +24,6 @@ impl Default for MaxSpeed {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default, Reflect)]
|
|
||||||
#[reflect(Component)]
|
|
||||||
pub struct MonitorsCollisions;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deref, DerefMut, Reflect)]
|
|
||||||
#[reflect(Component)]
|
|
||||||
pub struct MotionBlocked(pub Vec<bool>);
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Deref, DerefMut, Reflect)]
|
#[derive(Clone, Copy, Debug, Deref, DerefMut, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct RotationSpeed(pub Angle);
|
pub struct RotationSpeed(pub Angle);
|
||||||
|
@ -58,13 +42,6 @@ pub struct Speed(pub f32);
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Sprinting;
|
pub struct Sprinting;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
|
||||||
pub struct Collision {
|
|
||||||
pub entity: Entity,
|
|
||||||
pub coordinates: (f32, f32),
|
|
||||||
pub index: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_map_colliders(mut commands: Commands, maps: Query<(Entity, &Map), Added<Map>>) {
|
fn add_map_colliders(mut commands: Commands, maps: Query<(Entity, &Map), Added<Map>>) {
|
||||||
for (map_entity, map) in maps.iter() {
|
for (map_entity, map) in maps.iter() {
|
||||||
let rigid_body_entity = commands
|
let rigid_body_entity = commands
|
||||||
|
@ -223,174 +200,6 @@ fn movement_controls<S, A: 'static>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn set_motion_blocked(
|
|
||||||
map: &Map,
|
|
||||||
index: usize,
|
|
||||||
motion_blockers: &Query<&BlocksMotion>,
|
|
||||||
motion_blocked: &mut MotionBlocked,
|
|
||||||
) {
|
|
||||||
let mut new_motion_blocked = map.base.tiles[index].blocks_motion();
|
|
||||||
if !new_motion_blocked {
|
|
||||||
for e in &map.entities[index] {
|
|
||||||
if motion_blockers.get(*e).is_ok() {
|
|
||||||
new_motion_blocked = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
motion_blocked[index] = new_motion_blocked;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const UPDATE_COLLISION_INDEX_LABEL: &str = "UPDATE_COLLISION_INDEX";
|
|
||||||
|
|
||||||
#[derive(Default, Deref, DerefMut)]
|
|
||||||
struct PreviousBlocksMotionIndex(HashMap<Entity, usize>);
|
|
||||||
|
|
||||||
fn blocks_motion_indexing(
|
|
||||||
mut map: Query<(&Map, &mut MotionBlocked)>,
|
|
||||||
mut prev_index: ResMut<PreviousBlocksMotionIndex>,
|
|
||||||
query: Query<
|
|
||||||
(Entity, &Coordinates, &BlocksMotion),
|
|
||||||
Or<(Changed<Coordinates>, Changed<BlocksMotion>)>,
|
|
||||||
>,
|
|
||||||
motion_blockers: Query<&BlocksMotion>,
|
|
||||||
) {
|
|
||||||
for (entity, coordinates, _) in query.iter() {
|
|
||||||
for (map, mut motion_blocked) in map.iter_mut() {
|
|
||||||
let idx = coordinates.to_index(map.width());
|
|
||||||
if let Some(prev_idx) = prev_index.get(&entity) {
|
|
||||||
if *prev_idx == idx {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
set_motion_blocked(map, *prev_idx, &motion_blockers, &mut motion_blocked);
|
|
||||||
}
|
|
||||||
motion_blocked[idx] = true;
|
|
||||||
prev_index.insert(entity, idx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn remove_blocks_motion(
|
|
||||||
mut prev_index: ResMut<PreviousBlocksMotionIndex>,
|
|
||||||
mut map: Query<(&Map, &mut MotionBlocked)>,
|
|
||||||
removed: RemovedComponents<BlocksMotion>,
|
|
||||||
coordinates: Query<&Coordinates>,
|
|
||||||
blocks_motion: Query<&BlocksMotion>,
|
|
||||||
) {
|
|
||||||
for entity in removed.iter() {
|
|
||||||
for (map, mut motion_blocked) in map.iter_mut() {
|
|
||||||
let prev = prev_index.get(&entity).cloned();
|
|
||||||
prev_index.remove(&entity);
|
|
||||||
if let Some(prev) = prev {
|
|
||||||
set_motion_blocked(&map, prev, &blocks_motion, &mut motion_blocked)
|
|
||||||
}
|
|
||||||
if let Ok(coordinates) = coordinates.get(entity) {
|
|
||||||
let idx = coordinates.to_index(map.width());
|
|
||||||
set_motion_blocked(&map, idx, &blocks_motion, &mut motion_blocked)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn set_monitors_collisions(
|
|
||||||
map: &Map,
|
|
||||||
index: usize,
|
|
||||||
collision_monitors: &Query<&MonitorsCollisions>,
|
|
||||||
collisions_monitored: &mut CollisionsMonitored,
|
|
||||||
) {
|
|
||||||
let mut new_collisions_monitored = false;
|
|
||||||
for e in &map.entities[index] {
|
|
||||||
if collision_monitors.get(*e).is_ok() {
|
|
||||||
new_collisions_monitored = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
collisions_monitored[index] = new_collisions_monitored;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default, Deref, DerefMut)]
|
|
||||||
struct PreviousMonitorsCollisionsIndex(HashMap<Entity, usize>);
|
|
||||||
|
|
||||||
fn monitors_collisions_indexing(
|
|
||||||
mut map: Query<(&Map, &mut CollisionsMonitored)>,
|
|
||||||
mut prev_index: ResMut<PreviousMonitorsCollisionsIndex>,
|
|
||||||
query: Query<
|
|
||||||
(Entity, &Coordinates, &MonitorsCollisions),
|
|
||||||
Or<(Changed<Coordinates>, Changed<MonitorsCollisions>)>,
|
|
||||||
>,
|
|
||||||
collision_monitors: Query<&MonitorsCollisions>,
|
|
||||||
) {
|
|
||||||
for (entity, coordinates, _) in query.iter() {
|
|
||||||
for (map, mut collisions_monitored) in map.iter_mut() {
|
|
||||||
let idx = coordinates.to_index(map.width());
|
|
||||||
if let Some(prev_idx) = prev_index.get(&entity) {
|
|
||||||
if *prev_idx == idx {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
set_monitors_collisions(
|
|
||||||
&map,
|
|
||||||
*prev_idx,
|
|
||||||
&collision_monitors,
|
|
||||||
&mut collisions_monitored,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
collisions_monitored[idx] = true;
|
|
||||||
prev_index.insert(entity, idx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn remove_monitors_collisions(
|
|
||||||
mut prev_index: ResMut<PreviousMonitorsCollisionsIndex>,
|
|
||||||
mut map: Query<(&Map, &mut CollisionsMonitored)>,
|
|
||||||
removed: RemovedComponents<MonitorsCollisions>,
|
|
||||||
coordinates: Query<&Coordinates>,
|
|
||||||
monitors_collisions: Query<&MonitorsCollisions>,
|
|
||||||
) {
|
|
||||||
for entity in removed.iter() {
|
|
||||||
for (map, mut collisions_monitored) in map.iter_mut() {
|
|
||||||
let prev = prev_index.get(&entity).cloned();
|
|
||||||
prev_index.remove(&entity);
|
|
||||||
if let Some(prev) = prev {
|
|
||||||
set_monitors_collisions(
|
|
||||||
&map,
|
|
||||||
prev,
|
|
||||||
&monitors_collisions,
|
|
||||||
&mut collisions_monitored,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if let Ok(coordinates) = coordinates.get_component::<Coordinates>(entity) {
|
|
||||||
let idx = coordinates.to_index(map.width());
|
|
||||||
set_monitors_collisions(&map, idx, &monitors_collisions, &mut collisions_monitored);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_collision_indices(
|
|
||||||
mut commands: Commands,
|
|
||||||
query: Query<
|
|
||||||
(Entity, &Map),
|
|
||||||
(
|
|
||||||
Added<Map>,
|
|
||||||
Without<MotionBlocked>,
|
|
||||||
Without<CollisionsMonitored>,
|
|
||||||
),
|
|
||||||
>,
|
|
||||||
) {
|
|
||||||
for (entity, map) in query.iter() {
|
|
||||||
let mut v = vec![];
|
|
||||||
for tile in &map.base.tiles {
|
|
||||||
v.push(tile.blocks_motion());
|
|
||||||
}
|
|
||||||
commands.entity(entity).insert(MotionBlocked(v));
|
|
||||||
let count = (map.width() * map.height()) as usize;
|
|
||||||
commands
|
|
||||||
.entity(entity)
|
|
||||||
.insert(CollisionsMonitored(vec![false; count]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn speak_direction(
|
fn speak_direction(
|
||||||
mut tts: ResMut<Tts>,
|
mut tts: ResMut<Tts>,
|
||||||
mut cache: Local<HashMap<Entity, CardinalDirection>>,
|
mut cache: Local<HashMap<Entity, CardinalDirection>>,
|
||||||
|
@ -477,48 +286,8 @@ where
|
||||||
app.register_type::<MaxSpeed>()
|
app.register_type::<MaxSpeed>()
|
||||||
.register_type::<RotationSpeed>()
|
.register_type::<RotationSpeed>()
|
||||||
.register_type::<Sprinting>()
|
.register_type::<Sprinting>()
|
||||||
.add_event::<Collision>()
|
|
||||||
.insert_resource(PreviousBlocksMotionIndex::default())
|
|
||||||
.add_system(add_map_colliders.system())
|
.add_system(add_map_colliders.system())
|
||||||
.add_system_to_stage(
|
.add_system(speak_direction.system().chain(error_handler.system()));
|
||||||
CoreStage::PostUpdate,
|
|
||||||
blocks_motion_indexing
|
|
||||||
.system()
|
|
||||||
.after(crate::map::UPDATE_ENTITY_INDEX_LABEL)
|
|
||||||
.label(UPDATE_COLLISION_INDEX_LABEL),
|
|
||||||
)
|
|
||||||
.add_system_to_stage(
|
|
||||||
CoreStage::PostUpdate,
|
|
||||||
remove_blocks_motion
|
|
||||||
.system()
|
|
||||||
.after(crate::map::UPDATE_ENTITY_INDEX_LABEL)
|
|
||||||
.before(UPDATE_COLLISION_INDEX_LABEL),
|
|
||||||
)
|
|
||||||
.insert_resource(PreviousMonitorsCollisionsIndex::default())
|
|
||||||
.add_system_to_stage(
|
|
||||||
CoreStage::PostUpdate,
|
|
||||||
monitors_collisions_indexing
|
|
||||||
.system()
|
|
||||||
.after(crate::map::UPDATE_ENTITY_INDEX_LABEL)
|
|
||||||
.label(UPDATE_COLLISION_INDEX_LABEL),
|
|
||||||
)
|
|
||||||
.add_system_to_stage(
|
|
||||||
CoreStage::PostUpdate,
|
|
||||||
remove_monitors_collisions
|
|
||||||
.system()
|
|
||||||
.after(crate::map::UPDATE_ENTITY_INDEX_LABEL)
|
|
||||||
.before(UPDATE_COLLISION_INDEX_LABEL),
|
|
||||||
)
|
|
||||||
.add_system_to_stage(
|
|
||||||
CoreStage::PostUpdate,
|
|
||||||
monitors_collisions_indexing
|
|
||||||
.system()
|
|
||||||
.after(crate::map::UPDATE_ENTITY_INDEX_LABEL)
|
|
||||||
.label(UPDATE_COLLISION_INDEX_LABEL),
|
|
||||||
)
|
|
||||||
.add_system(add_collision_indices.system())
|
|
||||||
.add_system(speak_direction.system().chain(error_handler.system()))
|
|
||||||
.add_system_to_stage(CoreStage::PostUpdate, add_collision_indices.system());
|
|
||||||
if config.movement_states.is_empty() {
|
if config.movement_states.is_empty() {
|
||||||
} else {
|
} else {
|
||||||
let states = config.movement_states;
|
let states = config.movement_states;
|
||||||
|
|
|
@ -9,7 +9,7 @@ use pathfinding::prelude::*;
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{Coordinates, PointLike},
|
core::{Coordinates, PointLike},
|
||||||
map::Map,
|
map::Map,
|
||||||
navigation::{MotionBlocked, RotationSpeed, Speed},
|
navigation::{RotationSpeed, Speed},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default, Deref, DerefMut, Eq, Hash, PartialEq, Reflect)]
|
#[derive(Clone, Copy, Debug, Default, Deref, DerefMut, Eq, Hash, PartialEq, Reflect)]
|
||||||
|
@ -117,7 +117,7 @@ fn calculate_path(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn negotiate_path(
|
/*fn negotiate_path(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
mut query: Query<(
|
mut query: Query<(
|
||||||
|
@ -129,12 +129,12 @@ fn negotiate_path(
|
||||||
Option<&RotationSpeed>,
|
Option<&RotationSpeed>,
|
||||||
&mut Transform,
|
&mut Transform,
|
||||||
)>,
|
)>,
|
||||||
map: Query<(&Map, &MotionBlocked)>,
|
map: Query<&Map>,
|
||||||
) {
|
) {
|
||||||
for (entity, mut path, mut coordinates, mut velocity, speed, rotation_speed, mut transform) in
|
for (entity, mut path, mut coordinates, mut velocity, speed, rotation_speed, mut transform) in
|
||||||
query.iter_mut()
|
query.iter_mut()
|
||||||
{
|
{
|
||||||
for (map, motion_blocked) in map.iter() {
|
for map in map.iter() {
|
||||||
let mut new_path = path.0.clone();
|
let mut new_path = path.0.clone();
|
||||||
let start_i32 = coordinates.i32();
|
let start_i32 = coordinates.i32();
|
||||||
let new_path_clone = new_path.clone();
|
let new_path_clone = new_path.clone();
|
||||||
|
@ -143,13 +143,6 @@ fn negotiate_path(
|
||||||
if let Some(upcoming) = iter.next() {
|
if let Some(upcoming) = iter.next() {
|
||||||
new_path = vec![start_i32];
|
new_path = vec![start_i32];
|
||||||
new_path.append(&mut upcoming.to_vec());
|
new_path.append(&mut upcoming.to_vec());
|
||||||
} else {
|
|
||||||
let point = new_path[0];
|
|
||||||
if let Some(new_coords) =
|
|
||||||
cheat_assign(**coordinates, point, map.width(), motion_blocked.0.clone())
|
|
||||||
{
|
|
||||||
**coordinates = new_coords;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
**path = new_path;
|
**path = new_path;
|
||||||
|
@ -204,13 +197,13 @@ fn negotiate_path(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
pub struct PathfindingPlugin;
|
pub struct PathfindingPlugin;
|
||||||
|
|
||||||
impl Plugin for PathfindingPlugin {
|
impl Plugin for PathfindingPlugin {
|
||||||
fn build(&self, app: &mut AppBuilder) {
|
fn build(&self, app: &mut AppBuilder) {
|
||||||
app.add_system_to_stage(CoreStage::PostUpdate, calculate_path.system())
|
app.add_system_to_stage(CoreStage::PostUpdate, calculate_path.system());
|
||||||
.add_system(negotiate_path.system());
|
//.add_system(negotiate_path.system());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user