diff --git a/src/main.rs b/src/main.rs index c683f6b..14bb48c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ pub(crate) mod world; pub(crate) mod prefabs; use rendering::{Rederer, RenderTarget, BackgroundColor, clear_background}; +use world::GameWorld; use prefabs::load_prefabs; #[derive(Unique)] @@ -44,13 +45,14 @@ fn main() { //Create a shipyard world let world = World::new(); - //Init and load things + //Add systems and uniques, Init and load things world.add_unique_non_send_sync( Rederer::init(&event_loop) ); load_prefabs(&world); - - //Add systems and uniques + world.add_unique_non_send_sync( + GameWorld::new() + ); world.add_unique(BackgroundColor(vec3(0.5, 0.5, 1.))); world.add_unique(DeltaTime(Duration::default())); world.add_workload(update); diff --git a/src/prefabs.rs b/src/prefabs.rs index 8b33afe..f4c0532 100644 --- a/src/prefabs.rs +++ b/src/prefabs.rs @@ -1,6 +1,6 @@ use shipyard::{World, NonSendSync, UniqueView, Unique}; -use strum::{EnumIter, IntoEnumIterator}; -use glium::{texture::{SrgbTexture2dArray, RawImage2d}, backend::Facade, Program}; +use glium::{texture::SrgbTexture2dArray, Program}; +use strum::EnumIter; use crate::rendering::Rederer; mod texture; @@ -50,8 +50,6 @@ impl AssetPaths for BlockTextures { } } - - #[derive(Unique)] pub struct BlockTexturesPrefab(SrgbTexture2dArray); diff --git a/src/prefabs/shaders.rs b/src/prefabs/shaders.rs index 5f7ed07..97bffa6 100644 --- a/src/prefabs/shaders.rs +++ b/src/prefabs/shaders.rs @@ -1,8 +1,9 @@ -use glium::{Program, backend::Facade}; + macro_rules! include_shader_prefab { ($vert: literal, $frag: literal, $geom: literal, $facade: expr) => { { + use ::glium::Program; log::info!("↓↓↓ compiling shader prefab ↓↓↓"); log::info!("{} {} {}", $vert, $frag, $geom); Program::from_source( @@ -15,6 +16,7 @@ macro_rules! include_shader_prefab { }; ($vert: literal, $frag: literal, $facade: expr) => { { + use ::glium::Program; log::info!("↓↓↓ compiling shader prefab ↓↓↓"); log::info!("{} {}", $vert, $frag); Program::from_source( diff --git a/src/prefabs/texture.rs b/src/prefabs/texture.rs index 7bfda57..bb1415d 100644 --- a/src/prefabs/texture.rs +++ b/src/prefabs/texture.rs @@ -4,7 +4,10 @@ use std::{fs::File, path::PathBuf, io::BufReader}; use glium::{texture::{SrgbTexture2dArray, RawImage2d}, backend::Facade}; use super::AssetPaths; -pub fn load_texture2darray_prefab( +pub fn load_texture2darray_prefab< + T: AssetPaths + IntoEnumIterator, + E: Facade +>( directory: PathBuf, facade: &E ) -> SrgbTexture2dArray { diff --git a/src/world.rs b/src/world.rs index c780f13..c53434b 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,4 +1,5 @@ -use glam::IVec2; +use shipyard::Unique; +use glam::{IVec3, ivec3}; use hashbrown::HashMap; pub mod chunk; @@ -7,6 +8,98 @@ pub mod render; use chunk::Chunk; -pub struct World { - pub chunks: HashMap + +//TODO separate world struct for render data +// because this is not send-sync + + +pub struct AllChunksNeighbors<'a> { + pub center: &'a Chunk, + pub top: &'a Chunk, + pub bottom: &'a Chunk, + pub left: &'a Chunk, + pub right: &'a Chunk, + pub front: &'a Chunk, + pub back: &'a Chunk, +} +pub struct AllChunksNeighborsMut<'a> { + pub center: &'a mut Chunk, + pub top: &'a mut Chunk, + pub bottom: &'a mut Chunk, + pub left: &'a mut Chunk, + pub right: &'a mut Chunk, + pub front: &'a mut Chunk, + pub back: &'a mut Chunk, +} +pub struct ChunksNeighbors<'a> { + pub center: Option<&'a Chunk>, + pub top: Option<&'a Chunk>, + pub bottom: Option<&'a Chunk>, + pub left: Option<&'a Chunk>, + pub right: Option<&'a Chunk>, + pub front: Option<&'a Chunk>, + pub back: Option<&'a Chunk>, +} +impl<'a> ChunksNeighbors<'a> { + pub fn all(&self) -> Option> { + Some(AllChunksNeighbors { + center: self.center?, + top: self.top?, + bottom: self.bottom?, + left: self.left?, + right: self.right?, + front: self.front?, + back: self.back?, + }) + } +} + +#[derive(Default, Unique)] +pub struct GameWorld { + pub chunks: HashMap +} +impl GameWorld { + pub fn new() -> Self { + Self::default() + } + pub fn neighbors(&self, coords: IVec3) -> ChunksNeighbors { + ChunksNeighbors { + center: self.chunks.get(&coords), + top: self.chunks.get(&(coords - ivec3(0, 1, 0))), + bottom: self.chunks.get(&(coords + ivec3(0, 1, 0))), + left: self.chunks.get(&(coords - ivec3(1, 0, 0))), + right: self.chunks.get(&(coords + ivec3(1, 0, 0))), + front: self.chunks.get(&(coords - ivec3(0, 0, 1))), + back: self.chunks.get(&(coords + ivec3(0, 0, 1))), + } + } + pub fn neighbors_all(&self, coords: IVec3) -> Option { + self.neighbors(coords).all() + } + pub fn neighbors_all_mut(&mut self, coords: IVec3) -> Option { + let mut refs = self.chunks.get_many_mut([ + &coords, + &(coords - ivec3(0, 1, 0)), + &(coords + ivec3(0, 1, 0)), + &(coords - ivec3(1, 0, 0)), + &(coords + ivec3(1, 0, 0)), + &(coords - ivec3(0, 0, 1)), + &(coords + ivec3(0, 0, 1)), + ])?.map(Some); + Some(AllChunksNeighborsMut { + center: std::mem::take(&mut refs[0]).unwrap(), + top: std::mem::take(&mut refs[1]).unwrap(), + bottom: std::mem::take(&mut refs[2]).unwrap(), + left: std::mem::take(&mut refs[3]).unwrap(), + right: std::mem::take(&mut refs[4]).unwrap(), + front: std::mem::take(&mut refs[5]).unwrap(), + back: std::mem::take(&mut refs[6]).unwrap(), + }) + } +} + +fn update_world( + +) { + } diff --git a/src/world/mesh.rs b/src/world/mesh.rs new file mode 100644 index 0000000..e69de29