raidillon/engine/src/system.rs
reo ef055a1bda Replace contexts with resources
- Implements a new macro to generate code for a new structure: TypeMap
- TypeMaps are wrappers for HashMaps that use TypeIDs as keys.
- Refactor the entire codebase to use the new resource structures.
- This commit is the first step towards getting rid of "god context objects everywhere".
2025-10-15 22:33:04 +03:00

47 lines
1.4 KiB
Rust

use indexmap::IndexMap;
use raidillon_core::scene::Scene;
use raidillon_core::DebugUIBuffer;
use raidillon_platform::PlatformContext;
use std::any::TypeId;
use std::cell::RefCell;
use std::rc::Rc;
use crate::input::InputState;
use crate::resources::EngineResources;
pub struct SystemContext<'a> {
pub scene: &'a mut Scene,
pub platform_context: PlatformContext,
pub debug_ui_buffer: Rc<RefCell<DebugUIBuffer>>,
pub input_state: Rc<RefCell<InputState>>,
}
pub trait System {
/// Initialize the system.
fn initialize(&mut self) {}
/// Spawn the first entities of the world.
fn load_world(&mut self, res: &mut EngineResources, scene: &mut Scene) {}
fn handle_event(&mut self, res: &mut EngineResources, scene: &mut Scene) {}
fn fixed_update(&mut self, res: &mut EngineResources, scene: &mut Scene) {}
fn frame_update(&mut self, res: &mut EngineResources, scene: &mut Scene) {}
}
pub struct SystemManager {
pub systems: IndexMap<TypeId, Box<dyn System>>,
}
impl SystemManager {
pub fn new() -> Self {
Self {
systems: IndexMap::default(),
}
}
pub fn add<S: System + Default + 'static>(&mut self) {
self.systems
.insert(TypeId::of::<S>(), Box::new(S::default()));
}
pub fn remove<S: 'static>(&mut self) {
self.systems.shift_remove(&TypeId::of::<S>());
}
}