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
|
|
@ -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