Newer
Older
use bevy::prelude::{Component, Resource};
use ldtk_rust::EntityInstance;
use num_traits::AsPrimitive;
use serde::de::DeserializeOwned;
use serde::Serialize;
use crate::get_ldtk_tile_scale;
pub trait SerdeClone {
fn serde_clone(&self) -> Self;
}
impl<T> SerdeClone for T
where
T: Serialize + DeserializeOwned,
{
fn serde_clone(&self) -> Self {
serde_json::from_value(serde_json::to_value(self).unwrap()).unwrap()
}
}
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;
#[derive(Component)]
pub struct WorldLinked;
#[derive(Default, Resource, Clone)]
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,
}
}
}
width: i64,
height: i64,
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 {
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 {
pub fn height(&self) -> i64 {