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
use crate::components::Cell;
use bevy::prelude::{Component, IVec2, Reflect};
use std::ops::Deref;
const NEIGHBOR_COORDINATES: [IVec2; 8] = [
// Left
IVec2::new(-1, 0),
// Top Left
IVec2::new(-1, 1),
// Top
IVec2::new(0, 1),
// Top Right
IVec2::new(1, 1),
// Right
IVec2::new(1, 0),
// Bottom Right
IVec2::new(1, -1),
// Bottom
IVec2::new(0, -1),
// Bottom Left
IVec2::new(-1, -1),
];
/// [Moore] 2D cell. It has 8 neighbors and uses `IVec2` coordinates.
///
/// ```ascii
/// +-------+-------+-------+
/// | | | |
/// | -1,1 | 0,1 | 1,1 |
/// | | | |
/// +-------+-------+-------+
/// | | | |
/// | -1,0 | 0,0 | 0,1 |
/// | | | |
/// +-------+-------+-------+
/// | | | |
/// | -1,-1 | 0,-1 | 1,-1 |
/// | | | |
/// +-------+-------+-------+
/// ```
///
/// [Moore]: https://en.wikipedia.org/wiki/Moore_neighborhood
#[derive(Debug, Clone, Component, Reflect)]
pub struct MooreCell2d {
/// The 2D cell coordinates
pub coords: IVec2,
}
impl Deref for MooreCell2d {
type Target = IVec2;
fn deref(&self) -> &Self::Target {
&self.coords
}
}
impl Cell for MooreCell2d {
type Coordinates = IVec2;
#[inline]
fn coords(&self) -> &Self::Coordinates {
&self.coords
}
#[inline]
fn neighbor_coordinates(&self) -> Vec<Self::Coordinates> {
NEIGHBOR_COORDINATES.map(|c| c + *self.coords()).to_vec()
}
}
impl MooreCell2d {
/// Instantiates a new cell with `coords` values
#[must_use]
#[inline]
pub const fn new(coords: IVec2) -> Self {
Self { coords }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn correct_coordinates() {
let cell = MooreCell2d {
coords: IVec2::new(10, 10),
};
let neighbors = cell.neighbor_coordinates();
assert_eq!(
neighbors,
vec![
// Left
IVec2::new(9, 10),
// Top Left
IVec2::new(9, 11),
// Top
IVec2::new(10, 11),
// Top Right
IVec2::new(11, 11),
// Right
IVec2::new(11, 10),
// Bottom Right
IVec2::new(11, 9),
// Bottom
IVec2::new(10, 9),
// Bottom Left
IVec2::new(9, 9),
]
);
}
#[test]
fn correct_coordinates_negative() {
let cell = MooreCell2d {
coords: IVec2::new(-10, 10),
};
let neighbors = cell.neighbor_coordinates();
assert_eq!(
neighbors,
vec![
// Left
IVec2::new(-11, 10),
// Top Left
IVec2::new(-11, 11),
// Top
IVec2::new(-10, 11),
// Top Right
IVec2::new(-9, 11),
// Right
IVec2::new(-9, 10),
// Bottom Right
IVec2::new(-9, 9),
// Bottom
IVec2::new(-10, 9),
// Bottom Left
IVec2::new(-11, 9),
]
);
}
#[test]
fn correct_coordinates_origin() {
let cell = MooreCell2d {
coords: IVec2::new(0, 0),
};
let neighbors = cell.neighbor_coordinates();
assert_eq!(
neighbors,
vec![
// Left
IVec2::new(-1, 0),
// Top Left
IVec2::new(-1, 1),
// Top
IVec2::new(0, 1),
// Top Right
IVec2::new(1, 1),
// Right
IVec2::new(1, 0),
// Bottom Right
IVec2::new(1, -1),
// Bottom
IVec2::new(0, -1),
// Bottom Left
IVec2::new(-1, -1),
]
);
}
}