From cafba57e0e0fcf35927497693efcc38985658374 Mon Sep 17 00:00:00 2001 From: janos-r <30606201+janos-r@users.noreply.github.com> Date: Thu, 24 Nov 2022 04:13:16 +0100 Subject: [PATCH] example: improve ground detection in platformer example (#137) * improve ground detection * remove DebugRender --- examples/platformer/main.rs | 1 + examples/platformer/systems.rs | 62 ++++++++++++++++------------------ 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/examples/platformer/main.rs b/examples/platformer/main.rs index c2c4db38..0763702a 100644 --- a/examples/platformer/main.rs +++ b/examples/platformer/main.rs @@ -37,6 +37,7 @@ fn main() { .add_system(systems::dbg_player_items) .add_system(systems::spawn_ground_sensor) .add_system(systems::ground_detection) + .add_system(systems::update_on_ground) .add_system(systems::restart_level) .register_ldtk_int_cell::(1) .register_ldtk_int_cell::(2) diff --git a/examples/platformer/systems.rs b/examples/platformer/systems.rs index 67f68abb..a072e801 100644 --- a/examples/platformer/systems.rs +++ b/examples/platformer/systems.rs @@ -442,47 +442,45 @@ pub fn spawn_ground_sensor( } pub fn ground_detection( - mut ground_detectors: Query<&mut GroundDetection>, - mut ground_sensors: Query<(Entity, &mut GroundSensor)>, + mut ground_sensors: Query<&mut GroundSensor>, mut collisions: EventReader, - collidables: Query, Without)>, + collidables: Query, Without>, ) { - for (entity, mut ground_sensor) in &mut ground_sensors { - for collision in collisions.iter() { - match collision { - CollisionEvent::Started(collider_a, collider_b, _) => { - let (sensor, other) = if *collider_a == entity { - (collider_a, collider_b) - } else if *collider_b == entity { - (collider_b, collider_a) - } else { - continue; - }; - - if collidables.contains(*other) && *sensor == entity { - ground_sensor.intersecting_ground_entities.insert(*other); + for collision_event in collisions.iter() { + match collision_event { + CollisionEvent::Started(e1, e2, _) => { + if collidables.contains(*e1) { + if let Ok(mut sensor) = ground_sensors.get_mut(*e2) { + sensor.intersecting_ground_entities.insert(*e1); + } + } else if collidables.contains(*e2) { + if let Ok(mut sensor) = ground_sensors.get_mut(*e1) { + sensor.intersecting_ground_entities.insert(*e2); } } - CollisionEvent::Stopped(collider_a, collider_b, _) => { - let (sensor, other) = if *collider_a == entity { - (collider_a, collider_b) - } else if *collider_b == entity { - (collider_b, collider_a) - } else { - continue; - }; - - if collidables.contains(*other) && *sensor == entity { - ground_sensor.intersecting_ground_entities.remove(other); + } + CollisionEvent::Stopped(e1, e2, _) => { + if collidables.contains(*e1) { + if let Ok(mut sensor) = ground_sensors.get_mut(*e2) { + sensor.intersecting_ground_entities.remove(e1); + } + } else if collidables.contains(*e2) { + if let Ok(mut sensor) = ground_sensors.get_mut(*e1) { + sensor.intersecting_ground_entities.remove(e2); } } } } + } +} - if let Ok(mut ground_detection) = - ground_detectors.get_mut(ground_sensor.ground_detection_entity) - { - ground_detection.on_ground = !ground_sensor.intersecting_ground_entities.is_empty(); +pub fn update_on_ground( + mut ground_detectors: Query<&mut GroundDetection>, + ground_sensors: Query<&GroundSensor, Changed>, +) { + for sensor in &ground_sensors { + if let Ok(mut ground_detection) = ground_detectors.get_mut(sensor.ground_detection_entity) { + ground_detection.on_ground = !sensor.intersecting_ground_entities.is_empty(); } } }