summaryrefslogtreecommitdiff
path: root/src/proc_gen/tileset_loader.rs
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2026-08-31 00:03:32 -0700
committerKai Stevenson <kai@kaistevenson.com>2026-08-31 00:03:32 -0700
commitd68cf09f7b8e5cb783dc495097c17eaf2a4d5427 (patch)
tree59216cae8c466ad63fa6ff47dc5c7f35c64c23d2 /src/proc_gen/tileset_loader.rs
parent8e128d3ca24fa10dc7d871f7daf6c43eaddc385c (diff)
Diffstat (limited to 'src/proc_gen/tileset_loader.rs')
-rw-r--r--src/proc_gen/tileset_loader.rs53
1 files changed, 52 insertions, 1 deletions
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<u8>, u32, u32), Box<dyn Error>> {
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<TilesetPixelType>,
}
+// 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,