From 1e70c0a42df6a13973e3fd5a916a52094d3592fd Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Mon, 23 Jun 2025 21:21:09 -0400 Subject: [PATCH] cleanup --- src/components/character_controller.rs | 63 ------------------- src/plugins/character_controller.rs | 84 -------------------------- 2 files changed, 147 deletions(-) delete mode 100644 src/components/character_controller.rs delete mode 100644 src/plugins/character_controller.rs diff --git a/src/components/character_controller.rs b/src/components/character_controller.rs deleted file mode 100644 index 3a450a7..0000000 --- a/src/components/character_controller.rs +++ /dev/null @@ -1,63 +0,0 @@ -use bevy::prelude::*; -use bevy_rapier3d::prelude::*; - -#[derive(Component, Reflect, Clone, Copy)] -#[require( - GravityDirection, - RigidBody, - Transform, - ExternalForce, - LinearVelocity, - GravityScale, - CharacterMotion, - CharacterRotation -)] -pub struct CharacterController { - pub step_height: f32, - pub max_slope: f32, - height: f32, - radius: f32, - pub is_grounded: bool, -} - -impl Default for CharacterController { - fn default() -> Self { - return Self { - height: 1.0, - radius: 0.5, - max_slope: 0.5, - step_height: 0.25, - is_grounded: false, - }; - } -} -impl CharacterController { - #[allow(dead_code)] - pub fn with_size(mut self, height: f32, radius: f32) -> Self { - self.height = height; - self.radius = radius; - return self; - } - - pub fn height(&self) -> f32 { - self.height - } - - pub fn radius(&self) -> f32 { - self.radius - } -} -#[derive(Component, Default, Reflect)] -pub struct CharacterMotion(pub Vec3); - -#[derive(Component, Default, Reflect)] -pub struct CharacterRotation(pub Quat); - -#[derive(Component, Reflect)] -pub struct GravityDirection(pub Dir3); - -impl Default for GravityDirection { - fn default() -> Self { - return GravityDirection(Dir3::NEG_Y); - } -} diff --git a/src/plugins/character_controller.rs b/src/plugins/character_controller.rs deleted file mode 100644 index cbbadc4..0000000 --- a/src/plugins/character_controller.rs +++ /dev/null @@ -1,84 +0,0 @@ -use bevy::prelude::*; -use bevy_rapier3d::prelude::*; - -use crate::components::character_controller::{ - CharacterController, CharacterMotion, CharacterRotation, GravityDirection, -}; - -pub struct CharacterControllerPlugin; - -impl Plugin for CharacterControllerPlugin { - fn build(&self, app: &mut App) { - app.add_systems(PreStartup, setup_hooks); - app.add_systems(Update, is_grounded); - app.add_systems(Update, (apply_gravity, apply_forces, apply_motion).chain()); - } -} - -fn setup_hooks(world: &mut World) { - world - .register_component_hooks::() - .on_insert(|mut world, ctx| { - let controller = world.get::(ctx.entity).unwrap(); - let height = controller.height(); - let radius = controller.radius(); - - let mut commands = world.commands(); - let mut entity_commands = commands.entity(ctx.entity); - entity_commands.insert(( - RigidBody::KinematicPositionBased, - Collider::capsule_y(radius, height), - ExternalForce::default(), - )); - }); -} - -fn is_grounded(controllers: Query<(&mut CharacterController, &RayHits)>) { - for (mut controller, hits) in controllers { - controller.is_grounded = !hits.is_empty(); - println!("{}", controller.is_grounded); - } -} - -fn apply_gravity( - controllers: Query<( - &mut ExternalForce, - &GravityScale, - &GravityDirection, - Option<&Mass>, - &CharacterController, - )>, - time: Res