Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFelidae committed Dec 15, 2024
1 parent 4524a94 commit c97d7be
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 64 deletions.
66 changes: 33 additions & 33 deletions src/client/renderer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
use bevy::{
app::{App, Plugin, Startup, Update}, asset::{AssetServer, Assets, Handle}, color::{palettes::css::WHEAT, Color}, math::Vec3, pbr::{MeshMaterial3d, StandardMaterial}, prelude::{AlphaMode, Camera3d, Commands, Component, Entity, EventWriter, Mesh, Mesh3d, Query, Res, ResMut, Transform, With}
app::{App, Plugin, Startup, Update},
asset::{AssetServer, Assets, Handle},
color::{palettes::css::WHEAT, Color},
math::Vec3,
pbr::{MeshMaterial3d, StandardMaterial},
prelude::{
AlphaMode, Camera3d, Commands, Component, Entity, EventWriter, Mesh, Mesh3d, Query, Res,
ResMut, Transform, With,
},
};
use bevy_meshem::{
prelude::{introduce_adjacent_chunks, mesh_grid, Face::{Back, Bottom, Forward, Left, Right, Top}, MeshMD, MeshingAlgorithm},
prelude::{
introduce_adjacent_chunks, mesh_grid,
Face::{Back, Bottom, Forward, Left, Right, Top},
MeshMD, MeshingAlgorithm,
},
Dimensions, VoxelRegistry,
};

use crate::{
data::world::{World, WorldChunk, WorldChunkStatus, WorldChunkStorage},
game::{registry::BlockRegistry, world_generator::{GameWorld, GenerateWorldSignal}},
game::{
registry::BlockRegistry,
world_generator::{GameWorld, GenerateWorldSignal},
},
};

#[derive(Component)]
struct WorldRendererChunk {
pub position: (i32, i32, i32),
pub meta: MeshMD<<BlockRegistry as VoxelRegistry>::Voxel>,
pub mesh: Handle<Mesh>
pub mesh: Handle<Mesh>,
}

#[derive(Component)]
Expand Down Expand Up @@ -44,14 +59,7 @@ impl Plugin for WorldRenderer {
}
}

fn adjacent_add(
x: i32,
y: i32,
z: i32,

world: &GameWorld,
block_registry: &BlockRegistry,
) {
fn adjacent_add(x: i32, y: i32, z: i32, world: &GameWorld, block_registry: &BlockRegistry) {
let direction_table = {
[
(0, 0, 1),
Expand All @@ -62,30 +70,20 @@ fn adjacent_add(
(-1, 0, 0),
]
};
let face_table = {
[
Right,
Left,
Top,
Bottom,
Forward,
Back,
]
};
let face_table = { [Right, Left, Top, Bottom, Forward, Back] };

for i in 0..6 {
let direction = direction_table[i];
let face = face_table[i];


}
}

fn sys_setup(mut commands: Commands,
fn sys_setup(
mut commands: Commands,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
) {
// let texture_handle = asset_server.load("textures/block/default_cobble.png");
// let texture_handle = asset_server.load("textures/block/default_cobble.png");
let cobble = materials.add(Color::Srgba(WHEAT));

let mut renderer = WorldRenderer::default();
Expand Down Expand Up @@ -123,11 +121,14 @@ fn sys_update(
renderer.chunks.retain(|c| c.position != chunk.1.position);
}
}

// Identify chunks that should be loaded in by x y z
for x in camera_grid_x - renderer.render_distance..camera_grid_x + renderer.render_distance {
for y in camera_grid_y - renderer.render_distance..camera_grid_y + renderer.render_distance {
for z in camera_grid_z - renderer.render_distance..camera_grid_z + renderer.render_distance {
for y in camera_grid_y - renderer.render_distance..camera_grid_y + renderer.render_distance
{
for z in
camera_grid_z - renderer.render_distance..camera_grid_z + renderer.render_distance
{
let mut found = false;
// Check if chunk is already loaded (in ECS)
for chunk in render_chunks.iter_mut() {
Expand All @@ -137,9 +138,8 @@ fn sys_update(
}
}


if !found {
match world.map.chunk_at(x, y,z) {
match world.map.chunk_at(x, y, z) {
crate::data::world::WorldChunkStatus::Stored(stored) => {
let r = stored.read().unwrap();
if r.is_loaded() {
Expand Down Expand Up @@ -174,7 +174,7 @@ fn sys_update(
z as f32 * WorldChunk::SIZE as f32,
)),
MeshMaterial3d(renderer.material.clone()),
new_chunk
new_chunk,
));
}
None => {
Expand All @@ -185,7 +185,7 @@ fn sys_update(
}
WorldChunkStatus::Unloaded => {
continue;
},
}
}
}
}
Expand Down
19 changes: 8 additions & 11 deletions src/data/world.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{
cell::RefCell, fmt::{Display, Formatter}, sync::{Arc, Mutex, RwLock}
cell::RefCell,
fmt::{Display, Formatter},
sync::{Arc, Mutex, RwLock},
};

use bevy::prelude::{Component, Resource};
Expand Down Expand Up @@ -101,23 +103,19 @@ impl WorldChunkStorage {
pub struct WorldChunkCoordinate {
pub x: i32,
pub y: i32,
pub z: i32
pub z: i32,
}

impl WorldChunkCoordinate {
pub fn new(x: i32, y: i32, z: i32) -> Self {
Self {
x,
y,
z
}
Self { x, y, z }
}

pub fn from_world(x: f32, y: f32, z: f32) -> Self {
Self {
x: (x / 16.0).floor() as i32,
y: (y / 16.0).floor() as i32,
z: (z / 16.0).floor() as i32
z: (z / 16.0).floor() as i32,
}
}
}
Expand Down Expand Up @@ -165,13 +163,13 @@ impl SimplePerlinGenerator {

impl WorldGenerator for SimplePerlinGenerator {
fn generate_chunk(&self, w_x: i32, w_y: i32, w_z: i32) -> WorldChunkStorage {
// let begin = std::time::Instant::now();
// let begin = std::time::Instant::now();
let mut chunk = WorldChunk::new();
let mut empty = true;

// Precompute scaling factor once to avoid repeating it
let scale_factor = 1.0 / 64.0;

// Precompute the world coordinates
let w_x = w_x as f64;
let w_y = w_y as f64;
Expand Down Expand Up @@ -223,7 +221,6 @@ impl WorldGenerator for SimplePerlinGenerator {
/* In-memory World Implementation */
/* -------------------------------------------------------------------------- */


pub struct MemoryWorldData {
pub chunks: Vec<(i32, i32, i32, Arc<RwLock<WorldChunkStorage>>)>,
}
Expand Down
5 changes: 4 additions & 1 deletion src/game/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use bevy::{
app::{App, Startup, Update}, prelude::Event, transform::systems, DefaultPlugins
app::{App, Startup, Update},
prelude::Event,
transform::systems,
DefaultPlugins,
};
use bevy_flycam::PlayerPlugin;
use world_generator::{GenerateWorldSignal, WorldGeneratorPlugin};
Expand Down
29 changes: 19 additions & 10 deletions src/game/world_generator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::sync::{Arc, Mutex, RwLock};

use bevy::{app::{App, Plugins, Startup, Update}, prelude::{Commands, Component, Event, EventReader, EventWriter, Query, Res}, tasks::AsyncComputeTaskPool};
use bevy::{
app::{App, Plugins, Startup, Update},
prelude::{Commands, Component, Event, EventReader, EventWriter, Query, Res},
tasks::AsyncComputeTaskPool,
};
use rayon::iter::{IntoParallelIterator, ParallelBridge, ParallelIterator};

use crate::data::world::{self, MemoryWorld, SimplePerlinGenerator, World, WorldGenerator};
Expand All @@ -11,9 +15,7 @@ pub mod vis;
/* Plugin */
/* -------------------------------------------------------------------------- */

pub struct WorldGeneratorPlugin {

}
pub struct WorldGeneratorPlugin {}

impl Default for WorldGeneratorPlugin {
fn default() -> Self {
Expand All @@ -34,7 +36,6 @@ impl bevy::prelude::Plugin for WorldGeneratorPlugin {
/* Events */
/* -------------------------------------------------------------------------- */


// TODO: There is a better way to do this without using an event
#[derive(Event, Debug, Clone)]
pub struct GenerateWorldSignal {
Expand Down Expand Up @@ -111,7 +112,7 @@ pub fn sys_update(

let mut requests: Arc<RwLock<Vec<GenerateWorldSignal>>> = Arc::new(RwLock::new(Vec::new()));

// Concurrently check if a chunk is loaded up to 8 chunks in each direction,
// Concurrently check if a chunk is loaded up to 8 chunks in each direction,
// and if not, submit a load request
let par_iter = (-5..5).into_par_iter();
par_iter.for_each(|x| {
Expand All @@ -136,7 +137,11 @@ pub fn sys_update(
}

// Store last user position
world.prev_user_position = (camera_translation.x, camera_translation.y, camera_translation.z);
world.prev_user_position = (
camera_translation.x,
camera_translation.y,
camera_translation.z,
);
}

fn task_generate_chunk(
Expand All @@ -146,7 +151,7 @@ fn task_generate_chunk(
world: &GameWorld,
generator: &SimplePerlinGenerator,
) {
let chunk = generator.generate_chunk(y*16, z*16, x*16);
let chunk = generator.generate_chunk(y * 16, z * 16, x * 16);
world.map.add_chunk(chunk, x, y, z);
}

Expand All @@ -156,8 +161,12 @@ pub fn sys_generate_chunk(
) {
let world = world.single();
use rayon::prelude::*;
let par_iter = ev_generate_world.read().into_iter().par_bridge().into_par_iter();
let par_iter = ev_generate_world
.read()
.into_iter()
.par_bridge()
.into_par_iter();
par_iter.for_each(|signal| {
task_generate_chunk(signal.x, signal.y, signal.z, world, &world.generator);
});
}
}
13 changes: 4 additions & 9 deletions src/game/world_generator/vis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ pub struct WorldObserver {
pub status: WorldObserverStatus,
}

pub fn sys_setup() {

}
pub fn sys_setup() {}

#[inline]
fn poke_chunk(world: &dyn World) -> bool {
Expand All @@ -31,11 +29,8 @@ pub fn sys_update(
) {
for (entity, observer) in query.iter_mut() {
match observer.status {
WorldObserverStatus::NeedsRefresh => {

},
WorldObserverStatus::FromPosition(pos) => {
},
WorldObserverStatus::NeedsRefresh => {}
WorldObserverStatus::FromPosition(pos) => {}
}
}
}
}

0 comments on commit c97d7be

Please sign in to comment.