Compare commits

...

10 Commits

12 changed files with 276 additions and 600 deletions

View File

@ -11,7 +11,7 @@ edition = "2021"
speech_dispatcher_0_10 = ["bevy_tts/speech_dispatcher_0_10"]
[dependencies.bevy]
version = "0.6"
version = "0.7"
default-features = false
features = [
"bevy_gilrs",
@ -22,18 +22,13 @@ features = [
"serialize",
]
[dependencies.bevy_rapier2d]
git = "https://github.com/dimforge/bevy_rapier"
branch = "rapier-master"
features = ["simd-stable"]
[dependencies]
backtrace = "0.3"
bevy_input_actionmap = { git = "https://github.com/lightsoutgames/bevy_input_actionmap" }
bevy_openal = { git = "https://github.com/lightsoutgames/bevy_openal" }
bevy_rapier2d = "0.13"
bevy_tts = { git = "https://github.com/lightsoutgames/bevy_tts", default-features = false, features = ["tolk"] }
coord_2d = "0.3"
derive_more = "0.99"
futures-lite = "1"
gilrs = "0.8"
here_be_dragons = "0.1"
@ -44,6 +39,3 @@ rand = "0.8"
sentry = "0.25"
serde = "1"
shadowcast = "0.8"
[patch.crates-io]
rapier2d = { git = "https://github.com/dimforge/rapier", rev = "7efcff615e821" }

View File

@ -3,116 +3,16 @@ use std::{
f32::consts::PI,
fmt::Display,
marker::PhantomData,
ops::{Add, AddAssign, Sub, SubAssign},
ops::Sub,
sync::RwLock,
};
use bevy::{core::FloatOrd, ecs::query::WorldQuery, prelude::*, transform::TransformSystem};
use bevy::{core::FloatOrd, ecs::query::WorldQuery, prelude::*};
use bevy_rapier2d::prelude::*;
use derive_more::{Deref, DerefMut};
use once_cell::sync::Lazy;
use rand::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(
Component, Clone, Copy, Debug, Default, Deref, DerefMut, PartialEq, PartialOrd, Reflect,
)]
#[reflect(Component)]
pub struct Coordinates(pub (f32, f32));
impl Coordinates {
pub fn from_transform(transform: &Transform, config: &CoreConfig) -> Self {
Self((
transform.x() / config.pixels_per_unit as f32,
transform.y() / config.pixels_per_unit as f32,
))
}
pub fn to_transform(&self, config: &CoreConfig) -> Transform {
Transform::from_translation(Vec3::new(
self.0 .0 * config.pixels_per_unit as f32,
self.0 .1 * config.pixels_per_unit as f32,
0.,
))
}
}
impl Add for Coordinates {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self((self.0 .0 + rhs.0 .0, self.0 .1 + rhs.0 .1))
}
}
impl AddAssign for Coordinates {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl Sub for Coordinates {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self((self.0 .0 - rhs.0 .0, self.0 .1 - rhs.0 .1))
}
}
impl SubAssign for Coordinates {
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs
}
}
impl From<Vec2> for Coordinates {
fn from(v: Vec2) -> Self {
Self((v.x, v.y))
}
}
impl From<Vec3> for Coordinates {
fn from(v: Vec3) -> Self {
Self((v.x, v.y))
}
}
impl From<Coordinates> for Vec2 {
fn from(c: Coordinates) -> Self {
Vec2::new(c.0 .0, c.0 .1)
}
}
impl From<Coordinates> for Vec3 {
fn from(c: Coordinates) -> Self {
Vec3::new(c.0 .0, c.0 .1, 0.)
}
}
impl From<(f32, f32)> for Coordinates {
fn from(v: (f32, f32)) -> Self {
Coordinates((v.0, v.1))
}
}
impl From<(i32, i32)> for Coordinates {
fn from(v: (i32, i32)) -> Self {
Coordinates((v.0 as f32, v.1 as f32))
}
}
impl From<(u32, u32)> for Coordinates {
fn from(v: (u32, u32)) -> Self {
Coordinates((v.0 as f32, v.1 as f32))
}
}
impl From<(usize, usize)> for Coordinates {
fn from(v: (usize, usize)) -> Self {
Coordinates((v.0 as f32, v.1 as f32))
}
}
#[derive(Clone, Copy, Debug, Reflect)]
pub enum Angle {
Degrees(f32),
@ -468,6 +368,26 @@ impl PointLike for Transform {
}
}
impl PointLike for &Transform {
fn x(&self) -> f32 {
self.translation.x
}
fn y(&self) -> f32 {
self.translation.y
}
}
impl PointLike for GlobalTransform {
fn x(&self) -> f32 {
self.translation.x
}
fn y(&self) -> f32 {
self.translation.y
}
}
impl PointLike for Vec2 {
fn x(&self) -> f32 {
self.x
@ -508,16 +428,6 @@ impl PointLike for (usize, usize) {
}
}
impl PointLike for &Coordinates {
fn x(&self) -> f32 {
self.0 .0
}
fn y(&self) -> f32 {
self.0 .1
}
}
impl PointLike for here_be_dragons::geometry::Point {
fn x(&self) -> f32 {
self.x as f32
@ -543,8 +453,6 @@ macro_rules! impl_pointlike_for_tuple_component {
};
}
impl_pointlike_for_tuple_component!(Coordinates);
impl From<&dyn PointLike> for (i32, i32) {
fn from(val: &dyn PointLike) -> Self {
val.i32()
@ -614,76 +522,11 @@ where
}
}
fn setup(core_config: Res<CoreConfig>, mut rapier_config: ResMut<RapierConfiguration>) {
rapier_config.scale = core_config.pixels_per_unit as f32;
fn setup(core_config: Res<CoreConfig>) {
let mut mode = RELATIVE_DIRECTION_MODE.write().unwrap();
*mode = core_config.relative_direction_mode;
}
fn copy_coordinates_to_transform(
config: Res<CoreConfig>,
mut query: Query<
(&Coordinates, &mut Transform),
(
Changed<Transform>,
Without<RigidBodyPositionComponent>,
Without<RigidBodyPositionSync>,
Without<ColliderPositionComponent>,
Without<ColliderPositionSync>,
),
>,
) {
for (coordinates, mut transform) in query.iter_mut() {
let x = coordinates.0 .0 * config.pixels_per_unit as f32;
if transform.translation.x != x {
transform.translation.x = x;
}
let y = coordinates.0 .1 * config.pixels_per_unit as f32;
if transform.translation.y != y {
transform.translation.y = y;
}
}
}
fn copy_rigid_body_position_to_coordinates(
mut query: Query<
(&mut Coordinates, &RigidBodyPositionComponent),
(
Changed<RigidBodyPositionComponent>,
With<RigidBodyPositionSync>,
),
>,
) {
for (mut coordinates, position) in query.iter_mut() {
if coordinates.0 .0 != position.position.translation.x {
coordinates.0 .0 = position.position.translation.x;
}
if coordinates.0 .1 != position.position.translation.y {
coordinates.0 .1 = position.position.translation.y;
}
}
}
fn copy_collider_position_to_coordinates(
mut query: Query<
(&mut Coordinates, &ColliderPositionComponent),
(
Without<RigidBodyPositionComponent>,
Changed<ColliderPositionComponent>,
With<ColliderPositionSync>,
),
>,
) {
for (mut coordinates, position) in query.iter_mut() {
if coordinates.0 .0 != position.translation.x {
coordinates.0 .0 = position.translation.x;
}
if coordinates.0 .1 != position.translation.y {
coordinates.0 .1 = position.translation.y;
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct CoreConfig {
pub relative_direction_mode: RelativeDirectionMode,
@ -706,24 +549,24 @@ fn sync_config(config: Res<CoreConfig>) {
}
}
pub struct CorePlugin;
pub struct CorePlugin<RapierUserData>(PhantomData<RapierUserData>);
impl Plugin for CorePlugin {
impl<RapierUserData> Default for CorePlugin<RapierUserData> {
fn default() -> Self {
Self(PhantomData)
}
}
impl<RapierUserData: 'static + WorldQuery + Send + Sync> Plugin for CorePlugin<RapierUserData> {
fn build(&self, app: &mut App) {
if !app.world.contains_resource::<CoreConfig>() {
app.insert_resource(CoreConfig::default());
}
app.register_type::<Coordinates>()
let config = *app.world.get_resource::<CoreConfig>().unwrap();
app.add_plugin(RapierPhysicsPlugin::<RapierUserData>::pixels_per_meter(
config.pixels_per_unit as f32,
))
.add_startup_system(setup)
.add_system_to_stage(
CoreStage::PostUpdate,
copy_coordinates_to_transform.before(TransformSystem::TransformPropagate),
)
.add_system_to_stage(
CoreStage::PostUpdate,
copy_rigid_body_position_to_coordinates,
)
.add_system_to_stage(CoreStage::PostUpdate, copy_collider_position_to_coordinates)
.add_system(sync_config);
}
}
@ -743,7 +586,6 @@ impl<RapierUserData: 'static + WorldQuery + Send + Sync> PluginGroup
group
.add(crate::bevy_tts::TtsPlugin)
.add(crate::bevy_openal::OpenAlPlugin)
.add(RapierPhysicsPlugin::<RapierUserData>::default())
.add(CorePlugin);
.add(CorePlugin::<RapierUserData>::default());
}
}

View File

@ -62,7 +62,7 @@ impl Plugin for ErrorPlugin {
dsn.as_str(),
sentry::ClientOptions {
release: Some(release.into()),
..Default::default()
..default()
},
));
app.insert_resource(guard);

View File

@ -4,10 +4,9 @@ use bevy::prelude::*;
use bevy_input_actionmap::InputMap;
use bevy_rapier2d::prelude::*;
use bevy_tts::Tts;
use derive_more::{Deref, DerefMut};
use crate::{
core::{Coordinates, Player, PointLike},
core::{Player, PointLike},
error::error_handler,
map::Map,
pathfinding::Destination,
@ -155,7 +154,7 @@ fn exploration_type_focus<S, A: 'static>(
&FocusedExplorationType,
Option<&Exploring>,
)>,
features: Query<(Entity, &Coordinates, &ExplorationType)>,
features: Query<(Entity, &Transform, &ExplorationType)>,
) -> Result<(), Box<dyn Error>>
where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
@ -247,7 +246,7 @@ fn exploration_focus<S, A: 'static, D: 'static + Clone + Default + Send + Sync>(
config: Res<ExplorationConfig<S, A>>,
input: Res<InputMap<A>>,
map: Query<&Map<D>>,
explorers: Query<(Entity, &Coordinates, Option<&Exploring>), With<Player>>,
explorers: Query<(Entity, &Transform, Option<&Exploring>), With<Player>>,
) where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
A: Hash + Eq + Clone + Send + Sync,
@ -264,12 +263,13 @@ fn exploration_focus<S, A: 'static, D: 'static + Clone + Default + Send + Sync>(
config.action_explore_right.clone(),
) {
for map in map.iter() {
if let Ok((entity, coordinates, exploring)) = explorers.get_single() {
let coordinates = **coordinates;
if let Ok((entity, transform, exploring)) = explorers.get_single() {
let coordinates = transform.translation;
let mut exploring = if let Some(exploring) = exploring {
**exploring
} else {
coordinates.floor()
let floor = coordinates.floor();
(floor.x, floor.y)
};
let orig = exploring;
if input.just_active(explore_forward.clone()) {
@ -324,22 +324,20 @@ fn exploration_changed_announcement<D: 'static + Clone + Default + Send + Sync>(
mut commands: Commands,
mut tts: ResMut<Tts>,
map: Query<(&Map<D>, &RevealedTiles)>,
explorer: Query<(&Coordinates, &Exploring, &Viewshed), Changed<Exploring>>,
explorer: Query<(&Transform, &Exploring, &Viewshed), Changed<Exploring>>,
focused: Query<Entity, With<ExplorationFocused>>,
explorable: Query<Entity, Or<(With<Visible>, With<Explorable>)>>,
names: Query<&Name>,
types: Query<&ExplorationType>,
mappables: Query<&Mappable>,
query_pipeline: Res<QueryPipeline>,
collider_query: QueryPipelineColliderComponentsQuery,
rapier_context: Res<RapierContext>,
) -> Result<(), Box<dyn Error>> {
if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() {
let collider_set = QueryPipelineColliderComponentsSet(&collider_query);
let coordinates = coordinates.floor();
for (map, revealed_tiles) in map.iter() {
let point = **exploring;
let idx = point.to_index(map.width);
let shape = Cuboid::new(Vec2::new(0.49, 0.49).into());
let shape = Collider::cuboid(0.49, 0.49);
let known = revealed_tiles[idx];
let visible = viewshed.is_point_visible(exploring);
let fog_of_war = known && !visible;
@ -348,15 +346,14 @@ fn exploration_changed_announcement<D: 'static + Clone + Default + Send + Sync>(
for entity in focused.iter() {
commands.entity(entity).remove::<ExplorationFocused>();
}
let shape_pos = (Vec2::new(exploring.x(), exploring.y()), 0.);
query_pipeline.intersections_with_shape(
&collider_set,
&shape_pos.into(),
let exploring = Vec2::new(exploring.x(), exploring.y());
rapier_context.intersections_with_shape(
exploring,
0.,
&shape,
InteractionGroups::all(),
Some(&|v| explorable.get(v.entity()).is_ok()),
|handle| {
let entity = handle.entity();
Some(&|v| explorable.get(v).is_ok()),
|entity| {
commands.entity(entity).insert(ExplorationFocused);
if visible || mappables.get(entity).is_ok() {
if let Ok(name) = names.get(entity) {

View File

@ -10,7 +10,6 @@ pub mod commands;
pub use coord_2d;
#[macro_use]
pub mod core;
pub use derive_more;
pub mod error;
pub mod exploration;
pub use futures_lite;

View File

@ -2,7 +2,6 @@ use std::{error::Error, time::Instant};
use bevy::prelude::*;
use bevy_tts::Tts;
use derive_more::{Deref, DerefMut};
use crate::error::error_handler;

View File

@ -1,27 +1,24 @@
use std::marker::PhantomData;
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
use derive_more::{Deref, DerefMut};
use bevy_rapier2d::{
na::{Isometry2, Vector2},
prelude::*,
rapier::prelude::AABB,
};
pub use here_be_dragons::Map as MapgenMap;
use here_be_dragons::{geometry::Rect as MRect, MapFilter, Tile};
use maze_generator::{prelude::*, recursive_backtracking::RbGenerator};
use rand::prelude::StdRng;
use crate::{
core::{Coordinates, Player, PointLike},
core::{Player, PointLike},
exploration::{ExplorationType, Mappable},
log::Log,
utils::target_and_other,
visibility::Visible,
};
impl From<here_be_dragons::geometry::Point> for Coordinates {
fn from(point: here_be_dragons::geometry::Point) -> Self {
Self((point.x as f32, point.y as f32))
}
}
#[derive(Component, Copy, Clone, Debug, Deref, DerefMut, PartialEq)]
pub struct Area(pub AABB);
@ -112,7 +109,6 @@ impl Default for MapConfig {
#[derive(Bundle)]
pub struct PortalBundle {
pub coordinates: Coordinates,
pub portal: Portal,
pub exploration_type: ExplorationType,
pub mappable: Mappable,
@ -123,7 +119,6 @@ pub struct PortalBundle {
impl Default for PortalBundle {
fn default() -> Self {
Self {
coordinates: Default::default(),
portal: Default::default(),
exploration_type: ExplorationType::Portal,
mappable: Default::default(),
@ -139,7 +134,6 @@ pub struct MapBundle<D: 'static + Clone + Default + Send + Sync> {
pub spawn_colliders: SpawnColliders,
pub spawn_collider_per_tile: SpawnColliderPerTile,
pub spawn_portals: SpawnPortals,
pub children: Children,
pub transform: Transform,
pub global_transform: GlobalTransform,
}
@ -238,21 +232,16 @@ fn spawn_colliders<D: 'static + Clone + Default + Send + Sync>(
.entity(map_entity)
.remove::<SpawnColliders>()
.remove::<SpawnColliderPerTile>()
.insert_bundle(RigidBodyBundle {
body_type: RigidBodyTypeComponent(RigidBodyType::Fixed),
..Default::default()
});
.insert(RigidBody::Fixed);
if **spawn_collider_per_tile {
for y in 0..map.height {
for x in 0..map.width {
let tile = map.at(x, y);
if tile.blocks_motion() {
let id = commands
.spawn_bundle(ColliderBundle {
shape: ColliderShape::cuboid(0.5, 0.5).into(),
position: Vec2::new(x as f32 + 0.5, y as f32 + 0.5).into(),
..Default::default()
})
.spawn()
.insert(Collider::cuboid(0.5, 0.5))
.insert(Transform::from_xyz(x as f32 + 0.5, y as f32 + 0.5, 0.))
.insert(MapObstruction)
.id();
if tile.blocks_visibility() {
@ -334,11 +323,9 @@ fn spawn_colliders<D: 'static + Clone + Default + Send + Sync>(
height
);
let id = commands
.spawn_bundle(ColliderBundle {
shape: ColliderShape::cuboid(half_width, half_height).into(),
position: Vec2::new(center.x(), center.y()).into(),
..Default::default()
})
.spawn()
.insert(Collider::cuboid(half_width, half_height))
.insert(Transform::from_xyz(center.x(), center.y(), 0.))
.insert(MapObstruction)
.id();
if map.at(x as usize, y as usize).blocks_visibility() {
@ -352,19 +339,20 @@ fn spawn_colliders<D: 'static + Clone + Default + Send + Sync>(
}
}
for room in &map.rooms {
let shape =
ColliderShape::cuboid((room.width() / 2) as f32, (room.height() / 2) as f32);
let position: ColliderPositionComponent =
point!(room.center().x(), room.center().y()).into();
let aabb = shape.compute_aabb(&position);
let shape = Collider::cuboid((room.width() / 2) as f32, (room.height() / 2) as f32);
let position =
Isometry2::new(Vector2::new(room.center().x(), room.center().y()), 0.);
let aabb = shape.raw.compute_aabb(&position);
let id = commands
.spawn_bundle(ColliderBundle {
collider_type: ColliderType::Sensor.into(),
shape: shape.clone().into(),
flags: ActiveEvents::COLLISION_EVENTS.into(),
position,
..Default::default()
})
.spawn()
.insert(shape)
.insert(Sensor(true))
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(Transform::from_xyz(
position.translation.x,
position.translation.y,
0.,
))
.insert(Area(aabb))
.id();
commands.entity(map_entity).push_children(&[id]);
@ -377,9 +365,9 @@ fn spawn_portals<D: 'static + Clone + Default + Send + Sync>(
mut commands: Commands,
map: Query<(Entity, &Map<D>, &SpawnPortals), Changed<SpawnPortals>>,
) {
for (entity, map, spawn_portals) in map.iter() {
for (map_entity, map, spawn_portals) in map.iter() {
if **spawn_portals {
commands.entity(entity).remove::<SpawnPortals>();
commands.entity(map_entity).remove::<SpawnPortals>();
let mut portals: Vec<(f32, f32)> = vec![];
for x in 1..map.width {
for y in 1..map.height {
@ -415,14 +403,13 @@ fn spawn_portals<D: 'static + Clone + Default + Send + Sync>(
for portal in portals {
let x = portal.0 as f32;
let y = portal.1 as f32;
let coordinates = Coordinates((x, y));
let portal = commands
.spawn_bundle(PortalBundle {
coordinates,
..Default::default()
transform: Transform::from_translation(Vec3::new(x, y, 0.)),
..default()
})
.id();
commands.entity(entity).push_children(&[portal]);
commands.entity(map_entity).push_children(&[portal]);
}
}
}
@ -430,24 +417,19 @@ fn spawn_portals<D: 'static + Clone + Default + Send + Sync>(
fn spawn_portal_colliders<D: 'static + Clone + Default + Send + Sync>(
mut commands: Commands,
map: Query<(Entity, &SpawnColliders), With<Map<D>>>,
portals: Query<(Entity, &Coordinates), Without<ColliderShapeComponent>>,
map: Query<&SpawnColliders, With<Map<D>>>,
portals: Query<(Entity, &Transform), (With<Portal>, Without<Collider>)>,
) {
for (map_entity, spawn_colliders) in map.iter() {
for spawn_colliders in map.iter() {
if **spawn_colliders {
for (portal_entity, coordinates) in portals.iter() {
let position = Vec2::new(coordinates.x() + 0.5, coordinates.y() + 0.5).into();
let position = Vec2::new(coordinates.x() + 0.5, coordinates.y() + 0.5);
commands
.entity(portal_entity)
.insert_bundle(ColliderBundle {
collider_type: ColliderTypeComponent(ColliderType::Sensor),
shape: ColliderShape::cuboid(0.5, 0.5).into(),
position,
flags: ActiveEvents::COLLISION_EVENTS.into(),
..Default::default()
})
.insert(ColliderPositionSync::Discrete);
commands.entity(map_entity).push_children(&[portal_entity]);
.insert(Transform::from_xyz(position.x(), position.y(), 0.))
.insert(Collider::cuboid(0.5, 0.5))
.insert(Sensor(true))
.insert(ActiveEvents::COLLISION_EVENTS);
}
}
}
@ -462,14 +444,11 @@ fn area_description(
) {
for event in events.iter() {
let (entity1, entity2, started) = match event {
CollisionEvent::Started(collider1, collider2) => {
(collider1.entity(), collider2.entity(), true)
}
CollisionEvent::Stopped(collider1, collider2, _) => {
(collider1.entity(), collider2.entity(), false)
}
CollisionEvent::Started(collider1, collider2, _) => (collider1, collider2, true),
CollisionEvent::Stopped(collider1, collider2, _) => (collider1, collider2, false),
};
if let Some((area, other)) = target_and_other(entity1, entity2, &|v| areas.get(v).is_ok()) {
if let Some((area, other)) = target_and_other(*entity1, *entity2, &|v| areas.get(v).is_ok())
{
if players.get(other).is_ok() {
if let Ok((aabb, area_name)) = areas.get(area) {
let name = if let Some(name) = area_name {

View File

@ -2,9 +2,8 @@ use std::{error::Error, f32::consts::PI, fmt::Debug, hash::Hash, marker::Phantom
use bevy::prelude::*;
use bevy_input_actionmap::InputMap;
use bevy_rapier2d::{na::UnitComplex, prelude::*};
use bevy_rapier2d::prelude::*;
use bevy_tts::Tts;
use derive_more::{Deref, DerefMut};
use crate::{
core::{Angle, CardinalDirection, Player},
@ -53,11 +52,11 @@ fn movement_controls<S, A: 'static>(
mut query: Query<
(
Entity,
&mut RigidBodyVelocityComponent,
&mut Velocity,
&mut Speed,
&MaxSpeed,
Option<&RotationSpeed>,
&mut RigidBodyPositionComponent,
&mut Transform,
Option<&Destination>,
),
With<Player>,
@ -65,16 +64,16 @@ fn movement_controls<S, A: 'static>(
exploration_focused: Query<Entity, With<ExplorationFocused>>,
) where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
A: Hash + Eq + Clone + Send + Sync,
A: Hash + Eq + Copy + Send + Sync,
{
for (entity, mut velocity, mut speed, max_speed, rotation_speed, mut position, destination) in
for (entity, mut velocity, mut speed, max_speed, rotation_speed, mut transform, destination) in
query.iter_mut()
{
if **snapping {
if let (Some(_), Some(rotate_left), Some(rotate_right)) = (
rotation_speed,
config.action_rotate_left.clone(),
config.action_rotate_right.clone(),
config.action_rotate_left,
config.action_rotate_right,
) {
if input.active(rotate_left) {
continue;
@ -86,7 +85,7 @@ fn movement_controls<S, A: 'static>(
**snapping = false;
continue;
} else {
let sprinting = if let Some(action) = config.action_sprint.clone() {
let sprinting = if let Some(action) = config.action_sprint {
input.active(action)
} else {
false
@ -97,38 +96,39 @@ fn movement_controls<S, A: 'static>(
commands.entity(entity).remove::<Sprinting>();
}
let mut direction = Vec2::default();
if let Some(action) = config.action_forward.clone() {
if let Some(action) = config.action_forward {
if input.active(action) {
direction.x += 1.;
}
}
if let Some(action) = config.action_backward.clone() {
if let Some(action) = config.action_backward {
if input.active(action) {
direction.x -= 1.;
}
}
if let Some(action) = config.action_left.clone() {
if let Some(action) = config.action_left {
if input.active(action) {
direction.y += 1.;
}
}
if let Some(action) = config.action_right.clone() {
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.clone(),
config.action_rotate_right.clone(),
config.action_rotate_left,
config.action_rotate_right,
) {
let delta = rotation_speed.radians() * time.delta_seconds();
if input.active(rotate_left) {
position.position.rotation *= UnitComplex::new(delta);
transform.rotate(Quat::from_rotation_z(delta));
}
if input.active(rotate_right) {
position.position.rotation *= UnitComplex::new(-delta);
} else {
transform.rotate(Quat::from_rotation_z(-delta));
}
if !input.active(rotate_left) && !input.active(rotate_right) {
velocity.angvel = 0.;
}
} else {
@ -150,10 +150,10 @@ fn movement_controls<S, A: 'static>(
direction.y *= config.strafe_movement_factor;
}
let strength = if let (Some(forward), Some(backward), Some(left), Some(right)) = (
config.action_forward.clone(),
config.action_backward.clone(),
config.action_left.clone(),
config.action_right.clone(),
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();
@ -178,17 +178,19 @@ fn movement_controls<S, A: 'static>(
if let Some(strength) = strength {
direction *= strength;
}
let mut v: Vector<Real> = (direction * **speed).into();
v = position.position.rotation.transform_vector(&v);
let mut v = direction * **speed;
v = transform
.compute_matrix()
.transform_vector3(v.extend(0.))
.truncate();
velocity.linvel = v;
//velocity.linvel = position.position.rotation.transform_vector(&velocity.linvel);
} else if destination.is_none() {
velocity.linvel = Vec2::ZERO.into();
velocity.linvel = Vec2::ZERO;
**speed = 0.;
} else if sprinting {
**speed = **max_speed;
} else {
velocity.linvel = Vec2::ZERO.into();
velocity.linvel = Vec2::ZERO;
**speed = **max_speed / 3.;
}
}
@ -200,24 +202,17 @@ fn snap<S, A: 'static>(
input: Res<InputMap<A>>,
config: Res<NavigationConfig<S, A>>,
mut snapping: ResMut<Snapping>,
mut query: Query<
(
&mut RigidBodyPositionComponent,
&mut RigidBodyVelocityComponent,
&CardinalDirection,
),
With<Player>,
>,
mut query: Query<(&mut Transform, &mut Velocity, &CardinalDirection), With<Player>>,
) -> Result<(), Box<dyn Error>>
where
S: 'static + Clone + 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() {
if input.just_active(action) {
for (mut position, mut velocity, direction) in query.iter_mut() {
**snapping = true;
position.position.rotation = UnitComplex::new(match direction {
position.rotation = Quat::from_rotation_z(match direction {
CardinalDirection::North => PI,
CardinalDirection::East => PI / 2.,
CardinalDirection::South => 0.,
@ -229,9 +224,9 @@ where
}
if let Some(action) = config.action_snap_right.clone() {
if input.just_active(action) {
for (mut position, mut velocity, direction) in query.iter_mut() {
for (mut transform, mut velocity, direction) in query.iter_mut() {
**snapping = true;
position.position.rotation = UnitComplex::new(match direction {
transform.rotation = Quat::from_rotation_z(match direction {
CardinalDirection::North => 0.,
CardinalDirection::East => PI * 1.5,
CardinalDirection::South => PI,
@ -243,11 +238,11 @@ where
}
if let Some(action) = config.action_snap_cardinal.clone() {
if input.just_active(action) {
for (mut position, mut velocity, direction) in query.iter_mut() {
for (mut transform, mut velocity, direction) in query.iter_mut() {
**snapping = true;
let yaw: Angle = direction.into();
let yaw = yaw.radians();
position.position.rotation = UnitComplex::new(yaw);
transform.rotation = Quat::from_rotation_z(yaw);
velocity.angvel = 0.;
tts.speak(direction.to_string(), true)?;
}
@ -255,9 +250,9 @@ where
}
if let Some(action) = config.action_snap_reverse.clone() {
if input.just_active(action) {
for (mut position, mut velocity, _) in query.iter_mut() {
for (mut transform, mut velocity, _) in query.iter_mut() {
**snapping = true;
position.position.rotation *= UnitComplex::new(PI);
transform.rotate(Quat::from_rotation_z(PI));
velocity.angvel = 0.;
}
}
@ -302,13 +297,19 @@ fn speak_direction(
Ok(())
}
fn remove_speed(
removed: RemovedComponents<Speed>,
mut query: Query<&mut RigidBodyVelocityComponent>,
) {
fn add_speed(mut commands: Commands, query: Query<Entity, (Added<Speed>, Without<Velocity>)>) {
for entity in query.iter() {
commands.entity(entity).insert(Velocity {
linvel: Vec2::ZERO,
..default()
});
}
}
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) {
velocity.linvel = Vec2::ZERO.into();
velocity.linvel = Vec2::ZERO;
}
}
}
@ -366,8 +367,8 @@ impl<'a, S, A> Default for NavigationPlugin<'a, S, A> {
impl<'a, S, A> Plugin for NavigationPlugin<'a, S, A>
where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
A: Hash + Eq + Clone + Send + Sync,
S: 'static + Clone + Copy + Debug + Eq + Hash + Send + Sync,
A: Hash + Eq + Copy + Send + Sync,
'a: 'static,
{
fn build(&self, app: &mut App) {
@ -385,6 +386,7 @@ where
.register_type::<Sprinting>()
.add_system(update_direction)
.add_system(speak_direction.chain(error_handler))
.add_system(add_speed)
.add_system_to_stage(CoreStage::PostUpdate, remove_speed);
const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS";
if config.movement_control_states.is_empty() {

View File

@ -1,4 +1,4 @@
use std::{collections::HashMap, marker::PhantomData};
use std::marker::PhantomData;
use bevy::{
ecs::entity::Entities,
@ -6,18 +6,17 @@ use bevy::{
tasks::{prelude::*, Task},
};
use bevy_rapier2d::{
na::UnitComplex,
na::{Isometry2, Vector2},
prelude::*,
rapier::data::{ComponentSet, ComponentSetOption, Index},
rapier::prelude::{ColliderHandle, ColliderSet, QueryPipeline},
};
use derive_more::{Deref, DerefMut};
use futures_lite::future;
use pathfinding::prelude::*;
use crate::{
commands::RunIfExistsExt,
core::{Coordinates, PointLike},
core::PointLike,
map::{Map, MapObstruction},
navigation::{RotationSpeed, Speed},
};
@ -60,107 +59,14 @@ pub fn find_path<D: 'static + Clone + Default + Send + Sync>(
)
}
struct StaticColliderComponentsSet(
HashMap<Entity, (ColliderPosition, ColliderShape, ColliderFlags)>,
);
impl<'world, 'state>
From<(
&Query<
'world,
'state,
(
Entity,
&ColliderPositionComponent,
&ColliderShapeComponent,
&ColliderFlagsComponent,
),
>,
&Query<'world, 'state, &MapObstruction>,
)> for StaticColliderComponentsSet
{
fn from(
query: (
&Query<(
Entity,
&ColliderPositionComponent,
&ColliderShapeComponent,
&ColliderFlagsComponent,
)>,
&Query<&MapObstruction>,
),
) -> Self {
let entries = query
.0
.iter()
.filter(|(a, _, _, _)| query.1.get(*a).is_ok())
.map(|(a, b, c, d)| (a, **b, (*c).clone(), **d))
.collect::<Vec<(Entity, ColliderPosition, ColliderShape, ColliderFlags)>>();
let mut m = HashMap::new();
for (e, a, b, c) in entries {
m.insert(e, (a, b, c));
}
Self(m)
}
}
impl ComponentSet<ColliderShape> for StaticColliderComponentsSet {
fn size_hint(&self) -> usize {
self.0.len()
}
fn for_each(&self, _f: impl FnMut(Index, &ColliderShape)) {
unimplemented!()
}
}
impl ComponentSetOption<ColliderShape> for StaticColliderComponentsSet {
fn get(&self, index: Index) -> Option<&ColliderShape> {
self.0.get(&index.entity()).map(|v| &v.1)
}
}
impl ComponentSet<ColliderFlags> for StaticColliderComponentsSet {
fn size_hint(&self) -> usize {
self.0.len()
}
fn for_each(&self, _f: impl FnMut(Index, &ColliderFlags)) {
unimplemented!()
}
}
impl ComponentSetOption<ColliderFlags> for StaticColliderComponentsSet {
fn get(&self, index: Index) -> Option<&ColliderFlags> {
self.0.get(&index.entity()).map(|v| &v.2)
}
}
impl ComponentSet<ColliderPosition> for StaticColliderComponentsSet {
fn size_hint(&self) -> usize {
self.0.len()
}
fn for_each(&self, _f: impl FnMut(Index, &ColliderPosition)) {
unimplemented!()
}
}
impl ComponentSetOption<ColliderPosition> for StaticColliderComponentsSet {
fn get(&self, index: Index) -> Option<&ColliderPosition> {
let v = self.0.get(&index.entity()).map(|v| &v.0);
v
}
}
fn find_path_for_shape<D: 'static + Clone + Default + Send + Sync>(
initiator: Entity,
start: Coordinates,
initiator: ColliderHandle,
start: Transform,
destination: Destination,
query_pipeline: QueryPipeline,
map: Map<D>,
collider_set: StaticColliderComponentsSet,
shape: ColliderShape,
query_pipeline: QueryPipeline,
collider_set: ColliderSet,
shape: Collider,
) -> Option<Path> {
let path = astar(
&start.i32(),
@ -169,13 +75,13 @@ fn find_path_for_shape<D: 'static + Clone + Default + Send + Sync>(
if map.at(p.0 as usize, p.1 as usize).is_walkable() {
for tile in map.get_available_exits(p.0 as usize, p.1 as usize) {
let mut should_push = true;
let shape_pos = Isometry::new(vector![tile.0 as f32, tile.1 as f32], 0.);
let shape_pos = Isometry2::new(Vector2::new(tile.0 as f32, tile.1 as f32), 0.);
query_pipeline.intersections_with_shape(
&collider_set,
&shape_pos,
&*shape,
&*shape.raw,
InteractionGroups::all(),
Some(&|v| v.entity() != initiator),
Some(&|v| v != initiator),
|_handle| {
should_push = false;
false
@ -201,16 +107,21 @@ fn find_path_for_shape<D: 'static + Clone + Default + Send + Sync>(
fn calculate_path<D: 'static + Clone + Default + Send + Sync>(
mut commands: Commands,
pool: Res<AsyncComputeTaskPool>,
query_pipeline: Res<QueryPipeline>,
obstructions: Query<&MapObstruction>,
collider_query: QueryPipelineColliderComponentsQuery,
rapier_context: Res<RapierContext>,
obstructions: Query<&RapierColliderHandle, With<MapObstruction>>,
query: Query<
(Entity, &Destination, &Coordinates, &ColliderShapeComponent),
(
Entity,
&RapierColliderHandle,
&Destination,
&Transform,
&Collider,
),
Changed<Destination>,
>,
map: Query<&Map<D>>,
) {
for (entity, destination, coordinates, shape) in query.iter() {
for (entity, handle, destination, coordinates, shape) in query.iter() {
if coordinates.i32() == **destination {
commands
.entity(entity)
@ -224,17 +135,31 @@ fn calculate_path<D: 'static + Clone + Default + Send + Sync>(
if let Ok(map) = map.get_single() {
let coordinates_clone = *coordinates;
let destination_clone = *destination;
let query_pipeline_clone = query_pipeline.clone();
let query_pipeline = rapier_context.query_pipeline.clone();
let map_clone = map.clone();
let handle_clone = *handle;
let mut collider_set = rapier_context.colliders.clone();
let mut to_remove = vec![];
for handle in collider_set.iter() {
if !obstructions.iter().map(|v| v.0).any(|x| x == handle.0) {
to_remove.push(handle.0);
}
}
if !to_remove.is_empty() {
let mut islands = rapier_context.islands.clone();
let mut bodies = rapier_context.bodies.clone();
for handle in to_remove {
collider_set.remove(handle, &mut islands, &mut bodies, false);
}
}
let shape_clone = (*shape).clone();
let collider_set: StaticColliderComponentsSet = (&collider_query, &obstructions).into();
let task = pool.spawn(async move {
find_path_for_shape(
entity,
handle_clone.0,
coordinates_clone,
destination_clone,
query_pipeline_clone,
map_clone,
query_pipeline,
collider_set,
shape_clone,
)
@ -278,18 +203,14 @@ fn negotiate_path(
mut query: Query<(
Entity,
&mut Path,
&mut RigidBodyPositionComponent,
&mut RigidBodyVelocityComponent,
&mut Transform,
&mut Velocity,
&Speed,
Option<&RotationSpeed>,
)>,
) {
for (entity, mut path, mut position, mut velocity, speed, rotation_speed) in query.iter_mut() {
let start_i32 = (
position.position.translation.x,
position.position.translation.y,
)
.i32();
for (entity, mut path, mut transform, mut velocity, speed, rotation_speed) in query.iter_mut() {
let start_i32 = (transform.translation.x, transform.translation.y).i32();
let mut new_path = path
.iter()
.cloned()
@ -298,19 +219,16 @@ fn negotiate_path(
new_path.retain(|v| *v != start_i32);
**path = new_path;
if let Some(next) = path.first() {
let start = Vec2::new(
position.position.translation.x,
position.position.translation.y,
);
let start = Vec2::new(transform.translation.x, transform.translation.y);
let next = Vec2::new(next.0 as f32 + 0.5, next.1 as f32 + 0.5);
let mut direction = next - start;
direction = direction.normalize();
direction *= **speed;
velocity.linvel = direction.into();
velocity.linvel = direction;
if rotation_speed.is_some() {
let v = next - start;
let angle = v.y.atan2(v.x);
position.position.rotation = UnitComplex::new(angle);
transform.rotation = Quat::from_rotation_z(angle);
}
continue;
} else {
@ -320,7 +238,7 @@ fn negotiate_path(
.remove::<NoPath>()
.remove::<Destination>()
.remove::<Speed>();
velocity.linvel = Vec2::ZERO.into();
velocity.linvel = Vec2::ZERO;
}
}
}

View File

@ -5,7 +5,6 @@ use bevy::prelude::*;
use crate::{
bevy_openal::{Listener, Sound},
commands::RunIfExistsExt,
derive_more::{Deref, DerefMut},
sound::SoundIcon,
};

View File

@ -7,9 +7,9 @@ use rand::random;
use crate::{
commands::RunIfExistsExt,
core::{Coordinates, CoreConfig, Player, PointLike},
core::{CoreConfig, Player, PointLike},
exploration::ExplorationFocused,
visibility::VisibleEntities,
visibility::{Visible, VisibleEntities},
};
#[derive(Component, Clone, Debug, Reflect)]
@ -92,25 +92,23 @@ fn add_footstep_sounds(
) {
for (entity, footstep) in footsteps.iter() {
let buffer = assets.get_handle(footstep.sound);
let gain = footstep.gain;
commands.run_if_exists(entity, move |mut entity| {
entity.insert(Sound {
buffer,
state: SoundState::Stopped,
gain,
..Default::default()
..default()
});
});
}
}
fn footstep(
mut last_step_distance: Local<HashMap<Entity, (f32, Coordinates)>>,
mut last_step_distance: Local<HashMap<Entity, (f32, Transform)>>,
mut footsteps: Query<(Entity, &Footstep, &Parent, &mut Sound), Changed<GlobalTransform>>,
coordinates_storage: Query<&Coordinates>,
transforms_storage: Query<&Transform>,
) {
for (entity, footstep, parent, mut sound) in footsteps.iter_mut() {
let coordinates = coordinates_storage.get(**parent).unwrap();
let coordinates = transforms_storage.get(**parent).unwrap();
if let Some(last) = last_step_distance.get(&entity) {
let distance = last.0 + (last.1.distance(coordinates));
if distance >= footstep.step_length {
@ -158,7 +156,7 @@ fn add_sound_icon_sounds(
reference_distance,
max_distance,
rolloff_factor,
..Default::default()
..default()
});
});
}
@ -172,7 +170,7 @@ fn sound_icon<S>(
mut icons: Query<(
Entity,
&mut SoundIcon,
Option<&Coordinates>,
Option<&Visible>,
Option<&Parent>,
&mut Sound,
)>,
@ -186,8 +184,8 @@ fn sound_icon<S>(
return;
}
for visible in viewers.iter() {
for (icon_entity, mut icon, coordinates, parent, mut sound) in icons.iter_mut() {
let entity = if coordinates.is_some() {
for (icon_entity, mut icon, visibility, parent, mut sound) in icons.iter_mut() {
let entity = if visibility.is_some() {
icon_entity
} else {
**parent.unwrap()

View File

@ -8,15 +8,14 @@ use bevy::{
core::{FixedTimestep, FloatOrd},
prelude::*,
};
use bevy_rapier2d::na;
use bevy_rapier2d::na::{self, Point2};
use coord_2d::{Coord, Size};
use derive_more::{Deref, DerefMut};
use shadowcast::{vision_distance, Context, InputGrid};
use crate::{
bevy_rapier2d::prelude::*,
commands::RunIfExistsExt,
core::{Angle, Coordinates, Player, PointLike},
core::{Angle, Player, PointLike},
log::Log,
map::{ITileType, Map, MapConfig},
};
@ -52,13 +51,13 @@ impl Viewshed {
pub fn is_point_visible(&self, point: &dyn PointLike) -> bool {
self.visible_points.contains(&point.into())
}
fn update<D: 'static + Clone + Default + Send + Sync>(
&mut self,
viewer_entity: &Entity,
visible_entities: &mut VisibleEntities,
start: &dyn PointLike,
query_pipeline: &QueryPipeline,
collider_query: &QueryPipelineColliderComponentsQuery,
rapier_context: &RapierContext,
map: &Map<D>,
visible_query: &Query<&Visible>,
events: &mut EventWriter<VisibilityChanged>,
@ -66,22 +65,28 @@ impl Viewshed {
) {
let mut context: Context<u8> = Context::default();
let vision_distance = vision_distance::Circle::new(self.range);
let shape = ColliderShape::cuboid(0.5, 0.5);
let origin = point!(start.x(), start.y());
let shape = Collider::cuboid(0.5, 0.5);
let origin = Point2::new(start.x(), start.y());
let coord = Coord::new(start.x_i32(), start.y_i32());
let collider_set = QueryPipelineColliderComponentsSet(collider_query);
let mut new_visible_entities = HashSet::new();
let visibility_grid = VisibilityGrid(
map,
RefCell::new(Box::new(|coord: Coord| {
let dest = point!(coord.x as f32 + 0.5, coord.y as f32 + 0.5);
let dest = Point2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5);
if na::distance(&origin, &dest) > self.range as f32 {
return u8::MAX;
}
if let Some((opacity, entities)) = cache.get(&(FloatOrd(dest.x), FloatOrd(dest.y)))
{
for e in entities {
new_visible_entities.insert(*e);
}
if entities.contains(&viewer_entity) {
return 0;
} else {
return *opacity;
}
}
let tile = map.at(coord.x as usize, coord.y as usize);
if tile.blocks_visibility() {
cache.insert(
@ -90,37 +95,32 @@ impl Viewshed {
);
return u8::MAX;
}
if na::distance(&origin, &dest) > self.range as f32 {
return u8::MAX;
}
let mut opacity = 0;
let mut entities = HashSet::new();
let shape_pos = (Vec2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5), 0.);
query_pipeline.intersections_with_shape(
&collider_set,
&shape_pos.into(),
&*shape,
let shape_pos = Vec2::new(dest.x, dest.y);
rapier_context.intersections_with_shape(
shape_pos,
0.,
&shape,
InteractionGroups::all(),
Some(&|v| {
v.entity() != *viewer_entity && visible_query.get(v.entity()).is_ok()
}),
|handle| {
let entity = handle.entity();
Some(&|v| visible_query.get(v).is_ok()),
|entity| {
entities.insert(entity);
if let Ok(visible) = visible_query.get(entity) {
opacity = **visible;
opacity = opacity.max(**visible);
}
cache.insert(
(FloatOrd(dest.x), FloatOrd(dest.y)),
(opacity, entities.clone()),
);
true
},
);
for e in &entities {
new_visible_entities.insert(*e);
}
if entities.contains(&viewer_entity) {
0
} else {
cache.insert((FloatOrd(dest.x), FloatOrd(dest.y)), (opacity, entities));
opacity
}
})),
);
let mut new_visible = HashSet::new();
@ -187,11 +187,8 @@ pub struct ViewshedBundle {
impl ViewshedBundle {
pub fn new(range: u32) -> Self {
Self {
viewshed: Viewshed {
range,
..Default::default()
},
..Default::default()
viewshed: Viewshed { range, ..default() },
..default()
}
}
}
@ -268,50 +265,24 @@ fn update_viewshed<D: 'static + Clone + Default + Send + Sync>(
mut commands: Commands,
config: Res<RapierConfiguration>,
positions: Query<
(
Entity,
Option<&RigidBodyPositionComponent>,
Option<&ColliderPositionComponent>,
Option<&LastCoordinates>,
),
Or<(
Changed<RigidBodyPositionComponent>,
Changed<ColliderPositionComponent>,
)>,
(Entity, &Transform, &Collider, Option<&LastCoordinates>),
Or<(Changed<Transform>, Changed<Visible>)>,
>,
visible: Query<&Visible>,
mut viewers: Query<(
Entity,
&mut Viewshed,
&mut VisibleEntities,
&Coordinates,
Option<&LastCoordinates>,
)>,
mut viewers: Query<(Entity, &mut Viewshed, &mut VisibleEntities, &Transform)>,
map: Query<&Map<D>>,
query_pipeline: Res<QueryPipeline>,
collider_query: QueryPipelineColliderComponentsQuery,
rapier_context: Res<RapierContext>,
mut changed: EventWriter<VisibilityChanged>,
) {
if !config.query_pipeline_active {
return;
}
let mut to_update = HashSet::new();
for (entity, body_position, collider_position, last_coordinates) in positions.iter() {
for (entity, transform, collider, last_coordinates) in positions.iter() {
if to_update.contains(&entity) {
continue;
}
let coordinates = if let Some(body_position) = body_position {
(
body_position.position.translation.x,
body_position.position.translation.y,
)
} else {
let collider_position = collider_position.unwrap();
(
collider_position.translation.x,
collider_position.translation.y,
)
};
let coordinates = (transform.translation.x, transform.translation.y);
let coordinates_i32 = coordinates.i32();
if viewers.get_mut(entity).is_ok() {
commands.run_if_exists(entity, move |mut entity| {
@ -328,34 +299,26 @@ fn update_viewshed<D: 'static + Clone + Default + Send + Sync>(
if visible.get(entity).is_err() {
continue;
}
for (
viewer_entity,
viewshed,
visible_entities,
viewer_coordinates,
viewer_last_coordinates,
) in viewers.iter_mut()
{
if let (Some(last_coordinates), Some(viewer_last_coordinates)) =
(last_coordinates, viewer_last_coordinates)
{
if viewer_coordinates.i32() == **viewer_last_coordinates
&& coordinates.i32() == **last_coordinates
{
continue;
}
}
if viewer_entity != entity
&& (viewer_coordinates.distance(&coordinates) <= viewshed.range as f32
|| visible_entities.contains(&entity))
{
for (viewer_entity, viewshed, visible_entities, viewer_coordinates) in viewers.iter_mut() {
if viewer_entity != entity {
let forward = transform.local_x();
let rotation = forward.y.atan2(forward.x);
let distance = collider.distance_to_point(
transform.translation.truncate(),
rotation,
viewer_coordinates.translation.truncate(),
true,
);
if distance <= viewshed.range as f32 || visible_entities.contains(&entity) {
to_update.insert(viewer_entity);
to_update.insert(entity);
}
}
}
}
let mut cache = HashMap::new();
for entity in to_update.iter() {
if let Ok((viewer_entity, mut viewshed, mut visible_entities, viewer_coordinates, _)) =
if let Ok((viewer_entity, mut viewshed, mut visible_entities, viewer_coordinates)) =
viewers.get_mut(*entity)
{
if let Ok(map) = map.get_single() {
@ -363,8 +326,7 @@ fn update_viewshed<D: 'static + Clone + Default + Send + Sync>(
&viewer_entity,
&mut visible_entities,
viewer_coordinates,
&*query_pipeline,
&collider_query,
&*rapier_context,
map,
&visible,
&mut changed,
@ -377,10 +339,9 @@ fn update_viewshed<D: 'static + Clone + Default + Send + Sync>(
fn remove_visible<D: 'static + Clone + Default + Send + Sync>(
removed: RemovedComponents<Visible>,
mut viewers: Query<(Entity, &mut Viewshed, &mut VisibleEntities, &Coordinates)>,
mut viewers: Query<(Entity, &mut Viewshed, &mut VisibleEntities, &Transform)>,
map: Query<&Map<D>>,
query_pipeline: Res<QueryPipeline>,
collider_query: QueryPipelineColliderComponentsQuery,
rapier_context: Res<RapierContext>,
visible: Query<&Visible>,
mut changed: EventWriter<VisibilityChanged>,
) {
@ -396,8 +357,7 @@ fn remove_visible<D: 'static + Clone + Default + Send + Sync>(
&viewer_entity,
&mut visible_entities,
start,
&*query_pipeline,
&collider_query,
&*rapier_context,
map,
&visible,
&mut changed,
@ -430,15 +390,8 @@ fn log_visible(
mut recently_lost: Local<HashMap<Entity, Timer>>,
mut events: EventReader<VisibilityChanged>,
mut log: Query<&mut Log>,
viewers: Query<(Entity, &Coordinates, &Transform), (With<Player>, With<Viewshed>)>,
visible: Query<
(
&Name,
Option<&RigidBodyPositionComponent>,
Option<&ColliderPositionComponent>,
),
Without<DontLogWhenVisible>,
>,
viewers: Query<(Entity, &Transform), (With<Player>, With<Viewshed>)>,
visible: Query<(&Name, &Transform), Without<DontLogWhenVisible>>,
) {
for timer in recently_lost.values_mut() {
timer.tick(time.delta());
@ -449,25 +402,23 @@ fn log_visible(
VisibilityChanged::Gained { viewer, .. } => viewer,
VisibilityChanged::Lost { viewer, .. } => viewer,
};
if let Ok((viewer_entity, viewer_coordinates, viewer_transform)) = viewers.get(*viewer) {
if let Ok((viewer_entity, viewer_transform)) = viewers.get(*viewer) {
if let VisibilityChanged::Gained { viewed, .. } = event {
if *viewed == viewer_entity || recently_lost.contains_key(viewed) {
continue;
}
if let Ok((name, body_position, collider_position)) = visible.get(*viewed) {
let viewed_coordinates = if let Some(p) = body_position {
(p.position.translation.x, p.position.translation.y)
} else if let Some(p) = collider_position {
(p.translation.x, p.translation.y)
} else {
(0., 0.)
};
if let Ok((name, transform)) = visible.get(*viewed) {
let viewed_coordinates = (transform.translation.x, transform.translation.y);
let forward = viewer_transform.local_x();
let yaw = Angle::Radians(forward.y.atan2(forward.x));
let viewer_coordinates = (
viewer_transform.translation.x,
viewer_transform.translation.y,
);
let location =
viewer_coordinates.direction_and_distance(&viewed_coordinates, Some(yaw));
if let Ok(mut log) = log.get_single_mut() {
log.push(format!("{}: {location}", **name));
log.push(format!("{}: {location}", name));
}
}
} else if let VisibilityChanged::Lost { viewed, .. } = event {