Compare commits
5 Commits
da413ef4e4
...
627f63211d
Author | SHA1 | Date | |
---|---|---|---|
627f63211d | |||
c0a1befd98 | |||
4495174be0 | |||
5f14a90b92 | |||
2ff8a858ed |
12
Cargo.toml
12
Cargo.toml
|
@ -11,7 +11,7 @@ edition = "2021"
|
||||||
speech_dispatcher_0_10 = ["bevy_tts/speech_dispatcher_0_10"]
|
speech_dispatcher_0_10 = ["bevy_tts/speech_dispatcher_0_10"]
|
||||||
|
|
||||||
[dependencies.bevy]
|
[dependencies.bevy]
|
||||||
version = "0.7"
|
version = "0.8"
|
||||||
default-features = false
|
default-features = false
|
||||||
features = [
|
features = [
|
||||||
"bevy_gilrs",
|
"bevy_gilrs",
|
||||||
|
@ -24,14 +24,14 @@ features = [
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
backtrace = "0.3"
|
backtrace = "0.3"
|
||||||
bevy_input_actionmap = { git = "https://github.com/lightsoutgames/bevy_input_actionmap" }
|
bevy_rapier2d = { version = "0.16", features = ["serde-serialize"] }
|
||||||
bevy_rapier2d = { version = "0.15", features = ["serde-serialize"] }
|
bevy_synthizer = { git = "https://labs.lightsout.games/projects/bevy_synthizer", branch = "bevy-0.8" }
|
||||||
bevy_synthizer = { git = "https://labs.lightsout.games/projects/bevy_synthizer" }
|
bevy_tts = { git = "https://github.com/lightsoutgames/bevy_tts", default-features = false, features = ["tolk"], branch = "bevy-0.8" }
|
||||||
bevy_tts = { git = "https://github.com/lightsoutgames/bevy_tts", default-features = false, features = ["tolk"] }
|
|
||||||
coord_2d = "0.3"
|
coord_2d = "0.3"
|
||||||
futures-lite = "1"
|
futures-lite = "1"
|
||||||
gilrs = "0.8"
|
gilrs = "0.9"
|
||||||
here_be_dragons = "0.1"
|
here_be_dragons = "0.1"
|
||||||
|
leafwing_input_manager = { git = "https://github.com/ndarilek/leafwing-input-manager", branch = "dev" }
|
||||||
maze_generator = "2"
|
maze_generator = "2"
|
||||||
once_cell = "1"
|
once_cell = "1"
|
||||||
pathfinding = "3"
|
pathfinding = "3"
|
||||||
|
|
33
src/core.rs
33
src/core.rs
|
@ -7,10 +7,9 @@ use std::{
|
||||||
sync::RwLock,
|
sync::RwLock,
|
||||||
};
|
};
|
||||||
|
|
||||||
use bevy::{core::FloatOrd, ecs::query::WorldQuery, prelude::*};
|
use bevy::{ecs::query::WorldQuery, prelude::*, utils::FloatOrd};
|
||||||
use bevy_rapier2d::{
|
use bevy_rapier2d::{
|
||||||
na,
|
parry::query::{closest_points, distance, ClosestPoints},
|
||||||
parry::query::ClosestPoints,
|
|
||||||
prelude::*,
|
prelude::*,
|
||||||
rapier::{math::Isometry, prelude::AABB},
|
rapier::{math::Isometry, prelude::AABB},
|
||||||
};
|
};
|
||||||
|
@ -391,11 +390,11 @@ impl PointLike for &Transform {
|
||||||
|
|
||||||
impl PointLike for GlobalTransform {
|
impl PointLike for GlobalTransform {
|
||||||
fn x(&self) -> f32 {
|
fn x(&self) -> f32 {
|
||||||
self.translation.x
|
self.translation().x
|
||||||
}
|
}
|
||||||
|
|
||||||
fn y(&self) -> f32 {
|
fn y(&self) -> f32 {
|
||||||
self.translation.y
|
self.translation().y
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -482,7 +481,7 @@ pub trait GlobalTransformExt {
|
||||||
|
|
||||||
impl GlobalTransformExt for GlobalTransform {
|
impl GlobalTransformExt for GlobalTransform {
|
||||||
fn yaw(&self) -> Angle {
|
fn yaw(&self) -> Angle {
|
||||||
let forward = self.local_x();
|
let forward = self.right();
|
||||||
Angle::Radians(forward.y.atan2(forward.x))
|
Angle::Radians(forward.y.atan2(forward.x))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -494,27 +493,25 @@ impl GlobalTransformExt for GlobalTransform {
|
||||||
) -> String {
|
) -> String {
|
||||||
use bevy::math::Vec3Swizzles;
|
use bevy::math::Vec3Swizzles;
|
||||||
let scale = PHYSICS_SCALE.read().unwrap();
|
let scale = PHYSICS_SCALE.read().unwrap();
|
||||||
|
let (_, rotation, _) = self.to_scale_rotation_translation();
|
||||||
let pos1 = Isometry::new(
|
let pos1 = Isometry::new(
|
||||||
(self.translation / *scale).xy().into(),
|
(self.translation() / *scale).xy().into(),
|
||||||
self.rotation.to_scaled_axis().z,
|
rotation.to_scaled_axis().z,
|
||||||
);
|
);
|
||||||
|
let (_, other_rotation, _) = self.to_scale_rotation_translation();
|
||||||
let pos2 = Isometry::new(
|
let pos2 = Isometry::new(
|
||||||
(other.translation / *scale).xy().into(),
|
(other.translation() / *scale).xy().into(),
|
||||||
other.rotation.to_scaled_axis().z,
|
other_rotation.to_scaled_axis().z,
|
||||||
);
|
);
|
||||||
let closest = bevy_rapier2d::parry::query::closest_points(
|
let closest =
|
||||||
|
closest_points(&pos1, &*collider.raw, &pos2, &*other_collider.raw, f32::MAX).unwrap();
|
||||||
|
let distance = distance(
|
||||||
&pos1,
|
&pos1,
|
||||||
&*collider.raw,
|
&*collider.raw,
|
||||||
&pos2,
|
&pos2,
|
||||||
&*other_collider.raw,
|
&*other_collider.raw,
|
||||||
f32::MAX,
|
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap() as u32;
|
||||||
let distance: u32 = match closest {
|
|
||||||
ClosestPoints::Intersecting => 0,
|
|
||||||
ClosestPoints::WithinMargin(p1, p2) => na::distance(&p1, &p2) as u32,
|
|
||||||
ClosestPoints::Disjoint => 0,
|
|
||||||
};
|
|
||||||
let tile_or_tiles = if distance == 1 { "tile" } else { "tiles" };
|
let tile_or_tiles = if distance == 1 { "tile" } else { "tiles" };
|
||||||
if distance > 0 {
|
if distance > 0 {
|
||||||
if let ClosestPoints::WithinMargin(p1, p2) = closest {
|
if let ClosestPoints::WithinMargin(p1, p2) = closest {
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use std::{error::Error, fmt::Debug, hash::Hash, marker::PhantomData};
|
use std::{error::Error, fmt::Debug, hash::Hash, marker::PhantomData};
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_input_actionmap::InputMap;
|
|
||||||
use bevy_rapier2d::prelude::*;
|
use bevy_rapier2d::prelude::*;
|
||||||
use bevy_tts::Tts;
|
use bevy_tts::Tts;
|
||||||
|
use leafwing_input_manager::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{Player, PointLike},
|
core::{Player, PointLike},
|
||||||
|
@ -13,6 +13,19 @@ use crate::{
|
||||||
visibility::{RevealedTiles, Viewshed, Visible, VisibleEntities},
|
visibility::{RevealedTiles, Viewshed, Visible, VisibleEntities},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug)]
|
||||||
|
pub enum ExplorationAction {
|
||||||
|
Forward,
|
||||||
|
Backward,
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
FocusNext,
|
||||||
|
FocusPrev,
|
||||||
|
SelectNextType,
|
||||||
|
SelectPrevType,
|
||||||
|
NavigateTo,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Reflect)]
|
#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Explorable;
|
pub struct Explorable;
|
||||||
|
@ -34,85 +47,75 @@ pub struct FocusedExplorationType<T>(pub Option<T>);
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Mappable;
|
pub struct Mappable;
|
||||||
|
|
||||||
fn exploration_type_change<T, S, A: 'static>(
|
fn exploration_type_change<T, S>(
|
||||||
config: Res<ExplorationConfig<S, A>>,
|
|
||||||
mut tts: ResMut<Tts>,
|
mut tts: ResMut<Tts>,
|
||||||
input: Res<InputMap<A>>,
|
mut explorers: Query<(
|
||||||
mut explorers: Query<(&VisibleEntities, &mut FocusedExplorationType<T>)>,
|
&ActionState<ExplorationAction>,
|
||||||
|
&VisibleEntities,
|
||||||
|
&mut FocusedExplorationType<T>,
|
||||||
|
)>,
|
||||||
features: Query<&T>,
|
features: Query<&T>,
|
||||||
) -> Result<(), Box<dyn Error>>
|
) -> Result<(), Box<dyn Error>>
|
||||||
where
|
where
|
||||||
T: Component + Copy + Ord,
|
T: Component + Copy + Ord,
|
||||||
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
||||||
A: Hash + Eq + Clone + Send + Sync,
|
|
||||||
{
|
{
|
||||||
if let (Some(select_next_type), Some(select_prev_type)) = (
|
for (actions, visible, mut focused) in explorers.iter_mut() {
|
||||||
config.action_explore_select_next_type.clone(),
|
let mut types: Vec<T> = vec![];
|
||||||
config.action_explore_select_prev_type.clone(),
|
for e in visible.iter() {
|
||||||
) {
|
if let Ok(t) = features.get(*e) {
|
||||||
let changed = input.just_active(select_next_type.clone())
|
types.push(*t);
|
||||||
|| input.just_active(select_prev_type.clone());
|
|
||||||
if !changed {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
for (visible, mut focused) in explorers.iter_mut() {
|
|
||||||
let mut types: Vec<T> = vec![];
|
|
||||||
for e in visible.iter() {
|
|
||||||
if let Ok(t) = features.get(*e) {
|
|
||||||
types.push(*t);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
types.sort();
|
}
|
||||||
types.dedup();
|
types.sort();
|
||||||
if types.is_empty() {
|
types.dedup();
|
||||||
tts.speak("Nothing visible.", true)?;
|
if types.is_empty() {
|
||||||
} else if input.just_active(select_prev_type.clone()) {
|
tts.speak("Nothing visible.", true)?;
|
||||||
if let Some(t) = &focused.0 {
|
} else if actions.just_pressed(ExplorationAction::SelectPrevType) {
|
||||||
if let Some(i) = types.iter().position(|v| *v == *t) {
|
if let Some(t) = &focused.0 {
|
||||||
if i == 0 {
|
if let Some(i) = types.iter().position(|v| *v == *t) {
|
||||||
focused.0 = None;
|
if i == 0 {
|
||||||
} else {
|
focused.0 = None;
|
||||||
let t = &types[i - 1];
|
|
||||||
focused.0 = Some(*t);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
let t = types.last().unwrap();
|
let t = &types[i - 1];
|
||||||
focused.0 = Some(*t);
|
focused.0 = Some(*t);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let t = types.last().unwrap();
|
let t = types.last().unwrap();
|
||||||
focused.0 = Some(*t);
|
focused.0 = Some(*t);
|
||||||
}
|
}
|
||||||
} else if input.just_active(select_next_type.clone()) {
|
} else {
|
||||||
if let Some(t) = &focused.0 {
|
let t = types.last().unwrap();
|
||||||
if let Some(i) = types.iter().position(|v| *v == *t) {
|
focused.0 = Some(*t);
|
||||||
if i == types.len() - 1 {
|
}
|
||||||
focused.0 = None;
|
} else if actions.just_pressed(ExplorationAction::SelectNextType) {
|
||||||
} else {
|
if let Some(t) = &focused.0 {
|
||||||
let t = &types[i + 1];
|
if let Some(i) = types.iter().position(|v| *v == *t) {
|
||||||
focused.0 = Some(*t);
|
if i == types.len() - 1 {
|
||||||
}
|
focused.0 = None;
|
||||||
} else {
|
} else {
|
||||||
let t = types.first().unwrap();
|
let t = &types[i + 1];
|
||||||
focused.0 = Some(*t);
|
focused.0 = Some(*t);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let t = types.first().unwrap();
|
let t = types.first().unwrap();
|
||||||
focused.0 = Some(*t)
|
focused.0 = Some(*t);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
let t = types.first().unwrap();
|
||||||
|
focused.0 = Some(*t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exploration_type_focus<T, S, A>(
|
fn exploration_type_focus<T, S>(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
config: Res<ExplorationConfig<S, A>>,
|
|
||||||
input: Res<InputMap<A>>,
|
|
||||||
mut tts: ResMut<Tts>,
|
mut tts: ResMut<Tts>,
|
||||||
explorers: Query<(
|
explorers: Query<(
|
||||||
Entity,
|
Entity,
|
||||||
|
&ActionState<ExplorationAction>,
|
||||||
&VisibleEntities,
|
&VisibleEntities,
|
||||||
&FocusedExplorationType<T>,
|
&FocusedExplorationType<T>,
|
||||||
Option<&Exploring>,
|
Option<&Exploring>,
|
||||||
|
@ -122,57 +125,46 @@ fn exploration_type_focus<T, S, A>(
|
||||||
where
|
where
|
||||||
T: Component + PartialEq,
|
T: Component + PartialEq,
|
||||||
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
||||||
A: 'static + Hash + Eq + Clone + Send + Sync,
|
|
||||||
{
|
{
|
||||||
if let (Some(explore_focus_next), Some(explore_focus_prev)) = (
|
for (entity, actions, visible_entities, focused_type, exploring) in explorers.iter() {
|
||||||
config.action_explore_focus_next.clone(),
|
let mut features = features
|
||||||
config.action_explore_focus_prev.clone(),
|
.iter()
|
||||||
) {
|
.filter(|v| visible_entities.contains(&v.0))
|
||||||
let changed = input.just_active(explore_focus_next.clone())
|
.map(|v| (v.1.floor(), v.2))
|
||||||
|| input.just_active(explore_focus_prev.clone());
|
.collect::<Vec<((f32, f32), &T)>>();
|
||||||
if !changed {
|
if features.is_empty() {
|
||||||
|
tts.speak("Nothing visible.", true)?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
for (entity, visible_entities, focused_type, exploring) in explorers.iter() {
|
features.sort_by(|(c1, _), (c2, _)| c1.partial_cmp(c2).unwrap());
|
||||||
let mut features = features
|
if let Some(focused) = &focused_type.0 {
|
||||||
.iter()
|
features.retain(|(_, t)| **t == *focused);
|
||||||
.filter(|v| visible_entities.contains(&v.0))
|
}
|
||||||
.map(|v| (v.1.floor(), v.2))
|
let mut target: Option<&((f32, f32), &T)> = None;
|
||||||
.collect::<Vec<((f32, f32), &T)>>();
|
if actions.just_pressed(ExplorationAction::FocusNext) {
|
||||||
if features.is_empty() {
|
if let Some(exploring) = exploring {
|
||||||
tts.speak("Nothing visible.", true)?;
|
target = features.iter().find(|(c, _)| *c > **exploring);
|
||||||
return Ok(());
|
if target.is_none() {
|
||||||
}
|
|
||||||
features.sort_by(|(c1, _), (c2, _)| c1.partial_cmp(c2).unwrap());
|
|
||||||
if let Some(focused) = &focused_type.0 {
|
|
||||||
features.retain(|(_, t)| **t == *focused);
|
|
||||||
}
|
|
||||||
let mut target: Option<&((f32, f32), &T)> = None;
|
|
||||||
if input.just_active(explore_focus_next.clone()) {
|
|
||||||
if let Some(exploring) = exploring {
|
|
||||||
target = features.iter().find(|(c, _)| *c > **exploring);
|
|
||||||
if target.is_none() {
|
|
||||||
target = features.first();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
target = features.first();
|
target = features.first();
|
||||||
}
|
}
|
||||||
} else if input.just_active(explore_focus_prev.clone()) {
|
} else {
|
||||||
if let Some(exploring) = exploring {
|
target = features.first();
|
||||||
features.reverse();
|
}
|
||||||
target = features.iter().find(|(c, _)| *c < **exploring);
|
} else if actions.just_pressed(ExplorationAction::FocusPrev) {
|
||||||
if target.is_none() {
|
if let Some(exploring) = exploring {
|
||||||
target = features.first();
|
features.reverse();
|
||||||
}
|
target = features.iter().find(|(c, _)| *c < **exploring);
|
||||||
} else {
|
if target.is_none() {
|
||||||
target = features.last();
|
target = features.first();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
target = features.last();
|
||||||
}
|
}
|
||||||
if let Some((coordinates, _)) = target {
|
}
|
||||||
commands
|
if let Some((coordinates, _)) = target {
|
||||||
.entity(entity)
|
commands
|
||||||
.insert(Exploring(coordinates.floor()));
|
.entity(entity)
|
||||||
}
|
.insert(Exploring(coordinates.floor()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -208,83 +200,71 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exploration_focus<S, A: 'static, D: 'static + Clone + Default + Send + Sync>(
|
fn exploration_focus<S, D: 'static + Clone + Default + Send + Sync>(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
config: Res<ExplorationConfig<S, A>>,
|
|
||||||
input: Res<InputMap<A>>,
|
|
||||||
map: Query<&Map<D>>,
|
map: Query<&Map<D>>,
|
||||||
explorers: Query<(Entity, &Transform, Option<&Exploring>), With<Player>>,
|
explorers: Query<
|
||||||
|
(
|
||||||
|
Entity,
|
||||||
|
&ActionState<ExplorationAction>,
|
||||||
|
&Transform,
|
||||||
|
Option<&Exploring>,
|
||||||
|
),
|
||||||
|
With<Player>,
|
||||||
|
>,
|
||||||
) where
|
) where
|
||||||
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
||||||
A: Hash + Eq + Clone + Send + Sync,
|
|
||||||
{
|
{
|
||||||
if let (
|
for (entity, actions, transform, exploring) in explorers.iter() {
|
||||||
Some(explore_forward),
|
let coordinates = transform.translation;
|
||||||
Some(explore_backward),
|
let mut exploring = if let Some(exploring) = exploring {
|
||||||
Some(explore_left),
|
**exploring
|
||||||
Some(explore_right),
|
} else {
|
||||||
) = (
|
let floor = coordinates.floor();
|
||||||
config.action_explore_forward.clone(),
|
(floor.x, floor.y)
|
||||||
config.action_explore_backward.clone(),
|
};
|
||||||
config.action_explore_left.clone(),
|
let orig = exploring;
|
||||||
config.action_explore_right.clone(),
|
if actions.just_pressed(ExplorationAction::Forward) {
|
||||||
) {
|
exploring.1 += 1.;
|
||||||
if let Ok((entity, transform, exploring)) = explorers.get_single() {
|
} else if actions.just_pressed(ExplorationAction::Backward) {
|
||||||
let coordinates = transform.translation;
|
exploring.1 -= 1.;
|
||||||
let mut exploring = if let Some(exploring) = exploring {
|
} else if actions.just_pressed(ExplorationAction::Left) {
|
||||||
**exploring
|
exploring.0 -= 1.;
|
||||||
} else {
|
} else if actions.just_pressed(ExplorationAction::Right) {
|
||||||
let floor = coordinates.floor();
|
exploring.0 += 1.;
|
||||||
(floor.x, floor.y)
|
}
|
||||||
};
|
let dimensions = if let Ok(map) = map.get_single() {
|
||||||
let orig = exploring;
|
Some((map.width as f32, map.height as f32))
|
||||||
if input.just_active(explore_forward) {
|
} else {
|
||||||
exploring.1 += 1.;
|
None
|
||||||
} else if input.just_active(explore_backward) {
|
};
|
||||||
exploring.1 -= 1.;
|
if let Some((width, height)) = dimensions {
|
||||||
} else if input.just_active(explore_left) {
|
if exploring.0 >= width || exploring.1 >= height {
|
||||||
exploring.0 -= 1.;
|
return;
|
||||||
} else if input.just_active(explore_right) {
|
|
||||||
exploring.0 += 1.;
|
|
||||||
}
|
|
||||||
let dimensions = if let Ok(map) = map.get_single() {
|
|
||||||
Some((map.width as f32, map.height as f32))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
if let Some((width, height)) = dimensions {
|
|
||||||
if exploring.0 >= width || exploring.1 >= height {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if orig != exploring && exploring.0 >= 0. && exploring.1 >= 0. {
|
|
||||||
commands.entity(entity).insert(Exploring(exploring));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if orig != exploring && exploring.0 >= 0. && exploring.1 >= 0. {
|
||||||
|
commands.entity(entity).insert(Exploring(exploring));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn navigate_to_explored<S, A: 'static, D: 'static + Clone + Default + Send + Sync>(
|
fn navigate_to_explored<S, D: 'static + Clone + Default + Send + Sync>(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
config: Res<ExplorationConfig<S, A>>,
|
|
||||||
input: Res<InputMap<A>>,
|
|
||||||
map: Query<(&Map<D>, &RevealedTiles)>,
|
map: Query<(&Map<D>, &RevealedTiles)>,
|
||||||
explorers: Query<(Entity, &Exploring)>,
|
explorers: Query<(Entity, &ActionState<ExplorationAction>, &Exploring)>,
|
||||||
) where
|
) where
|
||||||
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
||||||
A: Hash + Eq + Clone + Send + Sync,
|
|
||||||
{
|
{
|
||||||
if let Some(navigate_to_explored) = config.action_navigate_to_explored.clone() {
|
for (entity, actions, exploring) in explorers.iter() {
|
||||||
for (entity, exploring) in explorers.iter() {
|
for (map, revealed_tiles) in map.iter() {
|
||||||
for (map, revealed_tiles) in map.iter() {
|
let point = **exploring;
|
||||||
let point = **exploring;
|
let idx = point.to_index(map.width);
|
||||||
let idx = point.to_index(map.width);
|
let known = revealed_tiles[idx];
|
||||||
let known = revealed_tiles[idx];
|
if actions.just_pressed(ExplorationAction::NavigateTo) && known {
|
||||||
if input.just_active(navigate_to_explored.clone()) && known {
|
commands
|
||||||
commands
|
.entity(entity)
|
||||||
.entity(entity)
|
.insert(Destination((point.x_i32(), point.y_i32())));
|
||||||
.insert(Destination((point.x_i32(), point.y_i32())));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -388,32 +368,14 @@ fn cleanup(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct ExplorationConfig<S, A> {
|
pub struct ExplorationConfig<S> {
|
||||||
pub exploration_control_states: Vec<S>,
|
pub exploration_control_states: Vec<S>,
|
||||||
pub action_explore_forward: Option<A>,
|
|
||||||
pub action_explore_backward: Option<A>,
|
|
||||||
pub action_explore_left: Option<A>,
|
|
||||||
pub action_explore_right: Option<A>,
|
|
||||||
pub action_explore_focus_next: Option<A>,
|
|
||||||
pub action_explore_focus_prev: Option<A>,
|
|
||||||
pub action_explore_select_next_type: Option<A>,
|
|
||||||
pub action_explore_select_prev_type: Option<A>,
|
|
||||||
pub action_navigate_to_explored: Option<A>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, A> Default for ExplorationConfig<S, A> {
|
impl<S> Default for ExplorationConfig<S> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
exploration_control_states: vec![],
|
exploration_control_states: vec![],
|
||||||
action_explore_forward: None,
|
|
||||||
action_explore_backward: None,
|
|
||||||
action_explore_left: None,
|
|
||||||
action_explore_right: None,
|
|
||||||
action_explore_focus_next: None,
|
|
||||||
action_explore_focus_prev: None,
|
|
||||||
action_explore_select_next_type: None,
|
|
||||||
action_explore_select_prev_type: None,
|
|
||||||
action_navigate_to_explored: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -439,22 +401,23 @@ where
|
||||||
D: 'static + Clone + Default + Send + Sync,
|
D: 'static + Clone + Default + Send + Sync,
|
||||||
{
|
{
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
if !app.world.contains_resource::<ExplorationConfig<S, A>>() {
|
if !app.world.contains_resource::<ExplorationConfig<S>>() {
|
||||||
app.insert_resource(ExplorationConfig::<S, A>::default());
|
app.insert_resource(ExplorationConfig::<S>::default());
|
||||||
}
|
}
|
||||||
let config = app
|
let config = app
|
||||||
.world
|
.world
|
||||||
.get_resource::<ExplorationConfig<S, A>>()
|
.get_resource::<ExplorationConfig<S>>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.clone();
|
.clone();
|
||||||
app.register_type::<ExplorationFocused>()
|
app.register_type::<ExplorationFocused>()
|
||||||
.register_type::<Mappable>()
|
.register_type::<Mappable>()
|
||||||
|
.add_plugin(InputManagerPlugin::<ExplorationAction>::default())
|
||||||
.add_system(exploration_changed_announcement::<T, D>.chain(error_handler));
|
.add_system(exploration_changed_announcement::<T, D>.chain(error_handler));
|
||||||
if config.exploration_control_states.is_empty() {
|
if config.exploration_control_states.is_empty() {
|
||||||
app.add_system(exploration_focus::<S, A, D>)
|
app.add_system(exploration_focus::<S, D>)
|
||||||
.add_system(exploration_type_focus::<T, S, A>.chain(error_handler))
|
.add_system(exploration_type_focus::<T, S>.chain(error_handler))
|
||||||
.add_system(exploration_type_change::<T, S, A>.chain(error_handler))
|
.add_system(exploration_type_change::<T, S>.chain(error_handler))
|
||||||
.add_system(navigate_to_explored::<S, A, D>)
|
.add_system(navigate_to_explored::<S, D>)
|
||||||
.add_system_to_stage(
|
.add_system_to_stage(
|
||||||
CoreStage::PostUpdate,
|
CoreStage::PostUpdate,
|
||||||
exploration_type_changed_announcement::<T>.chain(error_handler),
|
exploration_type_changed_announcement::<T>.chain(error_handler),
|
||||||
|
@ -464,10 +427,10 @@ where
|
||||||
for state in states {
|
for state in states {
|
||||||
app.add_system_set(
|
app.add_system_set(
|
||||||
SystemSet::on_update(state.clone())
|
SystemSet::on_update(state.clone())
|
||||||
.with_system(exploration_focus::<S, A, D>)
|
.with_system(exploration_focus::<S, D>)
|
||||||
.with_system(exploration_type_focus::<T, S, A>.chain(error_handler))
|
.with_system(exploration_type_focus::<T, S>.chain(error_handler))
|
||||||
.with_system(exploration_type_change::<T, S, A>.chain(error_handler))
|
.with_system(exploration_type_change::<T, S>.chain(error_handler))
|
||||||
.with_system(navigate_to_explored::<S, A, D>)
|
.with_system(navigate_to_explored::<S, D>)
|
||||||
.with_system(
|
.with_system(
|
||||||
exploration_type_changed_announcement::<T>.chain(error_handler),
|
exploration_type_changed_announcement::<T>.chain(error_handler),
|
||||||
),
|
),
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#![allow(clippy::too_many_arguments)]
|
#![allow(clippy::too_many_arguments)]
|
||||||
#![allow(clippy::type_complexity)]
|
#![allow(clippy::type_complexity)]
|
||||||
|
|
||||||
pub use bevy_input_actionmap;
|
|
||||||
pub use bevy_rapier2d;
|
pub use bevy_rapier2d;
|
||||||
pub use bevy_synthizer;
|
pub use bevy_synthizer;
|
||||||
pub use bevy_tts;
|
pub use bevy_tts;
|
||||||
|
@ -15,6 +14,7 @@ pub mod exploration;
|
||||||
pub use futures_lite;
|
pub use futures_lite;
|
||||||
pub use gilrs;
|
pub use gilrs;
|
||||||
pub use here_be_dragons as mapgen;
|
pub use here_be_dragons as mapgen;
|
||||||
|
pub use leafwing_input_manager;
|
||||||
pub mod log;
|
pub mod log;
|
||||||
pub mod map;
|
pub mod map;
|
||||||
pub mod navigation;
|
pub mod navigation;
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use std::{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::prelude::*;
|
||||||
use bevy_input_actionmap::InputMap;
|
|
||||||
use bevy_rapier2d::prelude::*;
|
use bevy_rapier2d::prelude::*;
|
||||||
use bevy_tts::Tts;
|
use bevy_tts::Tts;
|
||||||
|
use leafwing_input_manager::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
commands::RunIfExistsExt,
|
commands::RunIfExistsExt,
|
||||||
|
@ -15,6 +15,17 @@ use crate::{
|
||||||
utils::target_and_other,
|
utils::target_and_other,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug)]
|
||||||
|
pub enum NavigationAction {
|
||||||
|
Move,
|
||||||
|
Rotate,
|
||||||
|
SnapLeft,
|
||||||
|
SnapRight,
|
||||||
|
SnapCardinal,
|
||||||
|
SnapReverse,
|
||||||
|
Sprint,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Component, Clone, Copy, Debug, Deref, DerefMut, Reflect)]
|
#[derive(Component, Clone, Copy, Debug, Deref, DerefMut, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct MaxSpeed(pub f32);
|
pub struct MaxSpeed(pub f32);
|
||||||
|
@ -46,15 +57,15 @@ pub struct Sprinting;
|
||||||
#[derive(Default, Deref, DerefMut)]
|
#[derive(Default, Deref, DerefMut)]
|
||||||
struct Snapping(bool);
|
struct Snapping(bool);
|
||||||
|
|
||||||
fn movement_controls<S, A: 'static>(
|
fn movement_controls<S>(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
config: Res<NavigationConfig<S, A>>,
|
config: Res<NavigationConfig<S>>,
|
||||||
input: Res<InputMap<A>>,
|
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
mut snapping: ResMut<Snapping>,
|
mut snapping: ResMut<Snapping>,
|
||||||
mut query: Query<
|
mut query: Query<
|
||||||
(
|
(
|
||||||
Entity,
|
Entity,
|
||||||
|
&ActionState<NavigationAction>,
|
||||||
&mut Velocity,
|
&mut Velocity,
|
||||||
&mut Speed,
|
&mut Speed,
|
||||||
&MaxSpeed,
|
&MaxSpeed,
|
||||||
|
@ -67,197 +78,133 @@ fn movement_controls<S, A: 'static>(
|
||||||
exploration_focused: Query<Entity, With<ExplorationFocused>>,
|
exploration_focused: Query<Entity, With<ExplorationFocused>>,
|
||||||
) where
|
) where
|
||||||
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
||||||
A: Hash + Eq + Copy + Send + Sync,
|
|
||||||
{
|
{
|
||||||
for (entity, mut velocity, mut speed, max_speed, rotation_speed, mut transform, destination) in
|
for (
|
||||||
query.iter_mut()
|
entity,
|
||||||
|
actions,
|
||||||
|
mut velocity,
|
||||||
|
mut speed,
|
||||||
|
max_speed,
|
||||||
|
rotation_speed,
|
||||||
|
mut transform,
|
||||||
|
destination,
|
||||||
|
) in &mut query
|
||||||
{
|
{
|
||||||
if **snapping {
|
if **snapping {
|
||||||
if let (Some(_), Some(rotate_left), Some(rotate_right)) = (
|
if actions.pressed(NavigationAction::Rotate) {
|
||||||
rotation_speed,
|
continue;
|
||||||
config.action_rotate_left,
|
|
||||||
config.action_rotate_right,
|
|
||||||
) {
|
|
||||||
if input.active(rotate_left) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if input.active(rotate_right) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
**snapping = false;
|
**snapping = false;
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
let sprinting = if let Some(action) = config.action_sprint {
|
let sprinting = actions.pressed(NavigationAction::Sprint);
|
||||||
input.active(action)
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
};
|
|
||||||
if sprinting {
|
if sprinting {
|
||||||
commands.entity(entity).insert(Sprinting::default());
|
commands.entity(entity).insert(Sprinting);
|
||||||
} else {
|
} else {
|
||||||
commands.entity(entity).remove::<Sprinting>();
|
commands.entity(entity).remove::<Sprinting>();
|
||||||
}
|
}
|
||||||
let mut direction = Vec2::default();
|
let mut cleanup = false;
|
||||||
if let Some(action) = config.action_forward {
|
if actions.pressed(NavigationAction::Move) {
|
||||||
if input.active(action) {
|
if let Some(pair) = actions.clamped_axis_pair(NavigationAction::Move) {
|
||||||
direction.x += 1.;
|
cleanup = true;
|
||||||
|
let mut direction = pair.xy();
|
||||||
|
let strength = direction.length();
|
||||||
|
if direction.x > 0. {
|
||||||
|
direction.x *= config.forward_movement_factor;
|
||||||
|
} else if direction.x < 0. {
|
||||||
|
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
|
||||||
|
};
|
||||||
|
**speed = s;
|
||||||
|
direction *= strength;
|
||||||
|
let mut v = direction * **speed;
|
||||||
|
v = transform
|
||||||
|
.compute_matrix()
|
||||||
|
.transform_vector3(v.extend(0.))
|
||||||
|
.truncate();
|
||||||
|
velocity.linvel = v;
|
||||||
}
|
}
|
||||||
|
} else if destination.is_none() {
|
||||||
|
velocity.linvel = Vec2::ZERO;
|
||||||
}
|
}
|
||||||
if let Some(action) = config.action_backward {
|
if let Some(rotation_speed) = rotation_speed {
|
||||||
if input.active(action) {
|
if actions.pressed(NavigationAction::Rotate) {
|
||||||
direction.x -= 1.;
|
cleanup = true;
|
||||||
}
|
let delta = rotation_speed.radians()
|
||||||
}
|
* time.delta_seconds()
|
||||||
if let Some(action) = config.action_left {
|
* actions.clamped_value(NavigationAction::Rotate);
|
||||||
if input.active(action) {
|
|
||||||
direction.y += 1.;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Some(action) = config.action_right {
|
|
||||||
if input.active(action) {
|
|
||||||
direction.y -= 1.;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let (Some(rotation_speed), Some(rotate_left), Some(rotate_right)) = (
|
|
||||||
rotation_speed,
|
|
||||||
config.action_rotate_left,
|
|
||||||
config.action_rotate_right,
|
|
||||||
) {
|
|
||||||
let delta = rotation_speed.radians() * time.delta_seconds();
|
|
||||||
if input.active(rotate_left) {
|
|
||||||
transform.rotate(Quat::from_rotation_z(delta));
|
transform.rotate(Quat::from_rotation_z(delta));
|
||||||
}
|
}
|
||||||
if input.active(rotate_right) {
|
|
||||||
transform.rotate(Quat::from_rotation_z(-delta));
|
|
||||||
}
|
|
||||||
if !input.active(rotate_left) && !input.active(rotate_right) {
|
|
||||||
velocity.angvel = 0.;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
velocity.angvel = 0.;
|
velocity.angvel = 0.;
|
||||||
}
|
}
|
||||||
if direction.length_squared() != 0. {
|
if cleanup {
|
||||||
commands.entity(entity).remove::<Destination>();
|
commands.entity(entity).remove::<Destination>();
|
||||||
commands.entity(entity).remove::<Exploring>();
|
commands.entity(entity).remove::<Exploring>();
|
||||||
for entity in exploration_focused.iter() {
|
for entity in exploration_focused.iter() {
|
||||||
commands.entity(entity).remove::<ExplorationFocused>();
|
commands.entity(entity).remove::<ExplorationFocused>();
|
||||||
}
|
}
|
||||||
direction = direction.normalize();
|
|
||||||
if direction.x > 0. {
|
|
||||||
direction.x *= config.forward_movement_factor;
|
|
||||||
} else if direction.x < 0. {
|
|
||||||
direction.x *= config.backward_movement_factor;
|
|
||||||
}
|
|
||||||
if direction.y != 0. {
|
|
||||||
direction.y *= config.strafe_movement_factor;
|
|
||||||
}
|
|
||||||
let strength = if let (Some(forward), Some(backward), Some(left), Some(right)) = (
|
|
||||||
config.action_forward,
|
|
||||||
config.action_backward,
|
|
||||||
config.action_left,
|
|
||||||
config.action_right,
|
|
||||||
) {
|
|
||||||
let forward_x = input.strength(forward).abs();
|
|
||||||
let backward_x = input.strength(backward).abs();
|
|
||||||
let x = if forward_x > backward_x {
|
|
||||||
forward_x
|
|
||||||
} else {
|
|
||||||
backward_x
|
|
||||||
};
|
|
||||||
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(Vec2::new(x, y))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
let s = if sprinting {
|
|
||||||
**max_speed
|
|
||||||
} else {
|
|
||||||
**max_speed / config.sprint_movement_factor
|
|
||||||
};
|
|
||||||
**speed = s;
|
|
||||||
if let Some(strength) = strength {
|
|
||||||
direction *= strength;
|
|
||||||
}
|
|
||||||
let mut v = direction * **speed;
|
|
||||||
v = transform
|
|
||||||
.compute_matrix()
|
|
||||||
.transform_vector3(v.extend(0.))
|
|
||||||
.truncate();
|
|
||||||
velocity.linvel = v;
|
|
||||||
} else if destination.is_none() {
|
|
||||||
velocity.linvel = Vec2::ZERO;
|
|
||||||
**speed = 0.;
|
|
||||||
} else if sprinting {
|
|
||||||
**speed = **max_speed;
|
|
||||||
} else {
|
|
||||||
velocity.linvel = Vec2::ZERO;
|
|
||||||
**speed = **max_speed / 3.;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn snap<S, A: 'static>(
|
fn snap<S>(
|
||||||
mut tts: ResMut<Tts>,
|
mut tts: ResMut<Tts>,
|
||||||
input: Res<InputMap<A>>,
|
|
||||||
config: Res<NavigationConfig<S, A>>,
|
|
||||||
mut snapping: ResMut<Snapping>,
|
mut snapping: ResMut<Snapping>,
|
||||||
mut query: Query<(&mut Transform, &mut Velocity, &CardinalDirection), With<Player>>,
|
mut query: Query<
|
||||||
|
(
|
||||||
|
&ActionState<NavigationAction>,
|
||||||
|
&mut Transform,
|
||||||
|
&mut Velocity,
|
||||||
|
&CardinalDirection,
|
||||||
|
),
|
||||||
|
With<Player>,
|
||||||
|
>,
|
||||||
) -> Result<(), Box<dyn Error>>
|
) -> Result<(), Box<dyn Error>>
|
||||||
where
|
where
|
||||||
S: 'static + Copy + Debug + Eq + Hash + Send + Sync,
|
S: 'static + Copy + Debug + Eq + Hash + Send + Sync,
|
||||||
A: Hash + Eq + Clone + Send + Sync,
|
|
||||||
{
|
{
|
||||||
if let Some(action) = config.action_snap_left.clone() {
|
for (actions, mut transform, mut velocity, direction) in query.iter_mut() {
|
||||||
if input.just_active(action) {
|
if actions.just_pressed(NavigationAction::SnapLeft) {
|
||||||
for (mut position, mut velocity, direction) in query.iter_mut() {
|
**snapping = true;
|
||||||
**snapping = true;
|
transform.rotation = Quat::from_rotation_z(match direction {
|
||||||
position.rotation = Quat::from_rotation_z(match direction {
|
CardinalDirection::North => PI,
|
||||||
CardinalDirection::North => PI,
|
CardinalDirection::East => PI / 2.,
|
||||||
CardinalDirection::East => PI / 2.,
|
CardinalDirection::South => 0.,
|
||||||
CardinalDirection::South => 0.,
|
CardinalDirection::West => PI * 1.5,
|
||||||
CardinalDirection::West => PI * 1.5,
|
});
|
||||||
});
|
velocity.angvel = 0.;
|
||||||
velocity.angvel = 0.;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
if actions.just_pressed(NavigationAction::SnapRight) {
|
||||||
if let Some(action) = config.action_snap_right.clone() {
|
**snapping = true;
|
||||||
if input.just_active(action) {
|
transform.rotation = Quat::from_rotation_z(match direction {
|
||||||
for (mut transform, mut velocity, direction) in query.iter_mut() {
|
CardinalDirection::North => 0.,
|
||||||
**snapping = true;
|
CardinalDirection::East => PI * 1.5,
|
||||||
transform.rotation = Quat::from_rotation_z(match direction {
|
CardinalDirection::South => PI,
|
||||||
CardinalDirection::North => 0.,
|
CardinalDirection::West => PI / 2.,
|
||||||
CardinalDirection::East => PI * 1.5,
|
});
|
||||||
CardinalDirection::South => PI,
|
velocity.angvel = 0.;
|
||||||
CardinalDirection::West => PI / 2.,
|
|
||||||
});
|
|
||||||
velocity.angvel = 0.;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
if actions.just_pressed(NavigationAction::SnapCardinal) {
|
||||||
if let Some(action) = config.action_snap_cardinal.clone() {
|
**snapping = true;
|
||||||
if input.just_active(action) {
|
let yaw: Angle = direction.into();
|
||||||
for (mut transform, mut velocity, direction) in query.iter_mut() {
|
let yaw = yaw.radians();
|
||||||
**snapping = true;
|
transform.rotation = Quat::from_rotation_z(yaw);
|
||||||
let yaw: Angle = direction.into();
|
velocity.angvel = 0.;
|
||||||
let yaw = yaw.radians();
|
tts.speak(direction.to_string(), true)?;
|
||||||
transform.rotation = Quat::from_rotation_z(yaw);
|
|
||||||
velocity.angvel = 0.;
|
|
||||||
tts.speak(direction.to_string(), true)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
if actions.just_pressed(NavigationAction::SnapReverse) {
|
||||||
if let Some(action) = config.action_snap_reverse.clone() {
|
**snapping = true;
|
||||||
if input.just_active(action) {
|
transform.rotate(Quat::from_rotation_z(PI));
|
||||||
for (mut transform, mut velocity, _) in query.iter_mut() {
|
velocity.angvel = 0.;
|
||||||
**snapping = true;
|
|
||||||
transform.rotate(Quat::from_rotation_z(PI));
|
|
||||||
velocity.angvel = 0.;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -328,7 +275,7 @@ fn log_area_descriptions<S, A>(
|
||||||
mut events: EventReader<CollisionEvent>,
|
mut events: EventReader<CollisionEvent>,
|
||||||
areas: Query<(&Area, Option<&Name>)>,
|
areas: Query<(&Area, Option<&Name>)>,
|
||||||
players: Query<&Player>,
|
players: Query<&Player>,
|
||||||
config: Res<NavigationConfig<S, A>>,
|
config: Res<NavigationConfig<S>>,
|
||||||
mut log: Query<&mut Log>,
|
mut log: Query<&mut Log>,
|
||||||
) where
|
) where
|
||||||
S: 'static + Send + Sync,
|
S: 'static + Send + Sync,
|
||||||
|
@ -369,18 +316,7 @@ fn log_area_descriptions<S, A>(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct NavigationConfig<S, A> {
|
pub struct NavigationConfig<S> {
|
||||||
pub action_backward: Option<A>,
|
|
||||||
pub action_forward: Option<A>,
|
|
||||||
pub action_left: Option<A>,
|
|
||||||
pub action_right: Option<A>,
|
|
||||||
pub action_rotate_left: Option<A>,
|
|
||||||
pub action_rotate_right: Option<A>,
|
|
||||||
pub action_snap_left: Option<A>,
|
|
||||||
pub action_snap_right: Option<A>,
|
|
||||||
pub action_snap_reverse: Option<A>,
|
|
||||||
pub action_snap_cardinal: Option<A>,
|
|
||||||
pub action_sprint: Option<A>,
|
|
||||||
pub forward_movement_factor: f32,
|
pub forward_movement_factor: f32,
|
||||||
pub backward_movement_factor: f32,
|
pub backward_movement_factor: f32,
|
||||||
pub strafe_movement_factor: f32,
|
pub strafe_movement_factor: f32,
|
||||||
|
@ -390,20 +326,9 @@ pub struct NavigationConfig<S, A> {
|
||||||
pub log_area_descriptions: bool,
|
pub log_area_descriptions: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, A> Default for NavigationConfig<S, A> {
|
impl<S> Default for NavigationConfig<S> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
action_backward: None,
|
|
||||||
action_forward: None,
|
|
||||||
action_left: None,
|
|
||||||
action_right: None,
|
|
||||||
action_rotate_left: None,
|
|
||||||
action_rotate_right: None,
|
|
||||||
action_snap_left: None,
|
|
||||||
action_snap_right: None,
|
|
||||||
action_snap_reverse: None,
|
|
||||||
action_snap_cardinal: None,
|
|
||||||
action_sprint: None,
|
|
||||||
forward_movement_factor: 1.,
|
forward_movement_factor: 1.,
|
||||||
backward_movement_factor: 1.,
|
backward_movement_factor: 1.,
|
||||||
strafe_movement_factor: 1.,
|
strafe_movement_factor: 1.,
|
||||||
|
@ -429,12 +354,12 @@ where
|
||||||
A: Hash + Eq + Copy + Send + Sync,
|
A: Hash + Eq + Copy + Send + Sync,
|
||||||
{
|
{
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
if !app.world.contains_resource::<NavigationConfig<S, A>>() {
|
if !app.world.contains_resource::<NavigationConfig<S>>() {
|
||||||
app.insert_resource(NavigationConfig::<S, A>::default());
|
app.insert_resource(NavigationConfig::<S>::default());
|
||||||
}
|
}
|
||||||
let config = app
|
let config = app
|
||||||
.world
|
.world
|
||||||
.get_resource::<NavigationConfig<S, A>>()
|
.get_resource::<NavigationConfig<S>>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.clone();
|
.clone();
|
||||||
const SNAP: &str = "SNAP";
|
const SNAP: &str = "SNAP";
|
||||||
|
@ -442,6 +367,7 @@ where
|
||||||
.register_type::<MaxSpeed>()
|
.register_type::<MaxSpeed>()
|
||||||
.register_type::<RotationSpeed>()
|
.register_type::<RotationSpeed>()
|
||||||
.register_type::<Sprinting>()
|
.register_type::<Sprinting>()
|
||||||
|
.add_plugin(InputManagerPlugin::<NavigationAction>::default())
|
||||||
.add_system_to_stage(CoreStage::PostUpdate, update_direction)
|
.add_system_to_stage(CoreStage::PostUpdate, update_direction)
|
||||||
.add_system_to_stage(CoreStage::PostUpdate, remove_direction)
|
.add_system_to_stage(CoreStage::PostUpdate, remove_direction)
|
||||||
.add_system(speak_direction.chain(error_handler))
|
.add_system(speak_direction.chain(error_handler))
|
||||||
|
@ -450,9 +376,9 @@ where
|
||||||
.add_system_to_stage(CoreStage::PostUpdate, log_area_descriptions::<S, A>);
|
.add_system_to_stage(CoreStage::PostUpdate, log_area_descriptions::<S, A>);
|
||||||
const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS";
|
const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS";
|
||||||
if config.movement_control_states.is_empty() {
|
if config.movement_control_states.is_empty() {
|
||||||
app.add_system(movement_controls::<S, A>.label(MOVEMENT_CONTROLS))
|
app.add_system(movement_controls::<S>.label(MOVEMENT_CONTROLS))
|
||||||
.add_system(
|
.add_system(
|
||||||
snap::<S, A>
|
snap::<S>
|
||||||
.chain(error_handler)
|
.chain(error_handler)
|
||||||
.label(SNAP)
|
.label(SNAP)
|
||||||
.before(MOVEMENT_CONTROLS),
|
.before(MOVEMENT_CONTROLS),
|
||||||
|
@ -462,9 +388,9 @@ where
|
||||||
for state in states {
|
for state in states {
|
||||||
app.add_system_set(
|
app.add_system_set(
|
||||||
SystemSet::on_update(state)
|
SystemSet::on_update(state)
|
||||||
.with_system(movement_controls::<S, A>.label(MOVEMENT_CONTROLS))
|
.with_system(movement_controls::<S>.label(MOVEMENT_CONTROLS))
|
||||||
.with_system(
|
.with_system(
|
||||||
snap::<S, A>
|
snap::<S>
|
||||||
.chain(error_handler)
|
.chain(error_handler)
|
||||||
.label(SNAP)
|
.label(SNAP)
|
||||||
.before(MOVEMENT_CONTROLS),
|
.before(MOVEMENT_CONTROLS),
|
||||||
|
|
|
@ -110,7 +110,6 @@ fn find_path_for_shape<D: 'static + Clone + Default + Send + Sync>(
|
||||||
|
|
||||||
fn calculate_path<D: 'static + Clone + Default + Send + Sync>(
|
fn calculate_path<D: 'static + Clone + Default + Send + Sync>(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
pool: Res<AsyncComputeTaskPool>,
|
|
||||||
rapier_context: Res<RapierContext>,
|
rapier_context: Res<RapierContext>,
|
||||||
obstructions: Query<&RapierColliderHandle, With<MapObstruction>>,
|
obstructions: Query<&RapierColliderHandle, With<MapObstruction>>,
|
||||||
query: Query<
|
query: Query<
|
||||||
|
@ -157,6 +156,7 @@ fn calculate_path<D: 'static + Clone + Default + Send + Sync>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let shape_clone = (*shape).clone();
|
let shape_clone = (*shape).clone();
|
||||||
|
let pool = AsyncComputeTaskPool::get();
|
||||||
let task = pool.spawn(async move {
|
let task = pool.spawn(async move {
|
||||||
find_path_for_shape(
|
find_path_for_shape(
|
||||||
handle_clone.0,
|
handle_clone.0,
|
||||||
|
|
|
@ -35,12 +35,12 @@ fn tag_behind(
|
||||||
) {
|
) {
|
||||||
if config.downshift_behind {
|
if config.downshift_behind {
|
||||||
if let Ok(listener_transform) = listener.get_single() {
|
if let Ok(listener_transform) = listener.get_single() {
|
||||||
let listener_forward = listener_transform.local_x();
|
let listener_forward = listener_transform.forward();
|
||||||
for (entity, transform, sound, icon) in sounds.iter() {
|
for (entity, transform, sound, icon) in sounds.iter() {
|
||||||
if icon.is_none() && sound.is_none() {
|
if icon.is_none() && sound.is_none() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let v = transform.translation - listener_transform.translation;
|
let v = transform.translation() - listener_transform.translation();
|
||||||
let dot = v.dot(listener_forward);
|
let dot = v.dot(listener_forward);
|
||||||
let is_behind = dot <= 0.;
|
let is_behind = dot <= 0.;
|
||||||
if is_behind {
|
if is_behind {
|
||||||
|
|
|
@ -63,14 +63,14 @@ impl Viewshed {
|
||||||
let mut boxes = vec![];
|
let mut boxes = vec![];
|
||||||
let shape = Collider::cuboid(self.range as f32, self.range as f32);
|
let shape = Collider::cuboid(self.range as f32, self.range as f32);
|
||||||
rapier_context.intersections_with_shape(
|
rapier_context.intersections_with_shape(
|
||||||
start.translation.truncate(),
|
start.translation().truncate(),
|
||||||
0.,
|
0.,
|
||||||
&shape,
|
&shape,
|
||||||
QueryFilter::new().predicate(&|e| visible_query.get(e).is_ok()),
|
QueryFilter::new().predicate(&|e| visible_query.get(e).is_ok()),
|
||||||
|entity| {
|
|entity| {
|
||||||
if let Ok((_, collider, transform)) = visible_query.get(entity) {
|
if let Ok((_, collider, transform)) = visible_query.get(entity) {
|
||||||
let position = Isometry2::new(
|
let position = Isometry2::new(
|
||||||
Vector2::new(transform.translation.x, transform.translation.y),
|
Vector2::new(transform.translation().x, transform.translation().y),
|
||||||
0.,
|
0.,
|
||||||
);
|
);
|
||||||
// println!(
|
// println!(
|
||||||
|
@ -88,8 +88,8 @@ impl Viewshed {
|
||||||
let shape = Collider::cuboid(0.5, 0.5);
|
let shape = Collider::cuboid(0.5, 0.5);
|
||||||
let mut new_visible_entities = HashSet::new();
|
let mut new_visible_entities = HashSet::new();
|
||||||
let size = (
|
let size = (
|
||||||
(start.translation.x.abs() + self.range as f32) as u32,
|
(start.translation().x.abs() + self.range as f32) as u32,
|
||||||
(start.translation.y.abs() + self.range as f32) as u32,
|
(start.translation().y.abs() + self.range as f32) as u32,
|
||||||
);
|
);
|
||||||
let visibility_grid = VisibilityGrid(
|
let visibility_grid = VisibilityGrid(
|
||||||
size,
|
size,
|
||||||
|
@ -410,7 +410,7 @@ fn log_visible(
|
||||||
{
|
{
|
||||||
viewer_transform.collider_direction_and_distance(
|
viewer_transform.collider_direction_and_distance(
|
||||||
viewer_collider,
|
viewer_collider,
|
||||||
&viewed_transform,
|
viewed_transform,
|
||||||
viewed_collider,
|
viewed_collider,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user