From 9fcf3a996d5286f63026eea604435dbd3aaf76aa Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 28 Jun 2020 20:28:43 +0100 Subject: [PATCH] refactoring/optimisation --- src/game.rs | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/game.rs b/src/game.rs index 1327d86..2253b84 100644 --- a/src/game.rs +++ b/src/game.rs @@ -448,21 +448,15 @@ impl Game { if room.palette_id.is_some() { let key = room.palette_id.clone().unwrap(); - if palette_id_changes.contains_key(&key) { - let new_id = palette_id_changes.get(&key).unwrap(); - room.palette_id = Some(new_id.clone()); + let change = palette_id_changes.get(&key); + if change.is_some() { + room.palette_id = Some(change.unwrap().clone()); } } - let mut tiles = Vec::new(); - for tile_id in &room.tiles { - tiles.push(if tile_id_changes.contains_key(tile_id) { - tile_id_changes.get(tile_id).unwrap() - } else { - tile_id - }.clone()); - } - room.tiles = tiles; + room.tiles = room.tiles.iter().map(|tile_id| + tile_id_changes.get(tile_id).unwrap_or(tile_id).clone() + ).collect(); room.items = room.items.iter().map(|instance| if item_id_changes.contains_key(&instance.id) { @@ -672,7 +666,9 @@ impl Game { #[inline] pub fn add_palette(&mut self, mut palette: Palette) -> String { let new_id = try_id(self.palette_ids(), palette.id.clone()); - palette.id = new_id.clone(); + if new_id != palette.id { + palette.id = new_id.clone(); + } self.palettes.push(palette); new_id } @@ -680,17 +676,24 @@ impl Game { /// adds a tile safely and returns the new tile ID #[inline] pub fn add_tile(&mut self, mut tile: Tile) -> String { - let new_id = self.new_tile_id(); - tile.id = new_id.clone(); + if self.tile_ids().contains(&tile.id) { + let new_id = self.new_tile_id(); + if new_id != tile.id { + tile.id = new_id; + } + } + let id = tile.id.clone(); self.tiles.push(tile); - new_id + id } /// adds a sprite safely and returns the new sprite ID #[inline] pub fn add_sprite(&mut self, mut sprite: Sprite) -> String { let new_id = try_id(self.sprite_ids(), sprite.id.clone()); - sprite.id = new_id.clone(); + if new_id != sprite.id { + sprite.id = new_id.clone(); + } self.sprites.push(sprite); new_id }