clean up player grounding detection

This commit is contained in:
Schrottkatze 2024-05-10 20:00:31 +02:00
parent c73b3c7066
commit 6a63d13395
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc

View file

@ -1,6 +1,7 @@
use bevy::{
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
utils::{HashMap, HashSet},
};
use bevy_rapier2d::prelude::*;
@ -46,19 +47,17 @@ pub fn player_ground_collision(
mut collision_events: EventReader<CollisionEvent>,
) {
let (p, mut player) = player.single_mut();
let scene_objs = scene_objs.iter().collect::<HashSet<_>>();
for collision_event in collision_events.read() {
match collision_event {
CollisionEvent::Started(e1, e2, _) => {
if (e1 == &p && scene_objs.iter().any(|obj| *e2 == obj))
|| (e2 == &p && scene_objs.iter().any(|obj| *e1 == obj))
{
if (e1 == &p && scene_objs.contains(e2)) || (e2 == &p && scene_objs.contains(e1)) {
player.grounded = true;
}
}
CollisionEvent::Stopped(e1, e2, _) => {
if (e1 == &p && scene_objs.iter().any(|obj| *e2 == obj))
|| (e2 == &p && scene_objs.iter().any(|obj| *e1 == obj))
{
if (e1 == &p && scene_objs.contains(e2)) || (e2 == &p && scene_objs.contains(e1)) {
player.grounded = false;
}
}