World structs
This commit is contained in:
+10
-12
@@ -1,13 +1,13 @@
|
||||
use winit::event::Event;
|
||||
use noise::Perlin;
|
||||
|
||||
use crate::renderer::{camera::CameraTransform, Level};
|
||||
use crate::renderer::{camera::CameraTransform, RenderSpace};
|
||||
|
||||
mod camera_controller;
|
||||
use camera_controller::CameraController;
|
||||
|
||||
mod world_gen;
|
||||
use world_gen::WorldGen;
|
||||
mod world;
|
||||
use world::World;
|
||||
|
||||
pub enum GameState {
|
||||
Paused,
|
||||
@@ -15,19 +15,17 @@ pub enum GameState {
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
world_gen: WorldGen,
|
||||
camera_controller: CameraController
|
||||
camera_controller: CameraController,
|
||||
world: World
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new(level: &mut dyn Level) -> Self {
|
||||
let mut instance = Self {
|
||||
pub fn new(renderer: &mut dyn RenderSpace) -> Self {
|
||||
Self {
|
||||
camera_controller: CameraController::new(),
|
||||
world_gen: WorldGen::new()
|
||||
};
|
||||
|
||||
instance.world_gen.generate(level, &Perlin::new());
|
||||
instance
|
||||
world: World::new("default".to_string())
|
||||
}
|
||||
//instance.world_gen.generate(renderer, &Perlin::new());
|
||||
}
|
||||
|
||||
pub fn input(&mut self, event: &Event<()>) {
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
|
||||
|
||||
struct World {
|
||||
|
||||
}
|
||||
|
||||
impl World {
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
@@ -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 }
|
||||
}
|
||||
}
|
||||
@@ -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 rand::{Rng, prelude::ThreadRng};
|
||||
|
||||
use crate::renderer::Level;
|
||||
use crate::renderer::RenderSpace;
|
||||
use crate::renderer::buffers::BLOCK_SIZE;
|
||||
|
||||
const SCALE: f64 = 150.0;
|
||||
@@ -44,7 +44,7 @@ impl WorldGen {
|
||||
/*
|
||||
* 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 dirty: Vec<usize> = Vec::new();
|
||||
|
||||
@@ -57,7 +57,7 @@ impl WorldGen {
|
||||
// Generate blocks
|
||||
for by in 0..bh {
|
||||
// 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);
|
||||
// Fill
|
||||
for x in 0..BLOCK_SIZE {
|
||||
@@ -75,7 +75,7 @@ impl WorldGen {
|
||||
}
|
||||
|
||||
for id in dirty {
|
||||
level.block_dirty(id);
|
||||
renderer.block_dirty(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,12 @@ use crate::renderer::{buffers, buffers::{BLOCK_SIZE, BLOCK_TEX_SIZE, BLOCK_TEX_L
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Block {
|
||||
pub struct RenderBlock {
|
||||
pub id: usize,
|
||||
pub data: [u8;BLOCK_LEN]
|
||||
}
|
||||
|
||||
impl Block {
|
||||
impl RenderBlock {
|
||||
pub fn new() -> Self {
|
||||
Self { id: 0, data: [0_u8;BLOCK_LEN] }
|
||||
}
|
||||
@@ -21,7 +21,7 @@ impl Block {
|
||||
|
||||
|
||||
pub struct Blocks {
|
||||
blocks: Box<[Block]>,
|
||||
blocks: Box<[RenderBlock]>,
|
||||
free_block: usize,
|
||||
texture: wgpu::Texture,
|
||||
pub bind_layout: wgpu::BindGroupLayout,
|
||||
@@ -90,7 +90,7 @@ impl Blocks {
|
||||
);
|
||||
|
||||
// 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; }
|
||||
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]
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
if self.free_block >= BLOCK_ARR_LEN { panic!("No more free blocks! we should consider reusing blocks..."); }
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ pub use nodes::Nodes;
|
||||
|
||||
mod blocks;
|
||||
pub use blocks::Blocks;
|
||||
pub use blocks::Block;
|
||||
pub use blocks::RenderBlock;
|
||||
|
||||
mod uniform;
|
||||
pub use uniform::Uniform;
|
||||
|
||||
+2
-2
@@ -9,8 +9,8 @@ use passes::{RaytracePass, PostprocessPass};
|
||||
pub mod camera;
|
||||
use camera::Camera;
|
||||
|
||||
pub mod level;
|
||||
pub use level::Level;
|
||||
pub mod renderspace;
|
||||
pub use renderspace::RenderSpace;
|
||||
|
||||
pub struct Renderer {
|
||||
surface: wgpu::Surface,
|
||||
|
||||
@@ -2,12 +2,12 @@ use cgmath::Point3;
|
||||
use crate::renderer::{Renderer, buffers};
|
||||
|
||||
|
||||
pub trait Level {
|
||||
fn block_get(&mut self, block_pos: Point3<usize>) -> &mut buffers::Block;
|
||||
pub trait RenderSpace {
|
||||
fn block_get(&mut self, block_pos: Point3<usize>) -> &mut buffers::RenderBlock;
|
||||
fn block_dirty(&mut self, id: usize);
|
||||
}
|
||||
|
||||
impl Level for Renderer {
|
||||
impl RenderSpace for Renderer {
|
||||
|
||||
/*
|
||||
* 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_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
|
||||
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];
|
||||
Reference in New Issue
Block a user