use glam::IVec2;
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(bevy_ecs::component::Component))]
pub struct WorldLinked;
#[derive(Default, Clone, Debug)]
#[cfg_attr(feature = "bevy", derive(bevy_ecs::resource::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 {
match (self.width(), self.height()) {
(0, _) | (_, 0) => 0,
(w, _) => ((y.as_() * w) + 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) {
let index = index.as_();
match index {
0 => (0, 0),
_ => (
index.checked_rem(self.width).unwrap_or(0) as usize,
index.checked_div(self.width).unwrap_or(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
}
pub fn flip_y(&self, point: IVec2) -> IVec2 {
let new_y = self.height() as i32 - point.y;
IVec2::new(point.x, new_y)
}
}