fix dedupe_tiles function and test

This commit is contained in:
Max Bradbury 2020-07-01 14:42:36 +01:00
parent 3802ccfe93
commit 32addca850
1 changed files with 66 additions and 2 deletions

View File

@ -675,6 +675,17 @@ impl Game {
new_unique_id(self.variable_ids())
}
/// todo refactor?
pub fn get_tile_id(&self, matching_tile: &Tile) -> Option<String> {
for tile in &self.tiles {
if tile == matching_tile {
return Some(tile.id.clone());
}
}
None
}
/// adds a palette safely and returns the ID
#[inline]
pub fn add_palette(&mut self, mut palette: Palette) -> String {
@ -774,8 +785,10 @@ impl Game {
if tile == crate::mock::tile_background() {
tile_id_changes.insert(tile.id, "0".to_string());
} else if tiles_temp.contains(&tile) {
// todo get the ID of the first matching tile
//tile_id_changes.insert(tile.id, tiles_temp);
tile_id_changes.insert(
tile.id.clone(),
self.get_tile_id(&tile).unwrap()
);
} else {
unique_tiles.push(tile);
}
@ -785,6 +798,8 @@ impl Game {
room.change_tile_ids(&tile_id_changes);
}
unique_tiles.reverse();
self.tiles = unique_tiles;
}
@ -849,6 +864,8 @@ mod test {
use crate::game::{Version, Game};
use crate::text::{TextDirection, Font};
use crate::tile::Tile;
use std::sync::TryLockError;
use crate::image::Image;
#[test]
fn test_game_from_string() {
@ -989,5 +1006,52 @@ mod test {
game.add_tile(crate::mock::tile_background());
game.dedupe_tiles();
assert_eq!(game.tiles, vec![crate::mock::tile_default()]);
let tile_a = Tile {
id: "0".to_string(),
name: Some("apple".to_string()),
wall: Some(true),
animation_frames: vec![Image {
pixels: vec![
0,1,1,0,1,1,0,1,
0,1,1,0,1,1,0,1,
1,0,1,0,1,0,0,1,
1,0,1,0,1,0,0,1,
0,0,0,0,1,1,1,1,
0,0,0,0,1,1,1,1,
1,1,0,1,1,0,1,1,
1,1,0,1,1,0,1,1,
]
}],
colour_id: Some(1)
};
let tile_b = Tile {
id: "1".to_string(),
name: Some("frogspawn".to_string()),
wall: Some(false),
animation_frames: vec![Image {
pixels: vec![
1,0,1,0,1,0,0,1,
0,1,1,0,1,1,0,1,
0,1,1,0,1,1,0,1,
1,1,0,1,1,0,1,1,
1,0,1,0,1,0,0,1,
0,0,0,0,1,1,1,1,
0,0,0,0,1,1,1,1,
1,1,0,1,1,0,1,1,
]
}],
colour_id: None
};
game.add_tile(tile_a.clone());
game.add_tile(tile_b.clone());
game.add_tile(tile_a.clone());
game.add_tile(tile_b.clone());
game.dedupe_tiles();
assert_eq!(game.tiles, vec![crate::mock::tile_default(), tile_a, tile_b]);
}
}