23 id: self.id.clone(), |
24 id: self.id.clone(), |
24 symmetrical: self.symmetrical, |
25 symmetrical: self.symmetrical, |
25 reverse: !self.symmetrical && !self.reverse, |
26 reverse: !self.symmetrical && !self.reverse, |
26 } |
27 } |
27 } |
28 } |
|
29 |
|
30 pub fn is_compatible(&self, other: &Self) -> bool { |
|
31 self.id == other.id && (self.reverse != other.reverse || self.symmetrical) |
|
32 } |
28 } |
33 } |
29 |
34 |
30 #[derive(Clone)] |
35 #[derive(Clone)] |
31 pub struct TileImage<T, I: PartialEq + Clone> { |
36 pub struct TileImage<T, I: PartialEq + Clone> { |
32 image: Rc<Vec2D<T>>, |
37 image: Rc<Vec2D<T>>, |
33 transform: RotationTransform, |
38 transform: Transform, |
34 top: Edge<I>, |
39 top: Edge<I>, |
35 right: Edge<I>, |
40 right: Edge<I>, |
36 bottom: Edge<I>, |
41 bottom: Edge<I>, |
37 left: Edge<I>, |
42 left: Edge<I>, |
38 } |
43 } |
89 } |
94 } |
90 |
95 |
91 pub fn rotated180(&self) -> Self { |
96 pub fn rotated180(&self) -> Self { |
92 Self { |
97 Self { |
93 image: self.image.clone(), |
98 image: self.image.clone(), |
94 transform: self.transform.rotate90(), |
99 transform: self.transform.rotate180(), |
95 top: self.bottom.clone(), |
100 top: self.bottom.clone(), |
96 right: self.left.clone(), |
101 right: self.left.clone(), |
97 bottom: self.top.clone(), |
102 bottom: self.top.clone(), |
98 left: self.right.clone(), |
103 left: self.right.clone(), |
99 } |
104 } |
100 } |
105 } |
101 |
106 |
102 pub fn rotated270(&self) -> Self { |
107 pub fn rotated270(&self) -> Self { |
103 Self { |
108 Self { |
104 image: self.image.clone(), |
109 image: self.image.clone(), |
105 transform: self.transform.rotate90(), |
110 transform: self.transform.rotate270(), |
106 top: self.left.clone(), |
111 top: self.left.clone(), |
107 right: self.top.clone(), |
112 right: self.top.clone(), |
108 bottom: self.right.clone(), |
113 bottom: self.right.clone(), |
109 left: self.bottom.clone(), |
114 left: self.bottom.clone(), |
110 } |
115 } |
122 &self.left |
127 &self.left |
123 } |
128 } |
124 |
129 |
125 pub fn top_edge(&self) -> &Edge<I> { |
130 pub fn top_edge(&self) -> &Edge<I> { |
126 &self.top |
131 &self.top |
|
132 } |
|
133 |
|
134 pub fn size(&self) -> Size { |
|
135 match self.transform { |
|
136 Transform::Rotate0(_) => self.image.size(), |
|
137 Transform::Rotate90(_) => Size::new(self.image.size().height, self.image.size().width), |
|
138 } |
|
139 } |
|
140 |
|
141 pub fn get(&self, row: usize, column: usize) -> Option<&T> { |
|
142 match self.transform { |
|
143 Transform::Rotate0(_) => { |
|
144 let image_row = if self.transform.is_flipped() { |
|
145 self.image.height().wrapping_sub(1).wrapping_sub(row) |
|
146 } else { |
|
147 row |
|
148 }; |
|
149 |
|
150 let image_column = if self.transform.is_mirrored() { |
|
151 self.image.width().wrapping_sub(1).wrapping_sub(column) |
|
152 } else { |
|
153 column |
|
154 }; |
|
155 |
|
156 self.image.get(image_row, image_column) |
|
157 }, |
|
158 Transform::Rotate90(_) => { |
|
159 let image_row = if self.transform.is_flipped() { |
|
160 column |
|
161 } else { |
|
162 self.image.height().wrapping_sub(1).wrapping_sub(column) |
|
163 }; |
|
164 |
|
165 let image_column = if self.transform.is_mirrored() { |
|
166 self.image.width().wrapping_sub(1).wrapping_sub(row) |
|
167 } else { |
|
168 row |
|
169 }; |
|
170 |
|
171 self.image.get(image_row, image_column) |
|
172 }, |
|
173 } |
127 } |
174 } |
128 } |
175 } |
129 |
176 |
130 #[cfg(test)] |
177 #[cfg(test)] |
131 mod tests { |
178 mod tests { |