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

View File

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