Add per-event handling to systems

This commit is contained in:
Emre Osmanoğlu 2025-09-10 15:03:49 +03:00
parent 0c0d5cdb2a
commit ee2999a0d5
5 changed files with 95 additions and 78 deletions

View file

@ -1,16 +1,14 @@
use std::path::Path;
use glam::{Quat, Vec3};
use raidillon_engine::{Engine, system::System};
use raidillon_engine::system::SystemContext;
use raidillon_platform::{Platform, Camera};
use raidillon_assets::model_path;
use raidillon_core::engine::EngineTrait;
use raidillon_ecs::components::ModelHandle;
use raidillon_ecs::Transform;
use raidillon_core::scene::Scene;
use raidillon_ecs::Transform;
use raidillon_ecs::components::ModelHandle;
use raidillon_engine::system::SystemContext;
use raidillon_engine::{Engine, system::System};
#[cfg(feature = "glium")]
use raidillon_glium::GliumPlatform;
use raidillon_core::DebugUIBuffer;
use raidillon_platform::{Camera, Platform};
use winit::event::{Event, WindowEvent};
const TEST_GLTF: &str = "pink-monkey.gltf";
@ -27,18 +25,22 @@ impl System for UpdateAspectRatioSystem {
fn load_world(&mut self, ctx: &mut SystemContext) {}
fn update(&mut self, ctx: &mut SystemContext) {
// FIXME: Need an event handler rework for systems.
match &ctx.platform_context.current_event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::Resized(sz) => {
let _ = ctx.scene.world.query_mut::<&mut Camera>().into_iter().map(|mut cam| {
cam.1.aspect = sz.width as f32 / sz.height as f32;
});
}
_ => {}
},
_ => {}
fn update(&mut self, _ctx: &mut SystemContext) {}
fn handle_event(&mut self, ctx: &mut SystemContext) {
if let Event::WindowEvent {
event: WindowEvent::Resized(sz),
..
} = &ctx.platform_context.current_event
{
let aspect = sz.width as f32 / sz.height as f32;
ctx.scene
.world
.query_mut::<&mut Camera>()
.into_iter()
.for_each(|(_, mut cam)| {
cam.aspect = aspect;
});
}
}
}
@ -48,13 +50,13 @@ impl System for RenderingTestSystem {
fn initialize(&mut self) {}
fn load_world(&mut self, ctx: &mut SystemContext) {
ctx.scene.world.spawn((Camera {
eye: Vec3::new(0.0, 0.0, 2.0),
eye: Vec3::new(0.0, 0.0, 2.0),
center: Vec3::ZERO,
up: Vec3::Y,
fovy: 60_f32.to_radians(),
up: Vec3::Y,
fovy: 60_f32.to_radians(),
aspect: ctx.platform_context.frame_width / ctx.platform_context.frame_height,
znear: 0.1,
zfar: 100.0,
znear: 0.1,
zfar: 100.0,
},));
let mut am = ctx.platform_context.asset_manager.borrow_mut();
@ -63,9 +65,9 @@ impl System for RenderingTestSystem {
ctx.scene.world.spawn((
Transform {
translation: Vec3::new(0.0, 0.0, 0.0),
rotation: Quat::IDENTITY,
scale: Vec3::new(1.0, 1.0, 1.0),
translation: Vec3::new(0.0, 0.0, 0.0),
rotation: Quat::IDENTITY,
scale: Vec3::new(1.0, 1.0, 1.0),
},
ModelHandle(TEST_GLTF),
));
@ -75,7 +77,9 @@ impl System for RenderingTestSystem {
// if let Some(mut debug_ui) = ctx.platform_context.imgui_ui.as_ref().map(|ui| ui.borrow_mut()) {
// debug_ui.text("Hello World!");
// }
ctx.debug_ui_buffer.borrow_mut().text("Hello World!".to_owned());
ctx.debug_ui_buffer
.borrow_mut()
.text("Hello World!".to_owned());
}
}
@ -84,25 +88,22 @@ fn main() {
// Define systems
// engine.system_manager.add_system("spawn_chunks".to_string(), ChunkSystem);
// engine.system_manager.add_system("movement".to_string(), MovementSystem);
engine.system_manager.add_system(RENDERING_TEST_SYSTEM, Box::new(RenderingTestSystem));
engine.system_manager.add_system(UPDATE_ASPECT_RATIO_SYSTEM, Box::new(UpdateAspectRatioSystem));
engine
.system_manager
.add_system(RENDERING_TEST_SYSTEM, Box::new(RenderingTestSystem));
engine.system_manager.add_system(
UPDATE_ASPECT_RATIO_SYSTEM,
Box::new(UpdateAspectRatioSystem),
);
// Set up the scene
let main_scene = Scene::new(
MAIN_SCENE_ID.to_owned(),
None,
);
let main_scene = Scene::new(MAIN_SCENE_ID.to_owned(), None);
engine.scene_manager.add_scene(MAIN_SCENE_ID, main_scene);
engine.scene_manager.set_active_scene(MAIN_SCENE_ID);
#[cfg(feature = "glium")]
{
let mut platform = GliumPlatform::initialize(
engine,
"Raidillon".to_string(),
1920,
1080,
);
let mut platform = GliumPlatform::initialize(engine, "Raidillon".to_string(), 1920, 1080);
platform.run()
};