implement PartialEq so we can compare tiles without comparing IDs

This commit is contained in:
Max Bradbury 2020-06-30 00:32:59 +01:00
parent 9ece613a95
commit eef37898f7
2 changed files with 32 additions and 1 deletions

View File

@ -127,6 +127,17 @@ pub fn tile_default() -> Tile {
}
}
#[inline]
pub fn tile_background() -> Tile {
Tile {
id: "0".to_string(),
name: None,
wall: None,
animation_frames: vec![Image {pixels: vec![0; 64]}],
colour_id: None
}
}
#[inline]
pub fn sprite() -> Sprite {
Sprite {

View File

@ -1,7 +1,7 @@
use crate::{optional_data_line, AnimationFrames, Image};
use crate::image::animation_frames_from_string;
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq)]
pub struct Tile {
pub id: String,
pub name: Option<String>,
@ -10,6 +10,18 @@ pub struct Tile {
pub colour_id: Option<u64>,
}
impl PartialEq for Tile {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&&
self.wall == other.wall
&&
self.animation_frames == other.animation_frames
&&
self.colour_id == other.colour_id
}
}
impl Tile {
#[inline]
fn name_line(&self) -> String {
@ -130,4 +142,12 @@ mod test {
assert_eq!(output, expected);
}
#[test]
fn test_partial_eq() {
let tile_a = crate::mock::tile_default();
let mut tile_b = crate::mock::tile_default();
tile_b.id = "0".to_string();
assert_eq!(tile_a, tile_b);
}
}