started implementing hexmap

This commit is contained in:
dani 2023-07-01 14:36:51 +00:00
parent ee2eb7c34d
commit b313c473ac
3 changed files with 61 additions and 9 deletions

View File

@ -5,7 +5,7 @@ const HEIGHT: i32 = 720;
struct World {
img: Image,
pos: Vec2<i32>
pos: Vec2<i32>,
}
fn main() {
@ -18,7 +18,7 @@ impl Game for World {
window_state.scramble_palette();
Self {
img: Image::load("examples/assets/test.gif"),
pos: Vec2::zero()
pos: Vec2::zero(),
}
}
@ -37,9 +37,16 @@ impl Game for World {
fn draw(&self, target: &mut Image) {
target.clear();
target.draw_image(Vec2{x: 200, y: 100}, &self.img);
target.draw_image(Vec2 { x: 200, y: 100 }, &self.img);
target.draw_image(self.pos, &self.img);
target.draw_line(Vec2{x: WIDTH/2, y: HEIGHT/2}, self.pos, 10);
target.draw_line(
Vec2 {
x: WIDTH / 2,
y: HEIGHT / 2,
},
self.pos,
10,
);
}
}
}

43
src/hexmap.rs Normal file
View File

@ -0,0 +1,43 @@
use crate::Vec2;
//odd-q vertical layout https://www.redblobgames.com/grids/hexagons
pub struct HexMap<T: Default> {
size: Vec2<i32>,
data: Vec<T>,
tile_size: Vec2<i32>,
}
impl<T: Default + Clone> HexMap<T> {
//pub static
pub fn new(size: Vec2<i32>, tile_size: Vec2<i32>) -> Self {
HexMap {
size,
data: vec![T::default(); size.size()],
tile_size,
}
}
//pub
pub fn get(&self, coord: Vec2<i32>) -> &T {
&self.data[self.coord_to_idx(coord)]
}
pub fn get_mut(&mut self, coord: Vec2<i32>) -> &mut T {
let idx = self.coord_to_idx(coord);
&mut self.data[idx]
}
pub fn set(&mut self, coord: Vec2<i32>, val: T) {
let idx = self.coord_to_idx(coord);
self.data[idx] = val;
}
pub fn size(&self) -> Vec2<i32> {
self.size
}
//priv
fn coord_to_idx(&self, coord: Vec2<i32>) -> usize {
(coord.x + coord.y * self.size.x) as usize
}
}

View File

@ -1,9 +1,11 @@
mod window;
mod hexmap;
mod image;
mod vec2;
mod rect;
mod vec2;
mod window;
pub use window::*;
pub use hexmap::*;
pub use image::*;
pub use rect::*;
pub use vec2::*;
pub use rect::*;
pub use window::*;