From 13d1cb07340451598dccb7786558d38f272d0ffc Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 10 May 2022 13:56:49 -0500 Subject: [PATCH] Get rid of `Coordinates`. --- src/core.rs | 156 +++++---------------------------------------- src/error.rs | 2 +- src/exploration.rs | 15 +++-- src/map.rs | 17 ++--- src/pathfinding.rs | 6 +- src/sound.rs | 14 ++-- src/visibility.rs | 21 +++--- 7 files changed, 51 insertions(+), 180 deletions(-) diff --git a/src/core.rs b/src/core.rs index 884fa0c..b0e2d8f 100644 --- a/src/core.rs +++ b/src/core.rs @@ -3,115 +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 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 for Coordinates { - fn from(v: Vec2) -> Self { - Self((v.x, v.y)) - } -} - -impl From for Coordinates { - fn from(v: Vec3) -> Self { - Self((v.x, v.y)) - } -} - -impl From for Vec2 { - fn from(c: Coordinates) -> Self { - Vec2::new(c.0 .0, c.0 .1) - } -} - -impl From 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), @@ -467,6 +368,16 @@ 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 Vec2 { fn x(&self) -> f32 { self.x @@ -507,16 +418,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 @@ -542,8 +443,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() @@ -618,22 +517,6 @@ fn setup(core_config: Res) { *mode = core_config.relative_direction_mode; } -fn copy_coordinates_to_transform( - config: Res, - mut query: Query<(&Coordinates, &mut Transform), Changed>, -) { - 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; - } - } -} - #[derive(Clone, Copy, Debug)] pub struct CoreConfig { pub relative_direction_mode: RelativeDirectionMode, @@ -670,16 +553,11 @@ impl Plugin for CorePlugin().unwrap(); - app.register_type::() - .add_plugin(RapierPhysicsPlugin::::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(sync_config); + app.add_plugin(RapierPhysicsPlugin::::pixels_per_meter( + config.pixels_per_unit as f32, + )) + .add_startup_system(setup) + .add_system(sync_config); } } diff --git a/src/error.rs b/src/error.rs index 220b659..2f49bdc 100644 --- a/src/error.rs +++ b/src/error.rs @@ -62,7 +62,7 @@ impl Plugin for ErrorPlugin { dsn.as_str(), sentry::ClientOptions { release: Some(release.into()), - ..Default::default() + ..default() }, )); app.insert_resource(guard); diff --git a/src/exploration.rs b/src/exploration.rs index 84ff096..cb4b1cd 100644 --- a/src/exploration.rs +++ b/src/exploration.rs @@ -6,7 +6,7 @@ use bevy_rapier2d::prelude::*; use bevy_tts::Tts; use crate::{ - core::{Coordinates, Player, PointLike}, + core::{Player, PointLike}, error::error_handler, map::Map, pathfinding::Destination, @@ -154,7 +154,7 @@ fn exploration_type_focus( &FocusedExplorationType, Option<&Exploring>, )>, - features: Query<(Entity, &Coordinates, &ExplorationType)>, + features: Query<(Entity, &Transform, &ExplorationType)>, ) -> Result<(), Box> where S: 'static + Clone + Debug + Eq + Hash + Send + Sync, @@ -246,7 +246,7 @@ fn exploration_focus( config: Res>, input: Res>, map: Query<&Map>, - explorers: Query<(Entity, &Coordinates, Option<&Exploring>), With>, + explorers: Query<(Entity, &Transform, Option<&Exploring>), With>, ) where S: 'static + Clone + Debug + Eq + Hash + Send + Sync, A: Hash + Eq + Clone + Send + Sync, @@ -263,12 +263,13 @@ fn exploration_focus( 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()) { @@ -323,7 +324,7 @@ fn exploration_changed_announcement( mut commands: Commands, mut tts: ResMut, map: Query<(&Map, &RevealedTiles)>, - explorer: Query<(&Coordinates, &Exploring, &Viewshed), Changed>, + explorer: Query<(&Transform, &Exploring, &Viewshed), Changed>, focused: Query>, explorable: Query, With)>>, names: Query<&Name>, diff --git a/src/map.rs b/src/map.rs index dbb1f85..0aa2ebe 100644 --- a/src/map.rs +++ b/src/map.rs @@ -12,19 +12,13 @@ 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 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); @@ -115,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, @@ -126,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(), @@ -412,11 +404,10 @@ fn spawn_portals( 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]); @@ -428,7 +419,7 @@ fn spawn_portals( fn spawn_portal_colliders( mut commands: Commands, map: Query<(Entity, &SpawnColliders), With>>, - portals: Query<(Entity, &Coordinates), Without>, + portals: Query<(Entity, &Transform), Without>, ) { for (map_entity, spawn_colliders) in map.iter() { if **spawn_colliders { diff --git a/src/pathfinding.rs b/src/pathfinding.rs index 6770ef0..1145502 100644 --- a/src/pathfinding.rs +++ b/src/pathfinding.rs @@ -16,7 +16,7 @@ use pathfinding::prelude::*; use crate::{ commands::RunIfExistsExt, - core::{Coordinates, PointLike}, + core::PointLike, map::{Map, MapObstruction}, navigation::{RotationSpeed, Speed}, }; @@ -61,7 +61,7 @@ pub fn find_path( fn find_path_for_shape( initiator: ColliderHandle, - start: Coordinates, + start: Transform, destination: Destination, map: Map, query_pipeline: QueryPipeline, @@ -114,7 +114,7 @@ fn calculate_path( Entity, &RapierColliderHandle, &Destination, - &Coordinates, + &Transform, &Collider, ), Changed, diff --git a/src/sound.rs b/src/sound.rs index 6705ac3..9bbf164 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -7,7 +7,7 @@ use rand::random; use crate::{ commands::RunIfExistsExt, - core::{Coordinates, CoreConfig, Player, PointLike}, + core::{CoreConfig, Player, PointLike}, exploration::ExplorationFocused, visibility::VisibleEntities, }; @@ -98,19 +98,19 @@ fn add_footstep_sounds( buffer, state: SoundState::Stopped, gain, - ..Default::default() + ..default() }); }); } } fn footstep( - mut last_step_distance: Local>, + mut last_step_distance: Local>, mut footsteps: Query<(Entity, &Footstep, &Parent, &mut Sound), Changed>, - 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 +158,7 @@ fn add_sound_icon_sounds( reference_distance, max_distance, rolloff_factor, - ..Default::default() + ..default() }); }); } @@ -172,7 +172,7 @@ fn sound_icon( mut icons: Query<( Entity, &mut SoundIcon, - Option<&Coordinates>, + Option<&Transform>, Option<&Parent>, &mut Sound, )>, diff --git a/src/visibility.rs b/src/visibility.rs index 76bca90..d3eef4b 100644 --- a/src/visibility.rs +++ b/src/visibility.rs @@ -15,7 +15,7 @@ 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}, }; @@ -182,11 +182,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,7 +265,7 @@ fn update_viewshed( Entity, &mut Viewshed, &mut VisibleEntities, - &Coordinates, + &Transform, Option<&LastCoordinates>, )>, map: Query<&Map>, @@ -348,7 +345,7 @@ fn update_viewshed( fn remove_visible( removed: RemovedComponents, - mut viewers: Query<(Entity, &mut Viewshed, &mut VisibleEntities, &Coordinates)>, + mut viewers: Query<(Entity, &mut Viewshed, &mut VisibleEntities, &Transform)>, map: Query<&Map>, rapier_context: Res, visible: Query<&Visible>, @@ -399,7 +396,7 @@ fn log_visible( mut recently_lost: Local>, mut events: EventReader, mut log: Query<&mut Log>, - viewers: Query<(Entity, &Coordinates, &Transform), (With, With)>, + viewers: Query<(Entity, &Transform), (With, With)>, visible: Query<(&Name, &Transform), Without>, ) { for timer in recently_lost.values_mut() { @@ -411,7 +408,7 @@ 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; @@ -420,6 +417,10 @@ fn log_visible( 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() {