use std::collections::{HashMap, HashSet};
use std::path::Path;
use crate::core::components::tiles::atlas::data::{TileConfig, TilesetAtlas};
use crate::utils::file::read_file;
#[derive(Clone, Debug)]
pub struct Tileset {
pub(crate) name: String,
pub(crate) width: usize,
pub(crate) height: usize,
pub(crate) tile_width: usize,
#[allow(dead_code)]
pub(crate) tile_height: usize,
pub(crate) texture: String,
pub(crate) pathing: HashMap<String, HashSet<usize>>,
pub(crate) tiles: HashMap<usize, TileConfig>,
}
impl Tileset {
pub fn new(name: String, texture: String, width: usize,height: usize, tile_width: usize, tile_height: usize) -> Self {
Self { name, width, height, tile_width,tile_height, texture, pathing: HashMap::default(), tiles: HashMap::default() }
}
pub fn with_pathing(mut self, pathing: HashMap<String, HashSet<usize>>) -> Self {
self.pathing = pathing;
self
}
pub fn from_atlas(path_to_atlas: &str, path_to_texture: &str) -> Result<Self, ()> {
let path = Path::new(path_to_atlas);
if path.exists() {
match read_file(path) {
Ok(bytes) => match serde_json::from_slice::<TilesetAtlas>(bytes.as_slice()) {
Ok(atlas) => {
return Ok(atlas.into_tileset(path_to_texture.to_string()));
}
Err(e) => {println!("{}", e)}
},
Err(e) => {println!("{:?}", e)}
}
}
Err(())
}
}