Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve ground detection #137

Merged
merged 3 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/platformer/main.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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