From c835100272c20d660d9eeaa199d1c51919d220d6 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 3 Feb 2022 09:45:29 -0500 Subject: [PATCH] Create systems module, and move the Specs systems there --- src/effects/particles.rs | 2 +- src/gui/hud.rs | 4 +- src/main.rs | 15 ++----- src/state.rs | 21 +++------ src/systems.rs | 32 ++++++++++++++ src/{ => systems}/ai.rs | 0 src/{ => systems}/ai/adjacent_ai_system.rs | 0 src/{ => systems}/ai/approach_ai_system.rs | 0 src/{ => systems}/ai/chase_ai_system.rs | 0 src/{ => systems}/ai/default_move_system.rs | 0 src/{ => systems}/ai/encumbrance_system.rs | 0 src/{ => systems}/ai/flee_ai_system.rs | 0 src/{ => systems}/ai/initiative_system.rs | 0 src/{ => systems}/ai/quipping.rs | 0 src/{ => systems}/ai/turn_status.rs | 0 src/{ => systems}/ai/visible_ai_system.rs | 0 src/systems/dispatcher.rs | 44 +++++++++++++++++++ src/systems/dispatcher/single_thread.rs | 43 ++++++++++++++++++ src/{ => systems}/hunger_system.rs | 0 src/{ => systems}/inventory_system.rs | 0 .../inventory_system/collection_system.rs | 0 .../inventory_system/drop_system.rs | 0 .../inventory_system/equip_use.rs | 0 .../inventory_system/identification_system.rs | 0 .../inventory_system/remove_system.rs | 0 .../inventory_system/use_system.rs | 0 src/{ => systems}/lighting_system.rs | 4 +- src/{ => systems}/map_indexing_system.rs | 0 src/{ => systems}/melee_combat_system.rs | 0 src/{ => systems}/movement_system.rs | 0 src/{ => systems}/particle_system.rs | 0 src/{ => systems}/ranged_combat_system.rs | 0 src/{ => systems}/trigger_system.rs | 0 src/{ => systems}/visibility_system.rs | 0 34 files changed, 133 insertions(+), 32 deletions(-) create mode 100644 src/systems.rs rename src/{ => systems}/ai.rs (100%) rename src/{ => systems}/ai/adjacent_ai_system.rs (100%) rename src/{ => systems}/ai/approach_ai_system.rs (100%) rename src/{ => systems}/ai/chase_ai_system.rs (100%) rename src/{ => systems}/ai/default_move_system.rs (100%) rename src/{ => systems}/ai/encumbrance_system.rs (100%) rename src/{ => systems}/ai/flee_ai_system.rs (100%) rename src/{ => systems}/ai/initiative_system.rs (100%) rename src/{ => systems}/ai/quipping.rs (100%) rename src/{ => systems}/ai/turn_status.rs (100%) rename src/{ => systems}/ai/visible_ai_system.rs (100%) create mode 100644 src/systems/dispatcher.rs create mode 100644 src/systems/dispatcher/single_thread.rs rename src/{ => systems}/hunger_system.rs (100%) rename src/{ => systems}/inventory_system.rs (100%) rename src/{ => systems}/inventory_system/collection_system.rs (100%) rename src/{ => systems}/inventory_system/drop_system.rs (100%) rename src/{ => systems}/inventory_system/equip_use.rs (100%) rename src/{ => systems}/inventory_system/identification_system.rs (100%) rename src/{ => systems}/inventory_system/remove_system.rs (100%) rename src/{ => systems}/inventory_system/use_system.rs (100%) rename src/{ => systems}/lighting_system.rs (93%) rename src/{ => systems}/map_indexing_system.rs (100%) rename src/{ => systems}/melee_combat_system.rs (100%) rename src/{ => systems}/movement_system.rs (100%) rename src/{ => systems}/particle_system.rs (100%) rename src/{ => systems}/ranged_combat_system.rs (100%) rename src/{ => systems}/trigger_system.rs (100%) rename src/{ => systems}/visibility_system.rs (100%) diff --git a/src/effects/particles.rs b/src/effects/particles.rs index 64a58b7..0d4d5fd 100644 --- a/src/effects/particles.rs +++ b/src/effects/particles.rs @@ -3,7 +3,7 @@ use ::specs::prelude::*; use super::{EffectSpawner, EffectType}; use crate::components::{ParticleAnimation, ParticleLifetime, Position, Renderable}; use crate::map::Map; -use crate::particle_system::ParticleBuilder; +use crate::systems::particle_system::ParticleBuilder; pub fn particle_to_tile(ecs: &mut World, tile_idx: i32, effect: &EffectSpawner) { if let EffectType::Particle { diff --git a/src/gui/hud.rs b/src/gui/hud.rs index de3c580..f02d89f 100644 --- a/src/gui/hud.rs +++ b/src/gui/hud.rs @@ -105,13 +105,13 @@ pub fn map_label(ecs: &World, draw_batch: &mut DrawBatch) { to_cp437('┤'), ); draw_batch.set( - Point::new(x_pos + name_length as i32, 0), + Point::new(x_pos + name_length as i32 + 1, 0), ColorPair::new(colors::BOX_GRAY, colors::BLACK), to_cp437('├'), ); draw_batch.print_color( Point::new(x_pos + 1, 0), - &map.name, + &format!(" {} ", map.name), ColorPair::new(colors::WHITE, colors::BLACK), ); } diff --git a/src/main.rs b/src/main.rs index 0ce24d1..7970c25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ #[macro_use] extern crate lazy_static; -mod ai; mod colors; mod components; mod damage_system; @@ -12,18 +11,10 @@ mod effects; mod gamelog; mod gamesystem; mod gui; -mod hunger_system; -mod inventory_system; -mod lighting_system; mod map; pub mod map_builders; -mod map_indexing_system; -mod melee_combat_system; -mod movement_system; -mod particle_system; mod player; mod random_table; -mod ranged_combat_system; mod raws; mod rect; mod rex_assets; @@ -31,8 +22,7 @@ mod saveload_system; mod spatial; mod spawner; mod state; -mod trigger_system; -mod visibility_system; +mod systems; use ::specs::prelude::*; use ::specs::saveload::{SimpleMarker, SimpleMarkerAllocator}; @@ -164,6 +154,7 @@ fn register_components(state: &mut State) { /// * Generates the first [`map`](crate::state::State::generate_world_map) fn init_state() -> State { use ::rltk::{Point, RandomNumberGenerator}; + use systems::particle_system::ParticleBuilder; let mut state = State::new(); @@ -184,7 +175,7 @@ fn init_state() -> State { state.ecs.insert(player_entity); state.ecs.insert(RunState::MapGeneration {}); - state.ecs.insert(particle_system::ParticleBuilder::new()); + state.ecs.insert(ParticleBuilder::new()); state.ecs.insert(rex_assets::RexAssets::new()); state.generate_world_map(1, 0); diff --git a/src/state.rs b/src/state.rs index f216d67..d3daf64 100644 --- a/src/state.rs +++ b/src/state.rs @@ -4,25 +4,16 @@ use ::specs::prelude::*; use crate::components::*; use crate::gui::{self, show_cheat_mode, CheatMenuResult, MainMenuSelection}; -use crate::hunger_system::HungerSystem; -use crate::inventory_system::{ - ItemCollectionSystem, ItemDropSystem, ItemEquipOnUse, ItemIdentificationSystem, - ItemRemoveSystem, ItemUseSystem, SpellUseSystem, -}; -use crate::lighting_system::LightingSystem; use crate::map::{self, *}; -use crate::map_indexing_system::MapIndexingSystem; -use crate::melee_combat_system::MeleeCombatSystem; -use crate::movement_system::MovementSystem; -use crate::particle_system::{self, ParticleSpawnSystem}; use crate::player::*; -use crate::ranged_combat_system::RangedCombatSystem; use crate::raws::*; -use crate::trigger_system::TriggerSystem; -use crate::visibility_system::VisibilitySystem; -use crate::{ - ai, camera, colors, damage_system, effects, gamelog, player, saveload_system, spawner, +use crate::systems::{ + ai, particle_system, HungerSystem, ItemCollectionSystem, ItemDropSystem, ItemEquipOnUse, + ItemIdentificationSystem, ItemRemoveSystem, ItemUseSystem, LightingSystem, MapIndexingSystem, + MeleeCombatSystem, MovementSystem, ParticleSpawnSystem, RangedCombatSystem, SpellUseSystem, + TriggerSystem, VisibilitySystem, }; +use crate::{camera, colors, damage_system, effects, gamelog, player, saveload_system, spawner}; /// Whether to show a visual representation of map generation pub const SHOW_MAPGEN_VISUALIZER: bool = false; diff --git a/src/systems.rs b/src/systems.rs new file mode 100644 index 0000000..cd86105 --- /dev/null +++ b/src/systems.rs @@ -0,0 +1,32 @@ +mod dispatcher; +pub use dispatcher::UnifiedDispatcher; + +// Systems +pub mod ai; +mod hunger_system; +mod inventory_system; +mod lighting_system; +mod map_indexing_system; +mod melee_combat_system; +mod movement_system; +pub mod particle_system; +mod ranged_combat_system; +mod trigger_system; +mod visibility_system; + +// System imports +pub use ai::*; +pub use hunger_system::HungerSystem; +pub use inventory_system::*; +pub use lighting_system::LightingSystem; +pub use map_indexing_system::MapIndexingSystem; +pub use melee_combat_system::MeleeCombatSystem; +pub use movement_system::MovementSystem; +pub use particle_system::ParticleSpawnSystem; +pub use ranged_combat_system::RangedCombatSystem; +pub use trigger_system::TriggerSystem; +pub use visibility_system::VisibilitySystem; + +pub fn build() -> Box { + dispatcher::new() +} diff --git a/src/ai.rs b/src/systems/ai.rs similarity index 100% rename from src/ai.rs rename to src/systems/ai.rs diff --git a/src/ai/adjacent_ai_system.rs b/src/systems/ai/adjacent_ai_system.rs similarity index 100% rename from src/ai/adjacent_ai_system.rs rename to src/systems/ai/adjacent_ai_system.rs diff --git a/src/ai/approach_ai_system.rs b/src/systems/ai/approach_ai_system.rs similarity index 100% rename from src/ai/approach_ai_system.rs rename to src/systems/ai/approach_ai_system.rs diff --git a/src/ai/chase_ai_system.rs b/src/systems/ai/chase_ai_system.rs similarity index 100% rename from src/ai/chase_ai_system.rs rename to src/systems/ai/chase_ai_system.rs diff --git a/src/ai/default_move_system.rs b/src/systems/ai/default_move_system.rs similarity index 100% rename from src/ai/default_move_system.rs rename to src/systems/ai/default_move_system.rs diff --git a/src/ai/encumbrance_system.rs b/src/systems/ai/encumbrance_system.rs similarity index 100% rename from src/ai/encumbrance_system.rs rename to src/systems/ai/encumbrance_system.rs diff --git a/src/ai/flee_ai_system.rs b/src/systems/ai/flee_ai_system.rs similarity index 100% rename from src/ai/flee_ai_system.rs rename to src/systems/ai/flee_ai_system.rs diff --git a/src/ai/initiative_system.rs b/src/systems/ai/initiative_system.rs similarity index 100% rename from src/ai/initiative_system.rs rename to src/systems/ai/initiative_system.rs diff --git a/src/ai/quipping.rs b/src/systems/ai/quipping.rs similarity index 100% rename from src/ai/quipping.rs rename to src/systems/ai/quipping.rs diff --git a/src/ai/turn_status.rs b/src/systems/ai/turn_status.rs similarity index 100% rename from src/ai/turn_status.rs rename to src/systems/ai/turn_status.rs diff --git a/src/ai/visible_ai_system.rs b/src/systems/ai/visible_ai_system.rs similarity index 100% rename from src/ai/visible_ai_system.rs rename to src/systems/ai/visible_ai_system.rs diff --git a/src/systems/dispatcher.rs b/src/systems/dispatcher.rs new file mode 100644 index 0000000..1120a90 --- /dev/null +++ b/src/systems/dispatcher.rs @@ -0,0 +1,44 @@ +#[macro_use] +mod single_thread; + +use ::specs::prelude::World; +pub use single_thread::*; + +use super::*; + +pub trait UnifiedDispatcher { + fn run_now(&mut self, ecs: *mut World); +} + +construct_dispatcher!( + (MapIndexingSystem, "map_index", &[]), + (VisibilitySystem, "visibility", &[]), + (EncumbranceSystem, "encumbrance", &[]), + (InitiativeSystem, "initiative", &[]), + (TurnStatusSystem, "turnstatus", &[]), + (QuipSystem, "quips", &[]), + (AdjacentAI, "adjacent", &[]), + (VisibleAI, "visible", &[]), + (ApproachAI, "approach", &[]), + (FleeAI, "flee", &[]), + (ChaseAI, "chase", &[]), + (DefaultMoveAI, "default_move", &[]), + (MovementSystem, "movement", &[]), + (TriggerSystem, "triggers", &[]), + (MeleeCombatSystem, "melee", &[]), + (RangedCombatSystem, "ranged", &[]), + (ItemCollectionSystem, "pickup", &[]), + (ItemEquipOnUse, "equip", &[]), + (ItemUseSystem, "use", &[]), + (SpellUseSystem, "spells", &[]), + (ItemIdentificationSystem, "itemid", &[]), + (ItemDropSystem, "drop", &[]), + (ItemRemoveSystem, "remove", &[]), + (HungerSystem, "hunger", &[]), + (ParticleSpawnSystem, "particle_spawn", &[]), + (LightingSystem, "lighting", &[]) +); + +pub fn new() -> Box { + new_dispatch() +} diff --git a/src/systems/dispatcher/single_thread.rs b/src/systems/dispatcher/single_thread.rs new file mode 100644 index 0000000..da4247d --- /dev/null +++ b/src/systems/dispatcher/single_thread.rs @@ -0,0 +1,43 @@ +use ::specs::prelude::*; + +use super::super::*; +use super::UnifiedDispatcher; + +macro_rules! construct_dispatcher { + ( + $( + ( + $type:ident, + $name:expr, + $deps:expr + ) + ),* + ) => { + fn new_dispatch() -> Box { + let mut dispatch = SingleThreadedDispatcher { + systems: Vec::new() + }; + + $( + dispatch.systems.push(Box::new( $type {} )); + )* + + return Box::new(dispatch); + } + } +} + +pub struct SingleThreadedDispatcher<'a> { + pub systems: Vec>>, +} + +impl<'a> UnifiedDispatcher for SingleThreadedDispatcher<'a> { + fn run_now(&mut self, ecs: *mut World) { + unsafe { + for sys in self.systems.iter_mut() { + sys.run_now(&*ecs); + } + crate::effects::run_effects_queue(&mut *ecs); + } + } +} diff --git a/src/hunger_system.rs b/src/systems/hunger_system.rs similarity index 100% rename from src/hunger_system.rs rename to src/systems/hunger_system.rs diff --git a/src/inventory_system.rs b/src/systems/inventory_system.rs similarity index 100% rename from src/inventory_system.rs rename to src/systems/inventory_system.rs diff --git a/src/inventory_system/collection_system.rs b/src/systems/inventory_system/collection_system.rs similarity index 100% rename from src/inventory_system/collection_system.rs rename to src/systems/inventory_system/collection_system.rs diff --git a/src/inventory_system/drop_system.rs b/src/systems/inventory_system/drop_system.rs similarity index 100% rename from src/inventory_system/drop_system.rs rename to src/systems/inventory_system/drop_system.rs diff --git a/src/inventory_system/equip_use.rs b/src/systems/inventory_system/equip_use.rs similarity index 100% rename from src/inventory_system/equip_use.rs rename to src/systems/inventory_system/equip_use.rs diff --git a/src/inventory_system/identification_system.rs b/src/systems/inventory_system/identification_system.rs similarity index 100% rename from src/inventory_system/identification_system.rs rename to src/systems/inventory_system/identification_system.rs diff --git a/src/inventory_system/remove_system.rs b/src/systems/inventory_system/remove_system.rs similarity index 100% rename from src/inventory_system/remove_system.rs rename to src/systems/inventory_system/remove_system.rs diff --git a/src/inventory_system/use_system.rs b/src/systems/inventory_system/use_system.rs similarity index 100% rename from src/inventory_system/use_system.rs rename to src/systems/inventory_system/use_system.rs diff --git a/src/lighting_system.rs b/src/systems/lighting_system.rs similarity index 93% rename from src/lighting_system.rs rename to src/systems/lighting_system.rs index ed674da..610a2d1 100644 --- a/src/lighting_system.rs +++ b/src/systems/lighting_system.rs @@ -2,8 +2,8 @@ use ::rltk::Point; use ::specs::prelude::*; use rltk::DistanceAlg; -use super::{LightSource, Map, Position, Viewshed}; -use crate::colors; +use crate::components::{LightSource, Position, Viewshed}; +use crate::{colors, Map}; pub struct LightingSystem {} diff --git a/src/map_indexing_system.rs b/src/systems/map_indexing_system.rs similarity index 100% rename from src/map_indexing_system.rs rename to src/systems/map_indexing_system.rs diff --git a/src/melee_combat_system.rs b/src/systems/melee_combat_system.rs similarity index 100% rename from src/melee_combat_system.rs rename to src/systems/melee_combat_system.rs diff --git a/src/movement_system.rs b/src/systems/movement_system.rs similarity index 100% rename from src/movement_system.rs rename to src/systems/movement_system.rs diff --git a/src/particle_system.rs b/src/systems/particle_system.rs similarity index 100% rename from src/particle_system.rs rename to src/systems/particle_system.rs diff --git a/src/ranged_combat_system.rs b/src/systems/ranged_combat_system.rs similarity index 100% rename from src/ranged_combat_system.rs rename to src/systems/ranged_combat_system.rs diff --git a/src/trigger_system.rs b/src/systems/trigger_system.rs similarity index 100% rename from src/trigger_system.rs rename to src/systems/trigger_system.rs diff --git a/src/visibility_system.rs b/src/systems/visibility_system.rs similarity index 100% rename from src/visibility_system.rs rename to src/systems/visibility_system.rs