Skip to content

Commit

Permalink
Merge pull request CleanCut#6 from darren-rose/feature/multiple-levels
Browse files Browse the repository at this point in the history
feat(levels): add levels
  • Loading branch information
CleanCut authored Jun 27, 2021
2 parents 597c839 + be57675 commit 02151c6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
67 changes: 67 additions & 0 deletions src/level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::frame::{Drawable, Frame};

const MAX_LEVEL: u8 = 3;

pub struct Level {
level: u8,
}

impl Level {
pub fn new() -> Self {
Self { level: 1 }
}

pub fn increment_level(&mut self) -> bool {
if self.level <= MAX_LEVEL {
self.level += 1;
}
self.level == MAX_LEVEL
}
}

impl Default for Level {
fn default() -> Self {
Self::new()
}
}

impl Drawable for Level {
fn draw(&self, frame: &mut Frame) {
// format our level string
let formatted = format!("LEVEL: {:0>2}", self.level);

// iterate over all characters
for (i, c) in formatted.chars().enumerate() {
// put them in the first row
frame[i + 20][0] = c;
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn should_increment_level_and_return_false() {
// given
let mut level = Level::new();
// when
let actual = level.increment_level();
// then
assert_eq!(false, actual);
assert_eq!(2, level.level);
}

#[test]
fn should_increment_level_two_times_and_return_true() {
// given
let mut level = Level::new();
// when
level.increment_level();
let actual = level.increment_level();
// then
assert_eq!(true, actual);
assert_eq!(MAX_LEVEL, level.level);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod frame;
pub mod invaders;
pub mod level;
pub mod player;
pub mod render;
pub mod score;
Expand Down
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crossterm::terminal::{EnterAlternateScreen, LeaveAlternateScreen};
use crossterm::{event, terminal, ExecutableCommand};
use invaders::frame::{new_frame, Drawable};
use invaders::invaders::Invaders;
use invaders::level::Level;
use invaders::player::Player;
use invaders::score::Score;
use invaders::{frame, render};
Expand Down Expand Up @@ -46,6 +47,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut instant = Instant::now();
let mut invaders = Invaders::new();
let mut score = Score::new();
let mut level = Level::new();

'gameloop: loop {
// Per-frame init
Expand Down Expand Up @@ -85,7 +87,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}

// Draw & render
let drawables: Vec<&dyn Drawable> = vec![&player, &invaders, &score];
let drawables: Vec<&dyn Drawable> = vec![&player, &invaders, &score, &level];
for drawable in drawables {
drawable.draw(&mut curr_frame);
}
Expand All @@ -94,8 +96,11 @@ fn main() -> Result<(), Box<dyn Error>> {

// Win or lose?
if invaders.all_killed() {
audio.play("win");
break 'gameloop;
if level.increment_level() {
audio.play("win");
break 'gameloop;
}
invaders = Invaders::new();
}
if invaders.reached_bottom() {
audio.play("lose");
Expand Down

0 comments on commit 02151c6

Please sign in to comment.