Add a surface/underground divide

This commit is contained in:
crumblingstatue 2023-04-03 12:41:27 +02:00
parent 6f1c351b5f
commit bb25d62561
4 changed files with 16 additions and 7 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

Before After
Before After

View file

@ -86,7 +86,7 @@ impl App {
}
fn do_rendering(&mut self) {
self.rw.clear(Color::BLACK);
self.rw.clear(Color::rgb(55, 221, 231));
self.game.draw_world(&mut self.rw, &self.res);
self.sf_egui
.do_frame(|ctx| {

View file

@ -4,7 +4,7 @@ use crate::{
graphics::{ScreenPos, ScreenPosScalar, NATIVE_RESOLUTION},
math::{wp_to_tp, WorldPos, WorldPosScalar},
res::Res,
world::{TilePos, World},
world::{Tile, TilePos, World},
};
pub struct GameState {
@ -16,7 +16,10 @@ impl GameState {
let mut s = Sprite::with_texture(&res.tile_atlas);
for_each_tile_on_screen(self.camera_offset, |tp, sp| {
let tile = self.world.tile_at_mut(tp);
s.set_texture_rect(Rect::new(tile.id as i32 * 32, 0, 32, 32));
if tile.id == Tile::AIR {
return;
}
s.set_texture_rect(Rect::new((tile.id - 1) as i32 * 32, 0, 32, 32));
s.set_position(sp.to_sf_vec());
rw.draw(&s);
});

View file

@ -28,7 +28,7 @@ impl World {
/// Loads or generates the containing chunk if necessary.
pub fn tile_at_mut(&mut self, pos: TilePos) -> &mut Tile {
let (chk, local) = pos.to_chunk_and_local();
let chk = self.chunks.entry(chk).or_insert_with(Chunk::new_rand);
let chk = self.chunks.entry(chk).or_insert_with(|| Chunk::gen(chk));
chk.at_mut(local)
}
}
@ -107,11 +107,13 @@ pub struct Chunk {
}
impl Chunk {
pub fn new_rand() -> Self {
pub fn gen(pos: ChunkPos) -> Self {
let mut rng = thread_rng();
let mut tiles = [Tile { id: 0 }; CHUNK_N_TILES];
for b in &mut tiles {
b.id = rng.gen_range(0..8);
if pos.y > 40 {
for b in &mut tiles {
b.id = rng.gen_range(1..5);
}
}
Self { tiles }
}
@ -127,3 +129,7 @@ type TileId = u16;
pub struct Tile {
pub id: TileId,
}
impl Tile {
pub const AIR: TileId = 0;
}