block breaking!

This commit is contained in:
griffi-gh 2023-01-28 22:06:08 +01:00
parent f3e8459968
commit c1cd48101b
8 changed files with 53 additions and 13 deletions

View file

@ -34,7 +34,7 @@ use rendering::{
use world::{ use world::{
init_game_world, init_game_world,
loading::update_loaded_world_around_player, loading::update_loaded_world_around_player,
raycast::update_raycasts raycast::{update_raycasts, break_block_test_only}
}; };
use player::spawn_player; use player::spawn_player;
use prefabs::load_prefabs; use prefabs::load_prefabs;
@ -65,6 +65,7 @@ fn update() -> Workload {
update_controllers, update_controllers,
update_loaded_world_around_player, update_loaded_world_around_player,
update_raycasts, update_raycasts,
break_block_test_only,
compute_cameras compute_cameras
).into_workload() ).into_workload()
} }

View file

@ -7,7 +7,9 @@ use glium::{
VertexBuffer, uniform, VertexBuffer, uniform,
DrawParameters, DrawParameters,
BackfaceCullingMode, BackfaceCullingMode,
PolygonMode, Blend, BlendingFunction, LinearBlendingFactor, Depth, DepthTest Blend, BlendingFunction,
LinearBlendingFactor,
Depth, DepthTest,
}; };
use crate::{ use crate::{
world::raycast::LookingAtBlock, world::raycast::LookingAtBlock,

View file

@ -1,4 +1,4 @@
use glam::{vec3a, Vec3A, Vec4Swizzles, Vec3}; use glam::Vec3;
use shipyard::{NonSendSync, UniqueView, UniqueViewMut, View, IntoIter}; use shipyard::{NonSendSync, UniqueView, UniqueViewMut, View, IntoIter};
use glium::{ use glium::{
implement_vertex, uniform, implement_vertex, uniform,

View file

@ -51,6 +51,16 @@ impl ChunkStorage {
.get(block.z as usize)?; .get(block.z as usize)?;
Some(*block) Some(*block)
} }
pub fn get_block_mut(&mut self, position: IVec3) -> Option<&mut Block> {
let (chunk, block) = Self::to_chunk_coords(position);
let block = self.chunks
.get_mut(&chunk)?
.block_data.as_mut()?
.blocks.get_mut(block.x as usize)?
.get_mut(block.y as usize)?
.get_mut(block.z as usize)?;
Some(block)
}
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }

View file

@ -18,7 +18,6 @@ impl ChunkData {
} }
pub struct ChunkMesh { pub struct ChunkMesh {
pub is_dirty: bool,
pub vertex_buffer: VertexBuffer<ChunkVertex>, pub vertex_buffer: VertexBuffer<ChunkVertex>,
pub index_buffer: IndexBuffer<u32>, pub index_buffer: IndexBuffer<u32>,
} }
@ -50,6 +49,7 @@ pub struct Chunk {
pub mesh_index: Option<usize>, pub mesh_index: Option<usize>,
pub current_state: CurrentChunkState, pub current_state: CurrentChunkState,
pub desired_state: DesiredChunkState, pub desired_state: DesiredChunkState,
pub dirty: bool,
} }
impl Chunk { impl Chunk {
pub fn new(position: IVec3) -> Self { pub fn new(position: IVec3) -> Self {
@ -59,6 +59,7 @@ impl Chunk {
mesh_index: None, mesh_index: None,
current_state: Default::default(), current_state: Default::default(),
desired_state: Default::default(), desired_state: Default::default(),
dirty: false,
} }
} }
} }

View file

@ -95,7 +95,7 @@ fn unload_downgrade_chunks(
false false
} else { } else {
match chunk.desired_state { match chunk.desired_state {
DesiredChunkState::Loaded if matches!(chunk.current_state, CurrentChunkState::Rendered | CurrentChunkState::CalculatingMesh) => { DesiredChunkState::Loaded if matches!(chunk.current_state, CurrentChunkState::Rendered | CurrentChunkState::CalculatingMesh | CurrentChunkState::RecalculatingMesh) => {
if let Some(mesh_index) = chunk.mesh_index { if let Some(mesh_index) = chunk.mesh_index {
vm_meshes.remove(mesh_index).unwrap(); vm_meshes.remove(mesh_index).unwrap();
} }
@ -133,7 +133,7 @@ fn start_required_tasks(
// =========== // ===========
//log::trace!("Started loading chunk {position}"); //log::trace!("Started loading chunk {position}");
}, },
DesiredChunkState::Rendered if chunk.current_state == CurrentChunkState::Loaded => { DesiredChunkState::Rendered if (chunk.current_state == CurrentChunkState::Loaded || chunk.dirty) => {
//get needed data //get needed data
let Some(neighbors) = world.neighbors_all(position) else { let Some(neighbors) = world.neighbors_all(position) else {
continue continue
@ -145,7 +145,12 @@ fn start_required_tasks(
task_manager.spawn_task(ChunkTask::GenerateMesh { data, position }); task_manager.spawn_task(ChunkTask::GenerateMesh { data, position });
//Update chunk state //Update chunk state
let chunk = world.chunks.get_mut(&position).unwrap(); let chunk = world.chunks.get_mut(&position).unwrap();
if chunk.dirty {
chunk.current_state = CurrentChunkState::RecalculatingMesh;
chunk.dirty = false;
} else {
chunk.current_state = CurrentChunkState::CalculatingMesh; chunk.current_state = CurrentChunkState::CalculatingMesh;
}
// =========== // ===========
//log::trace!("Started generating mesh for chunk {position}"); //log::trace!("Started generating mesh for chunk {position}");
} }
@ -201,7 +206,6 @@ fn process_completed_tasks(
let vertex_buffer = VertexBuffer::new(&renderer.display, &vertices).unwrap(); let vertex_buffer = VertexBuffer::new(&renderer.display, &vertices).unwrap();
let index_buffer = IndexBuffer::new(&renderer.display, PrimitiveType::TrianglesList, &indexes).unwrap(); let index_buffer = IndexBuffer::new(&renderer.display, PrimitiveType::TrianglesList, &indexes).unwrap();
let mesh_index = meshes.insert(ChunkMesh { let mesh_index = meshes.insert(ChunkMesh {
is_dirty: false,
vertex_buffer, vertex_buffer,
index_buffer, index_buffer,
}); });

View file

@ -1,6 +1,6 @@
use glam::{Vec3, IVec3}; use glam::{Vec3, IVec3};
use shipyard::{View, Component, ViewMut, IntoIter, UniqueView}; use shipyard::{View, Component, ViewMut, IntoIter, UniqueView, UniqueViewMut};
use crate::{player::MainPlayer, transform::Transform}; use crate::{player::MainPlayer, transform::Transform, input::Inputs};
use super::{ChunkStorage, block::Block}; use super::{ChunkStorage, block::Block};
@ -46,9 +46,31 @@ pub fn update_raycasts(
mut raycast: ViewMut<LookingAtBlock>, mut raycast: ViewMut<LookingAtBlock>,
world: UniqueView<ChunkStorage>, world: UniqueView<ChunkStorage>,
) { ) {
for (transform, report) in (transform.inserted_or_modified(), &mut raycast).iter() { //idk if this check is even needed
if !(world.is_inserted_or_modified() || (transform.inserted_or_modified(), &raycast).iter().next().is_some()) {
return
}
for (transform, report) in (&transform, &mut raycast).iter() {
let (_, rotation, position) = transform.0.to_scale_rotation_translation(); let (_, rotation, position) = transform.0.to_scale_rotation_translation();
let direction = rotation * Vec3::NEG_Z; let direction = rotation * Vec3::NEG_Z;
*report = LookingAtBlock(world.raycast(position, direction, Some(30.))); *report = LookingAtBlock(world.raycast(position, direction, Some(30.)));
} }
} }
pub fn break_block_test_only(
raycast: View<LookingAtBlock>,
input: UniqueView<Inputs>,
mut world: UniqueViewMut<ChunkStorage>
) {
if input.action_a {
//get raycast info
let Some(ray) = raycast.iter().next().unwrap().0 else { return };
//update block
let Some(block) = world.get_block_mut(ray.block_position) else { return };
*block = Block::Air;
//mark chunk as dirty
let (chunk_pos, _) = ChunkStorage::to_chunk_coords(ray.block_position);
let chunk = world.chunks.get_mut(&chunk_pos).unwrap();
chunk.dirty = true;
}
}