Introduce physics to movement controls.

This commit is contained in:
Nolan Darilek 2021-06-02 16:53:35 -05:00
parent 1c970a3f41
commit b6fe9d420e
2 changed files with 36 additions and 46 deletions

View File

@ -58,10 +58,6 @@ pub struct Speed(pub f32);
#[reflect(Component)]
pub struct Sprinting;
#[derive(Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
#[reflect(Component)]
pub struct Velocity(pub Vec2);
#[derive(Clone, Copy, Debug)]
pub struct Collision {
pub entity: Entity,
@ -105,31 +101,25 @@ fn movement_controls<S, A: 'static>(
config: Res<NavigationConfig<S, A>>,
input: Res<InputMap<A>>,
time: Res<Time>,
mut query: Query<(
mut query: Query<
(
Entity,
&Player,
&mut Velocity,
&mut RigidBodyVelocity,
&mut Speed,
&MaxSpeed,
Option<&RotationSpeed>,
&mut Transform,
Option<&Destination>,
)>,
),
With<Player>,
>,
exploration_focused: Query<(Entity, &ExplorationFocused)>,
) where
S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash,
A: Hash + Eq + Clone + Send + Sync,
{
for (
entity,
_,
mut velocity,
mut speed,
max_speed,
rotation_speed,
mut transform,
destination,
) in query.iter_mut()
for (entity, mut velocity, mut speed, max_speed, rotation_speed, mut transform, destination) in
query.iter_mut()
{
let sprinting = if let Some(action) = config.action_sprint.clone() {
input.active(action)
@ -176,6 +166,11 @@ fn movement_controls<S, A: 'static>(
}
}
if direction.length_squared() != 0. {
commands.entity(entity).remove::<Destination>();
commands.entity(entity).remove::<Exploring>();
for (entity, _) in exploration_focused.iter() {
commands.entity(entity).remove::<ExplorationFocused>();
}
direction = direction.normalize();
if direction.x > 0. {
direction.x *= config.forward_movement_factor;
@ -201,7 +196,7 @@ fn movement_controls<S, A: 'static>(
let right_y = input.strength(right).abs();
let left_y = input.strength(left).abs();
let y = if right_y > left_y { right_y } else { left_y };
Some(Vec3::new(x, y, 0.))
Some(Vec2::new(x, y))
} else {
None
};
@ -210,26 +205,20 @@ fn movement_controls<S, A: 'static>(
} else {
**max_speed / config.sprint_movement_factor
};
speed.0 = s;
direction *= s;
if let Some(strength) = strength {
direction *= strength;
}
commands.entity(entity).remove::<Destination>();
commands.entity(entity).remove::<Exploring>();
for (entity, _) in exploration_focused.iter() {
commands.entity(entity).remove::<ExplorationFocused>();
}
**speed = s;
direction = transform.compute_matrix().transform_vector3(direction);
let direction = Vec2::new(direction.x, direction.y);
**velocity = direction;
let mut v = Vec2::new(direction.x, direction.y) * **speed;
if let Some(strength) = strength {
v *= strength;
}
velocity.linvel = v.into();
} else if destination.is_none() {
**velocity = Vec2::ZERO;
speed.0 = 0.;
velocity.linvel = Vec2::ZERO.into();
**speed = 0.;
} else if sprinting {
speed.0 = max_speed.0;
**speed = **max_speed;
} else {
speed.0 = max_speed.0 / 3.;
**speed = **max_speed / 3.;
}
}
}

View File

@ -1,6 +1,7 @@
use std::collections::HashMap;
use bevy::{prelude::*, tasks::prelude::*};
use bevy_rapier2d::prelude::*;
use crossbeam_channel::{unbounded, Receiver};
use derive_more::{Deref, DerefMut};
use pathfinding::prelude::*;
@ -8,7 +9,7 @@ use pathfinding::prelude::*;
use crate::{
core::{Coordinates, PointLike},
map::Map,
navigation::{MotionBlocked, RotationSpeed, Speed, Velocity},
navigation::{MotionBlocked, RotationSpeed, Speed},
};
#[derive(Clone, Copy, Debug, Default, Deref, DerefMut, Eq, Hash, PartialEq, Reflect)]
@ -123,7 +124,7 @@ fn negotiate_path(
Entity,
&mut Path,
&mut Coordinates,
&mut Velocity,
&mut RigidBodyVelocity,
&Speed,
Option<&RotationSpeed>,
&mut Transform,
@ -192,14 +193,14 @@ fn negotiate_path(
}
**coordinates = (cheat_x, cheat_y);
}
**velocity = Vec2::ZERO;
velocity.linvel = Vec2::ZERO.into();
} else {
**velocity = direction;
velocity.linvel = direction.into();
}
} else {
commands.entity(entity).remove::<Path>();
commands.entity(entity).remove::<Destination>();
**velocity = Vec2::ZERO;
velocity.linvel = Vec2::ZERO.into();
}
}
}