Skip to content

Commit

Permalink
Spawn chunks in spiral
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyliann committed Dec 15, 2022
1 parent fe940d5 commit 73d6e72
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::mesh;
use crate::voxel_data::{CHUNK_SIZE, WORLD_SIZE_IN_CHUNKS};
use crate::voxel_map;
use crate::mesh;
use crate::world::{ChunkCoord, ChunkMap};
use bevy::prelude::*;

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ pub const WIDTH: f32 = 1920.0;

mod block_types;
mod chunk;
mod mesh;
mod voxel_data;
mod voxel_map;
mod world;
mod mesh;

fn main() {
App::new()
Expand Down
3 changes: 1 addition & 2 deletions src/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::voxel_data::{CHUNK_SIZE, FACE_CHECKS, INDICES, NORMALS, VERTICES};
use crate::voxel_map;
use crate::world::{ChunkCoord, WORLD_HEIGHT, WORLD_SIZE};
use bevy::log::info_span;
use bevy::prelude::{Mesh, ResMut, Vec2, Vec3};
use bevy::render::mesh::{self, PrimitiveTopology};
use itertools::iproduct;
use crate::voxel_data::{CHUNK_SIZE, FACE_CHECKS, INDICES, NORMALS, VERTICES};

use super::block_types;
use super::world;
Expand Down Expand Up @@ -42,7 +42,6 @@ pub fn create_mesh(chunk_pos: &ChunkCoord, voxel_map: &mut ResMut<voxel_map::Vox
shifted_global_z + face_check.z as i32 + z as i32,
voxel_map,
) {

for position in VERTICES[i].iter() {
positions
.push((*position + Vec3::new(x as f32, y as f32, z as f32)).to_array());
Expand Down
2 changes: 1 addition & 1 deletion src/voxel_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::prelude::Vec3;
pub const CHUNK_SIZE: usize = 32;
pub const WORLD_SIZE_IN_CHUNKS: usize = 32;
pub const WORLD_HEIGHT_IN_CHUNKS: usize = 5;
pub const RENDER_DISTANCE: usize = 4;
pub const RENDER_DISTANCE: usize = 8;

pub const VERTICES: [[Vec3; 4]; 6] = [
[
Expand Down
5 changes: 2 additions & 3 deletions src/voxel_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ impl VoxelMap {
for y in shifted_y as i32 - 1..(shifted_y + CHUNK_SIZE) as i32 + 1 {
if y < WORLD_HEIGHT as i32 && y >= 0 {
if y < 50 {
self.voxels
[[global_x as usize, y as usize, global_z as usize]] = 5;
self.voxels[[global_x as usize, y as usize, global_z as usize]] = 5;
counter += 1;
}
match (y as usize).cmp(&threshold) {
Expand Down Expand Up @@ -87,6 +86,6 @@ impl VoxelMap {
}
}
}
counter == CHUNK_SIZE*CHUNK_SIZE*CHUNK_SIZE
counter == CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE
}
}
54 changes: 38 additions & 16 deletions src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,44 @@ pub const TEXTURE_ATLAS_SIZE_IN_BLOCKS: u8 = 16;
pub const NORMALIZED_BLOCK_TEXTURE_SIZE: f32 = 1.0 / TEXTURE_ATLAS_SIZE_IN_BLOCKS as f32;

pub fn spawn_world(mut chunk_queue: ResMut<ChunkToGenerateQueue>) {
for (x, y, z) in iproduct!(
(RENDER_DISTANCE as isize * -1..RENDER_DISTANCE as isize),
(0..WORLD_HEIGHT_IN_CHUNKS),
(RENDER_DISTANCE as isize * -1..RENDER_DISTANCE as isize)
) {
let chunk_pos = ChunkCoord {
x: x as i32,
y: y as i32,
z: z as i32,
};
chunk_queue.0.push(chunk_pos);
// spawn chunks in spiral starting from 0.0 https://stackoverflow.com/a/398302
let render_square = (2 * RENDER_DISTANCE).pow(2);
let mut x = 0;
let mut z = 0;
let mut dx = 0;
let mut dz = -1;
let mut chunks = Vec::new();

for _ in 0..render_square {
if x > -1 * RENDER_DISTANCE as isize
&& x <= RENDER_DISTANCE as isize
&& z > -1 * RENDER_DISTANCE as isize
&& x <= RENDER_DISTANCE as isize
{
chunks.push((x, z));
}

if x == z || (x < 0 && x == -z) || (x > 0 && x == 1 - z) {
(dx, dz) = (-dz, dx);
}
(x, z) = (x + dx, z + dz);
}

chunks.reverse();

for (x, z) in chunks {
for y in 0..WORLD_HEIGHT_IN_CHUNKS {
let chunk_pos = ChunkCoord {
x: x as i32,
y: y as i32,
z: z as i32,
};
chunk_queue.0.push(chunk_pos);
}
}
}

pub fn generate_chunk (
pub fn generate_chunk(
mut chunk_to_generate_queue: ResMut<ChunkToGenerateQueue>,
mut chunk_to_spawn_queue: ResMut<ChunkToSpawnQueue>,
mut voxel_map: ResMut<VoxelMap>,
Expand All @@ -36,7 +59,7 @@ pub fn generate_chunk (
mut generated_chunks: ResMut<GeneratedChunks>,
) {
if chunk_to_generate_queue.0.len() > 0 {
let chunk_pos = chunk_to_generate_queue.0.pop().unwrap();
let chunk_pos = chunk_to_generate_queue.0.pop().unwrap();
if chunk_map.0[[
(chunk_pos.x + WORLD_SIZE_IN_CHUNKS as i32 / 2) as usize,
chunk_pos.y as usize,
Expand Down Expand Up @@ -65,7 +88,7 @@ pub fn spawn_chunk(
mut voxel_map: ResMut<VoxelMap>,
mut chunk_map: ResMut<ChunkMap>,
mut chunk_to_spawn_queue: ResMut<ChunkToSpawnQueue>,
){
) {
while let Some((chunk_pos, is_full)) = chunk_to_spawn_queue.0.pop() {
let _span = info_span!("Chunk spawn").entered();
Chunk::new(
Expand Down Expand Up @@ -168,7 +191,7 @@ pub struct ChunkToSpawnQueue(pub Vec<(ChunkCoord, bool)>);

pub struct GeneratedChunks {
pub chunks:
[[[(bool, bool); WORLD_SIZE_IN_CHUNKS]; WORLD_HEIGHT_IN_CHUNKS]; WORLD_SIZE_IN_CHUNKS],
[[[(bool, bool); WORLD_SIZE_IN_CHUNKS]; WORLD_HEIGHT_IN_CHUNKS]; WORLD_SIZE_IN_CHUNKS],
}

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -211,4 +234,3 @@ impl ChunkMap {
))
}
}

0 comments on commit 73d6e72

Please sign in to comment.