Compare commits

..

No commits in common. "f255239517b7c37f57e5f5090c8ec4ec1b948ea9" and "1e9860e3a129e30bbb675ef182d4243e75239777" have entirely different histories.

7 changed files with 188 additions and 160 deletions

View File

@ -650,6 +650,15 @@ 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();
@ -657,30 +666,21 @@ fn sync_config(config: Res<CoreConfig>) {
} }
} }
pub struct CorePlugin<RapierUserData> { pub struct CorePlugin<RapierUserData>(PhantomData<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 { Self(PhantomData)
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) {
let config = CoreConfig { if !app.world.contains_resource::<CoreConfig>() {
relative_direction_mode: self.relative_direction_mode, app.insert_resource(CoreConfig::default());
pixels_per_unit: self.pixels_per_unit, }
}; let config = *app.world.get_resource::<CoreConfig>().unwrap();
app.insert_resource(config) app.register_type::<CardinalDirection>()
.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<ExplorationType, State>( fn exploration_type_change<T, S>(
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
mut explorers: Query<( mut explorers: Query<(
&ActionState<ExplorationAction>, &ActionState<ExplorationAction>,
&VisibleEntities, &VisibleEntities,
&mut FocusedExplorationType<ExplorationType>, &mut FocusedExplorationType<T>,
)>, )>,
features: Query<&ExplorationType>, features: Query<&T>,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
ExplorationType: Component + Default + Copy + Ord, T: Component + Default + Copy + Ord,
State: 'static + Clone + Debug + Eq + Hash + Send + Sync, S: '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<ExplorationType> = vec![]; let mut types: Vec<T> = 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<ExplorationType, State>( fn exploration_type_focus<T, S>(
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<ExplorationType>, &FocusedExplorationType<T>,
Option<&Exploring>, Option<&Exploring>,
)>, )>,
features: Query<(Entity, &Transform, &ExplorationType)>, features: Query<(Entity, &Transform, &T)>,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
ExplorationType: Component + Default + PartialEq, T: Component + Default + PartialEq,
State: 'static + Clone + Debug + Eq + Hash + Send + Sync, S: '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), &ExplorationType)>>(); .collect::<Vec<((f32, f32), &T)>>();
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), &ExplorationType)> = None; let mut target: Option<&((f32, f32), &T)> = 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<ExplorationType>( fn exploration_type_changed_announcement<T>(
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
focused: Query< focused: Query<
( (
&FocusedExplorationType<ExplorationType>, &FocusedExplorationType<T>,
ChangeTrackers<FocusedExplorationType<ExplorationType>>, ChangeTrackers<FocusedExplorationType<T>>,
), ),
Changed<FocusedExplorationType<ExplorationType>>, Changed<FocusedExplorationType<T>>,
>, >,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
ExplorationType: Component + Default + Copy + Into<String>, T: 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<State, MapData>( fn exploration_focus<S, D: 'static + Clone + Default + Send + Sync>(
mut commands: Commands, mut commands: Commands,
map: Query<&Map<MapData>>, map: Query<&Map<D>>,
explorers: Query< explorers: Query<
( (
Entity, Entity,
@ -215,8 +215,7 @@ fn exploration_focus<State, MapData>(
With<Player>, With<Player>,
>, >,
) where ) where
State: 'static + Clone + Debug + Eq + Hash + Send + Sync, S: '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;
@ -252,13 +251,12 @@ fn exploration_focus<State, MapData>(
} }
} }
fn navigate_to_explored<State, MapData>( fn navigate_to_explored<S, D: 'static + Clone + Default + Send + Sync>(
mut commands: Commands, mut commands: Commands,
map: Query<(&Map<MapData>, &RevealedTiles)>, map: Query<(&Map<D>, &RevealedTiles)>,
explorers: Query<(Entity, &ActionState<ExplorationAction>, &Exploring)>, explorers: Query<(Entity, &ActionState<ExplorationAction>, &Exploring)>,
) where ) where
State: 'static + Clone + Debug + Eq + Hash + Send + Sync, S: '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() {
@ -274,21 +272,21 @@ fn navigate_to_explored<State, MapData>(
} }
} }
fn exploration_changed_announcement<ExplorationType, MapData>( fn exploration_changed_announcement<T, D>(
mut commands: Commands, mut commands: Commands,
mut tts: ResMut<Tts>, mut tts: ResMut<Tts>,
map: Query<(&Map<MapData>, &RevealedTiles)>, map: Query<(&Map<D>, &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<&ExplorationType>, types: Query<&T>,
mappables: Query<&Mappable>, mappables: Query<&Mappable>,
rapier_context: Res<RapierContext>, rapier_context: Res<RapierContext>,
) -> Result<(), Box<dyn Error>> ) -> Result<(), Box<dyn Error>>
where where
ExplorationType: Component + Copy + Into<String>, T: Component + Copy + Into<String>,
MapData: 'static + Clone + Default + Send + Sync, D: '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();
@ -372,66 +370,72 @@ fn cleanup(
} }
#[derive(Resource, Clone, Debug)] #[derive(Resource, Clone, Debug)]
pub struct ExplorationConfig<State> { pub struct ExplorationConfig<S> {
pub states: Vec<State>, pub exploration_control_states: Vec<S>,
} }
impl<State> Default for ExplorationConfig<State> { impl<S> Default for ExplorationConfig<S> {
fn default() -> Self { fn default() -> Self {
Self { states: vec![] } Self {
exploration_control_states: vec![],
}
} }
} }
#[derive(Resource, Clone, Default)] pub struct ExplorationPlugin<'a, T, S: 'static, A: 'static, D>(
pub struct ExplorationPlugin<ExplorationType, State, MapData> { PhantomData<T>,
states: Vec<State>, PhantomData<&'a S>,
exploration_type: PhantomData<ExplorationType>, PhantomData<&'static A>,
map_data: PhantomData<MapData>, PhantomData<D>,
);
impl<T, S, A, D> Default for ExplorationPlugin<'static, S, A, D, T> {
fn default() -> Self {
Self(PhantomData, PhantomData, PhantomData, PhantomData)
}
} }
impl<ExplorationType, State, MapData> Plugin for ExplorationPlugin<ExplorationType, State, MapData> impl<T, S, A, D> Plugin for ExplorationPlugin<'static, T, S, A, D>
where where
ExplorationType: 'static + Component + Default + Copy + Ord + PartialEq + Into<String>, T: 'static + Component + Default + Copy + Ord + PartialEq + Into<String>,
State: 'static + Clone + Debug + Eq + Hash + Send + Sync, S: Clone + Debug + Eq + Hash + Send + Sync,
MapData: 'static + Clone + Default + Send + Sync, A: Hash + Eq + Clone + Send + Sync,
D: 'static + Clone + Default + Send + Sync,
{ {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
let config = ExplorationConfig { if !app.world.contains_resource::<ExplorationConfig<S>>() {
states: self.states.clone(), app.insert_resource(ExplorationConfig::<S>::default());
}; }
app.insert_resource(config.clone()) let config = app
.register_type::<ExplorationFocused>() .world
.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( .add_system(exploration_changed_announcement::<T, D>.pipe(error_handler));
exploration_changed_announcement::<ExplorationType, MapData>.pipe(error_handler), if config.exploration_control_states.is_empty() {
); app.add_system(exploration_focus::<S, D>)
if config.states.is_empty() { .add_system(exploration_type_focus::<T, S>.pipe(error_handler))
app.add_system(exploration_focus::<State, MapData>) .add_system(exploration_type_change::<T, S>.pipe(error_handler))
.add_system(exploration_type_focus::<ExplorationType, State>.pipe(error_handler)) .add_system(navigate_to_explored::<S, D>)
.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::<ExplorationType>.pipe(error_handler), exploration_type_changed_announcement::<T>.pipe(error_handler),
); );
} else { } else {
let states = config.states; let states = config.exploration_control_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::<State, MapData>) .with_system(exploration_focus::<S, D>)
.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_focus::<ExplorationType, State>.pipe(error_handler), exploration_type_changed_announcement::<T>.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,6 +78,11 @@ 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,
@ -309,27 +314,22 @@ fn spawn_portal_colliders<D: 'static + Clone + Default + Send + Sync>(
} }
} }
#[derive(Resource, Clone, Copy, Debug)] pub struct MapPlugin<D: 'static + Clone + Default + Send + Sync>(PhantomData<D>);
pub struct MapPlugin<MapData: 'static + Clone + Default + Send + Sync> {
pub start_revealed: bool,
pub map_data: PhantomData<MapData>,
}
impl<MapData: 'static + Clone + Default + Send + Sync> Default for MapPlugin<MapData> { impl<D: 'static + Clone + Default + Send + Sync> Default for MapPlugin<D> {
fn default() -> Self { fn default() -> Self {
Self { Self(Default::default())
start_revealed: default(),
map_data: default(),
}
} }
} }
impl<MapData: 'static + Clone + Default + Send + Sync> Plugin for MapPlugin<MapData> { impl<D: 'static + Clone + Default + Send + Sync> Plugin for MapPlugin<D> {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.insert_resource(self.clone()) if !app.world.contains_resource::<MapConfig>() {
.register_type::<Portal>() app.insert_resource(MapConfig::default());
.add_system_to_stage(CoreStage::PreUpdate, spawn_colliders::<MapData>) }
.add_system_to_stage(CoreStage::PreUpdate, spawn_portals::<MapData>) app.register_type::<Portal>()
.add_system_to_stage(CoreStage::PreUpdate, spawn_portal_colliders::<MapData>); .add_system_to_stage(CoreStage::PreUpdate, spawn_colliders::<D>)
.add_system_to_stage(CoreStage::PreUpdate, spawn_portals::<D>)
.add_system_to_stage(CoreStage::PreUpdate, spawn_portal_colliders::<D>);
} }
} }

View File

@ -1,4 +1,7 @@
use std::{collections::HashMap, error::Error, f32::consts::PI, fmt::Debug, hash::Hash}; use std::{
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::*;
@ -58,9 +61,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<State>( fn movement_controls<S>(
mut commands: Commands, mut commands: Commands,
config: Res<NavigationPlugin<State>>, config: Res<NavigationConfig<S>>,
snap_timers: Res<SnapTimers>, snap_timers: Res<SnapTimers>,
mut query: Query<( mut query: Query<(
Entity, Entity,
@ -73,7 +76,7 @@ fn movement_controls<State>(
)>, )>,
exploration_focused: Query<Entity, With<ExplorationFocused>>, exploration_focused: Query<Entity, With<ExplorationFocused>>,
) where ) where
State: 'static + Clone + Debug + Eq + Hash + Send + Sync, S: '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
@ -292,14 +295,15 @@ fn remove_speed(removed: RemovedComponents<Speed>, mut query: Query<&mut Velocit
} }
} }
fn log_area_descriptions<State>( fn log_area_descriptions<S, A>(
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<NavigationPlugin<State>>, config: Res<NavigationConfig<S>>,
mut log: Query<&mut Log>, mut log: Query<&mut Log>,
) where ) where
State: 'static + Send + Sync, S: 'static + Send + Sync,
A: 'static + Send + Sync,
{ {
if !config.log_area_descriptions { if !config.log_area_descriptions {
return; return;
@ -336,36 +340,52 @@ fn log_area_descriptions<State>(
} }
#[derive(Resource, Clone, Debug)] #[derive(Resource, Clone, Debug)]
pub struct NavigationPlugin<State> { pub struct NavigationConfig<S> {
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 states: Vec<State>, pub movement_control_states: Vec<S>,
pub describe_undescribed_areas: bool, pub describe_undescribed_areas: bool,
pub log_area_descriptions: bool, pub log_area_descriptions: bool,
} }
impl<State> Default for NavigationPlugin<State> { impl<S> Default for NavigationConfig<S> {
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.,
states: vec![], movement_control_states: vec![],
describe_undescribed_areas: false, describe_undescribed_areas: false,
log_area_descriptions: true, log_area_descriptions: true,
} }
} }
} }
impl<State> Plugin for NavigationPlugin<State> pub struct NavigationPlugin<'a, S, A>(PhantomData<&'a S>, PhantomData<&'a A>);
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
State: 'static + Clone + Copy + Debug + Eq + Hash + Send + Sync, S: '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) {
app.insert_resource(self.clone()); if !app.world.contains_resource::<NavigationConfig<S>>() {
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>()
@ -378,20 +398,21 @@ 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::<State>); .add_system_to_stage(CoreStage::PostUpdate, log_area_descriptions::<S, A>);
const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS"; const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS";
if self.states.is_empty() { if config.movement_control_states.is_empty() {
app.add_system( app.add_system(
movement_controls::<State> movement_controls::<S>
.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 {
for state in &self.states { let states = config.movement_control_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::<State>.label(MOVEMENT_CONTROLS)) .with_system(movement_controls::<S>.label(MOVEMENT_CONTROLS))
.with_system(snap.pipe(error_handler).before(MOVEMENT_CONTROLS)), .with_system(snap.pipe(error_handler).before(MOVEMENT_CONTROLS)),
); );
} }

View File

@ -12,9 +12,24 @@ 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<PitchShiftPlugin>, config: Res<PitchShiftConfig>,
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>>,
@ -56,7 +71,7 @@ struct LastIconPitch(HashMap<Entity, f64>);
struct LastSoundPitch(HashMap<Entity, f64>); struct LastSoundPitch(HashMap<Entity, f64>);
fn behind_added( fn behind_added(
config: Res<PitchShiftPlugin>, config: Res<PitchShiftConfig>,
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>>,
@ -73,7 +88,7 @@ fn behind_added(
} }
fn behind_removed( fn behind_removed(
config: Res<PitchShiftPlugin>, config: Res<PitchShiftConfig>,
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>,
@ -93,7 +108,7 @@ fn behind_removed(
} }
fn sound_icon_changed( fn sound_icon_changed(
config: Res<PitchShiftPlugin>, config: Res<PitchShiftConfig>,
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>)>,
) { ) {
@ -111,7 +126,7 @@ fn sound_icon_changed(
} }
fn sound_changed( fn sound_changed(
config: Res<PitchShiftPlugin>, config: Res<PitchShiftConfig>,
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>)>,
) { ) {
@ -130,7 +145,7 @@ fn sound_changed(
fn sync_config( fn sync_config(
mut commands: Commands, mut commands: Commands,
config: Res<PitchShiftPlugin>, config: Res<PitchShiftConfig>,
behind: Query<Entity, With<Behind>>, behind: Query<Entity, With<Behind>>,
) { ) {
if config.is_changed() { if config.is_changed() {
@ -140,25 +155,12 @@ fn sync_config(
} }
} }
#[derive(Resource, Clone, Copy, Debug)] pub struct PitchShiftPlugin;
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.insert_resource(*self) app.register_type::<Behind>()
.register_type::<Behind>() .init_resource::<PitchShiftConfig>()
.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<SoundIconPlugin<S>>, config: Res<SoundIconConfig<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,24 +183,25 @@ fn reset_timer_on_visibility_gain(
} }
} }
#[derive(Resource, Clone)] #[derive(Resource, Clone, Debug, Default)]
pub struct SoundIconPlugin<S> { pub struct SoundIconConfig<S> {
pub states: Vec<S>, pub states: Vec<S>,
} }
impl<S> Default for SoundIconPlugin<S> { pub struct SoundIconPlugin<'a, S>(std::marker::PhantomData<&'a S>);
impl<'a, S> Default for SoundIconPlugin<'a, S> {
fn default() -> Self { fn default() -> Self {
Self { states: default() } Self(std::marker::PhantomData)
} }
} }
impl<S> Plugin for SoundIconPlugin<S> impl<S> Plugin for SoundIconPlugin<'static, 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.insert_resource(self.clone()) app.register_type::<SoundIcon>()
.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, MapObstruction, MapPlugin}, map::{Map, MapConfig, MapObstruction},
}; };
#[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<MapData: 'static + Clone + Default + Send + Sync>( fn add_visibility_indices<D: 'static + Clone + Default + Send + Sync>(
mut commands: Commands, mut commands: Commands,
map_config: Res<MapPlugin<MapData>>, query: Query<(Entity, &Map<D>), (Added<Map<D>>, Without<RevealedTiles>)>,
query: Query<(Entity, &Map<MapData>), (Added<Map<MapData>>, Without<RevealedTiles>)>, map_config: Res<MapConfig>,
) { ) {
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<MapData: 'static + Clone + Default + Send + Sync>(PhantomData<MapData>); pub struct VisibilityPlugin<D: 'static + Clone + Default + Send + Sync>(PhantomData<D>);
impl<MapData: 'static + Clone + Default + Send + Sync> Default for VisibilityPlugin<MapData> { impl<D: 'static + Clone + Default + Send + Sync> Default for VisibilityPlugin<D> {
fn default() -> Self { fn default() -> Self {
Self(Default::default()) Self(Default::default())
} }
} }
impl<MapData: 'static + Clone + Default + Send + Sync> Plugin for VisibilityPlugin<MapData> { impl<D: 'static + Clone + Default + Send + Sync> Plugin for VisibilityPlugin<D> {
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::<MapData>) .add_system_to_stage(CoreStage::PreUpdate, add_visibility_indices::<D>)
.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::<MapData>) .add_system_to_stage(CoreStage::PreUpdate, update_revealed_tiles::<D>)
.add_system_to_stage(CoreStage::PreUpdate, log_visible); .add_system_to_stage(CoreStage::PreUpdate, log_visible);
} }
} }