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::{ use bevy::{
prelude::*, prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle}, sprite::{MaterialMesh2dBundle, Mesh2dHandle},
utils::{HashMap, HashSet},
}; };
use bevy_rapier2d::prelude::*; use bevy_rapier2d::prelude::*;
@ -46,19 +47,17 @@ pub fn player_ground_collision(
mut collision_events: EventReader<CollisionEvent>, mut collision_events: EventReader<CollisionEvent>,
) { ) {
let (p, mut player) = player.single_mut(); let (p, mut player) = player.single_mut();
let scene_objs = scene_objs.iter().collect::<HashSet<_>>();
for collision_event in collision_events.read() { for collision_event in collision_events.read() {
match collision_event { match collision_event {
CollisionEvent::Started(e1, e2, _) => { CollisionEvent::Started(e1, e2, _) => {
if (e1 == &p && scene_objs.iter().any(|obj| *e2 == obj)) if (e1 == &p && scene_objs.contains(e2)) || (e2 == &p && scene_objs.contains(e1)) {
|| (e2 == &p && scene_objs.iter().any(|obj| *e1 == obj))
{
player.grounded = true; player.grounded = true;
} }
} }
CollisionEvent::Stopped(e1, e2, _) => { CollisionEvent::Stopped(e1, e2, _) => {
if (e1 == &p && scene_objs.iter().any(|obj| *e2 == obj)) if (e1 == &p && scene_objs.contains(e2)) || (e2 == &p && scene_objs.contains(e1)) {
|| (e2 == &p && scene_objs.iter().any(|obj| *e1 == obj))
{
player.grounded = false; player.grounded = false;
} }
} }
@ -91,7 +90,7 @@ pub fn move_player(
if kb_input.pressed(KeyCode::KeyW) && player.grounded { if kb_input.pressed(KeyCode::KeyW) && player.grounded {
moved = true; moved = true;
*vel = Velocity::linear(Vec2::new(0., 9. * METER)); *vel = Velocity::linear(Vec2::new(0., 9. * METER));
// *vel = Velocity::linear(Vec2::new(0., 9.81 * METER)); // *vel = Velocity::linear(Vec2::new(0., 9.81 * METER));
} }