38 lines
940 B
Rust
38 lines
940 B
Rust
|
|
use bevy::prelude::*;
|
||
|
|
use serde::{Deserialize, Serialize};
|
||
|
|
|
||
|
|
use crate::components::GridPos;
|
||
|
|
|
||
|
|
/// World units per tile. Tiled maps are expected to use the same tile size,
|
||
|
|
/// other sizes are scaled to fit.
|
||
|
|
pub const TILE_SIZE: f32 = 32.0;
|
||
|
|
|
||
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||
|
|
pub enum Dir {
|
||
|
|
North,
|
||
|
|
#[default]
|
||
|
|
South,
|
||
|
|
East,
|
||
|
|
West,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Dir {
|
||
|
|
/// Grid delta, with `y` pointing down like in Tiled.
|
||
|
|
pub fn delta(self) -> (i32, i32) {
|
||
|
|
match self {
|
||
|
|
Dir::North => (0, -1),
|
||
|
|
Dir::South => (0, 1),
|
||
|
|
Dir::East => (1, 0),
|
||
|
|
Dir::West => (-1, 0),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Converts a grid position to the center of the tile in Bevy world space (y up).
|
||
|
|
pub fn grid_to_world(pos: GridPos) -> Vec2 {
|
||
|
|
Vec2::new(
|
||
|
|
pos.x as f32 * TILE_SIZE + TILE_SIZE / 2.0,
|
||
|
|
-(pos.y as f32 * TILE_SIZE + TILE_SIZE / 2.0),
|
||
|
|
)
|
||
|
|
}
|