From f80b7e380a19bdecb0e97fa5a8366a7fa4fa517c Mon Sep 17 00:00:00 2001 From: Mitchell M Date: Sat, 24 Jan 2026 22:41:38 -0600 Subject: [PATCH] more avian workd --- src/avian.rs | 200 ++++++++++++--------------------------------------- src/main.rs | 11 +-- 2 files changed, 49 insertions(+), 162 deletions(-) diff --git a/src/avian.rs b/src/avian.rs index aea431e..48428d6 100644 --- a/src/avian.rs +++ b/src/avian.rs @@ -6,15 +6,7 @@ pub struct CharacterControllerPlugin; impl Plugin for CharacterControllerPlugin { fn build(&self, app: &mut App) { app.add_message::() - .add_systems( - Update, - ( - (keyboard_input, gamepad_input), - movement, - apply_movement_damping, - ) - .chain(), - ) + .add_systems(Update, ((keyboard_input, gamepad_input), movement).chain()) .add_systems( // Run collision handling after collision detection. // @@ -27,31 +19,20 @@ impl Plugin for CharacterControllerPlugin { } /// A [`Message`] written for a movement input action. -#[derive(Message)] +#[derive(Message, Default, Copy, Clone)] pub struct MovementAction(Vec2); /// A marker component indicating that an entity is using a character controller. -#[derive(Component)] +#[derive(Component, Default, Copy, Clone)] pub struct CharacterController; -/// A marker component indicating that an entity is on the ground. -#[derive(Component)] -#[component(storage = "SparseSet")] -pub struct Grounded; +/// The max speed for a CharacterController. +#[derive(Component, Default, Copy, Clone)] +pub struct MaxSpeed(Scalar); -/// The acceleration used for character movement. -#[derive(Component)] -pub struct MovementAcceleration(Scalar); - -/// The damping factor used for slowing down movement. -#[derive(Component)] -pub struct MovementDampingFactor(Scalar); - -/// The maximum angle a slope can have for a character controller -/// to be able to climb and jump. If the slope is steeper than this angle, -/// the character will slide down. -#[derive(Component)] -pub struct MaxSlopeAngle(Scalar); +/// The max acceleration per second for a CharacterController. +#[derive(Component, Default, Copy, Clone)] +pub struct MaxAcceleration(Scalar); /// A bundle that contains the components needed for a basic /// kinematic character controller. @@ -60,59 +41,20 @@ pub struct CharacterControllerBundle { character_controller: CharacterController, body: RigidBody, collider: Collider, - ground_caster: ShapeCaster, - movement: MovementBundle, -} - -/// A bundle that contains components for character movement. -#[derive(Bundle)] -pub struct MovementBundle { - acceleration: MovementAcceleration, - damping: MovementDampingFactor, - max_slope_angle: MaxSlopeAngle, -} - -impl MovementBundle { - pub const fn new(acceleration: Scalar, damping: Scalar, max_slope_angle: Scalar) -> Self { - Self { - acceleration: MovementAcceleration(acceleration), - damping: MovementDampingFactor(damping), - max_slope_angle: MaxSlopeAngle(max_slope_angle), - } - } -} - -impl Default for MovementBundle { - fn default() -> Self { - Self::new(30.0, 0.99, PI * 0.45) - } + speed: MaxSpeed, + acceleration: MaxAcceleration, } impl CharacterControllerBundle { pub fn new(collider: Collider) -> Self { - // Create shape caster as a slightly smaller version of collider - let mut caster_shape = collider.clone(); - caster_shape.set_scale(Vector::ONE * 0.99, 10); - Self { character_controller: CharacterController, body: RigidBody::Kinematic, collider, - ground_caster: ShapeCaster::new(caster_shape, Vector::ZERO, 0.0, Dir2::NEG_Y) - .with_max_distance(10.0), - movement: MovementBundle::default(), + speed: MaxSpeed(10.), + acceleration: MaxAcceleration(10.), } } - - pub fn with_movement( - mut self, - acceleration: Scalar, - damping: Scalar, - max_slope_angle: Scalar, - ) -> Self { - self.movement = MovementBundle::new(acceleration, damping, max_slope_angle); - self - } } /// Sends [`MovementAction`] events based on keyboard input. @@ -159,43 +101,31 @@ fn gamepad_input(mut movement_writer: MessageWriter, gamepads: Q fn movement( time: Res