From e32027e0afd0b5aeb2b790fac982ec7666b42ea9 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Wed, 24 Jun 2020 18:45:06 +0100 Subject: [PATCH] only change item id on clash; test add_item --- src/game.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/game.rs b/src/game.rs index 786b93b..e1b80d1 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,4 +1,4 @@ -use crate::{Dialogue, Ending, Font, Item, Palette, Room, Sprite, TextDirection, Tile, Variable, transform_line_endings, segments_from_string, new_unique_id}; +use crate::{Dialogue, Ending, Font, Item, Palette, Room, Sprite, TextDirection, Tile, Variable, transform_line_endings, segments_from_string, new_unique_id, try_id}; use loe::TransformMode; use std::str::FromStr; @@ -540,8 +540,10 @@ impl Game { /// adds an item safely and returns the new item ID #[inline] pub fn add_item(&mut self, mut item: Item) -> String { - let new_id = self.new_item_id(); - item.id = new_id.clone(); + let new_id = try_id(self.item_ids(), item.id.clone()); + if new_id != item.id { + item.id = new_id.clone(); + } self.items.push(item); new_id } @@ -693,4 +695,12 @@ mod test { vec![&crate::mock::tile_default()] ) } + + #[test] + fn test_add_item() { + let mut game = crate::mock::game_default(); + game.add_item(crate::mock::item()); + game.add_item(crate::mock::item()); + assert_eq!(game.item_ids(), vec!["0".to_string(), "6".to_string(), "1".to_string()]); + } }