mirror of
https://github.com/Noratrieb/game-wip-dontplay.git
synced 2026-01-16 04:25:00 +01:00
Fix world pos to tile pos calculation for negative values
This commit is contained in:
parent
62bd6b2c7c
commit
7dba1b22e3
3 changed files with 26 additions and 8 deletions
26
src/math.rs
26
src/math.rs
|
|
@ -1,4 +1,4 @@
|
|||
use crate::world::TilePos;
|
||||
use crate::world::{TilePos, TilePosScalar};
|
||||
|
||||
pub type WorldPosScalar = i32;
|
||||
|
||||
|
|
@ -8,13 +8,31 @@ pub struct WorldPos {
|
|||
pub y: WorldPosScalar,
|
||||
}
|
||||
|
||||
pub const TILE_SIZE: u8 = 32;
|
||||
|
||||
impl WorldPos {
|
||||
pub fn tile_pos(&self) -> TilePos {
|
||||
TilePos {
|
||||
x: self.x / TILE_SIZE as i32,
|
||||
y: self.y / TILE_SIZE as i32,
|
||||
x: wp_to_tp(self.x),
|
||||
y: wp_to_tp(self.y),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub const TILE_SIZE: u8 = 32;
|
||||
pub fn wp_to_tp(wp: WorldPosScalar) -> TilePosScalar {
|
||||
if wp.is_negative() {
|
||||
(wp + 1) / TILE_SIZE as TilePosScalar - 1
|
||||
} else {
|
||||
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);
|
||||
assert_eq!(wp_to_tp(-1), -1);
|
||||
assert_eq!(wp_to_tp(-32), -1);
|
||||
assert_eq!(wp_to_tp(-33), -2);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue