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