some cleanup and naming changes to main loop, game context boilerplate

maybe sort of better?
This commit is contained in:
Gered 2023-02-19 13:15:00 -05:00
parent 24916ae4c7
commit b26eab6fb0

View file

@ -7,12 +7,10 @@ use crate::events::*;
use crate::states::*;
use crate::system::*;
pub trait PrimaryState {
pub trait CoreState {
fn system(&self) -> &System;
fn system_mut(&mut self) -> &mut System;
}
pub trait PrimaryStateWithFrameTiming: PrimaryState {
fn delta(&self) -> f32;
fn set_delta(&mut self, delta: f32);
@ -25,33 +23,29 @@ pub trait PrimaryStateWithFrameTiming: PrimaryState {
}
}
pub trait PrimaryStateWithEvents<EventType>: PrimaryState {
pub trait CoreStateWithEvents<EventType>: CoreState {
fn event_publisher(&mut self) -> &mut EventPublisher<EventType>;
}
pub trait SupportSystems {}
pub trait SupportSystemsWithEvents<EventType, ContextType>: SupportSystems
where
ContextType: PrimaryStateWithEvents<EventType>,
pub trait SupportSystemsWithEvents<EventType>: SupportSystems
{
fn event_listeners(&mut self) -> &mut EventListeners<EventType, ContextType>;
type ContextType: CoreStateWithEvents<EventType>;
fn event_listeners(&mut self) -> &mut EventListeners<EventType, Self::ContextType>;
fn do_events(&mut self, context: &mut ContextType) {
fn do_events(&mut self, context: &mut Self::ContextType) {
self.event_listeners().take_queue_from(context.event_publisher());
self.event_listeners().dispatch_queue(context);
}
}
pub struct App<PrimaryType, SupportType> {
pub primary_state: PrimaryType,
pub support_systems: SupportType,
}
pub trait AppContext {
type CoreType: CoreState;
type SupportType: SupportSystems;
impl<PrimaryType, SupportType> App<PrimaryType, SupportType> {
pub fn new(primary_state: PrimaryType, support_systems: SupportType) -> Self {
App { primary_state, support_systems }
}
fn core(&mut self) -> &mut Self::CoreType;
fn support(&mut self) -> &mut Self::SupportType;
}
#[derive(Error, Debug)]
@ -63,35 +57,32 @@ pub enum MainLoopError {
SystemError(#[from] SystemError),
}
pub fn main_loop<PrimaryType, SupportType, State>(
primary_state: PrimaryType,
support_systems: SupportType,
pub fn main_loop<ContextType, State>(
mut app: ContextType,
initial_state: State,
) -> Result<(), MainLoopError>
where
PrimaryType: PrimaryStateWithFrameTiming,
SupportType: SupportSystems,
State: AppState<App<PrimaryType, SupportType>> + 'static,
ContextType: AppContext,
State: AppState<ContextType> + 'static,
{
let mut app = App::new(primary_state, support_systems);
let mut states = States::new();
states.push(initial_state)?;
let mut is_running = true;
let mut last_ticks = app.primary_state.system().ticks();
let mut last_ticks = app.core().system().ticks();
while is_running && !states.is_empty() {
app.primary_state.system_mut().do_events_with(|event| match event {
app.core().system_mut().do_events_with(|event| match event {
SystemEvent::Quit => {
is_running = false;
}
_ => {}
});
last_ticks = app.primary_state.update_frame_delta(last_ticks);
last_ticks = app.core().update_frame_delta(last_ticks);
states.update(&mut app)?;
states.render(&mut app);
app.primary_state.system_mut().display()?;
app.core().system_mut().display()?;
}
Ok(())