refactoring/optimisation

This commit is contained in:
Max Bradbury 2020-06-28 20:28:43 +01:00
parent 586573c86a
commit 9fcf3a996d
1 changed files with 20 additions and 17 deletions

View File

@ -448,21 +448,15 @@ impl Game {
if room.palette_id.is_some() { if room.palette_id.is_some() {
let key = room.palette_id.clone().unwrap(); let key = room.palette_id.clone().unwrap();
if palette_id_changes.contains_key(&key) { let change = palette_id_changes.get(&key);
let new_id = palette_id_changes.get(&key).unwrap(); if change.is_some() {
room.palette_id = Some(new_id.clone()); room.palette_id = Some(change.unwrap().clone());
} }
} }
let mut tiles = Vec::new(); room.tiles = room.tiles.iter().map(|tile_id|
for tile_id in &room.tiles { tile_id_changes.get(tile_id).unwrap_or(tile_id).clone()
tiles.push(if tile_id_changes.contains_key(tile_id) { ).collect();
tile_id_changes.get(tile_id).unwrap()
} else {
tile_id
}.clone());
}
room.tiles = tiles;
room.items = room.items.iter().map(|instance| room.items = room.items.iter().map(|instance|
if item_id_changes.contains_key(&instance.id) { if item_id_changes.contains_key(&instance.id) {
@ -672,7 +666,9 @@ impl Game {
#[inline] #[inline]
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.clone()); 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); self.palettes.push(palette);
new_id new_id
} }
@ -680,17 +676,24 @@ impl Game {
/// adds a tile safely and returns the new tile ID /// adds a tile safely and returns the new tile ID
#[inline] #[inline]
pub fn add_tile(&mut self, mut tile: Tile) -> String { pub fn add_tile(&mut self, mut tile: Tile) -> String {
let new_id = self.new_tile_id(); if self.tile_ids().contains(&tile.id) {
tile.id = new_id.clone(); 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); self.tiles.push(tile);
new_id id
} }
/// adds a sprite safely and returns the new sprite ID /// adds a sprite safely and returns the new sprite ID
#[inline] #[inline]
pub fn add_sprite(&mut self, mut sprite: Sprite) -> String { pub fn add_sprite(&mut self, mut sprite: Sprite) -> String {
let new_id = try_id(self.sprite_ids(), sprite.id.clone()); 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); self.sprites.push(sprite);
new_id new_id
} }