add 'Resume' state that game states go through when they wake up again

this is to provide an easy way for game states to react to the specific
scenario where they were previously paused and then are re-awakened and
may want to perform (re-)initialization like they may have done in a
previous 'Pending' state (which is only set when states are first
created)
This commit is contained in:
Gered 2022-05-22 10:50:12 -04:00
parent 2c2ba24e6b
commit db261d2356

View file

@ -18,6 +18,7 @@ pub enum TransitionDirection {
#[derive(Copy, Clone, Eq, PartialEq, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum State { pub enum State {
Pending, Pending,
Resume,
Active, Active,
Paused, Paused,
TransitionIn, TransitionIn,
@ -108,7 +109,7 @@ impl<ContextType> StateContainer<ContextType> {
pub fn transition_in(&mut self, context: &mut ContextType) -> Result<(), StateError> { pub fn transition_in(&mut self, context: &mut ContextType) -> Result<(), StateError> {
match self.current_state { match self.current_state {
State::Pending | State::Paused => { State::Pending | State::Paused | State::Resume => {
self.change_state(State::TransitionIn, context); self.change_state(State::TransitionIn, context);
Ok(()) Ok(())
}, },
@ -312,6 +313,7 @@ impl<ContextType> States<ContextType> {
// otherwise, we're probably waking up a state that was paused and needs to // otherwise, we're probably waking up a state that was paused and needs to
// be resumed since it's once again on top // be resumed since it's once again on top
let state = self.states.front_mut().unwrap(); let state = self.states.front_mut().unwrap();
state.change_state(State::Resume, context);
state.transition_in(context)?; state.transition_in(context)?;
} }
}, },
@ -754,7 +756,8 @@ mod tests {
context.take_log(), context.take_log(),
vec![ vec![
StateChange(SECOND, Dead, TransitionOut(TransitionTo::Dead)), StateChange(SECOND, Dead, TransitionOut(TransitionTo::Dead)),
StateChange(FIRST, TransitionIn, Paused), StateChange(FIRST, Resume, Paused),
StateChange(FIRST, TransitionIn, Resume),
Transition(FIRST, TransitionIn), Transition(FIRST, TransitionIn),
Update(FIRST, TransitionIn), Update(FIRST, TransitionIn),
Render(FIRST, TransitionIn) Render(FIRST, TransitionIn)
@ -945,7 +948,8 @@ mod tests {
context.take_log(), context.take_log(),
vec![ vec![
StateChange(SECOND, Dead, TransitionOut(TransitionTo::Dead)), StateChange(SECOND, Dead, TransitionOut(TransitionTo::Dead)),
StateChange(FIRST, TransitionIn, Paused), StateChange(FIRST, Resume, Paused),
StateChange(FIRST, TransitionIn, Resume),
Transition(FIRST, TransitionIn), Transition(FIRST, TransitionIn),
Update(FIRST, TransitionIn), Update(FIRST, TransitionIn),
Render(FIRST, TransitionIn) Render(FIRST, TransitionIn)
@ -1387,7 +1391,8 @@ mod tests {
context.take_log(), context.take_log(),
vec![ vec![
StateChange(SECOND, Dead, TransitionOut(TransitionTo::Dead)), StateChange(SECOND, Dead, TransitionOut(TransitionTo::Dead)),
StateChange(FIRST, TransitionIn, Paused), StateChange(FIRST, Resume, Paused),
StateChange(FIRST, TransitionIn, Resume),
Transition(FIRST, TransitionIn), Transition(FIRST, TransitionIn),
Update(FIRST, TransitionIn), Update(FIRST, TransitionIn),
Render(FIRST, TransitionIn) Render(FIRST, TransitionIn)