From 32addca85094e6ed2da9ab7807997c9fae6a17b6 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Wed, 1 Jul 2020 14:42:36 +0100 Subject: [PATCH] fix dedupe_tiles function and test --- src/game.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/src/game.rs b/src/game.rs index 8b981b8..85261fa 100644 --- a/src/game.rs +++ b/src/game.rs @@ -675,6 +675,17 @@ impl Game { new_unique_id(self.variable_ids()) } + /// todo refactor? + pub fn get_tile_id(&self, matching_tile: &Tile) -> Option { + for tile in &self.tiles { + if tile == matching_tile { + return Some(tile.id.clone()); + } + } + + None + } + /// adds a palette safely and returns the ID #[inline] pub fn add_palette(&mut self, mut palette: Palette) -> String { @@ -774,8 +785,10 @@ impl Game { if tile == crate::mock::tile_background() { tile_id_changes.insert(tile.id, "0".to_string()); } else if tiles_temp.contains(&tile) { - // todo get the ID of the first matching tile - //tile_id_changes.insert(tile.id, tiles_temp); + tile_id_changes.insert( + tile.id.clone(), + self.get_tile_id(&tile).unwrap() + ); } else { unique_tiles.push(tile); } @@ -785,6 +798,8 @@ impl Game { room.change_tile_ids(&tile_id_changes); } + unique_tiles.reverse(); + self.tiles = unique_tiles; } @@ -849,6 +864,8 @@ mod test { use crate::game::{Version, Game}; use crate::text::{TextDirection, Font}; use crate::tile::Tile; + use std::sync::TryLockError; + use crate::image::Image; #[test] fn test_game_from_string() { @@ -989,5 +1006,52 @@ mod test { game.add_tile(crate::mock::tile_background()); game.dedupe_tiles(); assert_eq!(game.tiles, vec![crate::mock::tile_default()]); + + let tile_a = Tile { + id: "0".to_string(), + name: Some("apple".to_string()), + wall: Some(true), + animation_frames: vec![Image { + pixels: vec![ + 0,1,1,0,1,1,0,1, + 0,1,1,0,1,1,0,1, + 1,0,1,0,1,0,0,1, + 1,0,1,0,1,0,0,1, + 0,0,0,0,1,1,1,1, + 0,0,0,0,1,1,1,1, + 1,1,0,1,1,0,1,1, + 1,1,0,1,1,0,1,1, + ] + }], + colour_id: Some(1) + }; + + let tile_b = Tile { + id: "1".to_string(), + name: Some("frogspawn".to_string()), + wall: Some(false), + animation_frames: vec![Image { + pixels: vec![ + 1,0,1,0,1,0,0,1, + 0,1,1,0,1,1,0,1, + 0,1,1,0,1,1,0,1, + 1,1,0,1,1,0,1,1, + 1,0,1,0,1,0,0,1, + 0,0,0,0,1,1,1,1, + 0,0,0,0,1,1,1,1, + 1,1,0,1,1,0,1,1, + ] + }], + colour_id: None + }; + + game.add_tile(tile_a.clone()); + game.add_tile(tile_b.clone()); + game.add_tile(tile_a.clone()); + game.add_tile(tile_b.clone()); + + game.dedupe_tiles(); + + assert_eq!(game.tiles, vec![crate::mock::tile_default(), tile_a, tile_b]); } }