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

@ -111,8 +111,8 @@ const fn ij2k<const I: usize, const J: usize>() -> usize {
I * (9 - I) / 2 + J - 1
}
fn intersection<const A: usize, const B: usize, const C: usize>(planes: &[Vec4; PLANE_COUNT], crosses: &[Vec3A; PLANE_COMBINATIONS]) -> Vec3A {
let d = Vec3A::from(planes[A]).dot(crosses[ij2k::<B, C>()]);
let res = Mat3A::from_cols(
let d = Vec3A::from(planes[A]).dot(crosses[ij2k::<B, C>()]);
let res = Mat3A::from_cols(
crosses[ij2k::<B, C>()],
-crosses[ij2k::<A, C>()],
crosses[ij2k::<A, B>()],

View file

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

View file

@ -7,7 +7,9 @@ use glium::{
VertexBuffer, uniform,
DrawParameters,
BackfaceCullingMode,
PolygonMode, Blend, BlendingFunction, LinearBlendingFactor, Depth, DepthTest
Blend, BlendingFunction,
LinearBlendingFactor,
Depth, DepthTest,
};
use crate::{
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 glium::{
implement_vertex, uniform,

View file

@ -51,6 +51,16 @@ impl ChunkStorage {
.get(block.z as usize)?;
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 {
Self::default()
}

View file

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

View file

@ -95,7 +95,7 @@ fn unload_downgrade_chunks(
false
} else {
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 {
vm_meshes.remove(mesh_index).unwrap();
}
@ -133,7 +133,7 @@ fn start_required_tasks(
// ===========
//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
let Some(neighbors) = world.neighbors_all(position) else {
continue
@ -145,7 +145,12 @@ fn start_required_tasks(
task_manager.spawn_task(ChunkTask::GenerateMesh { data, position });
//Update chunk state
let chunk = world.chunks.get_mut(&position).unwrap();
chunk.current_state = CurrentChunkState::CalculatingMesh;
if chunk.dirty {
chunk.current_state = CurrentChunkState::RecalculatingMesh;
chunk.dirty = false;
} else {
chunk.current_state = CurrentChunkState::CalculatingMesh;
}
// ===========
//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 index_buffer = IndexBuffer::new(&renderer.display, PrimitiveType::TrianglesList, &indexes).unwrap();
let mesh_index = meshes.insert(ChunkMesh {
is_dirty: false,
vertex_buffer,
index_buffer,
});

View file

@ -1,6 +1,6 @@
use glam::{Vec3, IVec3};
use shipyard::{View, Component, ViewMut, IntoIter, UniqueView};
use crate::{player::MainPlayer, transform::Transform};
use shipyard::{View, Component, ViewMut, IntoIter, UniqueView, UniqueViewMut};
use crate::{player::MainPlayer, transform::Transform, input::Inputs};
use super::{ChunkStorage, block::Block};
@ -46,9 +46,31 @@ pub fn update_raycasts(
mut raycast: ViewMut<LookingAtBlock>,
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 direction = rotation * Vec3::NEG_Z;
*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;
}
}