From e84fa4cc7cf3de66e67004bebfe8c5af735b3b1e Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 13 Apr 2020 16:19:59 +0100 Subject: [PATCH] tile ID functions and tile mock --- src/game.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/mock.rs | 42 +++++++++++++++++--------------- 2 files changed, 93 insertions(+), 19 deletions(-) diff --git a/src/game.rs b/src/game.rs index 0ac5943..c37d014 100644 --- a/src/game.rs +++ b/src/game.rs @@ -163,6 +163,42 @@ impl ToString for Game { } } +pub trait TileIds { + fn tile_ids(&self) -> Vec; +} + +impl TileIds for Game { + fn tile_ids(&self) -> Vec { + 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 = 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); +} diff --git a/src/mock.rs b/src/mock.rs index 7914b4a..f6e3c8b 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -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![