add dedupe_tiles() function and test

This commit is contained in:
Max Bradbury 2020-07-01 14:22:59 +01:00
parent 92ca2ea5c3
commit 71ce6e7334
1 changed files with 34 additions and 1 deletions

View File

@ -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<Tile> = Vec::new();
let mut tile_id_changes: HashMap<String, String> = 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()]);
}
}