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".
This commit is contained in:
parent
6e42d94b44
commit
ef055a1bda
16 changed files with 287 additions and 93 deletions
|
|
@ -3,15 +3,22 @@ use std::rc::Rc;
|
|||
use raidillon_core::scene::{Scene, SceneManager};
|
||||
use crate::system::{SystemContext, SystemManager};
|
||||
use raidillon_platform::PlatformContext;
|
||||
use raidillon_core::DebugUIBuffer;
|
||||
use raidillon_core::{define_typemap, DebugUIBuffer};
|
||||
use raidillon_core::engine::EngineTrait;
|
||||
use crate::input::InputState;
|
||||
use crate::resources::EngineResources;
|
||||
|
||||
pub struct Engine {
|
||||
pub scene_manager: SceneManager,
|
||||
pub system_manager: SystemManager,
|
||||
debug_ui_buffer: Rc<RefCell<DebugUIBuffer>>,
|
||||
input_state: Rc<RefCell<InputState>>,
|
||||
pub resources: EngineResources,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
fn load_default_resources(&mut self) {
|
||||
let input = InputState::default();
|
||||
self.resources.insert(input);
|
||||
}
|
||||
}
|
||||
|
||||
impl EngineTrait for Engine {
|
||||
|
|
@ -19,12 +26,13 @@ impl EngineTrait for Engine {
|
|||
fn new() -> Self {
|
||||
let scene_manager = SceneManager::new();
|
||||
let system_manager = SystemManager::new();
|
||||
Self {
|
||||
let mut s = Self {
|
||||
scene_manager,
|
||||
system_manager,
|
||||
debug_ui_buffer: Rc::new(RefCell::new(DebugUIBuffer::new())),
|
||||
input_state: Default::default(),
|
||||
}
|
||||
resources: EngineResources::new(),
|
||||
};
|
||||
s.load_default_resources();
|
||||
s
|
||||
}
|
||||
|
||||
/// Initialize systems, load the world.
|
||||
|
|
@ -33,60 +41,35 @@ impl EngineTrait for Engine {
|
|||
for system in self.system_manager.systems.values_mut() {
|
||||
system.initialize();
|
||||
}
|
||||
|
||||
let mut ctx = SystemContext {
|
||||
scene: self.scene_manager.current_mut(),
|
||||
platform_context,
|
||||
debug_ui_buffer: self.debug_ui_buffer.clone(),
|
||||
input_state: self.input_state.clone(),
|
||||
};
|
||||
|
||||
self.resources.insert(platform_context);
|
||||
// Engine Loading Stage 2: load world
|
||||
for system in self.system_manager.systems.values_mut() {
|
||||
system.load_world(&mut ctx);
|
||||
system.load_world(&mut self.resources, &mut self.scene_manager.current_mut());
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the engine
|
||||
fn frame_update(&mut self, platform_context: PlatformContext) {
|
||||
self.debug_ui_buffer.borrow_mut().reset_buffer();
|
||||
let mut ctx = SystemContext {
|
||||
scene: self.scene_manager.current_mut(),
|
||||
platform_context,
|
||||
debug_ui_buffer: self.debug_ui_buffer.clone(),
|
||||
input_state: self.input_state.clone(),
|
||||
};
|
||||
self.scene_manager.current_mut().resources.get_mut::<DebugUIBuffer>().unwrap().reset_buffer();
|
||||
self.resources.insert(platform_context);
|
||||
|
||||
for system in self.system_manager.systems.values_mut() {
|
||||
system.frame_update(&mut ctx);
|
||||
system.frame_update(&mut self.resources, &mut self.scene_manager.current_mut());
|
||||
}
|
||||
}
|
||||
|
||||
fn fixed_update(&mut self, platform_context: PlatformContext) {
|
||||
let mut ctx = SystemContext {
|
||||
scene: self.scene_manager.current_mut(),
|
||||
platform_context,
|
||||
debug_ui_buffer: self.debug_ui_buffer.clone(),
|
||||
input_state: self.input_state.clone(),
|
||||
};
|
||||
|
||||
self.resources.insert(platform_context);
|
||||
for system in self.system_manager.systems.values_mut() {
|
||||
system.fixed_update(&mut ctx);
|
||||
system.fixed_update(&mut self.resources, &mut self.scene_manager.current_mut());
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_event(&mut self, platform_context: PlatformContext) {
|
||||
self.input_state.borrow_mut().handle_event(&platform_context.current_event);
|
||||
|
||||
let mut ctx = SystemContext {
|
||||
scene: self.scene_manager.current_mut(),
|
||||
platform_context,
|
||||
debug_ui_buffer: self.debug_ui_buffer.clone(),
|
||||
input_state: self.input_state.clone(),
|
||||
};
|
||||
|
||||
// self.input_state.borrow_mut().handle_event(&platform_context.current_event);
|
||||
self.resources.insert(platform_context);
|
||||
for system in self.system_manager.systems.values_mut() {
|
||||
system.handle_event(&mut ctx);
|
||||
system.handle_event(&mut self.resources, &mut self.scene_manager.current_mut());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -100,16 +83,20 @@ impl EngineTrait for Engine {
|
|||
self.scene_manager.current_mut()
|
||||
}
|
||||
|
||||
fn get_debug_ui_buffer(&self) -> Rc<RefCell<DebugUIBuffer>> {
|
||||
self.debug_ui_buffer.clone()
|
||||
fn current_scene(&self) -> &Scene {
|
||||
self.scene_manager.current()
|
||||
}
|
||||
|
||||
fn get_debug_ui_buffer(&self) -> &DebugUIBuffer {
|
||||
self.scene_manager.current().resources.get::<DebugUIBuffer>().unwrap()
|
||||
}
|
||||
|
||||
fn reset_debug_ui_buffer(&mut self) {
|
||||
self.debug_ui_buffer.borrow_mut().reset_buffer();
|
||||
self.scene_manager.current_mut().resources.get_mut::<DebugUIBuffer>().unwrap().reset_buffer();
|
||||
}
|
||||
|
||||
fn scene_and_debug_ui_buffer_mut(&mut self) -> (&mut Scene, Rc<RefCell<DebugUIBuffer>>) {
|
||||
let (sm, dub) = (&mut self.scene_manager, &mut self.debug_ui_buffer);
|
||||
(sm.current_mut(), dub.clone())
|
||||
}
|
||||
// fn scene_and_debug_ui_buffer_mut(&mut self) -> (&mut Scene, &DebugUIBuffer) {
|
||||
// let (sm, dub) = (&mut self.scene_manager, self.scene_manager.current().resources.get::<DebugUIBuffer>().unwrap());
|
||||
// (sm.current_mut(), dub)
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,5 +2,7 @@ pub mod engine;
|
|||
pub mod system;
|
||||
mod input;
|
||||
pub mod systems;
|
||||
mod resources;
|
||||
|
||||
pub use crate::engine::Engine;
|
||||
pub use crate::resources::EngineResources;
|
||||
|
|
|
|||
3
engine/src/resources.rs
Normal file
3
engine/src/resources.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
use raidillon_core::define_typemap;
|
||||
|
||||
define_typemap!(EngineResources,);
|
||||
|
|
@ -6,6 +6,7 @@ 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,
|
||||
|
|
@ -18,10 +19,10 @@ 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 handle_event(&mut self, _ctx: &mut SystemContext) {}
|
||||
fn fixed_update(&mut self, _ctx: &mut SystemContext) {}
|
||||
fn frame_update(&mut self, _ctx: &mut SystemContext) {}
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,10 @@ use winit::event::{ElementState, Event, MouseButton, WindowEvent};
|
|||
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||
use winit::window::CursorGrabMode;
|
||||
use raidillon_assets::model_path;
|
||||
use raidillon_platform::Camera;
|
||||
use raidillon_platform::{Camera, PlatformContext};
|
||||
use crate::input::InputState;
|
||||
use crate::resources::EngineResources;
|
||||
use raidillon_core::scene::Scene;
|
||||
|
||||
pub struct FPSDebugCameraSystem {
|
||||
mouse_delta: (f64, f64),
|
||||
|
|
@ -32,20 +35,22 @@ impl Default for FPSDebugCameraSystem {
|
|||
}
|
||||
|
||||
impl System for FPSDebugCameraSystem {
|
||||
fn load_world(&mut self, ctx: &mut SystemContext) {
|
||||
ctx.scene.world.spawn((Camera {
|
||||
fn load_world(&mut self, res: &mut EngineResources, scene: &mut Scene) {
|
||||
let pctx = res.get::<PlatformContext>().unwrap();
|
||||
scene.world.spawn((Camera {
|
||||
eye: Vec3::new(0.0, 0.0, 2.0),
|
||||
center: Vec3::ZERO,
|
||||
up: Vec3::Y,
|
||||
fovy: 60_f32.to_radians(),
|
||||
aspect: ctx.platform_context.frame_width / ctx.platform_context.frame_height,
|
||||
aspect: pctx.frame_width / pctx.frame_height,
|
||||
znear: 0.1,
|
||||
zfar: 100.0,
|
||||
},));
|
||||
}
|
||||
|
||||
fn handle_event(&mut self, ctx: &mut SystemContext) {
|
||||
let event2 = ctx.platform_context.current_event.clone();
|
||||
fn handle_event(&mut self, res: &mut EngineResources, scene: &mut Scene) {
|
||||
let pctx = res.get::<PlatformContext>().unwrap();
|
||||
let event2 = pctx.current_event.clone();
|
||||
match event2 {
|
||||
Event::DeviceEvent { device_id, event} => {
|
||||
match event {
|
||||
|
|
@ -60,7 +65,7 @@ impl System for FPSDebugCameraSystem {
|
|||
WindowEvent::MouseInput { state, button, .. } => {
|
||||
if button == MouseButton::Right {
|
||||
// blood and tear
|
||||
let window = ctx.platform_context.window.lock().unwrap();
|
||||
let window = pctx.window.lock().unwrap();
|
||||
match state {
|
||||
ElementState::Pressed => {
|
||||
if window
|
||||
|
|
@ -86,7 +91,10 @@ impl System for FPSDebugCameraSystem {
|
|||
}
|
||||
}
|
||||
|
||||
fn frame_update(&mut self, ctx: &mut SystemContext) {
|
||||
fn frame_update(&mut self, res: &mut EngineResources, scene: &mut Scene) {
|
||||
let pctx = res.get::<PlatformContext>().unwrap();
|
||||
let input = res.get::<InputState>().unwrap();
|
||||
|
||||
if self.mouse_enabled {
|
||||
self.yaw += self.mouse_delta.0 as f32 * self.sensitivity;
|
||||
self.pitch -= self.mouse_delta.1 as f32 * self.sensitivity;
|
||||
|
|
@ -95,22 +103,21 @@ impl System for FPSDebugCameraSystem {
|
|||
|
||||
let front = self.front();
|
||||
let right_vec = front.cross(Vec3::Y).normalize();
|
||||
let input = ctx.input_state.borrow_mut();
|
||||
|
||||
if input.key_held(KeyCode::KeyW) {
|
||||
self.position += front * ctx.platform_context.time_ctx.frame_dt * self.speed;
|
||||
self.position += front * pctx.time_ctx.frame_dt * self.speed;
|
||||
}
|
||||
if input.key_held(KeyCode::KeyS) {
|
||||
self.position -= front * ctx.platform_context.time_ctx.frame_dt * self.speed;
|
||||
self.position -= front * pctx.time_ctx.frame_dt * self.speed;
|
||||
}
|
||||
if input.key_held(KeyCode::KeyA) {
|
||||
self.position -= right_vec * ctx.platform_context.time_ctx.frame_dt * self.speed;
|
||||
self.position -= right_vec * pctx.time_ctx.frame_dt * self.speed;
|
||||
}
|
||||
if input.key_held(KeyCode::KeyD) {
|
||||
self.position += right_vec * ctx.platform_context.time_ctx.frame_dt * self.speed;
|
||||
self.position += right_vec * pctx.time_ctx.frame_dt * self.speed;
|
||||
}
|
||||
|
||||
ctx.scene.world.query_mut::<&mut Camera>().into_iter().for_each(|(_, camera)| {
|
||||
scene.world.query_mut::<&mut Camera>().into_iter().for_each(|(_, camera)| {
|
||||
camera.eye = self.position;
|
||||
camera.center = self.position + front;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue