diff --git a/src/game.rs b/src/game.rs index 3a54f00..23a02fe 100644 --- a/src/game.rs +++ b/src/game.rs @@ -763,6 +763,31 @@ impl Game { new_id } + pub fn dedupe_tiles(&mut self) { + let mut tiles_temp = self.tiles.clone(); + let mut unique_tiles: Vec = Vec::new(); + let mut tile_id_changes: HashMap = HashMap::new(); + + while tiles_temp.len() > 0 { + let tile = tiles_temp.pop().unwrap(); + + 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); + } else { + unique_tiles.push(tile); + } + } + + for room in &mut self.rooms { + room.change_tile_ids(&tile_id_changes); + } + + self.tiles = unique_tiles; + } + #[inline] fn version_line(&self) -> String { if self.version.is_some() { @@ -955,7 +980,15 @@ mod test { game_b.add_sprite(sprite); game_a.merge(game_b); assert_eq!(game_a.get_sprite_by_id("2".to_string()).unwrap().room_id, Some(room_id)); + } - + #[test] + fn test_dedupe_tiles() { + let mut game = crate::mock::game_default(); + game.add_tile(crate::mock::tile_default()); + game.add_tile(crate::mock::tile_default()); + game.add_tile(crate::mock::tile_background()); + game.dedupe_tiles(); + assert_eq!(game.tiles, vec![crate::mock::tile_default()]); } }