haaacky pixel->coord conversion

This commit is contained in:
dani 2023-07-03 14:41:34 +00:00
parent a9ffc9bc6f
commit 6fc78f924c
3 changed files with 41 additions and 6 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 925 B

After

Width:  |  Height:  |  Size: 1001 B

View File

@ -25,14 +25,16 @@ impl Game for World {
pos: Vec2::zero(), pos: Vec2::zero(),
map: HexMap::new( map: HexMap::new(
Vec2 { x: 27, y: 13 }, Vec2 { x: 27, y: 13 },
Tileset::load_data(include_bytes!("assets/hex2.gif"), Vec2 { x: 1, y: 1 }), Tileset::load_data(include_bytes!("assets/hex2.gif"), Vec2 { x: 2, y: 1 }),
Vec2 { x: 23, y: 13 },
), ),
} }
} }
fn update(&mut self, window_state: &mut WindowState) { fn update(&mut self, window_state: &mut WindowState) {
self.pos = window_state.mouse_pos(); self.pos = window_state.mouse_pos();
window_state.log(format!("{}x{}", self.pos.x, self.pos.y).as_str()); let coord = self.map.pixel_to_coord(self.pos);
window_state.log(format!("{}x{}", coord.x, coord.y).as_str());
} }
fn on_event(&mut self, window_state: &mut WindowState, event: Event) { fn on_event(&mut self, window_state: &mut WindowState, event: Event) {
@ -40,6 +42,10 @@ impl Game for World {
Event::MouseClick(MouseButton::Left, _) => { Event::MouseClick(MouseButton::Left, _) => {
window_state.scramble_palette(); window_state.scramble_palette();
} }
Event::MouseClick(MouseButton::Right, _) => {
self.map
.set(self.map.pixel_to_coord(window_state.mouse_pos()), 1);
}
_ => {} _ => {}
} }
} }

View File

@ -1,4 +1,5 @@
use crate::{Image, Tileset, Vec2}; use crate::{Image, Tileset, Vec2};
use num::complex::ComplexFloat;
use std::rc::Rc; use std::rc::Rc;
//odd-q vertical layout https://www.redblobgames.com/grids/hexagons //odd-q vertical layout https://www.redblobgames.com/grids/hexagons
@ -6,15 +7,18 @@ pub struct HexMap {
size: Vec2<i32>, size: Vec2<i32>,
data: Vec<i32>, data: Vec<i32>,
tileset: Rc<Tileset>, tileset: Rc<Tileset>,
pix_tile_off: Vec2<i32>,
} }
impl HexMap { impl HexMap {
//pub static //pub static
pub fn new(size: Vec2<i32>, tileset: Rc<Tileset>) -> Self { //pix_tile_off: x offset per tile, up/down alternating y offset on x axis
pub fn new(size: Vec2<i32>, tileset: Rc<Tileset>, pix_tile_off: Vec2<i32>) -> Self {
HexMap { HexMap {
size, size,
data: vec![0; size.size()], data: vec![0; size.size()],
tileset, tileset,
pix_tile_off,
} }
} }
@ -23,6 +27,32 @@ impl HexMap {
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> {
let tilesize = self.tileset.tile_size();
let xrepeat = pixel.x % self.pix_tile_off.x;
let mut x = pixel.x / self.pix_tile_off.x;
let py = if x % 2 == 0 {
pixel.y
} else {
pixel.y - self.pix_tile_off.y
};
let yrepeat = py % tilesize.y;
let mut y = py / tilesize.y;
let outside = self.tileset.get(0).get_pixel(Vec2 {
x: xrepeat,
y: yrepeat,
}) == 0;
if outside {
x -= 1;
if yrepeat > tilesize.y / 2 {
y += 1;
}
y -= x % 2;
}
let slice = xrepeat / (tilesize.x / 4);
Vec2 { x, 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;
@ -39,9 +69,8 @@ impl HexMap {
coord: Vec2<i32>, coord: Vec2<i32>,
offset: Vec2<i32>, offset: Vec2<i32>,
) { ) {
let x = coord.x * (self.tileset.tile_size().x as f64 * 3.0 / 4.0).ceil() as i32; let x = coord.x * self.pix_tile_off.x;
let y = let y = coord.y * self.tileset.tile_size().y + (coord.x % 2) * self.pix_tile_off.y;
coord.y * self.tileset.tile_size().y + (coord.x % 2) * (self.tileset.tile_size().y / 2);
target.draw_image( target.draw_image(
Vec2 { x, y } + offset, Vec2 { x, y } + offset,
self.tileset.get(self.data[self.coord_to_idx(coord)]), self.tileset.get(self.data[self.coord_to_idx(coord)]),