only change item id on clash; test add_item
This commit is contained in:
parent
a629757db0
commit
e32027e0af
14
src/game.rs
14
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 loe::TransformMode;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
@ -540,8 +540,10 @@ impl Game {
|
||||||
/// adds an item safely and returns the new item ID
|
/// adds an item safely and returns the new item ID
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn add_item(&mut self, mut item: Item) -> String {
|
pub fn add_item(&mut self, mut item: Item) -> String {
|
||||||
let new_id = self.new_item_id();
|
let new_id = try_id(self.item_ids(), item.id.clone());
|
||||||
|
if new_id != item.id {
|
||||||
item.id = new_id.clone();
|
item.id = new_id.clone();
|
||||||
|
}
|
||||||
self.items.push(item);
|
self.items.push(item);
|
||||||
new_id
|
new_id
|
||||||
}
|
}
|
||||||
|
@ -693,4 +695,12 @@ mod test {
|
||||||
vec![&crate::mock::tile_default()]
|
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()]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue