Skip to content

Commit

Permalink
#10 pipes in world!
Browse files Browse the repository at this point in the history
  • Loading branch information
justanothercell committed Jun 8, 2023
1 parent 86078cc commit f1c3ecd
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 13 deletions.
Binary file modified mods/world/assets/include/overlaymap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 45 additions & 2 deletions mods/world/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::client::materials::{WithGlow, WithTerrain};

use crate::common::{Chunk, CHUNK_SIZE, WorldView};
use crate::server::world::World;
use crate::tiles::Tile;
use crate::tiles::{Tile, FgTile};

use debug_mod::Debug;

Expand Down Expand Up @@ -103,10 +103,18 @@ impl WorldView for ClientWorld {
}
Nullable::Null
}

fn get_fg_tile_or_null(&self, pos: Vector2<i32>) -> Nullable<FgTile> {
if let ClientChunk::Chunk(chunk, _) = self.chunks.get(&Self::chunk(pos))? {
return Nullable::Value(chunk.get_fg_tile(Self::pos_in_chunk(pos)))
}
Nullable::Null
}
}

pub(crate) struct WorldHandle {
tile_sprites: SpriteSheet,
fg_tile_sprites: SpriteSheet,
}

impl WorldHandle {
Expand All @@ -116,12 +124,16 @@ impl WorldHandle {
Texture::from_bytes(include_bytes!("../../assets/include/tilemap.png")).unwrap(),
Vector2::new(16, 16)
).expect("error loading world spritesheet"),
fg_tile_sprites: SpriteSheet::from_texture(
Texture::from_bytes(include_bytes!("../../assets/include/overlaymap.png")).unwrap(),
Vector2::new(16, 16)
).expect("error loading world spritesheet"),
}
}

pub(crate) fn receive_chunk_data(&mut self, _messenger: &mut ClientMessenger, mut renderer: Nullable<&mut Renderer>, store: &mut DataStore, chunk: Chunk) {
let mut quads = vec![];
for (i, tile) in chunk.tiles().iter().enumerate() {
for (i, tile) in chunk.tiles.iter().enumerate() {
let index = tile.sprite_sheet_index();
if index == 0 {
continue;
Expand Down Expand Up @@ -152,6 +164,37 @@ impl WorldHandle {
quads.push(Block::Default(quad));
}
}
for (i, tile) in chunk.fg_tiles.iter().enumerate() {
let index = tile.sprite_sheet_index();
if index == 0 {
continue;
}

let x = (i % CHUNK_SIZE) as i32 + chunk.chunk_pos.x() * CHUNK_SIZE as i32;
let y = (i / CHUNK_SIZE) as i32 + chunk.chunk_pos.y() * CHUNK_SIZE as i32;

if index == Tile::Lamp as u16 {
let quad = Quad::with_glow_sprite(
Vector2::new(x as f32, y as f32),
Vector2::new(1.0, 1.0),
2,
self.tile_sprites.get(index as u32 - 1).unwrap(),
[0.9, 0.9, 0.7, 1.0]
);
quads.push(Block::add_glowing(quad, *renderer, store));
}
else {
let mut quad = Quad::with_terrain_sprite(
Vector2::new(x as f32, y as f32),
Vector2::new(1.0, 1.0),
0,
self.fg_tile_sprites.get(index as u32 - 1).unwrap(),
terrain_material(store)
);
renderer.add(&mut quad);
quads.push(Block::Default(quad));
}
}
store.mut_store::<ClientWorld>().chunks.insert(chunk.chunk_pos, ClientChunk::Chunk(chunk, quads));
}
}
Expand Down
30 changes: 23 additions & 7 deletions mods/world/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use aeonetica_engine::nanoserde::{SerBin, DeBin};
use aeonetica_engine::nanoserde;
use aeonetica_engine::math::vector::Vector2;
use aeonetica_engine::util::nullable::Nullable;
use crate::tiles::Tile;
use crate::tiles::{Tile, FgTile};

pub const CHUNK_SIZE: usize = 16;
pub const GRAVITY: f32 = -20.0;
Expand All @@ -21,22 +21,20 @@ pub enum Population {
pub struct Chunk {
pub population: Population,
pub chunk_pos: Vector2<i32>,
pub tiles: [Tile; CHUNK_SIZE*CHUNK_SIZE]
pub tiles: [Tile; CHUNK_SIZE*CHUNK_SIZE],
pub fg_tiles: [FgTile; CHUNK_SIZE*CHUNK_SIZE]
}

impl Chunk {
pub(crate) fn new(chunk_pos: Vector2<i32>) -> Self {
Self {
population: Population::Uninit,
chunk_pos,
tiles: [Tile::Wall; CHUNK_SIZE*CHUNK_SIZE]
tiles: [Tile::Wall; CHUNK_SIZE*CHUNK_SIZE],
fg_tiles: [FgTile::Empty; CHUNK_SIZE*CHUNK_SIZE]
}
}

pub(crate) fn tiles(&self) -> &[Tile; CHUNK_SIZE * CHUNK_SIZE] {
&self.tiles
}

pub fn get_tile(&self, pos: Vector2<i32>) -> Tile {
self.tiles[pos.y as usize * CHUNK_SIZE + pos.x as usize]
}
Expand All @@ -48,6 +46,18 @@ impl Chunk {
pub fn set_tile(&mut self, pos: Vector2<i32>, tile: Tile) {
self.tiles[pos.y as usize * CHUNK_SIZE + pos.x as usize] = tile
}

pub fn get_fg_tile(&self, pos: Vector2<i32>) -> FgTile {
self.fg_tiles[pos.y as usize * CHUNK_SIZE + pos.x as usize]
}

pub fn mut_fg_tile(&mut self, pos: Vector2<i32>) -> &mut FgTile {
&mut self.fg_tiles[pos.y as usize * CHUNK_SIZE + pos.x as usize]
}

pub fn set_fg_tile(&mut self, pos: Vector2<i32>, tile: FgTile) {
self.fg_tiles[pos.y as usize * CHUNK_SIZE + pos.x as usize] = tile
}
}

/// THis trait is used for both client and server.
Expand All @@ -71,6 +81,12 @@ pub trait WorldView {
fn get_tile(&self, pos: Vector2<i32>) -> Tile {
self.get_tile_or_null(pos).unwrap_or(Tile::Wall)
}
fn get_fg_tile_or_null(&self, pos: Vector2<i32>) -> Nullable<FgTile>;
/// Returns [`FgTile::Empty`] if not loaded
fn get_fg_tile(&self, pos: Vector2<i32>) -> FgTile {
self.get_fg_tile_or_null(pos).unwrap_or(FgTile::Empty)
}

fn is_loaded(&self, pos: Vector2<i32>) -> bool;

/// Returns [`true`] if the aabb bounding box collides with a tile.
Expand Down
21 changes: 19 additions & 2 deletions mods/world/src/server/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use aeonetica_engine::math::vector::Vector2;
use rand::{SeedableRng, Rng};
use crate::common::{CHUNK_SIZE, Population, Chunk, WorldView};
use crate::server::world::World;
use crate::tiles::Tile;
use crate::tiles::{Tile, FgTile};

pub(crate) struct GenProvider {
pub(crate) seed: u64,
Expand Down Expand Up @@ -53,6 +53,14 @@ impl World {
self.mut_init_chunk_at(World::chunk(pos), stage).set_tile(World::pos_in_chunk(pos), t)
}

pub fn get_init_fg_tile_at(&mut self, pos: Vector2<i32>, stage: Population) -> FgTile {
self.mut_init_chunk_at(World::chunk(pos), stage).get_fg_tile(World::pos_in_chunk(pos))
}

pub fn set_init_fg_tile_at(&mut self, pos: Vector2<i32>, stage: Population, t: FgTile) {
self.mut_init_chunk_at(World::chunk(pos), stage).set_fg_tile(World::pos_in_chunk(pos), t)
}

fn populate_terrain(&mut self, chunk_pos: Vector2<i32>) {
let gen = self.generator.clone();
let chunk = self.mut_chunk_at_raw(chunk_pos);
Expand Down Expand Up @@ -133,7 +141,16 @@ impl World {
for x in 3..(CHUNK_SIZE-3) as i32 {
for y in 3..(CHUNK_SIZE-3) as i32 {
self.set_init_tile_at(pos + Vector2::new(x, y), Population::TerrainPostProcess,
if (x == 3) as u8 + (y == 3) as u8 + (x == CHUNK_SIZE as i32 - 4) as u8 + (y == CHUNK_SIZE as i32 - 4) as u8 > 1 { Tile::Lamp } else { Tile::LabWall })
if (x == 3) as u8 + (y == 3) as u8 + (x == CHUNK_SIZE as i32 - 4) as u8 + (y == CHUNK_SIZE as i32 - 4) as u8 > 1 { Tile::QuarteredLamp } else { Tile::LabWall });
self.set_init_fg_tile_at(pos + Vector2::new(x, y), Population::TerrainPostProcess, match (x, y) {
(3, 3) => FgTile::PipeRD,
(3, 12) => FgTile::PipeRU,
(12, 3) => FgTile::PipeLD,
(12, 12) => FgTile::PipeLU,
(3|12, _) => FgTile::PipeUD,
(_, 3|12) => FgTile::PipeLR,
_ => FgTile::Empty
});
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion mods/world/src/server/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use aeonetica_server::ecs::module::Module;
use crate::client::WorldHandle;
use crate::common::{Chunk, Population, WorldView};
use crate::server::gen::GenProvider;
use crate::tiles::Tile;
use crate::tiles::{Tile, FgTile};

pub const WORLD: &str = "WORLD";

Expand Down Expand Up @@ -85,6 +85,14 @@ impl World {
self.mut_chunk_at(World::chunk(pos)).set_tile(World::pos_in_chunk(pos), t)
}

pub fn get_fg_tile_at(&mut self, pos: Vector2<i32>) -> FgTile {
self.get_chunk_at(World::chunk(pos)).get_fg_tile(World::pos_in_chunk(pos))
}

pub fn set_fg_tile_at(&mut self, pos: Vector2<i32>, t: FgTile) {
self.mut_chunk_at(World::chunk(pos)).set_fg_tile(World::pos_in_chunk(pos), t)
}

pub fn mut_chunk_at(&mut self, chunk_pos: Vector2<i32>) -> &mut Chunk {
self.mut_init_chunk_at(chunk_pos, Population::Finished)
}
Expand Down Expand Up @@ -152,6 +160,10 @@ impl World {
Nullable::Value(self.try_get_chunk_no_gen(World::chunk(pos))?.get_tile(World::pos_in_chunk(pos)))
}

pub fn try_get_fg_tile_no_gen(&self, pos: Vector2<i32>) -> Nullable<FgTile> {
Nullable::Value(self.try_get_chunk_no_gen(World::chunk(pos))?.get_fg_tile(World::pos_in_chunk(pos)))
}

pub fn try_get_chunk_no_gen(&self, chunk_pos: Vector2<i32>) -> Nullable<&Chunk> {
if self.cached_chunk_pos == chunk_pos {
return Nullable::Value(unsafe { &*(self.cached_chunk_raw_ptr as *const Chunk) })
Expand Down Expand Up @@ -205,6 +217,10 @@ impl WorldView for World {
fn get_tile_or_null(&self, pos: Vector2<i32>) -> Nullable<Tile> {
self.try_get_tile_no_gen(pos)
}

fn get_fg_tile_or_null(&self, pos: Vector2<i32>) -> Nullable<FgTile> {
self.try_get_fg_tile_no_gen(pos)
}
}

impl Module for World {
Expand Down
10 changes: 9 additions & 1 deletion mods/world/src/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ impl Tile {
}
}

pub enum ForegroundTile {
#[repr(u16)]
#[derive(Debug, Copy, Clone, SerBin, DeBin, PartialEq)]
pub enum FgTile {
Empty,
PipeEndL,
PipeLR,
Expand Down Expand Up @@ -56,4 +58,10 @@ pub enum ForegroundTile {
MetalFrameFloorR,
MetalFrameFloorMSupport,
Water
}

impl FgTile {
pub fn sprite_sheet_index(&self) -> u16 {
*self as u16
}
}

0 comments on commit f1c3ecd

Please sign in to comment.