diff --git a/src/game.rs b/src/game.rs index 0549bb5..a5f7453 100644 --- a/src/game.rs +++ b/src/game.rs @@ -28,12 +28,12 @@ fn for_each_tile(camera_offset: WorldPos, mut f: impl FnMut(TilePos, ScreenPos)) for x in (-32..NATIVE_RESOLUTION.w + 32).step_by(32) { f( TilePos { - x: wp_to_tp(camera_offset.x + x as WorldPosScalar), - y: wp_to_tp(camera_offset.y + y as WorldPosScalar), + x: wp_to_tp(camera_offset.x.saturating_add(x as WorldPosScalar)), + y: wp_to_tp(camera_offset.y.saturating_add(y as WorldPosScalar)), }, ScreenPos { - x: (x as i32 - camera_offset.x % 32) as i16, - y: (y as i32 - (camera_offset.y % 32)) as i16, + x: ((x as WorldPosScalar).saturating_sub(camera_offset.x % 32)) as i16, + y: ((y as WorldPosScalar).saturating_sub(camera_offset.y % 32)) as i16, }, ) } diff --git a/src/math.rs b/src/math.rs index c3a1c3c..6f14441 100644 --- a/src/math.rs +++ b/src/math.rs @@ -1,6 +1,6 @@ use crate::world::{TilePos, TilePosScalar}; -pub type WorldPosScalar = i32; +pub type WorldPosScalar = u32; #[derive(Clone, Copy)] pub struct WorldPos { @@ -20,11 +20,7 @@ impl WorldPos { } 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 - } + wp / TILE_SIZE as TilePosScalar } #[test] @@ -32,7 +28,4 @@ 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); } diff --git a/src/world.rs b/src/world.rs index 7bbb005..528f592 100644 --- a/src/world.rs +++ b/src/world.rs @@ -62,11 +62,7 @@ impl TilePos { } fn chk_pos(tile: TilePosScalar) -> ChunkPosScalar { - if tile.is_negative() { - ((tile + 1) / CHUNK_EXTENT as i32) as i16 - 1 - } else { - (tile / CHUNK_EXTENT as i32) as i16 - } + (tile / CHUNK_EXTENT as TilePosScalar) as i16 } #[test] @@ -75,26 +71,15 @@ fn test_chk_pos() { assert_eq!(chk_pos(1), 0); assert_eq!(chk_pos(127), 0); assert_eq!(chk_pos(128), 1); - assert_eq!(chk_pos(-1), -1); - assert_eq!(chk_pos(-2), -1); - assert_eq!(chk_pos(-127), -1); - assert_eq!(chk_pos(-128), -1); - assert_eq!(chk_pos(-129), -2); } fn chunk_local(global: TilePosScalar) -> ChunkLocalTilePosScalar { - let mut result = global % CHUNK_EXTENT as i32; - if result.is_negative() { - result += CHUNK_EXTENT as i32; - } - result as u8 + (global % CHUNK_EXTENT as TilePosScalar) as ChunkLocalTilePosScalar } #[test] fn test_chunk_local() { assert_eq!(chunk_local(0), 0); - assert_eq!(chunk_local(-1), 127); - assert_eq!(chunk_local(-128), 0); } #[test] @@ -107,32 +92,10 @@ fn test_to_chunk_and_local() { TilePos { x: 1, y: 1 }.to_chunk_and_local(), (ChunkPos { x: 0, y: 0 }, ChunkLocalTilePos { x: 1, y: 1 }) ); - assert_eq!( - TilePos { x: -1, y: -1 }.to_chunk_and_local(), - ( - ChunkPos { x: -1, y: -1 }, - ChunkLocalTilePos { x: 127, y: 127 } - ) - ); - assert_eq!( - TilePos { x: -127, y: -127 }.to_chunk_and_local(), - (ChunkPos { x: -1, y: -1 }, ChunkLocalTilePos { x: 1, y: 1 }) - ); - assert_eq!( - TilePos { x: -128, y: -128 }.to_chunk_and_local(), - (ChunkPos { x: -1, y: -1 }, ChunkLocalTilePos { x: 0, y: 0 }) - ); - assert_eq!( - TilePos { x: -129, y: -129 }.to_chunk_and_local(), - ( - ChunkPos { x: -2, y: -2 }, - ChunkLocalTilePos { x: 127, y: 127 } - ) - ); } // Need to support at least 4 million tiles long -pub type TilePosScalar = i32; +pub type TilePosScalar = u32; const CHUNK_EXTENT: u16 = 128; const CHUNK_N_TILES: usize = CHUNK_EXTENT as usize * CHUNK_EXTENT as usize;