From 24334ba51164948949bb92032a3a20f444fc7475 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 15 Mar 2022 10:37:28 -0500 Subject: [PATCH] Update mapgen to custom fork. --- Cargo.toml | 2 +- src/exploration.rs | 31 ++++++++++++++-------------- src/map.rs | 51 ++++++++++++++++++++++++++++------------------ src/pathfinding.rs | 26 ++++++++++++++--------- src/visibility.rs | 49 +++++++++++++++++++++++++++----------------- 5 files changed, 94 insertions(+), 65 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3f73bda..0192fdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ coord_2d = "0.3" derive_more = "0.99" futures-lite = "1" gilrs = "0.8" -mapgen = "0.5" +mapgen = { git = "https://github.com/ndarilek/mapgen.rs" } maze_generator = "1" once_cell = "1" pathfinding = "3" diff --git a/src/exploration.rs b/src/exploration.rs index 5f34ed3..87991ae 100644 --- a/src/exploration.rs +++ b/src/exploration.rs @@ -243,11 +243,11 @@ fn exploration_type_changed_announcement( Ok(()) } -fn exploration_focus( +fn exploration_focus( mut commands: Commands, config: Res>, input: Res>, - map: Query<&Map>, + map: Query<&Map>, explorers: Query<(Entity, &Coordinates, Option<&Exploring>), With>, ) where S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash, @@ -295,11 +295,11 @@ fn exploration_focus( } } -fn navigate_to_explored( +fn navigate_to_explored( mut commands: Commands, config: Res>, input: Res>, - map: Query<(&Map, &RevealedTiles)>, + map: Query<(&Map, &RevealedTiles)>, explorers: Query<(Entity, &Exploring)>, ) where S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash, @@ -321,10 +321,10 @@ fn navigate_to_explored( } } -fn exploration_changed_announcement( +fn exploration_changed_announcement( mut commands: Commands, mut tts: ResMut, - map: Query<(&Map, &RevealedTiles)>, + map: Query<(&Map, &RevealedTiles)>, explorer: Query<(&Coordinates, &Exploring, &Viewshed), Changed>, focused: Query>, explorable: Query, With)>>, @@ -442,18 +442,19 @@ impl Default for ExplorationConfig { } } -pub struct ExplorationPlugin<'a, S, A>(PhantomData<&'a S>, PhantomData<&'a A>); +pub struct ExplorationPlugin<'a, S, A, D>(PhantomData<&'a S>, PhantomData<&'a A>, PhantomData); -impl<'a, S, A> Default for ExplorationPlugin<'a, S, A> { +impl<'a, S, A, D> Default for ExplorationPlugin<'a, S, A, D> { fn default() -> Self { - Self(PhantomData, PhantomData) + Self(PhantomData, PhantomData, PhantomData) } } -impl<'a, S, A> Plugin for ExplorationPlugin<'a, S, A> +impl<'a, S, A, D> Plugin for ExplorationPlugin<'a, S, A, D> where S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash, A: Hash + Eq + Clone + Send + Sync, + D: 'static + Clone + Default + Send + Sync, 'a: 'static, { fn build(&self, app: &mut App) { @@ -470,13 +471,13 @@ where .register_type::() .add_system_to_stage( CoreStage::PostUpdate, - exploration_changed_announcement.chain(error_handler), + exploration_changed_announcement::.chain(error_handler), ); if config.exploration_control_states.is_empty() { - app.add_system(exploration_focus::) + app.add_system(exploration_focus::) .add_system(exploration_type_focus::.chain(error_handler)) .add_system(exploration_type_change::.chain(error_handler)) - .add_system(navigate_to_explored::) + .add_system(navigate_to_explored::) .add_system_to_stage( CoreStage::PostUpdate, exploration_type_changed_announcement.chain(error_handler), @@ -486,10 +487,10 @@ where for state in states { app.add_system_set( SystemSet::on_update(state.clone()) - .with_system(exploration_focus::) + .with_system(exploration_focus::) .with_system(exploration_type_focus::.chain(error_handler)) .with_system(exploration_type_change::.chain(error_handler)) - .with_system(navigate_to_explored::) + .with_system(navigate_to_explored::) .with_system(exploration_type_changed_announcement.chain(error_handler)), ) .add_system_set(SystemSet::on_exit(state).with_system(cleanup)); diff --git a/src/map.rs b/src/map.rs index 37d5668..15bf8d1 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,4 +1,7 @@ -use std::collections::{HashMap, HashSet}; +use std::{ + collections::{HashMap, HashSet}, + marker::PhantomData, +}; use bevy::prelude::*; use bevy_rapier2d::prelude::*; @@ -26,10 +29,10 @@ impl From for Coordinates { pub struct Area(pub AABB); #[derive(Component, Clone, Default, Deref, DerefMut)] -pub struct Map(pub MapgenMap); +pub struct Map(pub MapgenMap); -impl From for Map { - fn from(v: MapgenMap) -> Self { +impl From> for Map { + fn from(v: MapgenMap) -> Self { Self(v) } } @@ -134,8 +137,8 @@ impl Default for PortalBundle { } #[derive(Bundle, Clone, Default)] -pub struct MapBundle { - pub map: Map, +pub struct MapBundle { + pub map: Map, pub spawn_colliders: SpawnColliders, pub spawn_collider_per_tile: SpawnColliderPerTile, pub spawn_portals: SpawnPortals, @@ -167,8 +170,8 @@ impl GridBuilder { } } -impl MapFilter for GridBuilder { - fn modify_map(&self, _rng: &mut StdRng, map: &MapgenMap) -> MapgenMap { +impl MapFilter for GridBuilder { + fn modify_map(&self, _rng: &mut StdRng, map: &MapgenMap) -> MapgenMap { let mut map = map.clone(); let mut generator = RbGenerator::new(None); let maze = generator.generate(self.width_in_rooms as i32, self.height_in_rooms as i32); @@ -224,9 +227,9 @@ impl MapFilter for GridBuilder { } } -fn spawn_colliders( +fn spawn_colliders( mut commands: Commands, - maps: Query<(Entity, &Map, &SpawnColliders, &SpawnColliderPerTile), Changed>, + maps: Query<(Entity, &Map, &SpawnColliders, &SpawnColliderPerTile), Changed>, ) { for (map_entity, map, spawn_colliders, spawn_collider_per_tile) in maps.iter() { if **spawn_colliders { @@ -325,7 +328,9 @@ fn spawn_colliders( let center = (bl.0 as f32 + half_width, br.1 as f32 + half_height); trace!( "Create shape at {:?} of width {:?} and height {:?}", - center, width, height + center, + width, + height ); let id = commands .spawn_bundle(ColliderBundle { @@ -367,9 +372,9 @@ fn spawn_colliders( } } -fn spawn_portals( +fn spawn_portals( mut commands: Commands, - map: Query<(Entity, &Map, &SpawnPortals), Changed>, + map: Query<(Entity, &Map, &SpawnPortals), Changed>, ) { for (entity, map, spawn_portals) in map.iter() { if **spawn_portals { @@ -422,9 +427,9 @@ fn spawn_portals( } } -fn spawn_portal_colliders( +fn spawn_portal_colliders( mut commands: Commands, - map: Query<(Entity, &SpawnColliders), With>, + map: Query<(Entity, &SpawnColliders), With>>, portals: Query<(Entity, &Coordinates), Without>, ) { for (map_entity, spawn_colliders) in map.iter() { @@ -484,18 +489,24 @@ fn area_description( } } -pub struct MapPlugin; +pub struct MapPlugin(PhantomData); -impl Plugin for MapPlugin { +impl Default for MapPlugin { + fn default() -> Self { + Self(Default::default()) + } +} + +impl Plugin for MapPlugin { fn build(&self, app: &mut App) { if !app.world.contains_resource::() { app.insert_resource(MapConfig::default()); } let config = app.world.get_resource::().unwrap().clone(); app.register_type::() - .add_system(spawn_colliders) - .add_system(spawn_portals) - .add_system(spawn_portal_colliders); + .add_system(spawn_colliders::) + .add_system(spawn_portals::) + .add_system(spawn_portal_colliders::); if config.speak_area_descriptions { app.add_system_to_stage(CoreStage::PostUpdate, area_description); } diff --git a/src/pathfinding.rs b/src/pathfinding.rs index c43f31a..9543d7e 100644 --- a/src/pathfinding.rs +++ b/src/pathfinding.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::{collections::HashMap, marker::PhantomData}; use bevy::{ ecs::entity::Entities, @@ -39,10 +39,10 @@ pub struct NoPath; #[reflect(Component)] pub struct Path(pub Vec<(i32, i32)>); -pub fn find_path( +pub fn find_path( start: &dyn PointLike, destination: &dyn PointLike, - map: &Map, + map: &Map, ) -> Option<(Vec<(i32, i32)>, u32)> { astar( &start.into(), @@ -153,12 +153,12 @@ impl ComponentSetOption for StaticColliderComponentsSet { } } -fn find_path_for_shape( +fn find_path_for_shape( initiator: Entity, start: Coordinates, destination: Destination, query_pipeline: QueryPipeline, - map: Map, + map: Map, collider_set: StaticColliderComponentsSet, shape: ColliderShape, ) -> Option { @@ -198,7 +198,7 @@ fn find_path_for_shape( } } -fn calculate_path( +fn calculate_path( mut commands: Commands, pool: Res, query_pipeline: Res, @@ -208,7 +208,7 @@ fn calculate_path( (Entity, &Destination, &Coordinates, &ColliderShapeComponent), Changed, >, - map: Query<&Map>, + map: Query<&Map>, ) { for (entity, destination, coordinates, shape) in query.iter() { if coordinates.i32() == **destination { @@ -325,11 +325,17 @@ fn negotiate_path( } } -pub struct PathfindingPlugin; +pub struct PathfindingPlugin(PhantomData); -impl Plugin for PathfindingPlugin { +impl Default for PathfindingPlugin { + fn default() -> Self { + Self(Default::default()) + } +} + +impl Plugin for PathfindingPlugin { fn build(&self, app: &mut App) { - app.add_system(calculate_path) + app.add_system(calculate_path::) .add_system_to_stage(CoreStage::PostUpdate, remove_destination) .add_system(poll_tasks) .add_system(negotiate_path); diff --git a/src/visibility.rs b/src/visibility.rs index 3d0b0b9..8bf4f21 100644 --- a/src/visibility.rs +++ b/src/visibility.rs @@ -1,6 +1,7 @@ use std::{ cell::RefCell, collections::{HashMap, HashSet}, + marker::PhantomData, }; use bevy::{core::FixedTimestep, prelude::*}; @@ -44,14 +45,14 @@ impl Viewshed { pub fn is_point_visible(&self, point: &dyn PointLike) -> bool { self.visible_points.contains(&point.into()) } - fn update( + fn update( &mut self, viewer_entity: &Entity, visible_entities: &mut VisibleEntities, start: &dyn PointLike, query_pipeline: &QueryPipeline, collider_query: &QueryPipelineColliderComponentsQuery, - map: &Map, + map: &Map, visible_query: &Query<&Visible>, events: &mut EventWriter, ) { @@ -187,9 +188,9 @@ impl PointLike for Coord { } } -fn add_visibility_indices( +fn add_visibility_indices( mut commands: Commands, - query: Query<(Entity, &Map), (Added, Without)>, + query: Query<(Entity, &Map), (Added>, Without)>, map_config: Res, ) { for (entity, map) in query.iter() { @@ -217,13 +218,17 @@ fn viewshed_removed( } } -pub struct VisibilityGrid<'a, F>(pub &'a Map, pub RefCell>); +pub struct VisibilityGrid<'a, D: 'static + Clone + Default + Send + Sync, F>( + pub &'a Map, + pub RefCell>, +); -impl<'a, F> InputGrid for VisibilityGrid<'a, F> +impl<'a, D, F> InputGrid for VisibilityGrid<'a, D, F> where + D: 'static + Clone + Default + Send + Sync, F: FnMut(Coord) -> u8, { - type Grid = VisibilityGrid<'a, F>; + type Grid = VisibilityGrid<'a, D, F>; type Opacity = u8; @@ -236,12 +241,12 @@ where } } -fn update_viewshed( +fn update_viewshed( config: Res, positions: Query<(Entity, &RigidBodyPositionComponent), Changed>, visible: Query<&Visible>, mut viewers: Query<(Entity, &mut Viewshed, &mut VisibleEntities, &Coordinates)>, - map: Query<&Map>, + map: Query<&Map>, query_pipeline: Res, collider_query: QueryPipelineColliderComponentsQuery, mut changed: EventWriter, @@ -293,10 +298,10 @@ fn update_viewshed( } } -fn remove_visible( +fn remove_visible( removed: RemovedComponents, mut viewers: Query<(Entity, &mut Viewshed, &mut VisibleEntities, &Coordinates)>, - map: Query<&Map>, + map: Query<&Map>, query_pipeline: Res, collider_query: QueryPipelineColliderComponentsQuery, visible: Query<&Visible>, @@ -324,8 +329,8 @@ fn remove_visible( } } -fn update_revealed_tiles( - mut map: Query<(&Map, &mut RevealedTiles)>, +fn update_revealed_tiles( + mut map: Query<(&Map, &mut RevealedTiles)>, viewers: Query<&Viewshed, With>, ) { for (map, mut revealed_tiles) in map.iter_mut() { @@ -395,16 +400,22 @@ fn log_visible( pub const LOG_VISIBLE_LABEL: &str = "LOG_VISIBLE"; -pub struct VisibilityPlugin; +pub struct VisibilityPlugin(PhantomData); -impl Plugin for VisibilityPlugin { +impl Default for VisibilityPlugin { + fn default() -> Self { + Self(Default::default()) + } +} + +impl Plugin for VisibilityPlugin { fn build(&self, app: &mut App) { app.add_event::() - .add_system(add_visibility_indices) - .add_system_to_stage(CoreStage::PostUpdate, update_viewshed) + .add_system(add_visibility_indices::) + .add_system_to_stage(CoreStage::PostUpdate, update_viewshed::) .add_system_to_stage(CoreStage::PostUpdate, viewshed_removed) - .add_system_to_stage(CoreStage::PostUpdate, remove_visible) - .add_system_to_stage(CoreStage::PostUpdate, update_revealed_tiles) + .add_system_to_stage(CoreStage::PostUpdate, remove_visible::) + .add_system_to_stage(CoreStage::PostUpdate, update_revealed_tiles::) .add_system_to_stage(CoreStage::PostUpdate, log_visible); } }