2019-01-02 21:25:01 +00:00
|
|
|
mod menu;
|
2019-01-07 21:10:31 +00:00
|
|
|
mod render;
|
2019-01-02 21:25:01 +00:00
|
|
|
mod window;
|
|
|
|
|
|
|
|
// Standard
|
|
|
|
use std::mem;
|
|
|
|
|
2019-01-07 21:10:31 +00:00
|
|
|
// Library
|
|
|
|
use winit;
|
|
|
|
use failure;
|
|
|
|
|
2019-01-02 21:25:01 +00:00
|
|
|
// Crate
|
|
|
|
use crate::{
|
|
|
|
menu::title::TitleState,
|
|
|
|
window::Window,
|
|
|
|
};
|
|
|
|
|
2019-01-07 21:10:31 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum VoxygenErr {
|
|
|
|
WinitCreationErr(winit::CreationError),
|
|
|
|
Other(failure::Error),
|
|
|
|
}
|
|
|
|
|
2019-01-02 21:25:01 +00:00
|
|
|
// A type used to store state that is shared between all play states
|
|
|
|
pub struct GlobalState {
|
|
|
|
window: Window,
|
|
|
|
}
|
|
|
|
|
|
|
|
// States can either close (and revert to a previous state), push a new state on top of themselves,
|
|
|
|
// or switch to a totally different state
|
2019-01-07 21:10:31 +00:00
|
|
|
pub enum PlayStateResult {
|
|
|
|
/// Pop all play states in reverse order and shut down the program
|
|
|
|
Shutdown,
|
|
|
|
/// Close the current play state
|
2019-01-02 21:25:01 +00:00
|
|
|
Close,
|
2019-01-07 21:10:31 +00:00
|
|
|
/// Push a new play state onto the play state stack
|
2019-01-02 21:25:01 +00:00
|
|
|
Push(Box<dyn PlayState>),
|
2019-01-07 21:10:31 +00:00
|
|
|
/// Switch the current play state with a new play state
|
2019-01-02 21:25:01 +00:00
|
|
|
Switch(Box<dyn PlayState>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait PlayState {
|
2019-01-07 21:10:31 +00:00
|
|
|
fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult;
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut states: Vec<Box<dyn PlayState>> = vec![Box::new(TitleState::new())];
|
|
|
|
|
|
|
|
let mut global_state = GlobalState {
|
2019-01-07 21:10:31 +00:00
|
|
|
window: Window::new()
|
|
|
|
.expect("Failed to create window"),
|
2019-01-02 21:25:01 +00:00
|
|
|
};
|
|
|
|
|
2019-01-02 22:08:13 +00:00
|
|
|
while let Some(state_result) = states.last_mut().map(|last| last.play(&mut global_state)){
|
2019-01-02 21:25:01 +00:00
|
|
|
// Implement state transfer logic
|
2019-01-02 22:08:13 +00:00
|
|
|
match state_result {
|
2019-01-07 21:10:31 +00:00
|
|
|
PlayStateResult::Shutdown => while states.last().is_some() {
|
|
|
|
states.pop();
|
|
|
|
},
|
|
|
|
PlayStateResult::Close => {
|
2019-01-02 22:08:13 +00:00
|
|
|
states.pop();
|
|
|
|
},
|
2019-01-07 21:10:31 +00:00
|
|
|
PlayStateResult::Push(new_state) => {
|
2019-01-02 22:08:13 +00:00
|
|
|
states.push(new_state);
|
|
|
|
},
|
2019-01-07 21:10:31 +00:00
|
|
|
PlayStateResult::Switch(mut new_state) => {
|
2019-01-02 22:08:13 +00:00
|
|
|
states.last_mut().map(|old_state| mem::swap(old_state, &mut new_state));
|
2019-01-02 21:25:01 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|