Use new system dispatch setup, make 'mapgen' properties of 'State' struct into their own struct

This commit is contained in:
Timothy Warren 2022-02-03 09:56:06 -05:00
parent c835100272
commit 097b8afb8a

View File

@ -7,13 +7,8 @@ use crate::gui::{self, show_cheat_mode, CheatMenuResult, MainMenuSelection};
use crate::map::{self, *}; use crate::map::{self, *};
use crate::player::*; use crate::player::*;
use crate::raws::*; use crate::raws::*;
use crate::systems::{ use crate::systems::{self, particle_system};
ai, particle_system, HungerSystem, ItemCollectionSystem, ItemDropSystem, ItemEquipOnUse, use crate::{camera, colors, damage_system, gamelog, player, saveload_system, spawner};
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 /// Whether to show a visual representation of map generation
pub const SHOW_MAPGEN_VISUALIZER: bool = false; pub const SHOW_MAPGEN_VISUALIZER: bool = false;
@ -50,61 +45,38 @@ pub enum RunState {
ShowIdentify, ShowIdentify,
} }
struct MapGen {
next_state: Option<RunState>,
history: Vec<Map>,
index: usize,
timer: f32,
}
/// The main wrapper around the game's state /// The main wrapper around the game's state
pub struct State { pub struct State {
pub ecs: World, pub ecs: World,
mapgen_next_state: Option<RunState>, mapgen: MapGen,
mapgen_history: Vec<Map>, dispatcher: Box<dyn systems::UnifiedDispatcher + 'static>,
mapgen_index: usize,
mapgen_timer: f32,
} }
impl State { impl State {
pub(super) fn new() -> Self { pub(super) fn new() -> Self {
State { State {
ecs: World::new(), ecs: World::new(),
mapgen_next_state: Some(RunState::MainMenu { mapgen: MapGen {
menu_selection: MainMenuSelection::NewGame, next_state: Some(RunState::MainMenu {
}), menu_selection: MainMenuSelection::NewGame,
mapgen_index: 0, }),
mapgen_history: Vec::new(), index: 0,
mapgen_timer: 0.0, history: Vec::new(),
timer: 0.0,
},
dispatcher: systems::build(),
} }
} }
fn run_systems(&mut self) { fn run_systems(&mut self) {
MapIndexingSystem {}.run_now(&self.ecs); self.dispatcher.run_now(&mut self.ecs);
VisibilitySystem {}.run_now(&self.ecs);
ai::EncumbranceSystem {}.run_now(&self.ecs);
ai::InitiativeSystem {}.run_now(&self.ecs);
ai::TurnStatusSystem {}.run_now(&self.ecs);
ai::QuipSystem {}.run_now(&self.ecs);
ai::AdjacentAI {}.run_now(&self.ecs);
ai::VisibleAI {}.run_now(&self.ecs);
ai::ApproachAI {}.run_now(&self.ecs);
ai::FleeAI {}.run_now(&self.ecs);
ai::ChaseAI {}.run_now(&self.ecs);
ai::DefaultMoveAI {}.run_now(&self.ecs);
MovementSystem {}.run_now(&self.ecs);
TriggerSystem {}.run_now(&self.ecs);
MeleeCombatSystem {}.run_now(&self.ecs);
RangedCombatSystem {}.run_now(&self.ecs);
ItemCollectionSystem {}.run_now(&self.ecs);
ItemEquipOnUse {}.run_now(&self.ecs);
ItemUseSystem {}.run_now(&self.ecs);
SpellUseSystem {}.run_now(&self.ecs);
ItemIdentificationSystem {}.run_now(&self.ecs);
ItemDropSystem {}.run_now(&self.ecs);
ItemRemoveSystem {}.run_now(&self.ecs);
HungerSystem {}.run_now(&self.ecs);
effects::run_effects_queue(&mut self.ecs);
ParticleSpawnSystem {}.run_now(&self.ecs);
LightingSystem {}.run_now(&self.ecs);
self.ecs.maintain(); self.ecs.maintain();
} }
@ -146,12 +118,12 @@ impl State {
} }
pub(super) fn generate_world_map(&mut self, new_depth: i32, offset: i32) { pub(super) fn generate_world_map(&mut self, new_depth: i32, offset: i32) {
self.mapgen_index = 0; self.mapgen.index = 0;
self.mapgen_timer = 0.0; self.mapgen.timer = 0.0;
self.mapgen_history.clear(); self.mapgen.history.clear();
if let Some(history) = map::level_transition(&mut self.ecs, new_depth, offset) { if let Some(history) = map::level_transition(&mut self.ecs, new_depth, offset) {
self.mapgen_history = history; self.mapgen.history = history;
} else { } else {
map::thaw_level_entities(&mut self.ecs); map::thaw_level_entities(&mut self.ecs);
} }
@ -193,22 +165,22 @@ impl GameState for State {
match newrunstate { match newrunstate {
RunState::MapGeneration => { RunState::MapGeneration => {
if !SHOW_MAPGEN_VISUALIZER { if !SHOW_MAPGEN_VISUALIZER {
newrunstate = self.mapgen_next_state.unwrap(); newrunstate = self.mapgen.next_state.unwrap();
} }
ctx.set_active_console(1); ctx.set_active_console(1);
ctx.cls(); ctx.cls();
ctx.set_active_console(0); ctx.set_active_console(0);
ctx.cls(); ctx.cls();
if self.mapgen_index < self.mapgen_history.len() { if self.mapgen.index < self.mapgen.history.len() {
camera::render_debug_map(&self.mapgen_history[self.mapgen_index], ctx); camera::render_debug_map(&self.mapgen.history[self.mapgen.index], ctx);
} }
self.mapgen_timer += ctx.frame_time_ms; self.mapgen.timer += ctx.frame_time_ms;
if self.mapgen_timer > 300.0 { if self.mapgen.timer > 300.0 {
self.mapgen_timer = 0.0; self.mapgen.timer = 0.0;
self.mapgen_index += 1; self.mapgen.index += 1;
if self.mapgen_index >= self.mapgen_history.len() { if self.mapgen.index >= self.mapgen.history.len() {
newrunstate = self.mapgen_next_state.unwrap(); newrunstate = self.mapgen.next_state.unwrap();
} }
} }
} }
@ -396,7 +368,7 @@ impl GameState for State {
} }
RunState::PreviousLevel => { RunState::PreviousLevel => {
self.goto_level(-1); self.goto_level(-1);
self.mapgen_next_state = Some(RunState::PreRun); self.mapgen.next_state = Some(RunState::PreRun);
newrunstate = RunState::MapGeneration; newrunstate = RunState::MapGeneration;
} }
RunState::MagicMapReveal { row } => { RunState::MagicMapReveal { row } => {
@ -417,7 +389,7 @@ impl GameState for State {
CheatMenuResult::NoResponse => {} CheatMenuResult::NoResponse => {}
CheatMenuResult::TeleportToExit => { CheatMenuResult::TeleportToExit => {
self.goto_level(1); self.goto_level(1);
self.mapgen_next_state = Some(RunState::PreRun); self.mapgen.next_state = Some(RunState::PreRun);
newrunstate = RunState::MapGeneration newrunstate = RunState::MapGeneration
} }
@ -517,7 +489,7 @@ impl GameState for State {
let map_depth = self.ecs.fetch::<Map>().depth; let map_depth = self.ecs.fetch::<Map>().depth;
let destination_offset = 0 - (map_depth - 1); let destination_offset = 0 - (map_depth - 1);
self.goto_level(destination_offset); self.goto_level(destination_offset);
self.mapgen_next_state = Some(RunState::PreRun); self.mapgen.next_state = Some(RunState::PreRun);
newrunstate = RunState::MapGeneration; newrunstate = RunState::MapGeneration;
} }
@ -532,7 +504,7 @@ impl GameState for State {
let mut ppos = self.ecs.fetch_mut::<Point>(); let mut ppos = self.ecs.fetch_mut::<Point>();
ppos.x = x; ppos.x = x;
ppos.y = y; ppos.y = y;
self.mapgen_next_state = Some(RunState::PreRun); self.mapgen.next_state = Some(RunState::PreRun);
newrunstate = RunState::MapGeneration; newrunstate = RunState::MapGeneration;
} }