Make many plugins own their own config.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nolan Darilek 2022-12-20 09:15:09 -06:00
parent 4718d51624
commit f255239517
7 changed files with 101 additions and 136 deletions

View File

@ -650,15 +650,6 @@ pub struct CoreConfig {
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>) {
if config.is_changed() {
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> {
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> {
fn build(&self, app: &mut App) {
if !app.world.contains_resource::<CoreConfig>() {
app.insert_resource(CoreConfig::default());
}
let config = *app.world.get_resource::<CoreConfig>().unwrap();
app.register_type::<CardinalDirection>()
let config = CoreConfig {
relative_direction_mode: self.relative_direction_mode,
pixels_per_unit: self.pixels_per_unit,
};
app.insert_resource(config)
.register_type::<CardinalDirection>()
.add_plugin(RapierPhysicsPlugin::<RapierUserData>::pixels_per_meter(
config.pixels_per_unit as f32,
))

View File

@ -382,18 +382,11 @@ impl<State> Default for ExplorationConfig<State> {
}
}
pub struct ExplorationPlugin<ExplorationType, State, MapData>(
PhantomData<ExplorationType>,
PhantomData<State>,
PhantomData<MapData>,
);
impl<ExplorationType, State, MapData> Default
for ExplorationPlugin<State, MapData, ExplorationType>
{
fn default() -> Self {
Self(PhantomData, PhantomData, PhantomData)
}
#[derive(Resource, Clone, Default)]
pub struct ExplorationPlugin<ExplorationType, State, MapData> {
states: Vec<State>,
exploration_type: PhantomData<ExplorationType>,
map_data: PhantomData<MapData>,
}
impl<ExplorationType, State, MapData> Plugin for ExplorationPlugin<ExplorationType, State, MapData>
@ -403,15 +396,11 @@ where
MapData: 'static + Clone + Default + Send + Sync,
{
fn build(&self, app: &mut App) {
if !app.world.contains_resource::<ExplorationConfig<State>>() {
app.insert_resource(ExplorationConfig::<State>::default());
}
let config = app
.world
.get_resource::<ExplorationConfig<State>>()
.unwrap()
.clone();
app.register_type::<ExplorationFocused>()
let config = ExplorationConfig {
states: self.states.clone(),
};
app.insert_resource(config.clone())
.register_type::<ExplorationFocused>()
.register_type::<Mappable>()
.register_type::<Explorable>()
.add_plugin(InputManagerPlugin::<ExplorationAction>::default())

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)]
pub struct PortalBundle {
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 {
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) {
if !app.world.contains_resource::<MapConfig>() {
app.insert_resource(MapConfig::default());
}
app.register_type::<Portal>()
.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>);
app.insert_resource(self.clone())
.register_type::<Portal>()
.add_system_to_stage(CoreStage::PreUpdate, spawn_colliders::<MapData>)
.add_system_to_stage(CoreStage::PreUpdate, spawn_portals::<MapData>)
.add_system_to_stage(CoreStage::PreUpdate, spawn_portal_colliders::<MapData>);
}
}

View File

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

View File

@ -12,24 +12,9 @@ use crate::{
#[reflect(Component)]
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(
mut commands: Commands,
config: Res<PitchShiftConfig>,
config: Res<PitchShiftPlugin>,
sounds: Query<(Entity, &GlobalTransform, Option<&Sound>, Option<&SoundIcon>)>,
listener: Query<&GlobalTransform, With<Listener>>,
behind: Query<Entity, With<Behind>>,
@ -71,7 +56,7 @@ struct LastIconPitch(HashMap<Entity, f64>);
struct LastSoundPitch(HashMap<Entity, f64>);
fn behind_added(
config: Res<PitchShiftConfig>,
config: Res<PitchShiftPlugin>,
mut last_icon_pitch: ResMut<LastIconPitch>,
mut last_sound_pitch: ResMut<LastSoundPitch>,
mut query: Query<(Entity, Option<&mut SoundIcon>, Option<&mut Sound>), Added<Behind>>,
@ -88,7 +73,7 @@ fn behind_added(
}
fn behind_removed(
config: Res<PitchShiftConfig>,
config: Res<PitchShiftPlugin>,
mut last_icon_pitch: ResMut<LastIconPitch>,
mut last_sound_pitch: ResMut<LastSoundPitch>,
removed: RemovedComponents<Behind>,
@ -108,7 +93,7 @@ fn behind_removed(
}
fn sound_icon_changed(
config: Res<PitchShiftConfig>,
config: Res<PitchShiftPlugin>,
mut last_icon_pitch: ResMut<LastIconPitch>,
mut icons: Query<(Entity, &mut SoundIcon), (With<Behind>, Changed<SoundIcon>)>,
) {
@ -126,7 +111,7 @@ fn sound_icon_changed(
}
fn sound_changed(
config: Res<PitchShiftConfig>,
config: Res<PitchShiftPlugin>,
mut last_sound_pitch: ResMut<LastSoundPitch>,
mut sounds: Query<(Entity, &mut Sound), (With<Behind>, Without<SoundIcon>, Changed<Sound>)>,
) {
@ -145,7 +130,7 @@ fn sound_changed(
fn sync_config(
mut commands: Commands,
config: Res<PitchShiftConfig>,
config: Res<PitchShiftPlugin>,
behind: Query<Entity, With<Behind>>,
) {
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 {
fn build(&self, app: &mut App) {
app.register_type::<Behind>()
.init_resource::<PitchShiftConfig>()
app.insert_resource(*self)
.register_type::<Behind>()
.init_resource::<LastIconPitch>()
.init_resource::<LastSoundPitch>()
.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>(
config: Res<SoundIconConfig<S>>,
config: Res<SoundIconPlugin<S>>,
state: Res<State<S>>,
time: Res<Time>,
viewers: Query<&VisibleEntities, With<Player>>,
@ -183,25 +183,24 @@ fn reset_timer_on_visibility_gain(
}
}
#[derive(Resource, Clone, Debug, Default)]
pub struct SoundIconConfig<S> {
#[derive(Resource, Clone)]
pub struct SoundIconPlugin<S> {
pub states: Vec<S>,
}
pub struct SoundIconPlugin<'a, S>(std::marker::PhantomData<&'a S>);
impl<'a, S> Default for SoundIconPlugin<'a, S> {
impl<S> Default for SoundIconPlugin<S> {
fn default() -> Self {
Self(std::marker::PhantomData)
Self { states: default() }
}
}
impl<S> Plugin for SoundIconPlugin<'static, S>
impl<S> Plugin for SoundIconPlugin<S>
where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
{
fn build(&self, app: &mut App) {
app.register_type::<SoundIcon>()
app.insert_resource(self.clone())
.register_type::<SoundIcon>()
.add_system(added)
.add_system_to_stage(CoreStage::PostUpdate, update::<S>)
.add_system_to_stage(

View File

@ -17,7 +17,7 @@ use crate::{
bevy_rapier2d::prelude::*,
core::{GlobalTransformExt, Player, PointLike},
log::Log,
map::{Map, MapConfig, MapObstruction},
map::{Map, MapObstruction, MapPlugin},
};
#[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,
query: Query<(Entity, &Map<D>), (Added<Map<D>>, Without<RevealedTiles>)>,
map_config: Res<MapConfig>,
map_config: Res<MapPlugin<MapData>>,
query: Query<(Entity, &Map<MapData>), (Added<Map<MapData>>, Without<RevealedTiles>)>,
) {
for (entity, map) in query.iter() {
let count = map.width * map.height;
@ -430,22 +430,22 @@ fn 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 {
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) {
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::PostUpdate, viewshed_removed)
.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);
}
}