1use crate::{constants, neighbours::Neighbours};
2
3fn get_marching_tile_byte(neighbours: &Neighbours) -> u8 {
4 let mut sample = 0;
5 if neighbours.north_west() {
6 sample += 1;
7 }
8 if neighbours.north() {
9 sample += 2;
10 }
11 if neighbours.north_east() {
12 sample += 4;
13 }
14 if neighbours.west() {
15 sample += 8;
16 }
17 if neighbours.east() {
18 sample += 16;
19 }
20 if neighbours.south_west() {
21 sample += 32;
22 }
23 if neighbours.south() {
24 sample += 64;
25 }
26 if neighbours.south_east() {
27 sample += 128;
28 }
29 sample
30}
31
32pub fn get_tile_index(neighbours: Neighbours) -> u8 {
33 let index = get_marching_tile_byte(&neighbours);
34 constants::MARCHING_TILES[index as usize]
35}
36
37pub fn get_tile_position(neighbours: Neighbours) -> (u8, u8) {
38 let img_index = get_tile_index(neighbours);
39 let width = 8;
40
41 let x = img_index % width;
42 let y = img_index / width;
43
44 (x, y)
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn it_should_get_marching_tile_index_with_all_ones() {
53 assert_eq!(get_marching_tile_byte(&Neighbours::from_byte(0xff)), 0xff);
54 }
55
56 #[test]
57 fn it_should_get_marching_non_corner_tiles() {
58 assert_eq!(
59 get_marching_tile_byte(&Neighbours::from_byte(0b01011010)),
60 0x5a
61 );
62 }
63
64 #[test]
65 fn it_should_get_marching_tile_index_with_all_zeroes() {
66 assert_eq!(get_marching_tile_byte(&Neighbours::from_byte(0x00)), 0x00);
67 }
68}