haaacky pixel->coord conversion
This commit is contained in:
parent
a9ffc9bc6f
commit
6fc78f924c
Binary file not shown.
Before Width: | Height: | Size: 925 B After Width: | Height: | Size: 1001 B |
|
@ -25,14 +25,16 @@ impl Game for World {
|
|||
pos: Vec2::zero(),
|
||||
map: HexMap::new(
|
||||
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) {
|
||||
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) {
|
||||
|
@ -40,6 +42,10 @@ impl Game for World {
|
|||
Event::MouseClick(MouseButton::Left, _) => {
|
||||
window_state.scramble_palette();
|
||||
}
|
||||
Event::MouseClick(MouseButton::Right, _) => {
|
||||
self.map
|
||||
.set(self.map.pixel_to_coord(window_state.mouse_pos()), 1);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use crate::{Image, Tileset, Vec2};
|
||||
use num::complex::ComplexFloat;
|
||||
use std::rc::Rc;
|
||||
|
||||
//odd-q vertical layout https://www.redblobgames.com/grids/hexagons
|
||||
|
@ -6,15 +7,18 @@ pub struct HexMap {
|
|||
size: Vec2<i32>,
|
||||
data: Vec<i32>,
|
||||
tileset: Rc<Tileset>,
|
||||
pix_tile_off: Vec2<i32>,
|
||||
}
|
||||
|
||||
impl HexMap {
|
||||
//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 {
|
||||
size,
|
||||
data: vec![0; size.size()],
|
||||
tileset,
|
||||
pix_tile_off,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,6 +27,32 @@ impl HexMap {
|
|||
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) {
|
||||
let idx = self.coord_to_idx(coord);
|
||||
self.data[idx] = val;
|
||||
|
@ -39,9 +69,8 @@ impl HexMap {
|
|||
coord: Vec2<i32>,
|
||||
offset: Vec2<i32>,
|
||||
) {
|
||||
let x = coord.x * (self.tileset.tile_size().x as f64 * 3.0 / 4.0).ceil() as i32;
|
||||
let y =
|
||||
coord.y * self.tileset.tile_size().y + (coord.x % 2) * (self.tileset.tile_size().y / 2);
|
||||
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.tileset.get(self.data[self.coord_to_idx(coord)]),
|
||||
|
|
Loading…
Reference in New Issue