game-wip-dontplay/src/math.rs
crumblingstatue 2a2c8f721c Eliminate negative coordinates
They were making coordinate calculations more difficult,
and probably slower due to having to special case negative coordinates
2023-04-03 10:32:20 +02:00

31 lines
591 B
Rust

use crate::world::{TilePos, TilePosScalar};
pub type WorldPosScalar = u32;
#[derive(Clone, Copy)]
pub struct WorldPos {
pub x: WorldPosScalar,
pub y: WorldPosScalar,
}
pub const TILE_SIZE: u8 = 32;
impl WorldPos {
pub fn tile_pos(&self) -> TilePos {
TilePos {
x: wp_to_tp(self.x),
y: wp_to_tp(self.y),
}
}
}
pub fn wp_to_tp(wp: WorldPosScalar) -> TilePosScalar {
wp / TILE_SIZE as TilePosScalar
}
#[test]
fn test_wp_to_tp() {
assert_eq!(wp_to_tp(0), 0);
assert_eq!(wp_to_tp(1), 0);
assert_eq!(wp_to_tp(33), 1);
}