diff --git a/src/hexmap.rs b/src/hexmap.rs index 97e6cc7..8477128 100644 --- a/src/hexmap.rs +++ b/src/hexmap.rs @@ -9,6 +9,15 @@ pub struct HexMap { pix_tile_off: Vec2, } +pub enum Direction { + North, + NorthEast, + SouthEast, + South, + SouthWest, + NorthWest, +} + impl HexMap { //pub static //pix_tile_off: x offset per tile, up/down alternating y offset on x axis @@ -21,9 +30,28 @@ impl HexMap { } } + pub fn get_neighbour(coord: Vec2, dir: Direction) -> Vec2 { + let yoff = coord.x % 2; + (match dir { + Direction::North => Vec2 { x: 0, y: -1 }, + Direction::NorthEast => Vec2 { x: 1, y: -1 + yoff }, + Direction::SouthEast => Vec2 { x: 1, y: 0 + yoff }, + Direction::South => Vec2 { x: 0, y: 1 }, + Direction::SouthWest => Vec2 { x: -1, y: 0 + yoff }, + Direction::NorthWest => Vec2 { + x: -1, + y: -1 + yoff, + }, + }) + coord + } + //pub pub fn get(&self, coord: Vec2) -> i32 { - self.data[self.coord_to_idx(coord)] + if !self.is_valid_coord(coord) { + -1 + } else { + self.data[self.coord_to_idx(coord)] + } } pub fn pixel_to_coord(&self, pixel: Vec2) -> Vec2 { @@ -66,6 +94,10 @@ impl HexMap { }) } + pub fn is_valid_coord(&self, coord: Vec2) -> bool { + coord.x >= 0 && coord.y >= 0 && coord.x < self.size.x && coord.y < self.size.y + } + pub fn set(&mut self, coord: Vec2, val: i32) { let idx = self.coord_to_idx(coord); self.data[idx] = val;