From 299c3fa3766481fd530395014752fc1d13058370 Mon Sep 17 00:00:00 2001 From: zhouzihao <1042181618@qq.com> Date: Mon, 18 Sep 2023 01:15:27 +0800 Subject: [PATCH] =?UTF-8?q?fix-=E4=BF=AE=E5=A4=8D=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E6=89=93=E8=90=BD=E7=89=B9=E6=AE=8A=E7=B1=BB=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 +- src/client/sp_mesh_display.rs | 35 ++++++++++++++++++++++++++++++----- src/server/async_chunk.rs | 2 +- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e19897..001dc15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1239,7 +1239,7 @@ dependencies = [ [[package]] name = "bevy_vox_mesh" version = "0.7.0" -source = "git+https://github.com/zzhgithub/bevy_vox_mesh.git?branch=fix#ffb776c61c5fa9c1500c4fff36b2f8b465e59216" +source = "git+https://github.com/zzhgithub/bevy_vox_mesh.git?branch=fix#3d80c16be22b3b3198d7a760bf2cdba0f2c9d8e1" dependencies = [ "anyhow", "bevy", diff --git a/src/client/sp_mesh_display.rs b/src/client/sp_mesh_display.rs index 45a41b9..8bece55 100644 --- a/src/client/sp_mesh_display.rs +++ b/src/client/sp_mesh_display.rs @@ -6,7 +6,8 @@ use bevy::{ prelude::{ - Assets, Color, Commands, Entity, OnExit, PbrBundle, Plugin, Res, ResMut, Resource, + in_state, Assets, Color, Commands, Component, DespawnRecursiveExt, Entity, + IntoSystemConfigs, OnExit, PbrBundle, Plugin, Query, Res, ResMut, Resource, StandardMaterial, Transform, Update, Vec3, }, tasks::{AsyncComputeTaskPool, Task}, @@ -42,6 +43,8 @@ pub struct SpMeshTasks { pub tasks: Vec>, } +#[derive(Debug, Component)] +pub struct NeedDelete; pub struct SpMeshManagerPlugin; impl Plugin for SpMeshManagerPlugin { @@ -52,7 +55,13 @@ impl Plugin for SpMeshManagerPlugin { app.insert_resource(SpMeshTasks { tasks: Vec::new() }); app.add_systems( Update, - (sp_mesh_tasks_update, deal_sp_mesh_tasks, despawn_sp_mesh), + ( + sp_mesh_tasks_update, + deal_sp_mesh_delete, + deal_sp_mesh_tasks, + despawn_sp_mesh, + ) + .run_if(in_state(GameState::Game)), ); app.add_systems(OnExit(GameState::Game), unset_all); } @@ -114,10 +123,13 @@ fn sp_mesh_tasks_update( } // 有一批要删除的数据 for del_key in old_keys.iter() { + println!("要删除的数据{:?}", old_keys); if let Some(index_voxels_entity_map) = sp_mesh_manager.entities.get_mut(&chunk_key) { if let Some((_, entity)) = index_voxels_entity_map.remove(del_key) { - commands.entity(entity).despawn(); + println!("删除的entity[{:?}]", entity); + commands.entity(entity).insert(NeedDelete); + println!("删除了mesh{}", del_key); } } } @@ -125,6 +137,12 @@ fn sp_mesh_tasks_update( } } +fn deal_sp_mesh_delete(mut commands: Commands, mesh_query: Query) { + for entity in mesh_query.iter() { + commands.entity(entity).despawn_recursive(); + } +} + fn deal_sp_mesh_tasks( mut commands: Commands, mut sp_mesh_tasks: ResMut, @@ -154,8 +172,15 @@ fn deal_sp_mesh_tasks( )))) .id(); if let Some(inner_map) = sp_mesh_manager.entities.get_mut(&chunk_key) { - inner_map.insert(index as u32, (v, entity)); + if !inner_map.contains_key(&(index as u32)) { + println!("创建的entity[{:?}]", entity); + inner_map.insert(index as u32, (v, entity)); + } else { + // 这要处理不需要的数据! + commands.entity(entity).despawn(); + } } else { + println!("第一次创建的entity[{:?}]", entity); let mut map = HashMap::new(); map.insert(index as u32, (v, entity)); sp_mesh_manager.entities.insert(chunk_key, map); @@ -189,7 +214,7 @@ fn despawn_sp_mesh( if let Some(inner_map) = sp_mesh_manager.entities.remove(&key) { println!("要清理的数据{}", inner_map.len()); for (_, (_, entity)) in inner_map { - commands.entity(entity).despawn(); + commands.entity(entity).despawn_recursive(); } } } diff --git a/src/server/async_chunk.rs b/src/server/async_chunk.rs index dae295d..3d03cf2 100644 --- a/src/server/async_chunk.rs +++ b/src/server/async_chunk.rs @@ -163,7 +163,7 @@ pub fn deal_chunk_query_system( // FIXME: 这里要考虑把代码格式简化 一下 // 发送物体被打下来的消息 old_voxel chunk_key, pos, 还原物体的位置! if old_voxel.id != Voxel::EMPTY.id && voxel_type.id == Voxel::EMPTY.id { - println!("cube被打下来了: {:?}", voxel_type); + println!("cube被打下来了: {:?}", old_voxel); // 物体时被打下来了 这里通过配置掉落 if let Some(staff_list) = staff_info_stroge.voxel_to_staff_list(old_voxel)