diff options
author | HampusM <hampus@hampusmat.com> | 2024-12-09 14:09:49 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-12-09 14:09:49 +0100 |
commit | 936c801305dfb053dd3e67687f61b7201d9e1064 (patch) | |
tree | b52b404447e8a4415d7e34558a6036ae3eca5d35 | |
parent | dcc40c9205e5f4cf484523f97eb12a561d7b2b22 (diff) |
chore(engine): use ecs lib phases
-rw-r--r-- | engine/src/camera/fly.rs | 4 | ||||
-rw-r--r-- | engine/src/event.rs | 27 | ||||
-rw-r--r-- | engine/src/input.rs | 43 | ||||
-rw-r--r-- | engine/src/lib.rs | 43 | ||||
-rw-r--r-- | engine/src/renderer/opengl.rs | 6 | ||||
-rw-r--r-- | engine/src/window.rs | 14 |
6 files changed, 62 insertions, 75 deletions
diff --git a/engine/src/camera/fly.rs b/engine/src/camera/fly.rs index 99739e3..b71f875 100644 --- a/engine/src/camera/fly.rs +++ b/engine/src/camera/fly.rs @@ -1,11 +1,11 @@ use ecs::component::local::Local; +use ecs::phase::UPDATE as UPDATE_PHASE; use ecs::sole::Single; use ecs::system::{Into, System}; use ecs::{Component, Query}; use crate::camera::{Active as ActiveCamera, Camera}; use crate::delta_time::DeltaTime; -use crate::event::Update as UpdateEvent; use crate::input::{Cursor, CursorFlags, Key, KeyState, Keys}; use crate::transform::Position; use crate::util::builder; @@ -60,7 +60,7 @@ impl ecs::extension::Extension for Extension fn collect(self, mut collector: ecs::extension::Collector<'_>) { collector.add_system( - UpdateEvent, + *UPDATE_PHASE, update .into_system() .initialize((CursorState::default(), self.0)), diff --git a/engine/src/event.rs b/engine/src/event.rs deleted file mode 100644 index e5ae486..0000000 --- a/engine/src/event.rs +++ /dev/null @@ -1,27 +0,0 @@ -pub use ecs::event::start::Start; -use ecs::event::Event; - -#[derive(Debug)] -pub struct Update; - -impl Event for Update {} - -#[derive(Debug)] -pub struct PreUpdate; - -impl Event for PreUpdate {} - -#[derive(Debug)] -pub struct Present; - -impl Event for Present {} - -#[derive(Debug)] -pub struct PostPresent; - -impl Event for PostPresent {} - -#[derive(Debug)] -pub struct Conclude; - -impl Event for Conclude {} diff --git a/engine/src/input.rs b/engine/src/input.rs index e847702..95de048 100644 --- a/engine/src/input.rs +++ b/engine/src/input.rs @@ -1,16 +1,13 @@ use std::collections::HashMap; use ecs::extension::Collector as ExtensionCollector; +use ecs::phase::{Phase, PRE_UPDATE as PRE_UPDATE_PHASE, START as START_PHASE}; +use ecs::relationship::{ChildOf, Relationship}; use ecs::sole::Single; -use ecs::Sole; +use ecs::{static_entity, Sole}; -use crate::event::{ - PostPresent as PostPresentEvent, - PreUpdate as PreUpdateEvent, - Start as StartEvent, -}; use crate::vector::Vec2; -use crate::window::Window; +use crate::window::{Window, UPDATE_PHASE as WINDOW_UPDATE_PHASE}; mod reexports { @@ -19,10 +16,19 @@ mod reexports pub use reexports::*; +static_entity!( + SET_PREV_KEY_STATE_PHASE, + ( + Phase, + <Relationship<ChildOf, Phase>>::new(*WINDOW_UPDATE_PHASE) + ) +); + #[derive(Debug, Sole)] pub struct Keys { map: HashMap<Key, KeyData>, + pending: Vec<(Key, KeyState)>, } impl Keys @@ -43,6 +49,7 @@ impl Keys ) }) .collect(), + pending: Vec::with_capacity(Key::KEYS.len()), } } @@ -145,9 +152,9 @@ impl ecs::extension::Extension for Extension { fn collect(self, mut collector: ExtensionCollector<'_>) { - collector.add_system(StartEvent, initialize); - collector.add_system(PreUpdateEvent, maybe_clear_cursor_is_first_move); - collector.add_system(PostPresentEvent, set_keys_prev_tick_state); + collector.add_system(*START_PHASE, initialize); + collector.add_system(*PRE_UPDATE_PHASE, maybe_clear_cursor_is_first_move); + collector.add_system(*SET_PREV_KEY_STATE_PHASE, set_pending_key_states); collector.add_sole(Keys::default()).ok(); collector.add_sole(Cursor::default()).ok(); @@ -169,7 +176,7 @@ fn initialize( let mut keys = keys_ref.to_single(); - keys.set_key_state(key, key_state); + keys.pending.push((key, key_state)); }); let cursor_weak_ref = cursor.to_weak_ref(); @@ -220,11 +227,21 @@ fn maybe_clear_cursor_is_first_move( } } -fn set_keys_prev_tick_state(mut keys: Single<Keys>) +fn set_pending_key_states(mut keys: Single<Keys>) { - for key_data in keys.map.values_mut() { + let Keys { map, pending } = &mut *keys; + + for key_data in map.values_mut() { key_data.prev_tick_state = key_data.state; } + + for (key, key_state) in pending { + let Some(key_data) = map.get_mut(key) else { + unreachable!(); + }; + + key_data.state = *key_state; + } } #[derive(Debug)] diff --git a/engine/src/lib.rs b/engine/src/lib.rs index a2f8c34..5b9d8e1 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -1,24 +1,15 @@ #![deny(clippy::all, clippy::pedantic)] #![allow(clippy::needless_pass_by_value)] -use ecs::component::Sequence as ComponentSequence; -use ecs::event::component::TypeTransformComponentsToAddedEvents; -use ecs::event::{Event, Sequence as EventSequence}; +use ecs::component::{Component, Sequence as ComponentSequence}; use ecs::extension::Extension; +use ecs::phase::PRE_UPDATE as PRE_UPDATE_PHASE; use ecs::sole::Sole; use ecs::system::{Into, System}; -use ecs::tuple::Reduce as TupleReduce; use ecs::uid::Uid; use ecs::{SoleAlreadyExistsError, World}; use crate::delta_time::{update as update_delta_time, DeltaTime, LastUpdate}; -use crate::event::{ - Conclude as ConcludeEvent, - PostPresent as PostPresentEvent, - PreUpdate as PreUpdateEvent, - Present as PresentEvent, - Update as UpdateEvent, -}; mod opengl; mod util; @@ -27,7 +18,6 @@ pub mod camera; pub mod data_types; pub mod delta_time; pub mod draw_flags; -pub mod event; pub mod file_format; pub mod input; pub mod lighting; @@ -46,14 +36,6 @@ pub extern crate ecs; pub(crate) use crate::data_types::matrix; pub use crate::data_types::{color, vector}; -type EventOrder = ( - PreUpdateEvent, - UpdateEvent, - PresentEvent, - PostPresentEvent, - ConcludeEvent, -); - #[derive(Debug)] pub struct Engine { @@ -71,7 +53,7 @@ impl Engine world.add_sole(DeltaTime::default()).ok(); world.register_system( - PreUpdateEvent, + *PRE_UPDATE_PHASE, update_delta_time .into_system() .initialize((LastUpdate::default(),)), @@ -82,19 +64,28 @@ impl Engine pub fn spawn<Comps>(&mut self, components: Comps) -> Uid where - Comps: ComponentSequence + TupleReduce<TypeTransformComponentsToAddedEvents>, - Comps::Out: EventSequence, + Comps: ComponentSequence, { self.world.create_entity(components) } pub fn register_system<'this, SystemImpl>( &'this mut self, - event: impl Event, + phase_euid: Uid, system: impl System<'this, SystemImpl>, ) { - self.world.register_system(event, system); + self.world.register_system(phase_euid, system); + } + + pub fn register_observer_system<'this, SystemImpl, Event>( + &'this mut self, + system: impl System<'this, SystemImpl>, + event: Event, + ) where + Event: Component, + { + self.world.register_observer_system(system, event); } /// Adds a globally shared singleton value. @@ -114,7 +105,7 @@ impl Engine /// Runs the event loop. pub fn start(&self) { - self.world.event_loop::<EventOrder>(); + self.world.start_loop(); } } diff --git a/engine/src/renderer/opengl.rs b/engine/src/renderer/opengl.rs index c036cc0..236465d 100644 --- a/engine/src/renderer/opengl.rs +++ b/engine/src/renderer/opengl.rs @@ -9,6 +9,7 @@ use std::process::abort; use ecs::actions::Actions; use ecs::component::local::Local; +use ecs::phase::{PRESENT as PRESENT_PHASE, START as START_PHASE}; use ecs::query::options::{Not, With}; use ecs::sole::Single; use ecs::system::{Into as _, System}; @@ -18,7 +19,6 @@ use crate::camera::{Active as ActiveCamera, Camera}; use crate::color::Color; use crate::data_types::dimens::Dimens; use crate::draw_flags::{DrawFlags, NoDraw, PolygonModeConfig}; -use crate::event::{Present as PresentEvent, Start as StartEvent}; use crate::lighting::{DirectionalLight, GlobalLight, PointLight}; use crate::material::{Flags as MaterialFlags, Material}; use crate::matrix::Matrix; @@ -87,10 +87,10 @@ impl ecs::extension::Extension for Extension { fn collect(self, mut collector: ecs::extension::Collector<'_>) { - collector.add_system(StartEvent, initialize); + collector.add_system(*START_PHASE, initialize); collector.add_system( - PresentEvent, + *PRESENT_PHASE, render .into_system() .initialize((GlobalGlObjects::default(),)), diff --git a/engine/src/window.rs b/engine/src/window.rs index 008c09b..00c360e 100644 --- a/engine/src/window.rs +++ b/engine/src/window.rs @@ -4,16 +4,22 @@ use std::ffi::{CStr, CString}; use bitflags::bitflags; use ecs::actions::Actions; use ecs::extension::Collector as ExtensionCollector; +use ecs::phase::{Phase, PRESENT as PRESENT_PHASE, START as START_PHASE}; +use ecs::relationship::{ChildOf, Relationship}; use ecs::sole::Single; -use ecs::Sole; +use ecs::{static_entity, Sole}; use glfw::window::{Hint as WindowCreationHint, HintValue as WindowCreationHintValue}; use glfw::WindowSize; use util_macros::VariantArr; use crate::data_types::dimens::Dimens; -use crate::event::{Conclude as ConcludeEvent, Start as StartEvent}; use crate::vector::Vec2; +static_entity!( + pub UPDATE_PHASE, + (Phase, <Relationship<ChildOf, Phase>>::new(*PRESENT_PHASE)) +); + #[derive(Debug, Sole)] /// Has to be dropped last since it holds the OpenGL context. #[sole(drop_last)] @@ -685,8 +691,8 @@ impl ecs::extension::Extension for Extension { fn collect(self, mut collector: ExtensionCollector<'_>) { - collector.add_system(StartEvent, initialize); - collector.add_system(ConcludeEvent, update); + collector.add_system(*START_PHASE, initialize); + collector.add_system(*UPDATE_PHASE, update); let window = self .window_builder |