Create systems module, and move the Specs systems there

This commit is contained in:
Timothy Warren 2022-02-03 09:45:29 -05:00
parent 9fa0b46a16
commit c835100272
34 changed files with 133 additions and 32 deletions

View File

@ -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 {

View File

@ -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),
);
}

View File

@ -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);

View File

@ -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;

32
src/systems.rs Normal file
View File

@ -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<dyn UnifiedDispatcher + 'static> {
dispatcher::new()
}

44
src/systems/dispatcher.rs Normal file
View File

@ -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<dyn UnifiedDispatcher + 'static> {
new_dispatch()
}

View File

@ -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<dyn UnifiedDispatcher + 'static> {
let mut dispatch = SingleThreadedDispatcher {
systems: Vec::new()
};
$(
dispatch.systems.push(Box::new( $type {} ));
)*
return Box::new(dispatch);
}
}
}
pub struct SingleThreadedDispatcher<'a> {
pub systems: Vec<Box<dyn RunNow<'a>>>,
}
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);
}
}
}

View File

@ -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 {}