From a512fffcc4be75e149e9ec2d5730fd5e6067c881 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 26 Jan 2023 04:12:29 +0100 Subject: [PATCH] Chunk state downgrades --- src/camera.rs | 2 +- src/input.rs | 2 +- src/world/loading.rs | 18 ++++++++++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 61899e9..7551dea 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,4 +1,4 @@ -use glam::{Mat4, Vec3, EulerRot}; +use glam::{Mat4, Vec3}; use shipyard::{Component, ViewMut, View, IntoIter, Workload, IntoWorkload}; use std::f32::consts::PI; use crate::{transform::Transform, events::WindowResizedEvent}; diff --git a/src/input.rs b/src/input.rs index 498a0b8..cff8084 100644 --- a/src/input.rs +++ b/src/input.rs @@ -37,7 +37,7 @@ pub fn process_events( }; } }, - DeviceEvent::Button { button, state } => { + DeviceEvent::Button { button: _, state: _ } => { //log::debug!("Button {button} {state:?}"); }, _ => () diff --git a/src/world/loading.rs b/src/world/loading.rs index 0e5cbdf..61ef605 100644 --- a/src/world/loading.rs +++ b/src/world/loading.rs @@ -16,7 +16,7 @@ use super::{ pub fn update_loaded_world_around_player() -> Workload { ( update_chunks_if_player_moved, - unload_marked_chunks, + unload_downgrade_chunks, start_required_tasks, process_completed_tasks, ).into_workload() @@ -76,13 +76,14 @@ pub fn update_chunks_if_player_moved( } } -fn unload_marked_chunks( +fn unload_downgrade_chunks( mut vm_world: UniqueViewMut, mut vm_meshes: NonSendSync> ) { if !vm_world.is_modified() { return } + //TODO refactor this vm_world.chunks.retain(|_, chunk| { if chunk.desired_state == DesiredChunkState::ToUnload { if let Some(mesh_index) = chunk.mesh_index { @@ -90,6 +91,16 @@ fn unload_marked_chunks( } false } else { + match chunk.desired_state { + DesiredChunkState::Loaded if matches!(chunk.current_state, CurrentChunkState::Rendered | CurrentChunkState::CalculatingMesh) => { + if let Some(mesh_index) = chunk.mesh_index { + vm_meshes.remove(mesh_index).unwrap(); + } + chunk.mesh_index = None; + chunk.current_state = CurrentChunkState::Loaded; + }, + _ => (), + } true } }) @@ -99,6 +110,9 @@ fn start_required_tasks( task_manager: UniqueView, mut world: UniqueViewMut, ) { + if !world.is_modified() { + return + } //HACK: cant iterate over chunks.keys() or chunk directly! let hashmap_keys: Vec = world.chunks.keys().copied().collect(); for position in hashmap_keys {