From 462eede90fc628e7521ff32f6460e40020c8233f Mon Sep 17 00:00:00 2001 From: crumblingstatue Date: Wed, 5 Apr 2023 00:53:08 +0200 Subject: [PATCH] Adjust code to new 100,000 block limit --- src/app.rs | 21 ++++++++++++++++----- src/math.rs | 11 +++++++---- src/world.rs | 12 ++++++------ 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/app.rs b/src/app.rs index e620650..ee0bc95 100644 --- a/src/app.rs +++ b/src/app.rs @@ -15,7 +15,7 @@ use crate::{ input::Input, math::{px_per_frame_to_km_h, WorldPos, M_PER_PX, TILE_SIZE}, res::Res, - world::Tile, + world::{Tile, TilePosScalar, CHUNK_EXTENT}, }; /// Application level state (includes game and ui state, etc.) @@ -142,8 +142,10 @@ impl App { }); self.game.player.vspeed += self.game.gravity; let (x, y, _w, _h) = self.game.player.col_en.en.xywh(); - self.game.camera_offset.x = (x - NATIVE_RESOLUTION.w as i32 / 2) as u32; - self.game.camera_offset.y = (y - NATIVE_RESOLUTION.h as i32 / 2) as u32; + self.game.camera_offset.x = + (x - NATIVE_RESOLUTION.w as i32 / 2).try_into().unwrap_or(0); + self.game.camera_offset.y = + (y - NATIVE_RESOLUTION.h as i32 / 2).try_into().unwrap_or(0); } let loc = self.input.mouse_down_loc; let mut wpos = self.game.camera_offset; @@ -151,6 +153,11 @@ impl App { wpos.y = wpos.y.saturating_add_signed(loc.y.into()); let mouse_tpos = wpos.tile_pos(); imm!("Mouse @ tile {}, {}", mouse_tpos.x, mouse_tpos.y); + imm!( + "@ chunk {}, {}", + mouse_tpos.x / CHUNK_EXTENT as TilePosScalar, + mouse_tpos.y / CHUNK_EXTENT as TilePosScalar + ); if self.debug.freecam && self.input.pressed(Key::P) { self.game.player.col_en.en.pos.x = wpos.x as i32; self.game.player.col_en.en.pos.y = wpos.y as i32; @@ -167,7 +174,7 @@ impl App { t.bg = self.game.tile_to_place; } } - if self.game.camera_offset.y > 163800 { + if self.game.camera_offset.y > 134217712 { self.game.current_biome = Biome::Underground; } else { self.game.current_biome = Biome::Surface; @@ -301,7 +308,11 @@ impl fmt::Display for LengthDisp { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let meters = self.0 * M_PER_PX; if meters.abs() > 1000. { - let km = (meters / 1000.).floor(); + let km = if meters.is_sign_negative() { + (meters / 1000.).ceil() + } else { + (meters / 1000.).floor() + }; let m = meters % 1000.; write!(f, "{km} km, {m} m") } else { diff --git a/src/math.rs b/src/math.rs index c848b0d..ea4611f 100644 --- a/src/math.rs +++ b/src/math.rs @@ -25,6 +25,9 @@ pub fn px_per_frame_to_km_h(px_per_frame: f32) -> f32 { px_per_frame_to_m_per_s(px_per_frame) * 3.6 } +/// World extent in tiles. Roughly 50km*50km. +pub const WORLD_EXTENT: TilePosScalar = 100_000; + impl WorldPos { pub fn tile_pos(&self) -> TilePos { TilePos { @@ -33,10 +36,10 @@ impl WorldPos { } } /// Horizontal center of the world - pub const CENTER: WorldPosScalar = - (TilePosScalar::MAX / 2) as WorldPosScalar * TILE_SIZE as WorldPosScalar; - /// Vertical surface level. You can build 5,000 blocks upwards - pub const SURFACE: WorldPosScalar = 5000 * TILE_SIZE as WorldPosScalar; + pub const CENTER: WorldPosScalar = (WORLD_EXTENT / 2) * TILE_SIZE as WorldPosScalar; + /// Vertical surface level. + /// You can build 10 km high. + pub const SURFACE: WorldPosScalar = 20_000 * TILE_SIZE as WorldPosScalar; pub const SURFACE_CENTER: Self = Self { x: Self::CENTER, y: Self::SURFACE, diff --git a/src/world.rs b/src/world.rs index 6c85b93..9094415 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,7 +1,7 @@ use fnv::FnvHashMap; use rand::{thread_rng, Rng}; -type ChunkPosScalar = u16; +pub type ChunkPosScalar = u16; #[derive(Hash, PartialEq, Eq, Debug, Clone, Copy)] pub struct ChunkPos { @@ -88,9 +88,9 @@ fn test_to_chunk_and_local() { } // Need to support at least 4 million tiles long -pub type TilePosScalar = u16; +pub type TilePosScalar = u32; -const CHUNK_EXTENT: u16 = 128; +pub const CHUNK_EXTENT: u16 = 128; const CHUNK_N_TILES: usize = CHUNK_EXTENT as usize * CHUNK_EXTENT as usize; type ChunkTiles = [Tile; CHUNK_N_TILES]; @@ -107,7 +107,7 @@ impl Chunk { mid: 0, fg: 0, }; CHUNK_N_TILES]; - if pos.y == 39 { + if pos.y == 157 { for (i, b) in tiles.iter_mut().enumerate() { if i / CHUNK_EXTENT as usize == 0 { b.fg = 8; @@ -116,7 +116,7 @@ impl Chunk { b.bg = 9; } } - if pos.y > 39 { + if pos.y > 157 { for b in &mut tiles { b.bg = 7; b.mid = 1; @@ -126,7 +126,7 @@ impl Chunk { } } // Unbreakable layer at bottom - if pos.y > 510 { + if pos.y > 780 { for b in &mut tiles { b.mid = Tile::UNBREAKANIUM; }