replace tile ID with string
This commit is contained in:
34
src/game.rs
34
src/game.rs
@@ -1,4 +1,4 @@
|
||||
use crate::{Dialogue, Ending, Font, Item, Palette, Room, Sprite, TextDirection, Tile, ToBase36, Variable, transform_line_endings, segments_from_string, is_string_numeric};
|
||||
use crate::{Dialogue, Ending, Font, Item, Palette, Room, Sprite, TextDirection, Tile, ToBase36, Variable, transform_line_endings, segments_from_string, is_string_numeric, to_base36, from_base36};
|
||||
use std::error::Error;
|
||||
use loe::TransformMode;
|
||||
use std::str::FromStr;
|
||||
@@ -316,8 +316,8 @@ impl ToString for Game {
|
||||
|
||||
impl Game {
|
||||
#[inline]
|
||||
pub fn tile_ids(&self) -> Vec<u64> {
|
||||
self.tiles.iter().map(|tile| tile.id).collect()
|
||||
pub fn tile_ids(&self) -> Vec<String> {
|
||||
self.tiles.iter().map(|tile| tile.id.clone()).collect()
|
||||
}
|
||||
|
||||
/// first available tile ID.
|
||||
@@ -325,28 +325,28 @@ impl Game {
|
||||
/// if current tile IDs are [0, 1, 2] the result will be `3`
|
||||
/// todo this needs to be a generic function that takes a vec of string IDs and returns a new base64 string ID
|
||||
#[inline]
|
||||
pub fn new_tile_id(&self) -> u64 {
|
||||
pub fn new_tile_id(&self) -> String {
|
||||
let mut new_id = 0;
|
||||
|
||||
let mut ids = self.tile_ids();
|
||||
ids.sort();
|
||||
|
||||
for id in ids {
|
||||
if new_id == id {
|
||||
if new_id == from_base36(id.as_ref()) {
|
||||
new_id += 1;
|
||||
} else {
|
||||
return new_id;
|
||||
return to_base36(new_id);
|
||||
}
|
||||
}
|
||||
|
||||
new_id + 1
|
||||
to_base36(new_id + 1)
|
||||
}
|
||||
|
||||
/// adds a tile safely and returns the new tile ID
|
||||
#[inline]
|
||||
pub fn add_tile(&mut self, mut tile: Tile) -> u64 {
|
||||
pub fn add_tile(&mut self, mut tile: Tile) -> String {
|
||||
let new_id = self.new_tile_id();
|
||||
tile.id = new_id;
|
||||
tile.id = new_id.clone();
|
||||
self.tiles.push(tile);
|
||||
new_id
|
||||
}
|
||||
@@ -430,13 +430,13 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_tile_ids() {
|
||||
assert_eq!(crate::mock::game_default().tile_ids(), vec![10]);
|
||||
assert_eq!(crate::mock::game_default().tile_ids(), vec!["a".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_tile_id() {
|
||||
// default tile has an id of 10 ("a"), so 0 is available
|
||||
assert_eq!(crate::mock::game_default().new_tile_id(), 0);
|
||||
assert_eq!(crate::mock::game_default().new_tile_id(), "0".to_string());
|
||||
|
||||
let mut game = crate::mock::game_default();
|
||||
let mut tiles: Vec<Tile> = Vec::new();
|
||||
@@ -444,32 +444,32 @@ mod test {
|
||||
for n in 0..9 {
|
||||
if n != 4 {
|
||||
let mut new_tile = crate::mock::tile_default();
|
||||
new_tile.id = n;
|
||||
new_tile.id = format!("{}", n).to_string();
|
||||
tiles.push(new_tile);
|
||||
}
|
||||
}
|
||||
|
||||
game.tiles = tiles;
|
||||
|
||||
assert_eq!(game.new_tile_id(), 4);
|
||||
assert_eq!(game.new_tile_id(), "4".to_string());
|
||||
|
||||
// fill in the space created above, and test that tile IDs get sorted
|
||||
|
||||
let mut new_tile = crate::mock::tile_default();
|
||||
new_tile.id = 4;
|
||||
new_tile.id = "4".to_string();
|
||||
game.tiles.push(new_tile);
|
||||
|
||||
assert_eq!(game.new_tile_id(), 10);
|
||||
assert_eq!(game.new_tile_id(), "a".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_tile() {
|
||||
let mut game = crate::mock::game_default();
|
||||
let new_id = game.add_tile(crate::mock::tile_default());
|
||||
assert_eq!(new_id, 0);
|
||||
assert_eq!(new_id, "0".to_string());
|
||||
assert_eq!(game.tiles.len(), 2);
|
||||
let new_id = game.add_tile(crate::mock::tile_default());
|
||||
assert_eq!(new_id, 1);
|
||||
assert_eq!(new_id, "1".to_string());
|
||||
assert_eq!(game.tiles.len(), 3);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user