forked from CleanCut/invaders
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request CleanCut#6 from darren-rose/feature/multiple-levels
feat(levels): add levels
- Loading branch information
Showing
3 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters