Adjust code to new 100,000 block limit

This commit is contained in:
crumblingstatue 2023-04-05 00:53:08 +02:00
parent 30dbddeaff
commit 462eede90f
3 changed files with 29 additions and 15 deletions

View file

@ -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 {

View file

@ -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,

View file

@ -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;
}