Skip to content

Instantly share code, notes, and snippets.

@kiljacken
Last active August 29, 2015 14:10
Show Gist options
  • Save kiljacken/491be511e1fbc2814f8d to your computer and use it in GitHub Desktop.
Save kiljacken/491be511e1fbc2814f8d to your computer and use it in GitHub Desktop.
main.rs
// History : Event[]
// Event : Time|Period, (Person|Object)[], Action[]
// Time : basic
// Period : Time, Time
//
use std::io;
enum LoopAction<'a> {
Continue,
Break,
Switch(Box<Scene + 'a>),
}
trait Scene: Clone {
fn enter(&self);
fn exit(&self);
fn process(&self, &str) -> LoopAction;
}
#[deriving(Clone)]
struct MainMenu;
impl Scene for MainMenu {
fn enter(&self) {
println!("Welcome to Adventury!");
println!("");
}
fn exit(&self) {
println!("Bye.");
}
fn process(&self, input: &str) -> LoopAction {
match input {
"reset" => LoopAction::Switch(box MainMenu),
"quit" => LoopAction::Break,
_ => LoopAction::Continue,
}
}
}
fn main() {
let mut scene = box MainMenu as Box<Scene>;
let mut new_scene: Option<Box<Scene>> = None;
scene.enter();
loop {
print!("> ");
let input = io::stdin().read_line().ok().expect("Failed to read input");
let action = scene.process(input.trim());
match action {
LoopAction::Continue => continue,
LoopAction::Break => break,
LoopAction::Switch(s) => {
new_scene = Some(s);
}
}
new_scene = match new_scene {
Some(s) => {
scene.exit();
scene = s;
scene.enter();
None
},
None => None
};
}
scene.exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment