From d68cf09f7b8e5cb783dc495097c17eaf2a4d5427 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Mon, 31 Aug 2026 00:03:32 -0700 Subject: cleanup --- src/proc_gen/tileset_loader.rs | 53 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'src/proc_gen/tileset_loader.rs') diff --git a/src/proc_gen/tileset_loader.rs b/src/proc_gen/tileset_loader.rs index acb4d65..e0d9d18 100644 --- a/src/proc_gen/tileset_loader.rs +++ b/src/proc_gen/tileset_loader.rs @@ -1,6 +1,9 @@ +use glam::IVec2; use serde::Deserialize; use std::{collections::HashMap, error::Error, fs::File, io::BufReader, path::Path}; +use crate::sim::lib::marching_squares::Marchable; + fn load_img(path: &Path) -> Result<(Vec, u32, u32), Box> { let mut decoder = png::Decoder::new(BufReader::new(File::open(path)?)); decoder.set_transformations(png::Transformations::IDENTITY); @@ -32,7 +35,7 @@ pub struct TileDimensions { pub long: u32, } -#[derive(Deserialize, Clone, Copy, Debug)] +#[derive(Deserialize, Clone, Copy, Debug, PartialEq, Eq)] pub enum TilesetPixelType { Void = 0, Terrain, @@ -55,6 +58,54 @@ pub struct Tile { pub pixels: Vec, } +// TODO might want to use newtypes to hoist to biome +impl Marchable for Tile { + fn occupied(&self, pos: IVec2) -> bool { + // TODO this will break for tileset dimensions other than 22x44 + if pos.x < 0 || pos.x >= self.size().x || pos.y < 0 || pos.y >= self.size().y { + match self.orientation { + TileOrientation::Horizontal => { + let a = (pos.x == -1 || pos.x == (self.dimensions.long as i32)) + && pos.y >= 8 + && pos.y <= 13; + let b = (pos.y == -1 || pos.y == (self.dimensions.short as i32)) + && ((pos.x >= 8 && pos.x <= 13) || (pos.x >= 30 && pos.x <= 35)); + !(a || b) + } + TileOrientation::Vertical => { + let a = (pos.y == -1 || pos.y == (self.dimensions.long as i32)) + && pos.x >= 8 + && pos.x <= 13; + let b = (pos.x == -1 || pos.x == (self.dimensions.short as i32)) + && ((pos.y >= 8 && pos.y <= 13) || (pos.y >= 30 && pos.y <= 35)); + !(a || b) + } + } + } else { + self.pixel_at(pos) == TilesetPixelType::Terrain + } + } + fn marchable_size(&self) -> IVec2 { + self.size() + } +} + +impl Tile { + pub fn size(&self) -> IVec2 { + let short = self.dimensions.short as i32; + let long = self.dimensions.long as i32; + match self.orientation { + TileOrientation::Horizontal => IVec2::new(long, short), + TileOrientation::Vertical => IVec2::new(short, long), + } + } + + #[inline] + pub fn pixel_at(&self, position: IVec2) -> TilesetPixelType { + self.pixels[(position.x + position.y * self.size().x) as usize] + } +} + #[derive(Debug)] pub struct Tileset { pub dimensions: TileDimensions, -- cgit v1.3.1