Refactor system registration to use TypeIds

This commit is contained in:
Emre Osmanoğlu 2025-09-10 13:21:35 +03:00
parent 0c0d5cdb2a
commit 75fd59a504
5 changed files with 68 additions and 73 deletions

View file

@ -1,12 +1,10 @@
use std::cell::RefCell;
use std::rc::Rc;
use winit::event::Event;
use raidillon_core::scene::{Scene, SceneManager};
use crate::system::{SystemContext, SystemManager};
use raidillon_assets::{ModelManager, ModelManagerRef};
use raidillon_core::context::PlatformContext;
use raidillon_core::DebugUIBuffer;
use raidillon_core::engine::{EngineTrait};
use raidillon_core::engine::EngineTrait;
pub struct Engine {
pub scene_manager: SceneManager,
@ -28,7 +26,7 @@ impl EngineTrait for Engine {
/// Initialize systems, load the world.
fn initialize(&mut self, platform_context: PlatformContext) {
// Engine Loading Stage 1: initialize systems
for (system_id, system) in self.system_manager.systems.iter_mut() {
for system in self.system_manager.systems.values_mut() {
system.initialize();
}
@ -39,7 +37,7 @@ impl EngineTrait for Engine {
};
// Engine Loading Stage 2: load world
for (system_id, system) in self.system_manager.systems.iter_mut() {
for system in self.system_manager.systems.values_mut() {
system.load_world(&mut ctx);
}
}
@ -53,7 +51,7 @@ impl EngineTrait for Engine {
debug_ui_buffer: self.debug_ui_buffer.clone(),
};
for (system_id, system) in self.system_manager.systems.iter_mut() {
for system in self.system_manager.systems.values_mut() {
system.update(&mut ctx);
}
}

View file

@ -1,9 +1,9 @@
use std::any::TypeId;
use std::cell::RefCell;
use std::rc::Rc;
use raidillon_core::scene::Scene;
use indexmap::IndexMap;
use winit::event::Event;
use raidillon_core::context::PlatformContext;
use raidillon_core::scene::Scene;
use raidillon_core::DebugUIBuffer;
pub struct SystemContext<'a> {
@ -17,26 +17,27 @@ pub trait System {
/// Initialize the system.
fn initialize(&mut self) {}
/// Spawn the first entities of the world.
fn load_world(&mut self, ctx: &mut SystemContext) {}
fn update(&mut self, ctx: &mut SystemContext) {}
fn load_world(&mut self, _ctx: &mut SystemContext) {}
fn update(&mut self, _ctx: &mut SystemContext) {}
}
pub type SystemID = &'static str;
pub struct SystemManager {
pub systems: IndexMap<SystemID, Box<dyn System>>,
pub systems: IndexMap<TypeId, Box<dyn System>>,
}
impl SystemManager {
pub fn new() -> Self {
let systems = IndexMap::default();
Self { systems }
Self {
systems: IndexMap::default(),
}
}
pub fn add_system(&mut self, id: SystemID, system: Box<dyn System>) {
self.systems.insert(id, system);
pub fn add<S: System + Default + 'static>(&mut self) {
self.systems
.insert(TypeId::of::<S>(), Box::new(S::default()));
}
pub fn remove_system(&mut self, id: SystemID) {
self.systems.shift_remove(&id);
pub fn remove<S: 'static>(&mut self) {
self.systems.shift_remove(&TypeId::of::<S>());
}
}