new game::find_tile_with_animation function

This commit is contained in:
Max Bradbury 2020-07-26 12:07:00 +01:00
parent 8d5580f827
commit 50e4530984
1 changed files with 22 additions and 0 deletions

View File

@ -662,6 +662,10 @@ impl Game {
None None
} }
pub fn find_tile_with_animation(&self, animation: &Vec<Image>) -> Option<&Tile> {
self.tiles.iter().find(|&tile| &tile.animation_frames == animation)
}
/// adds a palette safely and returns the ID /// adds a palette safely and returns the ID
pub fn add_palette(&mut self, mut palette: Palette) -> String { pub fn add_palette(&mut self, mut palette: Palette) -> String {
let new_id = try_id(&self.palette_ids(), &palette.id); let new_id = try_id(&self.palette_ids(), &palette.id);
@ -1036,4 +1040,22 @@ mod test {
assert_eq!(game.tiles, vec![crate::mock::tile_default(), tile_a, tile_b]); assert_eq!(game.tiles, vec![crate::mock::tile_default(), tile_a, tile_b]);
} }
#[test]
fn find_tile_with_animation() {
let game = crate::mock::game_default();
let animation = 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,
]}];
let output = game.find_tile_with_animation(&animation);
let expected = Some(&game.tiles[0]);
assert_eq!(output, expected);
}
} }