Skip to content

Commit

Permalink
example: improve ground detection in platformer example (#137)
Browse files Browse the repository at this point in the history
* improve ground detection

* remove DebugRender
  • Loading branch information
janos-r committed Nov 24, 2022
1 parent 5b24216 commit cafba57
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
1 change: 1 addition & 0 deletions examples/platformer/main.rs
Expand Up @@ -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::<components::WallBundle>(1)
.register_ldtk_int_cell::<components::LadderBundle>(2)
Expand Down
62 changes: 30 additions & 32 deletions examples/platformer/systems.rs
Expand Up @@ -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<CollisionEvent>,
collidables: Query<Entity, (With<Collider>, Without<Sensor>)>,
collidables: Query<With<Collider>, Without<Sensor>>,
) {
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<GroundSensor>>,
) {
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();
}
}
}
Expand Down

0 comments on commit cafba57

Please sign in to comment.