use std::{sync::Arc, collections::VecDeque, ptr::NonNull}; use async_std::task::block_on; use lyra_ecs::{world::World, system::{System, IntoSystem}}; use tracing::{info, error, Level}; use tracing_appender::non_blocking; use tracing_subscriber::{ layer::SubscriberExt, filter, util::SubscriberInitExt, fmt, }; use winit::{window::{WindowBuilder, Window}, event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode, DeviceEvent}, event_loop::{EventLoop, ControlFlow}}; use crate::{render::{renderer::{Renderer, BasicRenderer}, window::WindowOptions}, input::InputEvent, plugin::Plugin, change_tracker::Ct, EventQueue, StagedExecutor, Stage}; #[derive(Clone, Copy, Hash, Debug)] pub enum GameStages { /// This stage runs before all other stages. First, /// This stage runs before `Update`. PreUpdate, /// This stage is where most game logic would be. Update, /// This stage is ran after `Update`. PostUpdate, /// This stage runs after all other stages. Last, } impl Stage for GameStages {} pub struct Controls<'a> { pub world: &'a mut World, } #[derive(Clone, Default)] pub struct WindowState { pub is_focused: bool, pub is_cursor_inside_window: bool, } impl WindowState { pub fn new() -> Self { Self::default() } } struct GameLoop { window: Arc, renderer: Box, world: World, staged_exec: StagedExecutor, } impl GameLoop { pub async fn new(window: Arc, world: World, staged_exec: StagedExecutor) -> GameLoop { Self { window: Arc::clone(&window), renderer: Box::new(BasicRenderer::create_with_window(window).await), world, staged_exec, } } pub async fn on_resize(&mut self, new_size: winit::dpi::PhysicalSize) { self.renderer.on_resize(new_size); } pub async fn on_init(&mut self) { // Create the EventQueue resource in the world self.world.add_resource(self.window.clone()); } pub fn run_sync(&mut self, event: Event<()>, control_flow: &mut ControlFlow) { block_on(self.run_event_loop(event, control_flow)) } async fn update(&mut self) { let world_ptr = NonNull::from(&self.world); if let Err(e) = self.staged_exec.execute(world_ptr, true) { error!("Error when executing staged systems: '{}'", e); } } async fn input_update(&mut self, event: &InputEvent) -> Option { match event { InputEvent::KeyboardInput { input: KeyboardInput { state: ElementState::Pressed, virtual_keycode: Some(VirtualKeyCode::Escape), .. }, .. } => { self.on_exit(); Some(ControlFlow::Exit) }, _ => { //debug!("Got unhandled input event: \"{:?}\"", event); None } } } fn on_exit(&mut self) { info!("On exit!"); } pub async fn run_event_loop(&mut self, event: Event<'_, ()>, control_flow: &mut ControlFlow) { *control_flow = ControlFlow::Poll; match event { Event::DeviceEvent { device_id, event: DeviceEvent::MouseMotion { delta } } => { //debug!("motion: {delta:?}"); // convert a MouseMotion event to an InputEvent // make sure that the mouse is inside the window and the mouse has focus before reporting mouse motion /* let trigger = matches!(self.world.get_resource::(), Some(window_state) if window_state.is_focused && window_state.is_cursor_inside_window); */ let trigger = matches!(self.world.try_get_resource::>(), Some(window) if window.focused && window.cursor_inside_window); if trigger { let mut event_queue = self.world.try_get_resource_mut::().unwrap(); let input_event = InputEvent::MouseMotion { device_id, delta, }; event_queue.trigger_event(input_event); } }, Event::WindowEvent { ref event, window_id, } if window_id == self.window.id() => { // If try_from failed, that means that the WindowEvent is not an // input related event. if let Ok(input_event) = InputEvent::try_from(event) { // Its possible to receive multiple input events before the update event for // the InputSystem is called, so we must use a queue for the events. { if let Some(mut event_queue) = self.world.try_get_resource_mut::() { event_queue.trigger_event(input_event.clone()); }; } if let Some(new_control) = self.input_update(&input_event).await { *control_flow = new_control; } } else { match event { WindowEvent::CloseRequested => { self.on_exit(); *control_flow = ControlFlow::Exit }, WindowEvent::Resized(physical_size) => { self.on_resize(*physical_size).await; }, WindowEvent::ScaleFactorChanged { new_inner_size, .. } => { self.on_resize(**new_inner_size).await; }, WindowEvent::Focused(is_focused) => { let mut state = self.world.get_resource_or_else(WindowState::new); state.is_focused = *is_focused; }, _ => {} } } }, Event::RedrawRequested(window_id) if window_id == self.window.id() => { // Update the world self.update().await; /* self.fps_counter.tick(); if let Some(fps) = self.fps_counter.get_change() { debug!("FPS: {}fps, {:.2}ms/frame", fps, self.fps_counter.get_tick_time()); } */ self.renderer.as_mut().prepare(&mut self.world); if let Some(mut event_queue) = self.world.try_get_resource_mut::() { event_queue.update_events(); } match self.renderer.as_mut().render() { Ok(_) => {} // Reconfigure the surface if lost Err(wgpu::SurfaceError::Lost) => self.on_resize(self.renderer.as_ref().surface_size()).await, // The system is out of memory, we should probably quit Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit, // All other errors (Outdated, Timeout) should be resolved by the next frame Err(e) => eprintln!("{:?}", e), } }, Event::MainEventsCleared => { self.window.request_redraw(); }, _ => {} } } } pub struct Game { world: Option, plugins: VecDeque>, system_exec: Option, startup_systems: VecDeque>, } impl Default for Game { fn default() -> Self { let mut staged = StagedExecutor::new(); staged.add_stage(GameStages::First); staged.add_stage_after(GameStages::First, GameStages::PreUpdate); staged.add_stage_after(GameStages::PreUpdate, GameStages::Update); staged.add_stage_after(GameStages::Update, GameStages::PostUpdate); staged.add_stage_after(GameStages::PostUpdate, GameStages::Last); Self { world: Some(World::new()), plugins: VecDeque::new(), system_exec: Some(staged), startup_systems: VecDeque::new(), } } } impl Game { pub async fn initialize() -> Game { Self::default() } /// Get the world of this game pub fn world(&mut self) -> &mut World { // world is always `Some`, so unwrapping is safe self.world.as_mut().unwrap() } /// Add a system to the ecs world pub fn with_system(&mut self, name: &str, system: S, depends: &[&str]) -> &mut Self where S: IntoSystem, >::System: 'static { let system_dispatcher = self.system_exec.as_mut().unwrap(); system_dispatcher.add_system_to_stage(GameStages::Update, name, system.into_system(), depends); self } /// Add a stage. /// /// This stage could run at any moment if nothing is dependent on it. pub fn add_stage(&mut self, stage: T) -> &mut Self { let system_dispatcher = self.system_exec.as_mut().unwrap(); system_dispatcher.add_stage(stage); self } /// Add a stage that executes after another one. /// /// Parameters: /// * `before` - The stage that will run before `after`. /// * `after` - The stage that will run after `before`. pub fn add_stage_after(&mut self, before: T, after: U) -> &mut Self { let system_dispatcher = self.system_exec.as_mut().unwrap(); system_dispatcher.add_stage_after(before, after); self } /// Add a system to an already existing stage. /// /// # Panics /// Panics if the stage was not already added to the executor pub fn add_system_to_stage(&mut self, stage: T, name: &str, system: S, depends: &[&str]) -> &mut Self where T: Stage, S: IntoSystem, >::System: 'static { let system_dispatcher = self.system_exec.as_mut().unwrap(); system_dispatcher.add_system_to_stage(stage, name, system.into_system(), depends); self } /// Add a startup system that will be ran right after plugins are setup. /// They will only be ran once pub fn with_startup_system(&mut self, system: S) -> &mut Self where S: System + 'static { self.startup_systems.push_back(Box::new(system)); self } /// Add a plugin to the game. These are executed as they are added. pub fn with_plugin

(&mut self, plugin: P) -> &mut Self where P: Plugin + 'static { let plugin = Box::new(plugin); plugin.as_ref().setup(self); self.plugins.push_back(plugin); self } /// Override the default (empty) world /// /// This isn't recommended, you should create a startup system and add it to `with_startup_system` pub fn with_world(&mut self, world: World) -> &mut Self { self.world = Some(world); self } /// Start the game pub async fn run(&mut self) { // init logging let (stdout_layer, _stdout_nb) = non_blocking(std::io::stdout()); tracing_subscriber::registry() .with(fmt::layer().with_writer(stdout_layer)) .with(filter::Targets::new() // done by prefix, so it includes all lyra subpackages .with_target("lyra", Level::DEBUG) .with_target("wgpu", Level::WARN) .with_default(Level::INFO)) .init(); let world = self.world.take().unwrap_or_default(); // run startup systems while let Some(mut startup) = self.startup_systems.pop_front() { let startup = startup.as_mut(); let world_ptr = NonNull::from(&world); startup.setup(world_ptr).expect("World returned an error!"); startup.execute(world_ptr).expect("World returned an error!"); } // start winit event loops let event_loop = EventLoop::new(); let window = Arc::new(WindowBuilder::new().build(&event_loop).unwrap()); let system_dispatcher = self.system_exec.take().unwrap(); let mut g_loop = GameLoop::new(Arc::clone(&window), world, system_dispatcher).await; g_loop.on_init().await; event_loop.run(move |event, _, control_flow| { g_loop.run_sync(event, control_flow); }); } }