Compare commits

..

No commits in common. "1fb194266cd6a15c07d5159ab699df0154ce0eb3" and "e7c9da6f915a999452c9e464bafea208936faa99" have entirely different histories.

2 changed files with 102 additions and 105 deletions

View File

@ -493,13 +493,15 @@ impl GlobalTransformExt for GlobalTransform {
) -> String {
use bevy::math::Vec3Swizzles;
let scale = PHYSICS_SCALE.read().unwrap();
let (_, rotation, _) = self.to_scale_rotation_translation();
let pos1 = Isometry::new(
(self.translation() / *scale).xy().into(),
self.yaw().radians(),
rotation.to_scaled_axis().z,
);
let (_, other_rotation, _) = self.to_scale_rotation_translation();
let pos2 = Isometry::new(
(other.translation() / *scale).xy().into(),
other.yaw().radians(),
other_rotation.to_scaled_axis().z,
);
let closest =
closest_points(&pos1, &*collider.raw, &pos2, &*other_collider.raw, f32::MAX).unwrap();

View File

@ -1,7 +1,4 @@
use std::{
collections::HashMap, error::Error, f32::consts::PI, fmt::Debug, hash::Hash,
marker::PhantomData,
};
use std::{error::Error, f32::consts::PI, fmt::Debug, hash::Hash, marker::PhantomData};
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
@ -53,21 +50,20 @@ impl Default for RotationSpeed {
#[reflect(Component)]
pub struct Speed(pub f32);
#[derive(Component, Deref, DerefMut)]
struct SnapTimer(Timer);
#[derive(Component, Clone, Copy, Debug, Default, Reflect)]
#[reflect(Component)]
pub struct Sprinting;
impl Default for SnapTimer {
fn default() -> Self {
Self(Timer::from_seconds(0.25, false))
}
}
#[derive(Default, Deref, DerefMut)]
struct Snapping(bool);
fn movement_controls<S>(
mut commands: Commands,
config: Res<NavigationConfig<S>>,
time: Res<Time>,
snap_timers: Res<HashMap<Entity, SnapTimer>>,
mut query: Query<(
mut snapping: ResMut<Snapping>,
mut query: Query<
(
Entity,
&ActionState<NavigationAction>,
&mut Velocity,
@ -76,7 +72,9 @@ fn movement_controls<S>(
Option<&RotationSpeed>,
&mut Transform,
Option<&Destination>,
)>,
),
With<Player>,
>,
exploration_focused: Query<Entity, With<ExplorationFocused>>,
) where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
@ -92,35 +90,40 @@ fn movement_controls<S>(
destination,
) in &mut query
{
if **snapping {
if actions.pressed(NavigationAction::Rotate) {
continue;
}
**snapping = false;
continue;
} else {
let sprinting = actions.pressed(NavigationAction::Sprint);
if sprinting {
commands.entity(entity).insert(Sprinting);
} else {
commands.entity(entity).remove::<Sprinting>();
}
let mut cleanup = false;
if actions.pressed(NavigationAction::Move) {
if let Some(pair) = actions.clamped_axis_pair(NavigationAction::Move) {
cleanup = true;
let direction = pair.xy();
let forward_backward_movement_factor = if direction.x > 0. {
config.forward_movement_factor
let mut direction = pair.xy();
let strength = direction.length();
if direction.x > 0. {
direction.x *= config.forward_movement_factor;
} else if direction.x < 0. {
config.backward_movement_factor
} else {
0.
};
let movement_factor = if direction.x != 0. && direction.y != 0. {
config
.strafe_movement_factor
.min(forward_backward_movement_factor)
} else if direction.y != 0. {
config.strafe_movement_factor
} else {
forward_backward_movement_factor
};
let mut s = if sprinting {
direction.x *= config.backward_movement_factor;
}
if direction.y != 0. {
direction.y *= config.strafe_movement_factor;
}
let s = if sprinting {
**max_speed
} else {
**max_speed / config.sprint_movement_factor
};
s *= movement_factor;
**speed = s;
direction *= strength;
let mut v = direction * **speed;
v = transform
.compute_matrix()
@ -131,7 +134,6 @@ fn movement_controls<S>(
} else if destination.is_none() {
velocity.linvel = Vec2::ZERO;
}
if !snap_timers.contains_key(&entity) {
if let Some(rotation_speed) = rotation_speed {
if actions.pressed(NavigationAction::Rotate) {
cleanup = true;
@ -143,9 +145,6 @@ fn movement_controls<S>(
} else {
velocity.angvel = 0.;
}
} else {
velocity.angvel = 0.;
}
if cleanup {
commands.entity(entity).remove::<Destination>();
commands.entity(entity).remove::<Exploring>();
@ -154,14 +153,14 @@ fn movement_controls<S>(
}
}
}
}
}
fn snap(
fn snap<S>(
mut tts: ResMut<Tts>,
mut snap_timers: ResMut<HashMap<Entity, SnapTimer>>,
mut snapping: ResMut<Snapping>,
mut query: Query<
(
Entity,
&ActionState<NavigationAction>,
&mut Transform,
&mut Velocity,
@ -169,32 +168,33 @@ fn snap(
),
With<Player>,
>,
) -> Result<(), Box<dyn Error>> {
for (entity, actions, mut transform, mut velocity, direction) in query.iter_mut() {
) -> Result<(), Box<dyn Error>>
where
S: 'static + Copy + Debug + Eq + Hash + Send + Sync,
{
for (actions, mut transform, mut velocity, direction) in query.iter_mut() {
if actions.just_pressed(NavigationAction::SnapLeft) {
println!("snap left");
snap_timers.insert(entity, SnapTimer::default());
**snapping = true;
transform.rotation = Quat::from_rotation_z(match direction {
CardinalDirection::North => PI,
CardinalDirection::East => PI / 2.,
CardinalDirection::South => 0.,
CardinalDirection::West => -PI / 2.,
CardinalDirection::West => PI * 1.5,
});
velocity.angvel = 0.;
}
if actions.just_pressed(NavigationAction::SnapRight) {
println!("Snap right");
snap_timers.insert(entity, SnapTimer::default());
**snapping = true;
transform.rotation = Quat::from_rotation_z(match direction {
CardinalDirection::North => 0.,
CardinalDirection::East => -PI / 2.,
CardinalDirection::East => PI * 1.5,
CardinalDirection::South => PI,
CardinalDirection::West => PI / 2.,
});
velocity.angvel = 0.;
}
if actions.just_pressed(NavigationAction::SnapCardinal) {
snap_timers.insert(entity, SnapTimer::default());
**snapping = true;
let yaw: Angle = direction.into();
let yaw = yaw.radians();
transform.rotation = Quat::from_rotation_z(yaw);
@ -202,7 +202,7 @@ fn snap(
tts.speak(direction.to_string(), true)?;
}
if actions.just_pressed(NavigationAction::SnapReverse) {
snap_timers.insert(entity, SnapTimer::default());
**snapping = true;
transform.rotate(Quat::from_rotation_z(PI));
velocity.angvel = 0.;
}
@ -210,13 +210,6 @@ fn snap(
Ok(())
}
fn tick_snap_timers(time: Res<Time>, mut snap_timers: ResMut<HashMap<Entity, SnapTimer>>) {
for timer in snap_timers.values_mut() {
timer.tick(time.delta());
}
snap_timers.retain(|_, v| !v.finished());
}
fn update_direction(
mut commands: Commands,
mut query: Query<
@ -270,14 +263,6 @@ fn add_speed(mut commands: Commands, query: Query<Entity, (Added<Speed>, Without
}
}
fn limit_speed(mut query: Query<(&mut Speed, &MaxSpeed)>) {
for (mut speed, max_speed) in &mut query {
if **speed > **max_speed {
**speed = **max_speed;
}
}
}
fn remove_speed(removed: RemovedComponents<Speed>, mut query: Query<&mut Velocity>) {
for entity in removed.iter() {
if let Ok(mut velocity) = query.get_mut(entity) {
@ -377,29 +362,39 @@ where
.get_resource::<NavigationConfig<S>>()
.unwrap()
.clone();
app.init_resource::<HashMap<Entity, SnapTimer>>()
const SNAP: &str = "SNAP";
app.init_resource::<Snapping>()
.register_type::<MaxSpeed>()
.register_type::<RotationSpeed>()
.register_type::<Sprinting>()
.add_plugin(InputManagerPlugin::<NavigationAction>::default())
.add_system_to_stage(CoreStage::PreUpdate, update_direction)
.add_system_to_stage(CoreStage::PostUpdate, update_direction)
.add_system_to_stage(CoreStage::PostUpdate, remove_direction)
.add_system(tick_snap_timers)
.add_system(speak_direction.chain(error_handler))
.add_system(add_speed)
.add_system(limit_speed)
.add_system_to_stage(CoreStage::PostUpdate, remove_speed)
.add_system_to_stage(CoreStage::PostUpdate, log_area_descriptions::<S, A>);
const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS";
if config.movement_control_states.is_empty() {
app.add_system(movement_controls::<S>.label(MOVEMENT_CONTROLS))
.add_system(snap.chain(error_handler).before(MOVEMENT_CONTROLS));
.add_system(
snap::<S>
.chain(error_handler)
.label(SNAP)
.before(MOVEMENT_CONTROLS),
);
} else {
let states = config.movement_control_states;
for state in states {
app.add_system_set(
SystemSet::on_update(state)
.with_system(movement_controls::<S>.label(MOVEMENT_CONTROLS))
.with_system(snap.chain(error_handler).before(MOVEMENT_CONTROLS)),
.with_system(
snap::<S>
.chain(error_handler)
.label(SNAP)
.before(MOVEMENT_CONTROLS),
),
);
}
}