From f6b640d06c4fd39f7de416fc87f7cc5fd3413ace Mon Sep 17 00:00:00 2001 From: GitGhillie Date: Sat, 22 Mar 2025 20:32:09 +0100 Subject: [PATCH] Refactor controls (#14) * Move controls * Use system sets * Use control states for controller * Unite controls * move collisions part * Right deadzone as well * Remove comment --- src/camera.rs | 2 +- src/control/collisions.rs | 169 +++++++++++ src/control/controller.rs | 315 ++++++++++++++++++++ src/{ => control}/controls.rs | 68 ++--- src/control/mod.rs | 33 +++ src/controller.rs | 539 ---------------------------------- src/main.rs | 7 +- src/player.rs | 4 +- 8 files changed, 555 insertions(+), 582 deletions(-) create mode 100644 src/control/collisions.rs create mode 100644 src/control/controller.rs rename src/{ => control}/controls.rs (79%) create mode 100644 src/control/mod.rs delete mode 100644 src/controller.rs diff --git a/src/camera.rs b/src/camera.rs index 4ad8fff..b68b5c2 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,7 +1,7 @@ use avian3d::prelude::*; use bevy::prelude::*; -use crate::{controller::PlayerMovement, controls::Controls, physics_layers::GameLayer}; +use crate::{control::Controls, control::controller::PlayerMovement, physics_layers::GameLayer}; #[derive(Component, Reflect, Debug)] pub struct CameraTarget; diff --git a/src/control/collisions.rs b/src/control/collisions.rs new file mode 100644 index 0000000..6533900 --- /dev/null +++ b/src/control/collisions.rs @@ -0,0 +1,169 @@ +use avian3d::{math::*, prelude::*}; +use bevy::prelude::*; + +use super::controller::{CharacterController, MaxSlopeAngle}; + +/// Kinematic bodies do not get pushed by collisions by default, +/// so it needs to be done manually. +/// +/// This system handles collision response for kinematic character controllers +/// by pushing them along their contact normals by the current penetration depth, +/// and applying velocity corrections in order to snap to slopes, slide along walls, +/// and predict collisions using speculative contacts. +#[allow(clippy::type_complexity)] +pub fn kinematic_controller_collisions( + collisions: Res, + bodies: Query<&RigidBody>, + collider_parents: Query<&ColliderParent, Without>, + mut character_controllers: Query< + ( + &mut Position, + &Rotation, + &mut LinearVelocity, + Option<&MaxSlopeAngle>, + ), + (With, With), + >, + time: Res