add world plugin
This commit is contained in:
+32
-20
@@ -26,6 +26,10 @@ pub struct MaxSpeed(Scalar);
|
||||
#[derive(Component, Default, Copy, Clone)]
|
||||
pub struct MaxAcceleration(Scalar);
|
||||
|
||||
/// Whether inputs are enabled for a CharacterController.
|
||||
#[derive(Component, Default, Copy, Clone)]
|
||||
pub struct InputEnabled(pub bool);
|
||||
|
||||
/// A bundle that contains the components needed for a basic
|
||||
/// kinematic character controller.
|
||||
#[derive(Bundle)]
|
||||
@@ -35,16 +39,18 @@ pub struct CharacterControllerBundle {
|
||||
collider: Collider,
|
||||
speed: MaxSpeed,
|
||||
acceleration: MaxAcceleration,
|
||||
enabled: InputEnabled,
|
||||
}
|
||||
|
||||
impl CharacterControllerBundle {
|
||||
pub fn new(collider: Collider) -> Self {
|
||||
Self {
|
||||
character_controller: CharacterController,
|
||||
body: RigidBody::Kinematic,
|
||||
body: RigidBody::Dynamic,
|
||||
collider,
|
||||
speed: MaxSpeed(300.),
|
||||
acceleration: MaxAcceleration(5000.),
|
||||
acceleration: MaxAcceleration(50000.),
|
||||
enabled: InputEnabled(true),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,31 +99,37 @@ fn gamepad_input(mut movement_writer: MessageWriter<MovementAction>, gamepads: Q
|
||||
fn movement(
|
||||
time: Res<Time>,
|
||||
mut movement_reader: MessageReader<MovementAction>,
|
||||
mut controllers: Query<(&MaxSpeed, &MaxAcceleration, &mut LinearVelocity)>,
|
||||
mut controllers: Query<(&MaxSpeed, &MaxAcceleration, &mut LinearVelocity, &InputEnabled)>,
|
||||
) {
|
||||
// Precision is adjusted so that the example works with
|
||||
// both the `f32` and `f64` features. Otherwise you don't need this.
|
||||
let delta_time = time.delta_secs_f64().adjust_precision();
|
||||
|
||||
for (max_speed, max_acceleration, mut linear_velocity) in &mut controllers {
|
||||
while movement_reader.len() > 1 {
|
||||
warn!("Extra movement message. Ignoring");
|
||||
movement_reader.read();
|
||||
}
|
||||
for (max_speed, max_acceleration, mut linear_velocity, enabled) in &mut controllers {
|
||||
if enabled.0 {
|
||||
// let len = movement_reader.len();
|
||||
// while len > 1 {
|
||||
// warn!("Extra movement message. Ignoring");
|
||||
// println!("{}", len);
|
||||
// let _ = movement_reader.read().take(len - 1).count();
|
||||
// }
|
||||
|
||||
let target = movement_reader
|
||||
.read()
|
||||
.next()
|
||||
.map(|ma| ma.0)
|
||||
.unwrap_or_default()
|
||||
* max_speed.0;
|
||||
let target = movement_reader
|
||||
.read()
|
||||
.last()
|
||||
.map(|ma| ma.0)
|
||||
.unwrap_or_default()
|
||||
* max_speed.0;
|
||||
|
||||
let mut delta = target - **linear_velocity;
|
||||
let delta_len = delta.length();
|
||||
let max_acceleration = max_acceleration.0 * delta_time;
|
||||
if delta_len > max_acceleration {
|
||||
delta = delta.normalize() * max_acceleration;
|
||||
let mut delta = target - **linear_velocity;
|
||||
let delta_len = delta.length();
|
||||
let max_acceleration = max_acceleration.0 * delta_time;
|
||||
if delta_len > max_acceleration {
|
||||
delta = delta.normalize() * max_acceleration;
|
||||
}
|
||||
**linear_velocity += delta;
|
||||
} else {
|
||||
**linear_velocity = Vec2::ZERO;
|
||||
}
|
||||
**linear_velocity += delta;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user