add hexmap coord_to_pixel

This commit is contained in:
dani 2023-07-03 14:47:05 +00:00
parent 6fc78f924c
commit 1e80472c96
1 changed files with 16 additions and 3 deletions

View File

@ -53,6 +53,21 @@ impl HexMap {
Vec2 { x, y }
}
pub fn coord_to_pixel(&self, coord: Vec2<i32>) -> Vec2<i32> {
Vec2 {
x: coord.x * self.pix_tile_off.x,
y: coord.y * self.tileset.tile_size().y + (coord.x % 2) * self.pix_tile_off.y,
}
}
pub fn coord_to_pixel_center(&self, coord: Vec2<i32>) -> Vec2<i32> {
let tilesize = self.tileset.tile_size();
self.coord_to_pixel(Vec2 {
x: coord.x + tilesize.x / 2,
y: coord.y + tilesize.y / 2,
})
}
pub fn set(&mut self, coord: Vec2<i32>, val: i32) {
let idx = self.coord_to_idx(coord);
self.data[idx] = val;
@ -69,10 +84,8 @@ impl HexMap {
coord: Vec2<i32>,
offset: Vec2<i32>,
) {
let x = coord.x * self.pix_tile_off.x;
let y = coord.y * self.tileset.tile_size().y + (coord.x % 2) * self.pix_tile_off.y;
target.draw_image(
Vec2 { x, y } + offset,
self.coord_to_pixel(coord) + offset,
self.tileset.get(self.data[self.coord_to_idx(coord)]),
)
}