only change item id on clash; test add_item

This commit is contained in:
Max Bradbury 2020-06-24 18:45:06 +01:00
parent a629757db0
commit e32027e0af
1 changed files with 13 additions and 3 deletions

View File

@ -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()]);
}
}