some cleanup and naming changes to main loop, game context boilerplate
maybe sort of better?
This commit is contained in:
parent
24916ae4c7
commit
b26eab6fb0
|
@ -7,12 +7,10 @@ use crate::events::*;
|
||||||
use crate::states::*;
|
use crate::states::*;
|
||||||
use crate::system::*;
|
use crate::system::*;
|
||||||
|
|
||||||
pub trait PrimaryState {
|
pub trait CoreState {
|
||||||
fn system(&self) -> &System;
|
fn system(&self) -> &System;
|
||||||
fn system_mut(&mut self) -> &mut System;
|
fn system_mut(&mut self) -> &mut System;
|
||||||
}
|
|
||||||
|
|
||||||
pub trait PrimaryStateWithFrameTiming: PrimaryState {
|
|
||||||
fn delta(&self) -> f32;
|
fn delta(&self) -> f32;
|
||||||
fn set_delta(&mut self, delta: 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>;
|
fn event_publisher(&mut self) -> &mut EventPublisher<EventType>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait SupportSystems {}
|
pub trait SupportSystems {}
|
||||||
|
|
||||||
pub trait SupportSystemsWithEvents<EventType, ContextType>: SupportSystems
|
pub trait SupportSystemsWithEvents<EventType>: SupportSystems
|
||||||
where
|
|
||||||
ContextType: PrimaryStateWithEvents<EventType>,
|
|
||||||
{
|
{
|
||||||
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().take_queue_from(context.event_publisher());
|
||||||
self.event_listeners().dispatch_queue(context);
|
self.event_listeners().dispatch_queue(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct App<PrimaryType, SupportType> {
|
pub trait AppContext {
|
||||||
pub primary_state: PrimaryType,
|
type CoreType: CoreState;
|
||||||
pub support_systems: SupportType,
|
type SupportType: SupportSystems;
|
||||||
}
|
|
||||||
|
|
||||||
impl<PrimaryType, SupportType> App<PrimaryType, SupportType> {
|
fn core(&mut self) -> &mut Self::CoreType;
|
||||||
pub fn new(primary_state: PrimaryType, support_systems: SupportType) -> Self {
|
fn support(&mut self) -> &mut Self::SupportType;
|
||||||
App { primary_state, support_systems }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
|
@ -63,35 +57,32 @@ pub enum MainLoopError {
|
||||||
SystemError(#[from] SystemError),
|
SystemError(#[from] SystemError),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main_loop<PrimaryType, SupportType, State>(
|
pub fn main_loop<ContextType, State>(
|
||||||
primary_state: PrimaryType,
|
mut app: ContextType,
|
||||||
support_systems: SupportType,
|
|
||||||
initial_state: State,
|
initial_state: State,
|
||||||
) -> Result<(), MainLoopError>
|
) -> Result<(), MainLoopError>
|
||||||
where
|
where
|
||||||
PrimaryType: PrimaryStateWithFrameTiming,
|
ContextType: AppContext,
|
||||||
SupportType: SupportSystems,
|
State: AppState<ContextType> + 'static,
|
||||||
State: AppState<App<PrimaryType, SupportType>> + 'static,
|
|
||||||
{
|
{
|
||||||
let mut app = App::new(primary_state, support_systems);
|
|
||||||
let mut states = States::new();
|
let mut states = States::new();
|
||||||
states.push(initial_state)?;
|
states.push(initial_state)?;
|
||||||
|
|
||||||
let mut is_running = true;
|
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() {
|
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 => {
|
SystemEvent::Quit => {
|
||||||
is_running = false;
|
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.update(&mut app)?;
|
||||||
states.render(&mut app);
|
states.render(&mut app);
|
||||||
app.primary_state.system_mut().display()?;
|
app.core().system_mut().display()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue