Add background and foreground tile layers

This commit is contained in:
crumblingstatue 2023-04-03 21:44:08 +02:00
parent 1f0cd8050c
commit ccdacbfa45
4 changed files with 37 additions and 15 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

Before After
Before After

View file

@ -98,8 +98,8 @@ impl App {
.move_y(self.game.player.vspeed, |player_en, off| {
let mut col = false;
for_each_tile_on_screen(self.game.camera_offset, |tp, _sp| {
let tid = self.game.world.tile_at_mut(tp).id;
if tid == Tile::AIR {
let tid = self.game.world.tile_at_mut(tp).mid;
if tid == Tile::EMPTY {
return;
}
let tsize = TILE_SIZE as i32;
@ -119,8 +119,8 @@ impl App {
.move_x(self.game.player.hspeed, |player_en, off| {
let mut col = false;
for_each_tile_on_screen(self.game.camera_offset, |tp, _sp| {
let tid = self.game.world.tile_at_mut(tp).id;
if tid == Tile::AIR {
let tid = self.game.world.tile_at_mut(tp).mid;
if tid == Tile::EMPTY {
return;
}
let tsize = TILE_SIZE as i32;
@ -147,7 +147,9 @@ impl App {
imm!("Mouse down at {}, {}", wpos.x, wpos.y);
let tpos = wpos.tile_pos();
imm_dbg!(tpos);
self.game.world.tile_at_mut(tpos).id = 0;
let t = self.game.world.tile_at_mut(tpos);
t.mid = 0;
t.fg = 0;
}
}

View file

@ -22,12 +22,19 @@ 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);
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);
if tile.bg != Tile::EMPTY {
s.set_texture_rect(Rect::new((tile.bg - 1) as i32 * 32, 0, 32, 32));
rw.draw(&s);
}
if tile.mid != Tile::EMPTY {
s.set_texture_rect(Rect::new((tile.mid - 1) as i32 * 32, 0, 32, 32));
rw.draw(&s);
}
if tile.fg != Tile::EMPTY {
s.set_texture_rect(Rect::new((tile.fg - 1) as i32 * 32, 0, 32, 32));
rw.draw(&s);
}
});
}
pub fn draw_entities(&mut self, rw: &mut RenderWindow) {

View file

@ -109,16 +109,24 @@ pub struct Chunk {
impl Chunk {
pub fn gen(pos: ChunkPos) -> Self {
let mut rng = thread_rng();
let mut tiles = [Tile { id: 0 }; CHUNK_N_TILES];
let mut tiles = [Tile {
bg: 0,
mid: 0,
fg: 0,
}; CHUNK_N_TILES];
if pos.y > 38 {
for b in &mut tiles {
b.id = rng.gen_range(1..5);
b.bg = 7;
b.mid = rng.gen_range(1..5);
if rng.gen_bool(0.1) {
b.fg = 6;
}
}
}
// Unbreakable layer at bottom
if pos.y > 510 {
for b in &mut tiles {
b.id = Tile::UNBREAKANIUM;
b.mid = Tile::UNBREAKANIUM;
}
}
Self { tiles }
@ -133,10 +141,15 @@ type TileId = u16;
#[derive(Clone, Copy)]
pub struct Tile {
pub id: TileId,
/// Background wall behind entities
pub bg: TileId,
/// The solid wall on the same level as entities
pub mid: TileId,
/// A layer on top of the mid wall. Usually ores or decorative pieces.
pub fg: TileId,
}
impl Tile {
pub const AIR: TileId = 0;
pub const EMPTY: TileId = 0;
pub const UNBREAKANIUM: TileId = 5;
}