From 6242abb35b2b2b5c0e7357c47f5250dd1dc995bb Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Sat, 6 Jan 2024 16:49:36 -0500 Subject: [PATCH] game: Create more stages and generalize them --- lyra-game/src/delta_time.rs | 9 ++++++--- lyra-game/src/game.rs | 30 +++++++++++++++++++++++------- lyra-game/src/input/action.rs | 4 ++-- lyra-game/src/input/system.rs | 4 ++-- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/lyra-game/src/delta_time.rs b/lyra-game/src/delta_time.rs index 49bd71a..9dbbff7 100644 --- a/lyra-game/src/delta_time.rs +++ b/lyra-game/src/delta_time.rs @@ -1,7 +1,7 @@ use instant::Instant; use lyra_ecs::{Component, world::World}; -use crate::plugin::Plugin; +use crate::{plugin::Plugin, game::GameStages}; #[derive(Clone, Component)] pub struct DeltaTime(f32, Option); @@ -20,7 +20,10 @@ impl std::ops::DerefMut for DeltaTime { } } -fn delta_time_system(world: &mut World) -> anyhow::Result<()> { +/// A system that updates the [`DeltaTime``] resource. +/// +/// The resource is updated in the [`GameStages::First`] stage. +pub fn delta_time_system(world: &mut World) -> anyhow::Result<()> { let now = Instant::now(); let mut delta = world.get_resource_mut::(); delta.0 = delta.1.unwrap_or(now).elapsed().as_secs_f32(); @@ -34,6 +37,6 @@ pub struct DeltaTimePlugin; impl Plugin for DeltaTimePlugin { fn setup(&self, game: &mut crate::game::Game) { game.world().add_resource(DeltaTime(0.0, None)); - game.with_system("delta_time", delta_time_system, &[]); + game.add_system_to_stage(GameStages::First, "delta_time", delta_time_system, &[]); } } \ No newline at end of file diff --git a/lyra-game/src/game.rs b/lyra-game/src/game.rs index 5330ffb..4ce2053 100755 --- a/lyra-game/src/game.rs +++ b/lyra-game/src/game.rs @@ -17,8 +17,16 @@ use crate::{render::{renderer::{Renderer, BasicRenderer}, window::WindowOptions} #[derive(Clone, Copy, Hash, Debug)] pub enum GameStages { - Core, - User, + /// 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 {} @@ -217,8 +225,11 @@ pub struct Game { impl Default for Game { fn default() -> Self { let mut staged = StagedExecutor::new(); - staged.add_stage(GameStages::Core); - staged.add_stage_after(GameStages::Core, GameStages::User); + 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()), @@ -247,7 +258,7 @@ impl Game { >::System: 'static { let system_dispatcher = self.system_exec.as_mut().unwrap(); - system_dispatcher.add_system_to_stage(GameStages::User, name, system.into_system(), depends); + system_dispatcher.add_system_to_stage(GameStages::Update, name, system.into_system(), depends); self } @@ -278,8 +289,13 @@ impl Game { /// /// # Panics /// Panics if the stage was not already added to the executor - pub fn add_system_to_stage + 'static>(&mut self, stage: ST, - name: &str, system: I, depends: &[&str]) -> &mut Self { + 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); diff --git a/lyra-game/src/input/action.rs b/lyra-game/src/input/action.rs index c9e8f9f..dad7f2e 100644 --- a/lyra-game/src/input/action.rs +++ b/lyra-game/src/input/action.rs @@ -2,7 +2,7 @@ use std::{collections::HashMap, ops::Deref}; use lyra_ecs::world::World; -use crate::plugin::Plugin; +use crate::{plugin::Plugin, game::GameStages}; use super::{Button, KeyCode, InputButtons}; @@ -392,6 +392,6 @@ pub struct InputActionPlugin; impl Plugin for InputActionPlugin { fn setup(&self, game: &mut crate::game::Game) { - game.with_system("input_actions", actions_system, &[]); + game.add_system_to_stage(GameStages::PreUpdate, "input_actions", actions_system, &[]); } } \ No newline at end of file diff --git a/lyra-game/src/input/system.rs b/lyra-game/src/input/system.rs index bdda83a..1a5171e 100755 --- a/lyra-game/src/input/system.rs +++ b/lyra-game/src/input/system.rs @@ -4,7 +4,7 @@ use glam::Vec2; use lyra_ecs::{world::World, system::IntoSystem}; use winit::event::MouseScrollDelta; -use crate::{EventQueue, plugin::Plugin}; +use crate::{EventQueue, plugin::Plugin, game::GameStages}; use super::{events::*, InputButtons, InputEvent}; @@ -136,6 +136,6 @@ pub struct InputPlugin; impl Plugin for InputPlugin { fn setup(&self, game: &mut crate::game::Game) { - game.with_system("input", InputSystem, &[]); + game.add_system_to_stage(GameStages::PreUpdate, "input", InputSystem, &[]); } } \ No newline at end of file