added neighbours to hexmap

This commit is contained in:
dani 2023-07-06 13:53:36 +00:00
parent 3aac7e87ab
commit ecaeceb1e7
1 changed files with 33 additions and 1 deletions

View File

@ -9,6 +9,15 @@ pub struct HexMap {
pix_tile_off: Vec2<i32>, pix_tile_off: Vec2<i32>,
} }
pub enum Direction {
North,
NorthEast,
SouthEast,
South,
SouthWest,
NorthWest,
}
impl HexMap { impl HexMap {
//pub static //pub static
//pix_tile_off: x offset per tile, up/down alternating y offset on x axis //pix_tile_off: x offset per tile, up/down alternating y offset on x axis
@ -21,10 +30,29 @@ impl HexMap {
} }
} }
pub fn get_neighbour(coord: Vec2<i32>, dir: Direction) -> Vec2<i32> {
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
pub fn get(&self, coord: Vec2<i32>) -> i32 { pub fn get(&self, coord: Vec2<i32>) -> i32 {
if !self.is_valid_coord(coord) {
-1
} else {
self.data[self.coord_to_idx(coord)] self.data[self.coord_to_idx(coord)]
} }
}
pub fn pixel_to_coord(&self, pixel: Vec2<i32>) -> Vec2<i32> { pub fn pixel_to_coord(&self, pixel: Vec2<i32>) -> Vec2<i32> {
let tilesize = self.tileset.tile_size(); let tilesize = self.tileset.tile_size();
@ -66,6 +94,10 @@ impl HexMap {
}) })
} }
pub fn is_valid_coord(&self, coord: Vec2<i32>) -> bool {
coord.x >= 0 && coord.y >= 0 && coord.x < self.size.x && coord.y < self.size.y
}
pub fn set(&mut self, coord: Vec2<i32>, val: i32) { pub fn set(&mut self, coord: Vec2<i32>, val: i32) {
let idx = self.coord_to_idx(coord); let idx = self.coord_to_idx(coord);
self.data[idx] = val; self.data[idx] = val;