utils.rs 1.13 KiB
use bevy::prelude::{Component, Resource};
use ldtk_rust::EntityInstance;
use num_traits::AsPrimitive;
pub const TILE_SCALE_INT: i64 = 4;
pub const TILE_SCALE_F32: f32 = 4.0;
pub fn px_to_grid<T: AsPrimitive<i64>>(t: T) -> i64 {
t.as_() / TILE_SCALE_INT
}
pub fn grid_to_px<T: AsPrimitive<f32>>(t: T) -> f32 {
t.as_() * TILE_SCALE_F32 // + (TILE_SCALE_F32 / 2.0)
}
pub fn entity_to_worldspace(level_height: i64, entity: &EntityInstance) -> (f32, f32) {
let centre_align_pixel_x = grid_to_px(entity.grid[0]) - (TILE_SCALE_F32 / 2.0);
let centre_align_pixel_y = grid_to_px(entity.grid[1]) - (TILE_SCALE_F32 / 2.0);
let inverted_pixel_y = level_height as f32 - centre_align_pixel_y - TILE_SCALE_F32;
let box_aligned_x = centre_align_pixel_x + (entity.width / 2) as f32;
let box_aligned_y = inverted_pixel_y - (entity.height / 2) as f32;
(box_aligned_x, box_aligned_y)
}
#[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,
}
}
}