#[cfg(feature = "bevy")] use bevy::prelude::{Component, IVec2, Resource}; use num_traits::AsPrimitive; use crate::get_ldtk_tile_scale; use crate::ldtk::EntityInstance; pub fn px_to_grid<T: AsPrimitive<i64>>(t: T) -> i64 { t.as_() / (get_ldtk_tile_scale() as i64) } pub fn grid_to_px<T: AsPrimitive<f32>>(t: T) -> f32 { t.as_() * get_ldtk_tile_scale() } pub fn entity_centre(level_height: i64, entity: &EntityInstance) -> (f32, f32) { let x = entity.px[0] - (entity.width / 2); let y = level_height - entity.px[1] - entity.height / 2; (x as f32, y as f32) } #[cfg_attr(feature="bevy", derive(Component))] pub struct WorldLinked; #[derive(Default, Clone, Debug)] #[cfg_attr(feature="bevy", derive(Resource))] pub struct ActiveLevel { pub map: String, pub dirty: bool, } impl ActiveLevel { pub fn new<T: ToString>(map: T) -> Self { ActiveLevel { map: map.to_string(), dirty: false, } } } #[derive(Debug, Copy, Clone)] pub struct Indexer { width: i64, height: i64, } impl Indexer { pub fn new(width: impl AsPrimitive<i64>, height: impl AsPrimitive<i64>) -> Self { Self { width: width.as_(), height: height.as_(), } } pub fn index(&self, x: impl AsPrimitive<i64>, y: impl AsPrimitive<i64>) -> usize { ((y.as_() * self.width) + x.as_()).as_() } pub fn index_checked( &self, x: impl AsPrimitive<i64>, y: impl AsPrimitive<i64>, ) -> Option<usize> { if self.is_valid(x, y) { Some(self.index(x, y)) } else { None } } pub fn reverse(&self, index: impl AsPrimitive<i64>) -> (usize, usize) { ( (index.as_() % self.width).max(0) as usize, (index.as_() / self.width).max(0) as usize, ) } pub fn width(&self) -> i64 { self.width } pub fn height(&self) -> i64 { self.height } pub fn is_valid(&self, x: impl AsPrimitive<i64>, y: impl AsPrimitive<i64>) -> bool { let x = x.as_(); let y = y.as_(); x >= 0 && x < self.width && y >= 0 && y < self.height } #[cfg(feature = "bevy")] /// Perform a transformation to flip a grid point (top down coordinates) into a render /// point (bottom up coordinates) pub fn flip_y(&self, point: IVec2) -> IVec2 { let new_y = self.height() as i32 - point.y; IVec2::new(point.x, new_y) } }