cleanup
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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::<CharacterController>()
|
||||
.on_insert(|mut world, ctx| {
|
||||
let controller = world.get::<CharacterController>(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<Time>,
|
||||
) {
|
||||
for (mut force, grav_scale, grav_dir, mass, controller) in controllers {
|
||||
if controller.is_grounded {
|
||||
continue;
|
||||
}
|
||||
let m = if let Some(mass) = mass { mass.0 } else { 1.0 };
|
||||
force.apply_force(grav_dir.0 * grav_scale.0 * 9.3 * m * time.delta_secs());
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_motion(
|
||||
controllers: Query<
|
||||
(&LinearVelocity, &CharacterMotion, &CharacterRotation, &mut Transform),
|
||||
With<CharacterController>,
|
||||
>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
for (lin_vel, motion, _rotation, mut transform) in controllers {
|
||||
let move_vec = transform.rotation * motion.0;
|
||||
let vel = (lin_vel.0 + move_vec) * time.delta_secs();
|
||||
transform.translation += vel;
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_forces(
|
||||
controllers: Query<(&mut LinearVelocity, &mut ExternalForce), With<CharacterController>>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
for (mut vel, mut force) in controllers {
|
||||
vel.0 += force.force() * time.delta_secs();
|
||||
force.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user