Compare commits
2 Commits
2379ee6900
...
ed10eabb63
Author | SHA1 | Date | |
---|---|---|---|
ed10eabb63 | |||
8dc4de0bd1 |
31
src/core.rs
31
src/core.rs
|
@ -333,6 +333,16 @@ impl PointLike for Vec2 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PointLike for IVec2 {
|
||||||
|
fn x(&self) -> f32 {
|
||||||
|
self.x as f32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn y(&self) -> f32 {
|
||||||
|
self.y as f32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl PointLike for (i32, i32) {
|
impl PointLike for (i32, i32) {
|
||||||
fn x(&self) -> f32 {
|
fn x(&self) -> f32 {
|
||||||
self.0 as f32
|
self.0 as f32
|
||||||
|
@ -373,27 +383,6 @@ impl PointLike for here_be_dragons::geometry::Point {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! impl_pointlike_for_tuple_component {
|
|
||||||
($source:ty) => {
|
|
||||||
impl PointLike for $source {
|
|
||||||
fn x(&self) -> f32 {
|
|
||||||
self.0 .0 as f32
|
|
||||||
}
|
|
||||||
|
|
||||||
fn y(&self) -> f32 {
|
|
||||||
self.0 .1 as f32
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<&dyn PointLike> for (i32, i32) {
|
|
||||||
fn from(val: &dyn PointLike) -> Self {
|
|
||||||
val.i32()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait TransformExt {
|
pub trait TransformExt {
|
||||||
fn yaw(&self) -> Rot2;
|
fn yaw(&self) -> Rot2;
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,9 +42,17 @@ pub struct ExplorationFocused;
|
||||||
|
|
||||||
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
|
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Exploring(pub (f32, f32));
|
pub struct Exploring(pub Vec2);
|
||||||
|
|
||||||
impl_pointlike_for_tuple_component!(Exploring);
|
impl PointLike for Exploring {
|
||||||
|
fn x(&self) -> f32 {
|
||||||
|
self.0.x
|
||||||
|
}
|
||||||
|
|
||||||
|
fn y(&self) -> f32 {
|
||||||
|
self.0.y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Component, Clone, Debug, Default, Deref, DerefMut)]
|
#[derive(Component, Clone, Debug, Default, Deref, DerefMut)]
|
||||||
pub struct FocusedExplorationType<T>(pub Option<T>)
|
pub struct FocusedExplorationType<T>(pub Option<T>)
|
||||||
|
@ -122,34 +130,45 @@ fn exploration_type_focus<ExplorationType>(
|
||||||
mut tts: ResMut<Tts>,
|
mut tts: ResMut<Tts>,
|
||||||
explorers: Query<(
|
explorers: Query<(
|
||||||
Entity,
|
Entity,
|
||||||
|
&GlobalTransform,
|
||||||
&ActionState<ExplorationAction>,
|
&ActionState<ExplorationAction>,
|
||||||
&VisibleEntities,
|
&VisibleEntities,
|
||||||
&FocusedExplorationType<ExplorationType>,
|
&FocusedExplorationType<ExplorationType>,
|
||||||
Option<&Exploring>,
|
Option<&Exploring>,
|
||||||
)>,
|
)>,
|
||||||
features: Query<(Entity, &Transform, &ExplorationType)>,
|
features: Query<(Entity, &GlobalTransform, &ExplorationType)>,
|
||||||
) -> Result<(), Box<dyn Error>>
|
) -> Result<(), Box<dyn Error>>
|
||||||
where
|
where
|
||||||
ExplorationType: Component + Default + PartialEq,
|
ExplorationType: Component + Default + PartialEq,
|
||||||
{
|
{
|
||||||
for (entity, actions, visible_entities, focused_type, exploring) in &explorers {
|
for (entity, transform, actions, visible_entities, focused_type, exploring) in &explorers {
|
||||||
let mut features = features
|
let mut features = features
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|v| visible_entities.contains(&v.0))
|
.filter(|v| visible_entities.contains(&v.0))
|
||||||
.map(|v| (v.1.trunc(), v.2))
|
.map(|v| (v.1.translation().truncate(), v.2))
|
||||||
.collect::<Vec<((f32, f32), &ExplorationType)>>();
|
.collect::<Vec<(Vec2, &ExplorationType)>>();
|
||||||
if features.is_empty() {
|
if features.is_empty() {
|
||||||
tts.speak("Nothing visible.", true)?;
|
tts.speak("Nothing visible.", true)?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
features.sort_by(|(c1, _), (c2, _)| c1.partial_cmp(c2).unwrap());
|
features.sort_by(|(c1, _), (c2, _)| {
|
||||||
|
transform
|
||||||
|
.translation()
|
||||||
|
.truncate()
|
||||||
|
.distance(*c1)
|
||||||
|
.partial_cmp(&transform.translation().truncate().distance(*c2))
|
||||||
|
.unwrap()
|
||||||
|
});
|
||||||
if let Some(focused) = &focused_type.0 {
|
if let Some(focused) = &focused_type.0 {
|
||||||
features.retain(|(_, t)| **t == *focused);
|
features.retain(|(_, t)| **t == *focused);
|
||||||
}
|
}
|
||||||
let mut target: Option<&((f32, f32), &ExplorationType)> = None;
|
let mut target = None;
|
||||||
if actions.just_pressed(&ExplorationAction::FocusNext) {
|
if actions.just_pressed(&ExplorationAction::FocusNext) {
|
||||||
if let Some(exploring) = exploring {
|
if let Some(exploring) = exploring {
|
||||||
target = features.iter().find(|(c, _)| *c > **exploring);
|
target = features.iter().find(|(c, _)| {
|
||||||
|
transform.translation().truncate().distance(*c)
|
||||||
|
> transform.translation().truncate().distance(**exploring)
|
||||||
|
});
|
||||||
if target.is_none() {
|
if target.is_none() {
|
||||||
target = features.first();
|
target = features.first();
|
||||||
}
|
}
|
||||||
|
@ -159,7 +178,10 @@ where
|
||||||
} else if actions.just_pressed(&ExplorationAction::FocusPrev) {
|
} else if actions.just_pressed(&ExplorationAction::FocusPrev) {
|
||||||
if let Some(exploring) = exploring {
|
if let Some(exploring) = exploring {
|
||||||
features.reverse();
|
features.reverse();
|
||||||
target = features.iter().find(|(c, _)| *c < **exploring);
|
target = features.iter().find(|(c, _)| {
|
||||||
|
transform.translation().truncate().distance(*c)
|
||||||
|
< transform.translation().truncate().distance(**exploring)
|
||||||
|
});
|
||||||
if target.is_none() {
|
if target.is_none() {
|
||||||
target = features.first();
|
target = features.first();
|
||||||
}
|
}
|
||||||
|
@ -170,7 +192,7 @@ where
|
||||||
if let Some((coordinates, _)) = target {
|
if let Some((coordinates, _)) = target {
|
||||||
commands
|
commands
|
||||||
.entity(entity)
|
.entity(entity)
|
||||||
.insert(Exploring(coordinates.trunc()));
|
.insert(Exploring(*coordinates));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -212,7 +234,7 @@ fn exploration_focus<MapData>(
|
||||||
(
|
(
|
||||||
Entity,
|
Entity,
|
||||||
&ActionState<ExplorationAction>,
|
&ActionState<ExplorationAction>,
|
||||||
&Transform,
|
&GlobalTransform,
|
||||||
Option<&Exploring>,
|
Option<&Exploring>,
|
||||||
),
|
),
|
||||||
With<Player>,
|
With<Player>,
|
||||||
|
@ -221,22 +243,20 @@ fn exploration_focus<MapData>(
|
||||||
MapData: 'static + Clone + Default + Send + Sync,
|
MapData: 'static + Clone + Default + Send + Sync,
|
||||||
{
|
{
|
||||||
for (entity, actions, transform, exploring) in &explorers {
|
for (entity, actions, transform, exploring) in &explorers {
|
||||||
let coordinates = transform.translation;
|
|
||||||
let mut exploring = if let Some(exploring) = exploring {
|
let mut exploring = if let Some(exploring) = exploring {
|
||||||
**exploring
|
**exploring
|
||||||
} else {
|
} else {
|
||||||
let floor = coordinates.floor();
|
transform.translation().truncate()
|
||||||
(floor.x, floor.y)
|
|
||||||
};
|
};
|
||||||
let orig = exploring;
|
let orig = exploring;
|
||||||
if actions.just_pressed(&ExplorationAction::Forward) {
|
if actions.just_pressed(&ExplorationAction::Forward) {
|
||||||
exploring.1 += 1.;
|
exploring.y += 1.;
|
||||||
} else if actions.just_pressed(&ExplorationAction::Backward) {
|
} else if actions.just_pressed(&ExplorationAction::Backward) {
|
||||||
exploring.1 -= 1.;
|
exploring.y -= 1.;
|
||||||
} else if actions.just_pressed(&ExplorationAction::Left) {
|
} else if actions.just_pressed(&ExplorationAction::Left) {
|
||||||
exploring.0 -= 1.;
|
exploring.x -= 1.;
|
||||||
} else if actions.just_pressed(&ExplorationAction::Right) {
|
} else if actions.just_pressed(&ExplorationAction::Right) {
|
||||||
exploring.0 += 1.;
|
exploring.x += 1.;
|
||||||
}
|
}
|
||||||
let dimensions = if let Ok(map) = map.get_single() {
|
let dimensions = if let Ok(map) = map.get_single() {
|
||||||
Some((map.width as f32, map.height as f32))
|
Some((map.width as f32, map.height as f32))
|
||||||
|
@ -244,11 +264,11 @@ fn exploration_focus<MapData>(
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
if let Some((width, height)) = dimensions {
|
if let Some((width, height)) = dimensions {
|
||||||
if exploring.0 >= width || exploring.1 >= height {
|
if exploring.x >= width || exploring.y >= height {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if orig != exploring && exploring.0 >= 0. && exploring.1 >= 0. {
|
if orig != exploring && exploring.x >= 0. && exploring.y >= 0. {
|
||||||
commands.entity(entity).insert(Exploring(exploring));
|
commands.entity(entity).insert(Exploring(exploring));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -269,7 +289,7 @@ fn navigate_to_explored<MapData>(
|
||||||
if actions.just_pressed(&ExplorationAction::NavigateTo) && known {
|
if actions.just_pressed(&ExplorationAction::NavigateTo) && known {
|
||||||
commands
|
commands
|
||||||
.entity(entity)
|
.entity(entity)
|
||||||
.insert(Destination((point.x_i32(), point.y_i32())));
|
.insert(Destination(point.as_ivec2()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -294,14 +314,14 @@ where
|
||||||
if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() {
|
if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() {
|
||||||
let coordinates = coordinates.trunc();
|
let coordinates = coordinates.trunc();
|
||||||
let point = **exploring;
|
let point = **exploring;
|
||||||
let shape = Collider::rectangle(0.5 - f32::EPSILON, 0.5 - f32::EPSILON);
|
let shape = Collider::rectangle(1. - f32::EPSILON, 1. - f32::EPSILON);
|
||||||
let (known, idx) = if let Ok((map, revealed_tiles)) = map.get_single() {
|
let (known, idx) = if let Ok((map, revealed_tiles)) = map.get_single() {
|
||||||
let idx = point.to_index(map.width);
|
let idx = point.to_index(map.width);
|
||||||
(revealed_tiles[idx], Some(idx))
|
(revealed_tiles[idx], Some(idx))
|
||||||
} else {
|
} else {
|
||||||
(false, None)
|
(false, None)
|
||||||
};
|
};
|
||||||
let visible = viewshed.is_point_visible(exploring);
|
let visible = viewshed.is_point_visible(exploring.as_ivec2());
|
||||||
let fog_of_war = !visible && known;
|
let fog_of_war = !visible && known;
|
||||||
let description: String = if known || visible {
|
let description: String = if known || visible {
|
||||||
let mut tokens: Vec<String> = vec![];
|
let mut tokens: Vec<String> = vec![];
|
||||||
|
|
|
@ -98,7 +98,7 @@ impl Default for TileBundle {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
transform: default(),
|
transform: default(),
|
||||||
collider: Collider::rectangle(0.5, 0.5),
|
collider: Collider::rectangle(1., 1.),
|
||||||
rigid_body: RigidBody::Static,
|
rigid_body: RigidBody::Static,
|
||||||
map_obstruction: default(),
|
map_obstruction: default(),
|
||||||
}
|
}
|
||||||
|
@ -136,8 +136,8 @@ impl ZoneBundle {
|
||||||
impl From<&MRect> for ZoneBundle {
|
impl From<&MRect> for ZoneBundle {
|
||||||
fn from(rect: &MRect) -> Self {
|
fn from(rect: &MRect) -> Self {
|
||||||
let collider = Collider::rectangle(
|
let collider = Collider::rectangle(
|
||||||
rect.width() as f32 / 2. + 0.5,
|
rect.width() as f32 + 1.,
|
||||||
rect.height() as f32 / 2. + 0.5,
|
rect.height() as f32 + 0.1,
|
||||||
);
|
);
|
||||||
let position = Vec2::new(rect.center().x(), rect.center().y());
|
let position = Vec2::new(rect.center().x(), rect.center().y());
|
||||||
Self::new(collider, position)
|
Self::new(collider, position)
|
||||||
|
@ -315,7 +315,7 @@ fn spawn_portal_colliders<D: 'static + Clone + Default + Send + Sync>(
|
||||||
for portal_entity in &portals {
|
for portal_entity in &portals {
|
||||||
commands
|
commands
|
||||||
.entity(portal_entity)
|
.entity(portal_entity)
|
||||||
.insert((Collider::rectangle(0.5, 0.5), Sensor));
|
.insert((Collider::rectangle(1., 1.), Sensor));
|
||||||
}
|
}
|
||||||
commands.entity(entity).remove::<SpawnColliders>();
|
commands.entity(entity).remove::<SpawnColliders>();
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use leafwing_input_manager::{plugin::InputManagerSystem, prelude::*};
|
||||||
use pathfinding::prelude::*;
|
use pathfinding::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{PointLike, TransformExt},
|
core::{GlobalTransformExt, TransformExt},
|
||||||
map::Map,
|
map::Map,
|
||||||
navigation::{NavigationAction, RotationSpeed, Speed},
|
navigation::{NavigationAction, RotationSpeed, Speed},
|
||||||
};
|
};
|
||||||
|
@ -20,10 +20,7 @@ impl Actionlike for NegotiatePathAction {
|
||||||
|
|
||||||
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Eq, Hash, PartialEq, Reflect)]
|
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Eq, Hash, PartialEq, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Destination(pub (i32, i32));
|
pub struct Destination(pub IVec2);
|
||||||
|
|
||||||
impl_pointlike_for_tuple_component!(Destination);
|
|
||||||
impl_pointlike_for_tuple_component!(&Destination);
|
|
||||||
|
|
||||||
#[derive(Component, Clone, Debug, Default, Reflect)]
|
#[derive(Component, Clone, Debug, Default, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
|
@ -31,11 +28,11 @@ pub struct NoPath;
|
||||||
|
|
||||||
#[derive(Component, Clone, Debug, Default, Deref, DerefMut, Reflect)]
|
#[derive(Component, Clone, Debug, Default, Deref, DerefMut, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Path(pub Vec<(i32, i32)>);
|
pub struct Path(pub Vec<IVec2>);
|
||||||
|
|
||||||
#[derive(Component, Clone, Debug, Default, Reflect, Deref, DerefMut)]
|
#[derive(Component, Clone, Debug, Default, Reflect, Deref, DerefMut)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct CostMap(pub HashMap<(i32, i32), f32>);
|
pub struct CostMap(pub HashMap<IVec2, f32>);
|
||||||
|
|
||||||
#[derive(Bundle, Deref, DerefMut)]
|
#[derive(Bundle, Deref, DerefMut)]
|
||||||
pub struct PathfindingControlsBundle(pub ActionState<NegotiatePathAction>);
|
pub struct PathfindingControlsBundle(pub ActionState<NegotiatePathAction>);
|
||||||
|
@ -49,36 +46,34 @@ impl Default for PathfindingControlsBundle {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_path<D: 'static + Clone + Default + Send + Sync>(
|
pub fn find_path<D: 'static + Clone + Default + Send + Sync>(
|
||||||
start: &dyn PointLike,
|
start: IVec2,
|
||||||
destination: &dyn PointLike,
|
destination: IVec2,
|
||||||
map: &Map<D>,
|
map: &Map<D>,
|
||||||
cost_map: &Option<CostMap>,
|
cost_map: &Option<CostMap>,
|
||||||
) -> Option<(Vec<(i32, i32)>, u32)> {
|
) -> Option<(Vec<IVec2>, u32)> {
|
||||||
astar(
|
astar(
|
||||||
&start.into(),
|
&start,
|
||||||
|p| {
|
|p| {
|
||||||
let mut successors: Vec<((i32, i32), u32)> = vec![];
|
let mut successors: Vec<(IVec2, u32)> = vec![];
|
||||||
if p.0 >= 0 && p.1 >= 0 {
|
if p.x >= 0 && p.y >= 0 {
|
||||||
if let Some(tile) = map.at(p.0 as usize, p.1 as usize) {
|
if let Some(tile) = map.at(p.x as usize, p.y as usize) {
|
||||||
if tile.is_walkable() {
|
if tile.is_walkable() {
|
||||||
for tile in map.get_available_exits(p.0 as usize, p.1 as usize) {
|
for tile in map.get_available_exits(p.x as usize, p.y as usize) {
|
||||||
let mut cost = tile.2 * 100.;
|
let mut cost = tile.2 * 100.;
|
||||||
if let Some(cost_map) = cost_map {
|
if let Some(cost_map) = cost_map {
|
||||||
if let Some(modifier) =
|
if let Some(modifier) = cost_map.get(p) {
|
||||||
cost_map.get(&(tile.0 as i32, tile.1 as i32))
|
|
||||||
{
|
|
||||||
cost *= modifier;
|
cost *= modifier;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
successors.push(((tile.0 as i32, tile.1 as i32), cost as u32));
|
successors.push((IVec2::new(tile.0 as i32, tile.1 as i32), cost as u32));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
successors
|
successors
|
||||||
},
|
},
|
||||||
|p| (p.distance_squared(destination) * 100.) as u32,
|
|p| (p.distance_squared(destination) * 100) as u32,
|
||||||
|p| *p == destination.into(),
|
|p| *p == destination,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +84,7 @@ fn calculate_path(
|
||||||
(
|
(
|
||||||
Entity,
|
Entity,
|
||||||
&Destination,
|
&Destination,
|
||||||
&Transform,
|
&GlobalTransform,
|
||||||
&Collider,
|
&Collider,
|
||||||
Option<&CostMap>,
|
Option<&CostMap>,
|
||||||
Option<&mut ActionState<NegotiatePathAction>>,
|
Option<&mut ActionState<NegotiatePathAction>>,
|
||||||
|
@ -97,9 +92,9 @@ fn calculate_path(
|
||||||
Changed<Destination>,
|
Changed<Destination>,
|
||||||
>,
|
>,
|
||||||
) {
|
) {
|
||||||
for (entity, destination, start, collider, cost_map, actions) in &mut query {
|
for (entity, destination, transform, collider, cost_map, actions) in &mut query {
|
||||||
trace!("{entity}: destination: {destination:?}");
|
trace!("{entity}: destination: {destination:?}");
|
||||||
if start.i32() == **destination {
|
if transform.translation().truncate().as_ivec2() == **destination {
|
||||||
trace!("{entity}: remove1");
|
trace!("{entity}: remove1");
|
||||||
commands
|
commands
|
||||||
.entity(entity)
|
.entity(entity)
|
||||||
|
@ -110,36 +105,32 @@ fn calculate_path(
|
||||||
}
|
}
|
||||||
trace!(
|
trace!(
|
||||||
"{entity:?}: Calculating path from {:?} to {destination:?}",
|
"{entity:?}: Calculating path from {:?} to {destination:?}",
|
||||||
start.translation.truncate().i32()
|
transform.translation().truncate().as_ivec2()
|
||||||
);
|
|
||||||
trace!(
|
|
||||||
"{entity:?}: path: calculating from {:?} to {destination:?}",
|
|
||||||
start.i32(),
|
|
||||||
);
|
);
|
||||||
commands.entity(entity).remove::<Path>().remove::<NoPath>();
|
commands.entity(entity).remove::<Path>().remove::<NoPath>();
|
||||||
let path = astar(
|
let path = astar(
|
||||||
&start.i32(),
|
&transform.translation().truncate().as_ivec2(),
|
||||||
|p| {
|
|p| {
|
||||||
let mut successors: Vec<((i32, i32), u32)> = vec![];
|
let mut successors: Vec<(IVec2, u32)> = vec![];
|
||||||
let x = p.0;
|
let x = p.x;
|
||||||
let y = p.1;
|
let y = p.y;
|
||||||
let exits = vec![
|
let exits = vec![
|
||||||
((x - 1, y), 1.),
|
(IVec2::new(x - 1, y), 1.),
|
||||||
((x + 1, y), 1.),
|
(IVec2::new(x + 1, y), 1.),
|
||||||
((x, y - 1), 1.),
|
(IVec2::new(x, y - 1), 1.),
|
||||||
((x, y + 1), 1.),
|
(IVec2::new(x, y + 1), 1.),
|
||||||
((x - 1, y - 1), 1.5),
|
(IVec2::new(x - 1, y - 1), 1.5),
|
||||||
((x + 1, y - 1), 1.5),
|
(IVec2::new(x + 1, y - 1), 1.5),
|
||||||
((x - 1, y + 1), 1.5),
|
(IVec2::new(x - 1, y + 1), 1.5),
|
||||||
((x + 1, y + 1), 1.5),
|
(IVec2::new(x + 1, y + 1), 1.5),
|
||||||
];
|
];
|
||||||
for exit in &exits {
|
for exit in &exits {
|
||||||
let mut should_push = true;
|
let mut should_push = true;
|
||||||
if !spatial_query
|
if !spatial_query
|
||||||
.shape_intersections(
|
.shape_intersections(
|
||||||
collider,
|
collider,
|
||||||
start.translation.truncate(),
|
transform.translation().truncate(),
|
||||||
start.yaw().as_radians(),
|
transform.yaw().as_radians(),
|
||||||
SpatialQueryFilter::default(),
|
SpatialQueryFilter::default(),
|
||||||
)
|
)
|
||||||
.is_empty()
|
.is_empty()
|
||||||
|
@ -149,7 +140,7 @@ fn calculate_path(
|
||||||
if should_push {
|
if should_push {
|
||||||
let mut cost = exit.1 * 100.;
|
let mut cost = exit.1 * 100.;
|
||||||
if let Some(cost_map) = cost_map {
|
if let Some(cost_map) = cost_map {
|
||||||
if let Some(modifier) = cost_map.get(&(exit.0 .0, exit.0 .1)) {
|
if let Some(modifier) = cost_map.get(&exit.0) {
|
||||||
cost *= modifier;
|
cost *= modifier;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -158,8 +149,8 @@ fn calculate_path(
|
||||||
}
|
}
|
||||||
successors
|
successors
|
||||||
},
|
},
|
||||||
|p| (p.distance_squared(&destination) * 100.) as u32,
|
|p| (p.distance_squared(**destination) * 100) as u32,
|
||||||
|p| *p == destination.i32(),
|
|p| *p == **destination,
|
||||||
);
|
);
|
||||||
if let Some(path) = path {
|
if let Some(path) = path {
|
||||||
commands.entity(entity).insert(Path(path.0));
|
commands.entity(entity).insert(Path(path.0));
|
||||||
|
@ -209,19 +200,19 @@ fn negotiate_path(
|
||||||
) in &mut query
|
) in &mut query
|
||||||
{
|
{
|
||||||
trace!("{entity:?}: negotiating path");
|
trace!("{entity:?}: negotiating path");
|
||||||
let start_i32 = transform.translation.truncate().i32();
|
let start = transform.translation.truncate().as_ivec2();
|
||||||
trace!(
|
trace!(
|
||||||
"{entity:?}: start pathfinding from {start_i32:?} to {:?}: at {:?}",
|
"{entity:?}: start pathfinding from {start:?} to {:?}: at {:?}",
|
||||||
path.last(),
|
path.last(),
|
||||||
transform.translation.truncate().i32()
|
transform.translation.truncate()
|
||||||
);
|
);
|
||||||
if path.len() > 0 && path[0] == start_i32 {
|
if path.len() > 0 && path[0] == start {
|
||||||
trace!("At start, removing");
|
trace!("At start, removing");
|
||||||
path.remove(0);
|
path.remove(0);
|
||||||
}
|
}
|
||||||
if let Some(next) = path.first() {
|
if let Some(next) = path.first() {
|
||||||
let start = transform.translation.truncate();
|
let start = transform.translation.truncate();
|
||||||
let mut next = Vec2::new(next.0 as f32, next.1 as f32);
|
let mut next = next.as_vec2();
|
||||||
if next.x >= 0. {
|
if next.x >= 0. {
|
||||||
next.x += 0.5;
|
next.x += 0.5;
|
||||||
} else {
|
} else {
|
||||||
|
@ -298,7 +289,7 @@ fn actions(
|
||||||
} else {
|
} else {
|
||||||
let pair = actions.axis_pair(&NegotiatePathAction);
|
let pair = actions.axis_pair(&NegotiatePathAction);
|
||||||
trace!("{entity:?}: Negotiating path to {pair:?}");
|
trace!("{entity:?}: Negotiating path to {pair:?}");
|
||||||
let dest = Destination(pair.xy().i32());
|
let dest = Destination(pair.as_ivec2());
|
||||||
if let Some(mut current_dest) = destination {
|
if let Some(mut current_dest) = destination {
|
||||||
trace!("Got a destination");
|
trace!("Got a destination");
|
||||||
if *current_dest != dest {
|
if *current_dest != dest {
|
||||||
|
|
|
@ -25,7 +25,7 @@ pub struct RevealedTiles(pub Vec<bool>);
|
||||||
#[derive(Component, Clone, Debug, Eq, PartialEq)]
|
#[derive(Component, Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct Viewshed {
|
pub struct Viewshed {
|
||||||
pub range: u32,
|
pub range: u32,
|
||||||
pub visible_points: HashSet<(i32, i32)>,
|
pub visible_points: HashSet<IVec2>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Viewshed {
|
impl Default for Viewshed {
|
||||||
|
@ -38,8 +38,8 @@ impl Default for Viewshed {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Viewshed {
|
impl Viewshed {
|
||||||
pub fn is_point_visible(&self, point: &dyn PointLike) -> bool {
|
pub fn is_point_visible(&self, point: IVec2) -> bool {
|
||||||
self.visible_points.contains(&point.into())
|
self.visible_points.contains(&point)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(
|
||||||
|
@ -61,7 +61,7 @@ impl Viewshed {
|
||||||
let visibility_grid = VisibilityGrid(size, *viewer_entity, opacity_map.clone());
|
let visibility_grid = VisibilityGrid(size, *viewer_entity, opacity_map.clone());
|
||||||
let mut new_visible = HashSet::new();
|
let mut new_visible = HashSet::new();
|
||||||
let mut new_visible_entities = HashSet::new();
|
let mut new_visible_entities = HashSet::new();
|
||||||
// println!("Start: {viewer_entity}");
|
println!("Start: {viewer_entity}");
|
||||||
context.for_each_visible(
|
context.for_each_visible(
|
||||||
Coord::new(start.x_i32(), start.y_i32()),
|
Coord::new(start.x_i32(), start.y_i32()),
|
||||||
&visibility_grid,
|
&visibility_grid,
|
||||||
|
@ -69,18 +69,19 @@ impl Viewshed {
|
||||||
vision_distance,
|
vision_distance,
|
||||||
u8::MAX,
|
u8::MAX,
|
||||||
|coord, _directions, _visibility| {
|
|coord, _directions, _visibility| {
|
||||||
new_visible.insert((coord.x, coord.y));
|
|
||||||
let coord = IVec2::new(coord.x, coord.y);
|
let coord = IVec2::new(coord.x, coord.y);
|
||||||
// println!("Checking {coord:?}");
|
new_visible.insert(coord);
|
||||||
|
println!("Checking {coord:?}");
|
||||||
if let Some((_, entities)) = opacity_map.get(&coord) {
|
if let Some((_, entities)) = opacity_map.get(&coord) {
|
||||||
for e in entities {
|
for e in entities {
|
||||||
if entities.len() == 1 || sensors.contains(*e) {
|
if entities.len() == 1 || sensors.contains(*e) {
|
||||||
|
println!("Spotted {e:?}");
|
||||||
new_visible_entities.insert(*e);
|
new_visible_entities.insert(*e);
|
||||||
} else {
|
} else {
|
||||||
let should_push = std::cell::RefCell::new(true);
|
let should_push = std::cell::RefCell::new(true);
|
||||||
let coord = Vec2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5);
|
let coord = Vec2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5);
|
||||||
let dir = Dir2::new_unchecked((coord - *start).normalize());
|
let dir = Dir2::new_unchecked((coord - *start).normalize());
|
||||||
// println!("Casting from {coord}");
|
println!("Casting from {coord} to {dir:?}");
|
||||||
spatial_query.cast_ray_predicate(
|
spatial_query.cast_ray_predicate(
|
||||||
*start,
|
*start,
|
||||||
dir,
|
dir,
|
||||||
|
@ -89,8 +90,7 @@ impl Viewshed {
|
||||||
default(),
|
default(),
|
||||||
&|entity| {
|
&|entity| {
|
||||||
if *e != entity && entities.contains(e) {
|
if *e != entity && entities.contains(e) {
|
||||||
// println!("{entities:?} contains {e} and hits at {:?}", hit.point);
|
println!("{entities:?} contains {e}");
|
||||||
// commands.entity(*e).log_components();
|
|
||||||
*should_push.borrow_mut() = false;
|
*should_push.borrow_mut() = false;
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
|
@ -109,9 +109,11 @@ impl Viewshed {
|
||||||
}
|
}
|
||||||
if new_visible_entities != **visible_entities {
|
if new_visible_entities != **visible_entities {
|
||||||
for lost in visible_entities.difference(&new_visible_entities) {
|
for lost in visible_entities.difference(&new_visible_entities) {
|
||||||
|
println!("Lost {lost}");
|
||||||
commands.trigger_targets(VisibilityChanged::Lost(*lost), *viewer_entity);
|
commands.trigger_targets(VisibilityChanged::Lost(*lost), *viewer_entity);
|
||||||
}
|
}
|
||||||
for gained in new_visible_entities.difference(visible_entities) {
|
for gained in new_visible_entities.difference(visible_entities) {
|
||||||
|
println!("Gained {gained}");
|
||||||
commands.trigger_targets(VisibilityChanged::Gained(*gained), *viewer_entity);
|
commands.trigger_targets(VisibilityChanged::Gained(*gained), *viewer_entity);
|
||||||
}
|
}
|
||||||
**visible_entities = new_visible_entities;
|
**visible_entities = new_visible_entities;
|
||||||
|
@ -150,7 +152,7 @@ impl OpacityMap {
|
||||||
visible: &Query<(Entity, &GlobalTransform, &Collider, &Visible)>,
|
visible: &Query<(Entity, &GlobalTransform, &Collider, &Visible)>,
|
||||||
) {
|
) {
|
||||||
self.retain(|k, _| !coordinates.contains(k));
|
self.retain(|k, _| !coordinates.contains(k));
|
||||||
let shape = Collider::rectangle(0.49, 0.49);
|
let shape = Collider::rectangle(0.98, 0.98);
|
||||||
for coords in &coordinates {
|
for coords in &coordinates {
|
||||||
let shape_pos = Vec2::new(coords.x as f32 + 0.5, coords.y as f32 + 0.5);
|
let shape_pos = Vec2::new(coords.x as f32 + 0.5, coords.y as f32 + 0.5);
|
||||||
let mut opacity = 0;
|
let mut opacity = 0;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user