1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::{ecs::Universe, id::ID};
use std::marker::PhantomData;

pub enum StateChange {
    None,
    Push(Box<dyn State>),
    Pop,
    Swap(Box<dyn State>),
    Quit,
}

pub trait State: Send + Sync {
    fn on_enter(&mut self, _universe: &mut Universe) {}

    fn on_exit(&mut self, _universe: &mut Universe) {}

    fn on_pause(&mut self, _universe: &mut Universe) {}

    fn on_resume(&mut self, _universe: &mut Universe) {}

    fn on_process(&mut self, _universe: &mut Universe) -> StateChange {
        StateChange::None
    }

    fn on_process_background(&mut self, _universe: &mut Universe) {}
}

impl State for () {}

impl State for bool {
    fn on_process(&mut self, _universe: &mut Universe) -> StateChange {
        if *self {
            StateChange::None
        } else {
            StateChange::Quit
        }
    }
}

impl State for usize {
    fn on_process(&mut self, _universe: &mut Universe) -> StateChange {
        if *self > 0 {
            *self -= 1;
            StateChange::None
        } else {
            StateChange::Quit
        }
    }
}

pub type StateToken = ID<PhantomData<dyn State + Send + Sync>>;