Compare commits

...

2 Commits

Author SHA1 Message Date
f255239517 Make many plugins own their own config.
All checks were successful
continuous-integration/drone/push Build is passing
2022-12-20 09:15:09 -06:00
4718d51624 Cleanup of exploration plugin. 2022-12-20 08:09:40 -06:00
7 changed files with 160 additions and 188 deletions

View File

@ -650,15 +650,6 @@ pub struct CoreConfig {
pub pixels_per_unit: u8, pub pixels_per_unit: u8,
} }
impl Default for CoreConfig {
fn default() -> Self {
Self {
relative_direction_mode: RelativeDirectionMode::Directional,
pixels_per_unit: 1,
}
}
}
fn sync_config(config: Res<CoreConfig>) { fn sync_config(config: Res<CoreConfig>) {
if config.is_changed() { if config.is_changed() {
let mut mode = RELATIVE_DIRECTION_MODE.write().unwrap(); let mut mode = RELATIVE_DIRECTION_MODE.write().unwrap();
@ -666,21 +657,30 @@ fn sync_config(config: Res<CoreConfig>) {
} }
} }
pub struct CorePlugin<RapierUserData>(PhantomData<RapierUserData>); pub struct CorePlugin<RapierUserData> {
pub relative_direction_mode: RelativeDirectionMode,
pub pixels_per_unit: u8,
pub rapier_user_data: PhantomData<RapierUserData>,
}
impl<RapierUserData> Default for CorePlugin<RapierUserData> { impl<RapierUserData> Default for CorePlugin<RapierUserData> {
fn default() -> Self { fn default() -> Self {
Self(PhantomData) Self {
relative_direction_mode: RelativeDirectionMode::Directional,
pixels_per_unit: 1,
rapier_user_data: default(),
}
} }
} }
impl<RapierUserData: 'static + WorldQuery + Send + Sync> Plugin for CorePlugin<RapierUserData> { impl<RapierUserData: 'static + WorldQuery + Send + Sync> Plugin for CorePlugin<RapierUserData> {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
if !app.world.contains_resource::<CoreConfig>() { let config = CoreConfig {
app.insert_resource(CoreConfig::default()); relative_direction_mode: self.relative_direction_mode,
} pixels_per_unit: self.pixels_per_unit,
let config = *app.world.get_resource::<CoreConfig>().unwrap(); };
app.register_type::<CardinalDirection>() app.insert_resource(config)
.register_type::<CardinalDirection>()
.add_plugin(RapierPhysicsPlugin::<RapierUserData>::pixels_per_meter( .add_plugin(RapierPhysicsPlugin::<RapierUserData>::pixels_per_meter(
config.pixels_per_unit as f32, config.pixels_per_unit as f32,
)) ))

View File

@ -49,21 +49,21 @@ where
#[reflect(Component)] #[reflect(Component)]
pub struct Mappable; pub struct Mappable;
fn exploration_type_change<T, S>( fn exploration_type_change<ExplorationType, State>(
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
mut explorers: Query<( mut explorers: Query<(
&ActionState<ExplorationAction>, &ActionState<ExplorationAction>,
&VisibleEntities, &VisibleEntities,
&mut FocusedExplorationType<T>, &mut FocusedExplorationType<ExplorationType>,
)>, )>,
features: Query<&T>, features: Query<&ExplorationType>,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
T: Component + Default + Copy + Ord, ExplorationType: Component + Default + Copy + Ord,
S: 'static + Clone + Debug + Eq + Hash + Send + Sync, State: 'static + Clone + Debug + Eq + Hash + Send + Sync,
{ {
for (actions, visible, mut focused) in explorers.iter_mut() { for (actions, visible, mut focused) in explorers.iter_mut() {
let mut types: Vec<T> = vec![]; let mut types: Vec<ExplorationType> = vec![];
for e in visible.iter() { for e in visible.iter() {
if let Ok(t) = features.get(*e) { if let Ok(t) = features.get(*e) {
types.push(*t); types.push(*t);
@ -112,28 +112,28 @@ where
Ok(()) Ok(())
} }
fn exploration_type_focus<T, S>( fn exploration_type_focus<ExplorationType, State>(
mut commands: Commands, mut commands: Commands,
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
explorers: Query<( explorers: Query<(
Entity, Entity,
&ActionState<ExplorationAction>, &ActionState<ExplorationAction>,
&VisibleEntities, &VisibleEntities,
&FocusedExplorationType<T>, &FocusedExplorationType<ExplorationType>,
Option<&Exploring>, Option<&Exploring>,
)>, )>,
features: Query<(Entity, &Transform, &T)>, features: Query<(Entity, &Transform, &ExplorationType)>,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
T: Component + Default + PartialEq, ExplorationType: Component + Default + PartialEq,
S: 'static + Clone + Debug + Eq + Hash + Send + Sync, State: 'static + Clone + Debug + Eq + Hash + Send + Sync,
{ {
for (entity, actions, visible_entities, focused_type, exploring) in explorers.iter() { for (entity, actions, visible_entities, focused_type, exploring) in explorers.iter() {
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.floor(), v.2)) .map(|v| (v.1.floor(), v.2))
.collect::<Vec<((f32, f32), &T)>>(); .collect::<Vec<((f32, f32), &ExplorationType)>>();
if features.is_empty() { if features.is_empty() {
tts.speak("Nothing visible.", true)?; tts.speak("Nothing visible.", true)?;
return Ok(()); return Ok(());
@ -142,7 +142,7 @@ where
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), &T)> = None; let mut target: Option<&((f32, f32), &ExplorationType)> = 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, _)| *c > **exploring);
@ -172,18 +172,18 @@ where
Ok(()) Ok(())
} }
fn exploration_type_changed_announcement<T>( fn exploration_type_changed_announcement<ExplorationType>(
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
focused: Query< focused: Query<
( (
&FocusedExplorationType<T>, &FocusedExplorationType<ExplorationType>,
ChangeTrackers<FocusedExplorationType<T>>, ChangeTrackers<FocusedExplorationType<ExplorationType>>,
), ),
Changed<FocusedExplorationType<T>>, Changed<FocusedExplorationType<ExplorationType>>,
>, >,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
T: Component + Default + Copy + Into<String>, ExplorationType: Component + Default + Copy + Into<String>,
{ {
for (focused, changed) in focused.iter() { for (focused, changed) in focused.iter() {
if changed.is_added() { if changed.is_added() {
@ -202,9 +202,9 @@ where
Ok(()) Ok(())
} }
fn exploration_focus<S, D: 'static + Clone + Default + Send + Sync>( fn exploration_focus<State, MapData>(
mut commands: Commands, mut commands: Commands,
map: Query<&Map<D>>, map: Query<&Map<MapData>>,
explorers: Query< explorers: Query<
( (
Entity, Entity,
@ -215,7 +215,8 @@ fn exploration_focus<S, D: 'static + Clone + Default + Send + Sync>(
With<Player>, With<Player>,
>, >,
) where ) where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync, State: 'static + Clone + Debug + Eq + Hash + Send + Sync,
MapData: 'static + Clone + Default + Send + Sync,
{ {
for (entity, actions, transform, exploring) in explorers.iter() { for (entity, actions, transform, exploring) in explorers.iter() {
let coordinates = transform.translation; let coordinates = transform.translation;
@ -251,12 +252,13 @@ fn exploration_focus<S, D: 'static + Clone + Default + Send + Sync>(
} }
} }
fn navigate_to_explored<S, D: 'static + Clone + Default + Send + Sync>( fn navigate_to_explored<State, MapData>(
mut commands: Commands, mut commands: Commands,
map: Query<(&Map<D>, &RevealedTiles)>, map: Query<(&Map<MapData>, &RevealedTiles)>,
explorers: Query<(Entity, &ActionState<ExplorationAction>, &Exploring)>, explorers: Query<(Entity, &ActionState<ExplorationAction>, &Exploring)>,
) where ) where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync, State: 'static + Clone + Debug + Eq + Hash + Send + Sync,
MapData: 'static + Clone + Default + Send + Sync,
{ {
for (entity, actions, exploring) in explorers.iter() { for (entity, actions, exploring) in explorers.iter() {
for (map, revealed_tiles) in map.iter() { for (map, revealed_tiles) in map.iter() {
@ -272,21 +274,21 @@ fn navigate_to_explored<S, D: 'static + Clone + Default + Send + Sync>(
} }
} }
fn exploration_changed_announcement<T, D>( fn exploration_changed_announcement<ExplorationType, MapData>(
mut commands: Commands, mut commands: Commands,
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
map: Query<(&Map<D>, &RevealedTiles)>, map: Query<(&Map<MapData>, &RevealedTiles)>,
explorer: Query<(&Transform, &Exploring, &Viewshed), Changed<Exploring>>, explorer: Query<(&Transform, &Exploring, &Viewshed), Changed<Exploring>>,
focused: Query<Entity, With<ExplorationFocused>>, focused: Query<Entity, With<ExplorationFocused>>,
explorable: Query<Entity, Or<(With<Visible>, With<Explorable>)>>, explorable: Query<Entity, Or<(With<Visible>, With<Explorable>)>>,
names: Query<&Name>, names: Query<&Name>,
types: Query<&T>, types: Query<&ExplorationType>,
mappables: Query<&Mappable>, mappables: Query<&Mappable>,
rapier_context: Res<RapierContext>, rapier_context: Res<RapierContext>,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
T: Component + Copy + Into<String>, ExplorationType: Component + Copy + Into<String>,
D: 'static + Clone + Default + Send + Sync, MapData: 'static + Clone + Default + Send + Sync,
{ {
if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() { if let Ok((coordinates, exploring, viewshed)) = explorer.get_single() {
let coordinates = coordinates.floor(); let coordinates = coordinates.floor();
@ -370,72 +372,66 @@ fn cleanup(
} }
#[derive(Resource, Clone, Debug)] #[derive(Resource, Clone, Debug)]
pub struct ExplorationConfig<S> { pub struct ExplorationConfig<State> {
pub exploration_control_states: Vec<S>, pub states: Vec<State>,
} }
impl<S> Default for ExplorationConfig<S> { impl<State> Default for ExplorationConfig<State> {
fn default() -> Self { fn default() -> Self {
Self { Self { states: vec![] }
exploration_control_states: vec![],
}
} }
} }
pub struct ExplorationPlugin<'a, T, S: 'static, A: 'static, D>( #[derive(Resource, Clone, Default)]
PhantomData<T>, pub struct ExplorationPlugin<ExplorationType, State, MapData> {
PhantomData<&'a S>, states: Vec<State>,
PhantomData<&'static A>, exploration_type: PhantomData<ExplorationType>,
PhantomData<D>, map_data: PhantomData<MapData>,
);
impl<T, S, A, D> Default for ExplorationPlugin<'static, S, A, D, T> {
fn default() -> Self {
Self(PhantomData, PhantomData, PhantomData, PhantomData)
}
} }
impl<T, S, A, D> Plugin for ExplorationPlugin<'static, T, S, A, D> impl<ExplorationType, State, MapData> Plugin for ExplorationPlugin<ExplorationType, State, MapData>
where where
T: 'static + Component + Default + Copy + Ord + PartialEq + Into<String>, ExplorationType: 'static + Component + Default + Copy + Ord + PartialEq + Into<String>,
S: Clone + Debug + Eq + Hash + Send + Sync, State: 'static + Clone + Debug + Eq + Hash + Send + Sync,
A: Hash + Eq + Clone + Send + Sync, MapData: 'static + Clone + Default + Send + Sync,
D: 'static + Clone + Default + Send + Sync,
{ {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
if !app.world.contains_resource::<ExplorationConfig<S>>() { let config = ExplorationConfig {
app.insert_resource(ExplorationConfig::<S>::default()); states: self.states.clone(),
} };
let config = app app.insert_resource(config.clone())
.world .register_type::<ExplorationFocused>()
.get_resource::<ExplorationConfig<S>>()
.unwrap()
.clone();
app.register_type::<ExplorationFocused>()
.register_type::<Mappable>() .register_type::<Mappable>()
.register_type::<Explorable>() .register_type::<Explorable>()
.add_plugin(InputManagerPlugin::<ExplorationAction>::default()) .add_plugin(InputManagerPlugin::<ExplorationAction>::default())
.add_system(exploration_changed_announcement::<T, D>.pipe(error_handler)); .add_system(
if config.exploration_control_states.is_empty() { exploration_changed_announcement::<ExplorationType, MapData>.pipe(error_handler),
app.add_system(exploration_focus::<S, D>) );
.add_system(exploration_type_focus::<T, S>.pipe(error_handler)) if config.states.is_empty() {
.add_system(exploration_type_change::<T, S>.pipe(error_handler)) app.add_system(exploration_focus::<State, MapData>)
.add_system(navigate_to_explored::<S, D>) .add_system(exploration_type_focus::<ExplorationType, State>.pipe(error_handler))
.add_system(exploration_type_change::<ExplorationType, State>.pipe(error_handler))
.add_system(navigate_to_explored::<State, MapData>)
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
exploration_type_changed_announcement::<T>.pipe(error_handler), exploration_type_changed_announcement::<ExplorationType>.pipe(error_handler),
); );
} else { } else {
let states = config.exploration_control_states; let states = config.states;
for state in states { for state in states {
app.add_system_set( app.add_system_set(
SystemSet::on_update(state.clone()) SystemSet::on_update(state.clone())
.with_system(exploration_focus::<S, D>) .with_system(exploration_focus::<State, MapData>)
.with_system(exploration_type_focus::<T, S>.pipe(error_handler))
.with_system(exploration_type_change::<T, S>.pipe(error_handler))
.with_system(navigate_to_explored::<S, D>)
.with_system( .with_system(
exploration_type_changed_announcement::<T>.pipe(error_handler), exploration_type_focus::<ExplorationType, State>.pipe(error_handler),
)
.with_system(
exploration_type_change::<ExplorationType, State>.pipe(error_handler),
)
.with_system(navigate_to_explored::<State, MapData>)
.with_system(
exploration_type_changed_announcement::<ExplorationType>
.pipe(error_handler),
), ),
) )
.add_system_set(SystemSet::on_exit(state).with_system(cleanup)); .add_system_set(SystemSet::on_exit(state).with_system(cleanup));

View File

@ -78,11 +78,6 @@ impl ITileType for Tile {
} }
} }
#[derive(Resource, Clone, Debug, Default)]
pub struct MapConfig {
pub start_revealed: bool,
}
#[derive(Bundle, Default)] #[derive(Bundle, Default)]
pub struct PortalBundle { pub struct PortalBundle {
pub portal: Portal, pub portal: Portal,
@ -314,22 +309,27 @@ fn spawn_portal_colliders<D: 'static + Clone + Default + Send + Sync>(
} }
} }
pub struct MapPlugin<D: 'static + Clone + Default + Send + Sync>(PhantomData<D>); #[derive(Resource, Clone, Copy, Debug)]
pub struct MapPlugin<MapData: 'static + Clone + Default + Send + Sync> {
pub start_revealed: bool,
pub map_data: PhantomData<MapData>,
}
impl<D: 'static + Clone + Default + Send + Sync> Default for MapPlugin<D> { impl<MapData: 'static + Clone + Default + Send + Sync> Default for MapPlugin<MapData> {
fn default() -> Self { fn default() -> Self {
Self(Default::default()) Self {
start_revealed: default(),
map_data: default(),
}
} }
} }
impl<D: 'static + Clone + Default + Send + Sync> Plugin for MapPlugin<D> { impl<MapData: 'static + Clone + Default + Send + Sync> Plugin for MapPlugin<MapData> {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
if !app.world.contains_resource::<MapConfig>() { app.insert_resource(self.clone())
app.insert_resource(MapConfig::default()); .register_type::<Portal>()
} .add_system_to_stage(CoreStage::PreUpdate, spawn_colliders::<MapData>)
app.register_type::<Portal>() .add_system_to_stage(CoreStage::PreUpdate, spawn_portals::<MapData>)
.add_system_to_stage(CoreStage::PreUpdate, spawn_colliders::<D>) .add_system_to_stage(CoreStage::PreUpdate, spawn_portal_colliders::<MapData>);
.add_system_to_stage(CoreStage::PreUpdate, spawn_portals::<D>)
.add_system_to_stage(CoreStage::PreUpdate, spawn_portal_colliders::<D>);
} }
} }

View File

@ -1,7 +1,4 @@
use std::{ use std::{collections::HashMap, error::Error, f32::consts::PI, fmt::Debug, hash::Hash};
collections::HashMap, error::Error, f32::consts::PI, fmt::Debug, hash::Hash,
marker::PhantomData,
};
use bevy::prelude::*; use bevy::prelude::*;
use bevy_rapier2d::prelude::*; use bevy_rapier2d::prelude::*;
@ -61,9 +58,9 @@ impl Default for SnapTimer {
#[derive(Resource, Default, Deref, DerefMut)] #[derive(Resource, Default, Deref, DerefMut)]
struct SnapTimers(HashMap<Entity, SnapTimer>); struct SnapTimers(HashMap<Entity, SnapTimer>);
fn movement_controls<S>( fn movement_controls<State>(
mut commands: Commands, mut commands: Commands,
config: Res<NavigationConfig<S>>, config: Res<NavigationPlugin<State>>,
snap_timers: Res<SnapTimers>, snap_timers: Res<SnapTimers>,
mut query: Query<( mut query: Query<(
Entity, Entity,
@ -76,7 +73,7 @@ fn movement_controls<S>(
)>, )>,
exploration_focused: Query<Entity, With<ExplorationFocused>>, exploration_focused: Query<Entity, With<ExplorationFocused>>,
) where ) where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync, State: 'static + Clone + Debug + Eq + Hash + Send + Sync,
{ {
for (entity, mut actions, mut velocity, mut speed, max_speed, rotation_speed, transform) in for (entity, mut actions, mut velocity, mut speed, max_speed, rotation_speed, transform) in
&mut query &mut query
@ -295,15 +292,14 @@ fn remove_speed(removed: RemovedComponents<Speed>, mut query: Query<&mut Velocit
} }
} }
fn log_area_descriptions<S, A>( fn log_area_descriptions<State>(
mut events: EventReader<CollisionEvent>, mut events: EventReader<CollisionEvent>,
areas: Query<(&Area, Option<&Name>)>, areas: Query<(&Area, Option<&Name>)>,
players: Query<&Player>, players: Query<&Player>,
config: Res<NavigationConfig<S>>, config: Res<NavigationPlugin<State>>,
mut log: Query<&mut Log>, mut log: Query<&mut Log>,
) where ) where
S: 'static + Send + Sync, State: 'static + Send + Sync,
A: 'static + Send + Sync,
{ {
if !config.log_area_descriptions { if !config.log_area_descriptions {
return; return;
@ -340,52 +336,36 @@ fn log_area_descriptions<S, A>(
} }
#[derive(Resource, Clone, Debug)] #[derive(Resource, Clone, Debug)]
pub struct NavigationConfig<S> { pub struct NavigationPlugin<State> {
pub forward_movement_factor: f32, pub forward_movement_factor: f32,
pub backward_movement_factor: f32, pub backward_movement_factor: f32,
pub strafe_movement_factor: f32, pub strafe_movement_factor: f32,
pub sprint_movement_factor: f32, pub sprint_movement_factor: f32,
pub movement_control_states: Vec<S>, pub states: Vec<State>,
pub describe_undescribed_areas: bool, pub describe_undescribed_areas: bool,
pub log_area_descriptions: bool, pub log_area_descriptions: bool,
} }
impl<S> Default for NavigationConfig<S> { impl<State> Default for NavigationPlugin<State> {
fn default() -> Self { fn default() -> Self {
Self { Self {
forward_movement_factor: 1., forward_movement_factor: 1.,
backward_movement_factor: 1., backward_movement_factor: 1.,
strafe_movement_factor: 1., strafe_movement_factor: 1.,
sprint_movement_factor: 3., sprint_movement_factor: 3.,
movement_control_states: vec![], states: vec![],
describe_undescribed_areas: false, describe_undescribed_areas: false,
log_area_descriptions: true, log_area_descriptions: true,
} }
} }
} }
pub struct NavigationPlugin<'a, S, A>(PhantomData<&'a S>, PhantomData<&'a A>); impl<State> Plugin for NavigationPlugin<State>
impl<'a, S, A> Default for NavigationPlugin<'a, S, A> {
fn default() -> Self {
Self(PhantomData, PhantomData)
}
}
impl<S, A> Plugin for NavigationPlugin<'static, S, A>
where where
S: 'static + Clone + Copy + Debug + Eq + Hash + Send + Sync, State: 'static + Clone + Copy + Debug + Eq + Hash + Send + Sync,
A: Hash + Eq + Copy + Send + Sync,
{ {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
if !app.world.contains_resource::<NavigationConfig<S>>() { app.insert_resource(self.clone());
app.insert_resource(NavigationConfig::<S>::default());
}
let config = app
.world
.get_resource::<NavigationConfig<S>>()
.unwrap()
.clone();
app.init_resource::<SnapTimers>() app.init_resource::<SnapTimers>()
.register_type::<MaxSpeed>() .register_type::<MaxSpeed>()
.register_type::<RotationSpeed>() .register_type::<RotationSpeed>()
@ -398,21 +378,20 @@ where
.add_system(add_speed) .add_system(add_speed)
.add_system(limit_speed) .add_system(limit_speed)
.add_system_to_stage(CoreStage::PostUpdate, remove_speed) .add_system_to_stage(CoreStage::PostUpdate, remove_speed)
.add_system_to_stage(CoreStage::PostUpdate, log_area_descriptions::<S, A>); .add_system_to_stage(CoreStage::PostUpdate, log_area_descriptions::<State>);
const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS"; const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS";
if config.movement_control_states.is_empty() { if self.states.is_empty() {
app.add_system( app.add_system(
movement_controls::<S> movement_controls::<State>
.label(MOVEMENT_CONTROLS) .label(MOVEMENT_CONTROLS)
.after(limit_speed), .after(limit_speed),
) )
.add_system(snap.pipe(error_handler).before(MOVEMENT_CONTROLS)); .add_system(snap.pipe(error_handler).before(MOVEMENT_CONTROLS));
} else { } else {
let states = config.movement_control_states; for state in &self.states {
for state in states {
app.add_system_set( app.add_system_set(
SystemSet::on_update(state) SystemSet::on_update(*state)
.with_system(movement_controls::<S>.label(MOVEMENT_CONTROLS)) .with_system(movement_controls::<State>.label(MOVEMENT_CONTROLS))
.with_system(snap.pipe(error_handler).before(MOVEMENT_CONTROLS)), .with_system(snap.pipe(error_handler).before(MOVEMENT_CONTROLS)),
); );
} }

View File

@ -12,24 +12,9 @@ use crate::{
#[reflect(Component)] #[reflect(Component)]
struct Behind; struct Behind;
#[derive(Resource, Clone, Copy, Debug)]
pub struct PitchShiftConfig {
pub downshift_behind: bool,
pub downshift: f64,
}
impl Default for PitchShiftConfig {
fn default() -> Self {
Self {
downshift_behind: false,
downshift: 0.95,
}
}
}
fn tag_behind( fn tag_behind(
mut commands: Commands, mut commands: Commands,
config: Res<PitchShiftConfig>, config: Res<PitchShiftPlugin>,
sounds: Query<(Entity, &GlobalTransform, Option<&Sound>, Option<&SoundIcon>)>, sounds: Query<(Entity, &GlobalTransform, Option<&Sound>, Option<&SoundIcon>)>,
listener: Query<&GlobalTransform, With<Listener>>, listener: Query<&GlobalTransform, With<Listener>>,
behind: Query<Entity, With<Behind>>, behind: Query<Entity, With<Behind>>,
@ -71,7 +56,7 @@ struct LastIconPitch(HashMap<Entity, f64>);
struct LastSoundPitch(HashMap<Entity, f64>); struct LastSoundPitch(HashMap<Entity, f64>);
fn behind_added( fn behind_added(
config: Res<PitchShiftConfig>, config: Res<PitchShiftPlugin>,
mut last_icon_pitch: ResMut<LastIconPitch>, mut last_icon_pitch: ResMut<LastIconPitch>,
mut last_sound_pitch: ResMut<LastSoundPitch>, mut last_sound_pitch: ResMut<LastSoundPitch>,
mut query: Query<(Entity, Option<&mut SoundIcon>, Option<&mut Sound>), Added<Behind>>, mut query: Query<(Entity, Option<&mut SoundIcon>, Option<&mut Sound>), Added<Behind>>,
@ -88,7 +73,7 @@ fn behind_added(
} }
fn behind_removed( fn behind_removed(
config: Res<PitchShiftConfig>, config: Res<PitchShiftPlugin>,
mut last_icon_pitch: ResMut<LastIconPitch>, mut last_icon_pitch: ResMut<LastIconPitch>,
mut last_sound_pitch: ResMut<LastSoundPitch>, mut last_sound_pitch: ResMut<LastSoundPitch>,
removed: RemovedComponents<Behind>, removed: RemovedComponents<Behind>,
@ -108,7 +93,7 @@ fn behind_removed(
} }
fn sound_icon_changed( fn sound_icon_changed(
config: Res<PitchShiftConfig>, config: Res<PitchShiftPlugin>,
mut last_icon_pitch: ResMut<LastIconPitch>, mut last_icon_pitch: ResMut<LastIconPitch>,
mut icons: Query<(Entity, &mut SoundIcon), (With<Behind>, Changed<SoundIcon>)>, mut icons: Query<(Entity, &mut SoundIcon), (With<Behind>, Changed<SoundIcon>)>,
) { ) {
@ -126,7 +111,7 @@ fn sound_icon_changed(
} }
fn sound_changed( fn sound_changed(
config: Res<PitchShiftConfig>, config: Res<PitchShiftPlugin>,
mut last_sound_pitch: ResMut<LastSoundPitch>, mut last_sound_pitch: ResMut<LastSoundPitch>,
mut sounds: Query<(Entity, &mut Sound), (With<Behind>, Without<SoundIcon>, Changed<Sound>)>, mut sounds: Query<(Entity, &mut Sound), (With<Behind>, Without<SoundIcon>, Changed<Sound>)>,
) { ) {
@ -145,7 +130,7 @@ fn sound_changed(
fn sync_config( fn sync_config(
mut commands: Commands, mut commands: Commands,
config: Res<PitchShiftConfig>, config: Res<PitchShiftPlugin>,
behind: Query<Entity, With<Behind>>, behind: Query<Entity, With<Behind>>,
) { ) {
if config.is_changed() { if config.is_changed() {
@ -155,12 +140,25 @@ fn sync_config(
} }
} }
pub struct PitchShiftPlugin; #[derive(Resource, Clone, Copy, Debug)]
pub struct PitchShiftPlugin {
pub downshift_behind: bool,
pub downshift: f64,
}
impl Default for PitchShiftPlugin {
fn default() -> Self {
Self {
downshift_behind: false,
downshift: 0.95,
}
}
}
impl Plugin for PitchShiftPlugin { impl Plugin for PitchShiftPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.register_type::<Behind>() app.insert_resource(*self)
.init_resource::<PitchShiftConfig>() .register_type::<Behind>()
.init_resource::<LastIconPitch>() .init_resource::<LastIconPitch>()
.init_resource::<LastSoundPitch>() .init_resource::<LastSoundPitch>()
.add_system_to_stage(CoreStage::PreUpdate, tag_behind) .add_system_to_stage(CoreStage::PreUpdate, tag_behind)

View File

@ -58,7 +58,7 @@ fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added<SoundI
} }
fn update<S>( fn update<S>(
config: Res<SoundIconConfig<S>>, config: Res<SoundIconPlugin<S>>,
state: Res<State<S>>, state: Res<State<S>>,
time: Res<Time>, time: Res<Time>,
viewers: Query<&VisibleEntities, With<Player>>, viewers: Query<&VisibleEntities, With<Player>>,
@ -183,25 +183,24 @@ fn reset_timer_on_visibility_gain(
} }
} }
#[derive(Resource, Clone, Debug, Default)] #[derive(Resource, Clone)]
pub struct SoundIconConfig<S> { pub struct SoundIconPlugin<S> {
pub states: Vec<S>, pub states: Vec<S>,
} }
pub struct SoundIconPlugin<'a, S>(std::marker::PhantomData<&'a S>); impl<S> Default for SoundIconPlugin<S> {
impl<'a, S> Default for SoundIconPlugin<'a, S> {
fn default() -> Self { fn default() -> Self {
Self(std::marker::PhantomData) Self { states: default() }
} }
} }
impl<S> Plugin for SoundIconPlugin<'static, S> impl<S> Plugin for SoundIconPlugin<S>
where where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync, S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
{ {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.register_type::<SoundIcon>() app.insert_resource(self.clone())
.register_type::<SoundIcon>()
.add_system(added) .add_system(added)
.add_system_to_stage(CoreStage::PostUpdate, update::<S>) .add_system_to_stage(CoreStage::PostUpdate, update::<S>)
.add_system_to_stage( .add_system_to_stage(

View File

@ -17,7 +17,7 @@ use crate::{
bevy_rapier2d::prelude::*, bevy_rapier2d::prelude::*,
core::{GlobalTransformExt, Player, PointLike}, core::{GlobalTransformExt, Player, PointLike},
log::Log, log::Log,
map::{Map, MapConfig, MapObstruction}, map::{Map, MapObstruction, MapPlugin},
}; };
#[derive(Component, Clone, Copy, Debug, Default, Reflect)] #[derive(Component, Clone, Copy, Debug, Default, Reflect)]
@ -248,10 +248,10 @@ impl PointLike for Coord {
} }
} }
fn add_visibility_indices<D: 'static + Clone + Default + Send + Sync>( fn add_visibility_indices<MapData: 'static + Clone + Default + Send + Sync>(
mut commands: Commands, mut commands: Commands,
query: Query<(Entity, &Map<D>), (Added<Map<D>>, Without<RevealedTiles>)>, map_config: Res<MapPlugin<MapData>>,
map_config: Res<MapConfig>, query: Query<(Entity, &Map<MapData>), (Added<Map<MapData>>, Without<RevealedTiles>)>,
) { ) {
for (entity, map) in query.iter() { for (entity, map) in query.iter() {
let count = map.width * map.height; let count = map.width * map.height;
@ -430,22 +430,22 @@ fn log_visible(
pub const LOG_VISIBLE_LABEL: &str = "LOG_VISIBLE"; pub const LOG_VISIBLE_LABEL: &str = "LOG_VISIBLE";
pub struct VisibilityPlugin<D: 'static + Clone + Default + Send + Sync>(PhantomData<D>); pub struct VisibilityPlugin<MapData: 'static + Clone + Default + Send + Sync>(PhantomData<MapData>);
impl<D: 'static + Clone + Default + Send + Sync> Default for VisibilityPlugin<D> { impl<MapData: 'static + Clone + Default + Send + Sync> Default for VisibilityPlugin<MapData> {
fn default() -> Self { fn default() -> Self {
Self(Default::default()) Self(Default::default())
} }
} }
impl<D: 'static + Clone + Default + Send + Sync> Plugin for VisibilityPlugin<D> { impl<MapData: 'static + Clone + Default + Send + Sync> Plugin for VisibilityPlugin<MapData> {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_event::<VisibilityChanged>() app.add_event::<VisibilityChanged>()
.add_system_to_stage(CoreStage::PreUpdate, add_visibility_indices::<D>) .add_system_to_stage(CoreStage::PreUpdate, add_visibility_indices::<MapData>)
.add_system_to_stage(CoreStage::PreUpdate, update_viewshed) .add_system_to_stage(CoreStage::PreUpdate, update_viewshed)
.add_system_to_stage(CoreStage::PostUpdate, viewshed_removed) .add_system_to_stage(CoreStage::PostUpdate, viewshed_removed)
.add_system_to_stage(CoreStage::PostUpdate, remove_visible) .add_system_to_stage(CoreStage::PostUpdate, remove_visible)
.add_system_to_stage(CoreStage::PreUpdate, update_revealed_tiles::<D>) .add_system_to_stage(CoreStage::PreUpdate, update_revealed_tiles::<MapData>)
.add_system_to_stage(CoreStage::PreUpdate, log_visible); .add_system_to_stage(CoreStage::PreUpdate, log_visible);
} }
} }