World structs

This commit is contained in:
Piotrek
2021-05-05 14:38:12 +02:00
parent 89934ba97c
commit c04a1aea11
10 changed files with 79 additions and 41 deletions
+10 -12
View File
@@ -1,13 +1,13 @@
use winit::event::Event; use winit::event::Event;
use noise::Perlin; use noise::Perlin;
use crate::renderer::{camera::CameraTransform, Level}; use crate::renderer::{camera::CameraTransform, RenderSpace};
mod camera_controller; mod camera_controller;
use camera_controller::CameraController; use camera_controller::CameraController;
mod world_gen; mod world;
use world_gen::WorldGen; use world::World;
pub enum GameState { pub enum GameState {
Paused, Paused,
@@ -15,19 +15,17 @@ pub enum GameState {
} }
pub struct App { pub struct App {
world_gen: WorldGen, camera_controller: CameraController,
camera_controller: CameraController world: World
} }
impl App { impl App {
pub fn new(level: &mut dyn Level) -> Self { pub fn new(renderer: &mut dyn RenderSpace) -> Self {
let mut instance = Self { Self {
camera_controller: CameraController::new(), camera_controller: CameraController::new(),
world_gen: WorldGen::new() world: World::new("default".to_string())
}; }
//instance.world_gen.generate(renderer, &Perlin::new());
instance.world_gen.generate(level, &Perlin::new());
instance
} }
pub fn input(&mut self, event: &Event<()>) { pub fn input(&mut self, event: &Event<()>) {
-12
View File
@@ -1,12 +0,0 @@
struct World {
}
impl World {
pub fn new() -> Self {
Self {}
}
}
+12
View File
@@ -0,0 +1,12 @@
#[derive(Clone)]
pub struct WorldBlock {
pub data: [u8;32*32*32]
}
impl WorldBlock {
pub fn new() -> Self {
let data = [0_u8; 32*32*32];
Self { data }
}
}
+14
View File
@@ -0,0 +1,14 @@
use cgmath::Point3;
use crate::app::world::WorldBlock;
pub struct WorldChunk {
pos: Point3<u32>,
blocks: Box<[WorldBlock]>
}
impl WorldChunk {
pub fn new(pos: Point3<u32>) -> Self {
let blocks = vec![WorldBlock::new(); 16*16*16].into_boxed_slice();
Self { pos, blocks }
}
}
@@ -2,7 +2,7 @@ use cgmath::Point3;
use noise::{Perlin, NoiseFn}; use noise::{Perlin, NoiseFn};
use rand::{Rng, prelude::ThreadRng}; use rand::{Rng, prelude::ThreadRng};
use crate::renderer::Level; use crate::renderer::RenderSpace;
use crate::renderer::buffers::BLOCK_SIZE; use crate::renderer::buffers::BLOCK_SIZE;
const SCALE: f64 = 150.0; const SCALE: f64 = 150.0;
@@ -44,7 +44,7 @@ impl WorldGen {
/* /*
* Generate world * Generate world
*/ */
pub fn generate(&mut self, level: &mut dyn Level, noise: &Perlin) { pub fn generate(&mut self, renderer: &mut dyn RenderSpace, noise: &Perlin) {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let mut dirty: Vec<usize> = Vec::new(); let mut dirty: Vec<usize> = Vec::new();
@@ -57,7 +57,7 @@ impl WorldGen {
// Generate blocks // Generate blocks
for by in 0..bh { for by in 0..bh {
// Get // Get
let b = level.block_get(Point3{x:bx, y:by, z:bz}); let b = renderer.block_get(Point3{x:bx, y:by, z:bz});
dirty.push(b.id); dirty.push(b.id);
// Fill // Fill
for x in 0..BLOCK_SIZE { for x in 0..BLOCK_SIZE {
@@ -75,7 +75,7 @@ impl WorldGen {
} }
for id in dirty { for id in dirty {
level.block_dirty(id); renderer.block_dirty(id);
} }
} }
} }
+26
View File
@@ -0,0 +1,26 @@
mod generator;
use generator::WorldGen;
mod chunk;
use chunk::WorldChunk;
mod block;
use block::WorldBlock;
pub struct World {
name: String,
chunks: Vec<WorldChunk> // Loaded chunks
}
impl World {
pub fn new(name: String) -> Self {
Self { name, chunks: Vec::new() }
}
fn load(&mut self) {
}
fn save(&mut self) {
}
}
+6 -6
View File
@@ -4,12 +4,12 @@ use crate::renderer::{buffers, buffers::{BLOCK_SIZE, BLOCK_TEX_SIZE, BLOCK_TEX_L
#[derive(Clone)] #[derive(Clone)]
pub struct Block { pub struct RenderBlock {
pub id: usize, pub id: usize,
pub data: [u8;BLOCK_LEN] pub data: [u8;BLOCK_LEN]
} }
impl Block { impl RenderBlock {
pub fn new() -> Self { pub fn new() -> Self {
Self { id: 0, data: [0_u8;BLOCK_LEN] } Self { id: 0, data: [0_u8;BLOCK_LEN] }
} }
@@ -21,7 +21,7 @@ impl Block {
pub struct Blocks { pub struct Blocks {
blocks: Box<[Block]>, blocks: Box<[RenderBlock]>,
free_block: usize, free_block: usize,
texture: wgpu::Texture, texture: wgpu::Texture,
pub bind_layout: wgpu::BindGroupLayout, pub bind_layout: wgpu::BindGroupLayout,
@@ -90,7 +90,7 @@ impl Blocks {
); );
// Data // Data
let mut blocks = vec![Block::new(); BLOCK_ARR_LEN].into_boxed_slice(); let mut blocks = vec![RenderBlock::new(); BLOCK_ARR_LEN].into_boxed_slice();
for (i, b) in blocks.iter_mut().enumerate() { b.id = i; } for (i, b) in blocks.iter_mut().enumerate() { b.id = i; }
println!("Blocks size: {} MB, max: {}", BLOCK_TEX_LEN as f32 / 1024.0 / 1024.0, BLOCK_ARR_LEN); println!("Blocks size: {} MB, max: {}", BLOCK_TEX_LEN as f32 / 1024.0 / 1024.0, BLOCK_ARR_LEN);
@@ -122,14 +122,14 @@ impl Blocks {
); );
} }
pub fn get(&mut self, block_id: usize) -> &mut Block { pub fn get(&mut self, block_id: usize) -> &mut RenderBlock {
&mut self.blocks[block_id] &mut self.blocks[block_id]
} }
/* /*
* Allocates new block * Allocates new block
*/ */
pub fn alloc(&mut self) -> &mut Block { pub fn alloc(&mut self) -> &mut RenderBlock {
// Make sure we still have some free blocks // Make sure we still have some free blocks
if self.free_block >= BLOCK_ARR_LEN { panic!("No more free blocks! we should consider reusing blocks..."); } if self.free_block >= BLOCK_ARR_LEN { panic!("No more free blocks! we should consider reusing blocks..."); }
+1 -1
View File
@@ -20,7 +20,7 @@ pub use nodes::Nodes;
mod blocks; mod blocks;
pub use blocks::Blocks; pub use blocks::Blocks;
pub use blocks::Block; pub use blocks::RenderBlock;
mod uniform; mod uniform;
pub use uniform::Uniform; pub use uniform::Uniform;
+2 -2
View File
@@ -9,8 +9,8 @@ use passes::{RaytracePass, PostprocessPass};
pub mod camera; pub mod camera;
use camera::Camera; use camera::Camera;
pub mod level; pub mod renderspace;
pub use level::Level; pub use renderspace::RenderSpace;
pub struct Renderer { pub struct Renderer {
surface: wgpu::Surface, surface: wgpu::Surface,
@@ -2,12 +2,12 @@ use cgmath::Point3;
use crate::renderer::{Renderer, buffers}; use crate::renderer::{Renderer, buffers};
pub trait Level { pub trait RenderSpace {
fn block_get(&mut self, block_pos: Point3<usize>) -> &mut buffers::Block; fn block_get(&mut self, block_pos: Point3<usize>) -> &mut buffers::RenderBlock;
fn block_dirty(&mut self, id: usize); fn block_dirty(&mut self, id: usize);
} }
impl Level for Renderer { impl RenderSpace for Renderer {
/* /*
* Grabs existing or allocates new block for given position * Grabs existing or allocates new block for given position
@@ -15,7 +15,7 @@ impl Level for Renderer {
* block_pos: Position of the block in world, in block space * block_pos: Position of the block in world, in block space
* block_id: Block identifier, equal to Block.id * block_id: Block identifier, equal to Block.id
*/ */
fn block_get(&mut self, block_pos: Point3<usize>) -> &mut buffers::Block { fn block_get(&mut self, block_pos: Point3<usize>) -> &mut buffers::RenderBlock {
// Get node // Get node
let node_idx = block_pos.x + buffers::NODE_TEX_SIZE * (block_pos.y + buffers::NODE_TEX_SIZE * block_pos.z); let node_idx = block_pos.x + buffers::NODE_TEX_SIZE * (block_pos.y + buffers::NODE_TEX_SIZE * block_pos.z);
let node_value = self.buffers.node_buffer.nodes[node_idx]; let node_value = self.buffers.node_buffer.nodes[node_idx];