tile ID functions and tile mock

This commit is contained in:
Max Bradbury 2020-04-13 16:19:59 +01:00
parent 45ee94aae9
commit e84fa4cc7c
2 changed files with 93 additions and 19 deletions

View File

@ -163,6 +163,42 @@ impl ToString for Game {
}
}
pub trait TileIds {
fn tile_ids(&self) -> Vec<u64>;
}
impl TileIds for Game {
fn tile_ids(&self) -> Vec<u64> {
self.tiles.iter().map(|tile| {tile.id}).collect()
}
}
/// first available tile ID.
/// e.g. if current tile IDs are [0, 2, 3] the result will be `1`
/// if current tile IDs are [0, 1, 2] the result will be `3`
pub trait NewTileId {
fn new_tile_id(&self) -> u64;
}
impl NewTileId for Game {
fn new_tile_id(&self) -> u64 {
let mut new_id = 0;
let mut ids = self.tile_ids();
ids.sort();
for id in ids {
if new_id == id {
new_id += 1;
} else {
return new_id;
}
}
new_id + 1
}
}
#[test]
fn test_game_from_string() {
let output = Game::from(
@ -180,3 +216,37 @@ fn test_game_to_string() {
let expected = include_str!["../test/resources/default.bitsy"].to_string();
assert_eq!(output, expected);
}
#[test]
fn test_tile_ids() {
assert_eq!(mock::game_default().tile_ids(), vec![10]);
}
#[test]
fn test_new_tile_id() {
// default tile has an id of 10 ("a"), so 0 is available
assert_eq!(mock::game_default().new_tile_id(), 0);
let mut game = mock::game_default();
let mut tiles : Vec<Tile> = Vec::new();
for n in 0..9 {
if n != 4 {
let mut new_tile = mock::tile_default();
new_tile.id = n;
tiles.push(new_tile);
}
}
game.tiles = tiles;
assert_eq!(game.new_tile_id(), 4);
// fill in the space created above, and test that tile IDs get sorted
let mut new_tile = mock::tile_default();
new_tile.id = 4;
game.tiles.push(new_tile);
assert_eq!(game.new_tile_id(), 10);
}

View File

@ -67,6 +67,28 @@ pub fn avatar() -> Avatar {
}
}
pub fn tile_default() -> Tile {
Tile {
id: 10,
name: None,
wall: false,
animation_frames: vec![
Image {
pixels: vec![
1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,1,1,0,0,1,
1,0,0,1,1,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,
]
}
]
}
}
pub fn sprite() -> Sprite {
Sprite {
id: 10,
@ -207,25 +229,7 @@ pub fn game_default() -> Game {
}
],
tiles: vec![
Tile {
id: 10,
name: None,
wall: false,
animation_frames: vec![
Image {
pixels: vec![
1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,1,1,0,0,1,
1,0,0,1,1,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,
]
}
]
}
self::tile_default(),
],
avatar: Avatar {
animation_frames: vec![