This commit is contained in:
Michael Zhang 2020-06-08 11:33:47 -05:00
parent 17dfebda6c
commit c9dac158ee
2 changed files with 31 additions and 13 deletions

View file

@ -70,5 +70,6 @@ fn main() {
target.finish().unwrap(); target.finish().unwrap();
prev = now; prev = now;
std::thread::sleep(std::time::Duration::from_millis(17));
} }
} }

View file

@ -15,8 +15,9 @@ const LEVEL_1: &str = include_str!("../../levels/level1.json");
pub struct PlayScreen { pub struct PlayScreen {
animations: AnimationState, animations: AnimationState,
levels: Vec<Level>, levels: Vec<&'static str>,
current_level: usize, current_level: Level,
current_level_idx: usize,
} }
impl Screen for PlayScreen { impl Screen for PlayScreen {
@ -61,6 +62,11 @@ impl Screen for PlayScreen {
btn_handler!(VirtualKeyCode::J, Board::Right, PushDir::Left); btn_handler!(VirtualKeyCode::J, Board::Right, PushDir::Left);
btn_handler!(VirtualKeyCode::K, Board::Right, PushDir::Down); btn_handler!(VirtualKeyCode::K, Board::Right, PushDir::Down);
btn_handler!(VirtualKeyCode::L, Board::Right, PushDir::Right); btn_handler!(VirtualKeyCode::L, Board::Right, PushDir::Right);
if keymap.is_pressed(VirtualKeyCode::R) {
// restart the level
self.restart_level();
}
} }
ScreenAction::None ScreenAction::None
@ -74,34 +80,45 @@ impl Screen for PlayScreen {
impl PlayScreen { impl PlayScreen {
pub fn get_current_level(&self) -> &Level { pub fn get_current_level(&self) -> &Level {
self.levels.get(self.current_level).unwrap() &self.current_level
} }
pub fn get_current_level_mut(&mut self) -> &mut Level { pub fn get_current_level_mut(&mut self) -> &mut Level {
self.levels.get_mut(self.current_level).unwrap() &mut self.current_level
} }
pub fn new() -> PlayScreen { pub fn new() -> PlayScreen {
let levels = vec![ let levels = vec![
Level::from_json(&LEVEL_TUTORIAL), LEVEL_TUTORIAL,
Level::from_json(&LEVEL_TUTORIAL2), LEVEL_TUTORIAL2,
Level::from_json(&LEVEL_1), LEVEL_1,
]; ];
PlayScreen { PlayScreen {
levels, levels,
current_level: 0, current_level: Level::from_json(&LEVEL_TUTORIAL),
current_level_idx: 0,
animations: AnimationState::new(), animations: AnimationState::new(),
} }
} }
fn go_to_next_level(&mut self) { fn restart_level(&mut self) {
self.current_level += 1; self.switch_to_level(self.current_level_idx);
}
fn switch_to_level(&mut self, idx: usize) {
self.current_level = Level::from_json(self.levels[idx]);
self.current_level_idx = idx;
}
fn go_to_next_level(&mut self) {
// TODO: make an actual win screen // TODO: make an actual win screen
if self.current_level >= self.levels.len() { let next_level = if self.current_level_idx + 1 >= self.levels.len() {
self.current_level = 0; 0
} } else {
self.current_level_idx + 1
};
self.switch_to_level(next_level);
} }
fn check_win_condition(&mut self) { fn check_win_condition(&mut self) {