diff --git a/Cargo.toml b/Cargo.toml index 3b0cbf3..b807f8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ speech_dispatcher_0_10 = ["bevy_tts/speech_dispatcher_0_10"] speech_dispatcher_0_11 = ["bevy_tts/speech_dispatcher_0_11"] [dependencies] +avian2d = "0.1" bevy = "0.14" -bevy_rapier2d = "0.27" bevy_synthizer = "0.7" bevy_tts = { version = "0.9", default-features = false, features = ["tolk"] } coord_2d = "0.3" diff --git a/src/core.rs b/src/core.rs index 417c86a..a8e327d 100644 --- a/src/core.rs +++ b/src/core.rs @@ -6,12 +6,14 @@ use std::{ sync::RwLock, }; -use bevy::{app::PluginGroupBuilder, math::FloatOrd, prelude::*}; -use bevy_rapier2d::{ - parry::query::{closest_points, distance, ClosestPoints}, +use avian2d::{ + parry::{ + math::Isometry, + query::{closest_points, distance, ClosestPoints}, + }, prelude::*, - rapier::{math::Isometry, prelude::Aabb}, }; +use bevy::{app::PluginGroupBuilder, math::FloatOrd, prelude::*}; use once_cell::sync::Lazy; use rand::prelude::*; use serde::{Deserialize, Serialize}; @@ -530,7 +532,14 @@ impl GlobalTransformExt for GlobalTransform { (other.translation() / *scale).xy().into(), other.yaw().radians(), ); - closest_points(&pos1, &*collider.raw, &pos2, &*other_collider.raw, f32::MAX).unwrap() + closest_points( + &pos1, + collider.shape().as_ref(), + &pos2, + other_collider.shape().as_ref(), + f32::MAX, + ) + .unwrap() } fn collider_direction_and_distance( @@ -549,7 +558,13 @@ impl GlobalTransformExt for GlobalTransform { other.yaw().radians(), ); let closest = self.closest_points(collider, other, other_collider); - let distance = distance(&pos1, &*collider.raw, &pos2, &*other_collider.raw).unwrap() as u32; + let distance = distance( + &pos1, + collider.shape().as_ref(), + &pos2, + other_collider.shape().as_ref(), + ) + .unwrap() as u32; let tile_or_tiles = if distance == 1 { "tile" } else { "tiles" }; if distance > 0 { if let ClosestPoints::WithinMargin(p1, p2) = closest { @@ -568,9 +583,6 @@ impl GlobalTransformExt for GlobalTransform { } } -#[derive(Component, Copy, Clone, Debug, Deref, DerefMut, PartialEq)] -pub struct Area(pub Aabb); - #[derive(Component, Clone, Copy, Debug, Default, Reflect)] #[reflect(Component)] pub struct Player; @@ -676,9 +688,7 @@ impl Plugin for CorePlugin { }; app.insert_resource(config) .register_type::() - .add_plugins(RapierPhysicsPlugin::::pixels_per_meter( - config.pixels_per_unit as f32, - )) + .add_plugins(PhysicsPlugins::default().with_length_unit(config.pixels_per_unit as f32)) .add_systems(Startup, setup) .add_systems(Update, sync_config); } diff --git a/src/exploration.rs b/src/exploration.rs index b6f164b..a91660c 100644 --- a/src/exploration.rs +++ b/src/exploration.rs @@ -1,7 +1,7 @@ use std::{error::Error, fmt::Debug, hash::Hash, marker::PhantomData}; +use avian2d::prelude::*; use bevy::prelude::*; -use bevy_rapier2d::prelude::*; use bevy_tts::Tts; use leafwing_input_manager::prelude::*; @@ -281,7 +281,7 @@ fn exploration_changed_announcement( names: Query<&Name>, types: Query<&ExplorationType>, mappables: Query<&Mappable>, - rapier_context: Res, + spatial_query: SpatialQuery, ) -> Result<(), Box> where ExplorationType: Component + Copy + Into, @@ -290,7 +290,7 @@ where if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() { let coordinates = coordinates.trunc(); let point = **exploring; - let shape = Collider::cuboid(0.5 - f32::EPSILON, 0.5 - f32::EPSILON); + let shape = Collider::rectangle(0.5 - f32::EPSILON, 0.5 - f32::EPSILON); let (known, idx) = if let Ok((map, revealed_tiles)) = map.get_single() { let idx = point.to_index(map.width); (revealed_tiles[idx], Some(idx)) @@ -305,20 +305,22 @@ where commands.entity(entity).remove::(); } let exploring = Vec2::new(exploring.x(), exploring.y()); - rapier_context.intersections_with_shape( + spatial_query.shape_intersections_callback( + &shape, exploring, 0., - &shape, - QueryFilter::new().predicate(&|v| explorable.get(v).is_ok()), + SpatialQueryFilter::default(), |entity| { - commands.entity(entity).insert(ExplorationFocused); - if visible || mappables.get(entity).is_ok() { - if let Ok(name) = names.get(entity) { - tokens.push(name.to_string()); - } - if tokens.is_empty() { - if let Ok(t) = types.get(entity) { - tokens.push((*t).into()); + if explorable.contains(entity) { + commands.entity(entity).insert(ExplorationFocused); + if visible || mappables.get(entity).is_ok() { + if let Ok(name) = names.get(entity) { + tokens.push(name.to_string()); + } + if tokens.is_empty() { + if let Ok(t) = types.get(entity) { + tokens.push((*t).into()); + } } } } diff --git a/src/lib.rs b/src/lib.rs index 7d02e5f..ae3a49e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,8 +2,8 @@ #![allow(clippy::too_many_arguments)] #![allow(clippy::type_complexity)] +pub use avian2d; pub use bevy; -pub use bevy_rapier2d; pub use bevy_synthizer; pub use bevy_tts; pub use coord_2d; diff --git a/src/map.rs b/src/map.rs index 258135e..b8ffc4c 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,20 +1,13 @@ use std::marker::PhantomData; +use avian2d::prelude::*; use bevy::prelude::*; -use bevy_rapier2d::{ - na::{Isometry2, Vector2}, - prelude::*, -}; 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::{Area, PointLike}, - exploration::Mappable, - visibility::Visible, -}; +use crate::{core::PointLike, exploration::Mappable, visibility::Visible}; #[derive(Component, Clone, Default, Deref, DerefMut)] pub struct Map(pub MapgenMap); @@ -80,9 +73,9 @@ impl ITileType for Tile { #[derive(Bundle, Default)] pub struct PortalBundle { + pub transform: TransformBundle, pub portal: Portal, pub mappable: Mappable, - pub transform: TransformBundle, } #[derive(Bundle, Clone, Default)] @@ -98,7 +91,6 @@ pub struct TileBundle { pub transform: TransformBundle, pub collider: Collider, pub rigid_body: RigidBody, - pub active_collision_types: ActiveCollisionTypes, pub map_obstruction: MapObstruction, } @@ -106,12 +98,9 @@ impl Default for TileBundle { fn default() -> Self { Self { transform: default(), - collider: Collider::cuboid(0.5, 0.5), - rigid_body: RigidBody::Fixed, - active_collision_types: ActiveCollisionTypes::default() - | ActiveCollisionTypes::KINEMATIC_STATIC - | ActiveCollisionTypes::DYNAMIC_STATIC, - map_obstruction: MapObstruction, + collider: Collider::rectangle(0.5, 0.5), + rigid_body: RigidBody::Static, + map_obstruction: default(), } } } @@ -129,34 +118,28 @@ impl TileBundle { pub struct ZoneBundle { pub collider: Collider, pub transform: TransformBundle, - pub area: Area, pub zone: Zone, pub sensor: Sensor, - pub active_events: ActiveEvents, } impl ZoneBundle { - fn new(collider: Collider, position: (f32, f32)) -> Self { - let point = Isometry2::new(Vector2::new(position.0, position.1), 0.); - let aabb = collider.raw.compute_aabb(&point); + fn new(collider: Collider, position: Vec2) -> Self { Self { collider, - area: Area(aabb), - transform: Transform::from_translation(Vec3::new(position.0, position.1, 0.)).into(), + transform: Transform::from_translation(position.extend(0.)).into(), zone: default(), sensor: default(), - active_events: ActiveEvents::COLLISION_EVENTS, } } } impl From<&MRect> for ZoneBundle { fn from(rect: &MRect) -> Self { - let collider = Collider::cuboid( + let collider = Collider::rectangle( rect.width() as f32 / 2. + 0.5, rect.height() as f32 / 2. + 0.5, ); - let position = (rect.center().x(), rect.center().y()); + let position = Vec2::new(rect.center().x(), rect.center().y()); Self::new(collider, position) } } @@ -332,7 +315,7 @@ fn spawn_portal_colliders( for portal_entity in &portals { commands .entity(portal_entity) - .insert((Collider::cuboid(0.5, 0.5), Sensor)); + .insert((Collider::rectangle(0.5, 0.5), Sensor)); } commands.entity(entity).remove::(); } diff --git a/src/navigation.rs b/src/navigation.rs index 05ca405..fc90a05 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -1,14 +1,17 @@ +#![allow(clippy::map_entry)] + use std::{collections::HashMap, error::Error, f32::consts::PI, fmt::Debug, hash::Hash}; +use avian2d::prelude::*; use bevy::prelude::*; -use bevy_rapier2d::prelude::*; use bevy_tts::Tts; use leafwing_input_manager::prelude::*; use crate::{ - core::{Angle, Area, CardinalDirection, GlobalTransformExt, Player, TransformExt}, + core::{Angle, CardinalDirection, GlobalTransformExt, Player, TransformExt}, error::error_handler, log::Log, + map::Zone, utils::target_and_other, }; @@ -151,13 +154,14 @@ fn tick_snap_timers(time: Res