1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use crate::{optional_data_line, AnimationFrames, Image};
use crate::image::animation_frames_from_string;
#[derive(Clone, Debug, Eq)]
pub struct Tile {
pub id: String,
pub name: Option<String>,
pub wall: Option<bool>,
pub animation_frames: Vec<Image>,
pub colour_id: Option<u64>,
}
impl PartialEq for Tile {
fn eq(&self, other: &Self) -> bool {
self.wall == other.wall
&&
self.animation_frames == other.animation_frames
&&
self.colour_id == other.colour_id
}
}
impl Tile {
fn name_line(&self) -> String {
optional_data_line("NAME", self.name.as_ref())
}
fn wall_line(&self) -> String {
if self.wall.is_some() {
format!("\nWAL {}", self.wall.unwrap())
} else {
"".to_string()
}
}
fn colour_line(&self) -> String {
if self.colour_id.is_some() {
format!("\nCOL {}", self.colour_id.unwrap())
} else {
"".to_string()
}
}
pub fn invert(&mut self) {
self.animation_frames = self.animation_frames.iter().map(|frame: &Image| {
let mut image = frame.clone();
image.invert();
image
}).collect()
}
pub fn flip(&mut self) {
self.animation_frames = self.animation_frames.iter().map(|frame: &Image| {
let mut image = frame.clone();
image.flip();
image
}).collect()
}
pub fn mirror(&mut self) {
self.animation_frames = self.animation_frames.iter().map(|frame: &Image| {
let mut image = frame.clone();
image.mirror();
image
}).collect()
}
pub fn rotate(&mut self) {
self.animation_frames = self.animation_frames.iter().map(|frame: &Image| {
let mut image = frame.clone();
image.rotate();
image
}).collect()
}
}
impl From<String> for Tile {
fn from(string: String) -> Tile {
let mut lines: Vec<&str> = string.lines().collect();
let id = lines[0].replace("TIL ", "");
let mut wall = None;
let mut name = None;
let mut colour_id = None;
loop {
let last_line = lines.pop().unwrap();
if last_line.starts_with("WAL") {
wall = Some(last_line.ends_with("true"));
} else if last_line.starts_with("NAME") {
name = Some(last_line.replace("NAME ", "").to_string());
} else if last_line.starts_with("COL") {
colour_id = Some(last_line.replace("COL ", "").parse().unwrap());
} else {
lines.push(last_line);
break;
}
}
let animation_frames = animation_frames_from_string(
lines[1..].join("\n")
);
Tile {
id,
name,
wall,
animation_frames,
colour_id,
}
}
}
impl ToString for Tile {
fn to_string(&self) -> String {
format!(
"TIL {}\n{}{}{}{}",
self.id,
self.animation_frames.to_string(),
self.name_line(),
self.wall_line(),
self.colour_line(),
)
}
}
#[cfg(test)]
mod test {
use crate::{Image, Tile, mock};
#[test]
fn tile_from_string() {
let output = Tile::from(include_str!("test-resources/tile").to_string());
let expected = Tile {
id: "z".to_string(),
name: Some("concrete 1".to_string()),
wall: Some(true),
animation_frames: vec![Image {
pixels: vec![1; 64],
}],
colour_id: None,
};
assert_eq!(output, expected);
}
#[test]
fn tile_to_string() {
let output = Tile {
id: "7a".to_string(),
name: Some("chequers".to_string()),
wall: None,
animation_frames: vec![
mock::image::chequers_1(),
mock::image::chequers_2(),
],
colour_id: None,
}
.to_string();
let expected = include_str!("test-resources/tile-chequers").to_string();
assert_eq!(output, expected);
}
#[test]
fn 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);
tile_b.name = None;
assert_eq!(tile_a, tile_b);
}
}