From b313c473acfdae48b2e7eb21ca7394c561284937 Mon Sep 17 00:00:00 2001 From: dani Date: Sat, 1 Jul 2023 14:36:51 +0000 Subject: [PATCH] started implementing hexmap --- examples/test.rs | 17 ++++++++++++----- src/hexmap.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 10 ++++++---- 3 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 src/hexmap.rs diff --git a/examples/test.rs b/examples/test.rs index 954cb46..cc291b4 100644 --- a/examples/test.rs +++ b/examples/test.rs @@ -5,7 +5,7 @@ const HEIGHT: i32 = 720; struct World { img: Image, - pos: Vec2 + pos: Vec2, } 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, + ); } -} \ No newline at end of file +} diff --git a/src/hexmap.rs b/src/hexmap.rs new file mode 100644 index 0000000..d2edb63 --- /dev/null +++ b/src/hexmap.rs @@ -0,0 +1,43 @@ +use crate::Vec2; + +//odd-q vertical layout https://www.redblobgames.com/grids/hexagons +pub struct HexMap { + size: Vec2, + data: Vec, + tile_size: Vec2, +} + +impl HexMap { + //pub static + pub fn new(size: Vec2, tile_size: Vec2) -> Self { + HexMap { + size, + data: vec![T::default(); size.size()], + tile_size, + } + } + + //pub + pub fn get(&self, coord: Vec2) -> &T { + &self.data[self.coord_to_idx(coord)] + } + + pub fn get_mut(&mut self, coord: Vec2) -> &mut T { + let idx = self.coord_to_idx(coord); + &mut self.data[idx] + } + + pub fn set(&mut self, coord: Vec2, val: T) { + let idx = self.coord_to_idx(coord); + self.data[idx] = val; + } + + pub fn size(&self) -> Vec2 { + self.size + } + + //priv + fn coord_to_idx(&self, coord: Vec2) -> usize { + (coord.x + coord.y * self.size.x) as usize + } +} diff --git a/src/lib.rs b/src/lib.rs index cb780a1..38f3ec6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::*; \ No newline at end of file +pub use window::*;