diff --git "a/assets/textures/\346\243\215\345\255\220.png" "b/assets/textures/\346\243\215\345\255\220.png" new file mode 100644 index 0000000..e9eaed0 Binary files /dev/null and "b/assets/textures/\346\243\215\345\255\220.png" differ diff --git "a/assets/textures/\350\213\271\346\236\234.png" "b/assets/textures/\350\213\271\346\236\234.png" new file mode 100644 index 0000000..1fc23bf Binary files /dev/null and "b/assets/textures/\350\213\271\346\236\234.png" differ diff --git "a/assets/textures/\350\213\271\346\236\234\345\217\266\345\255\220.png" "b/assets/textures/\350\213\271\346\236\234\345\217\266\345\255\220.png" index 51d0c99..eac3399 100644 Binary files "a/assets/textures/\350\213\271\346\236\234\345\217\266\345\255\220.png" and "b/assets/textures/\350\213\271\346\236\234\345\217\266\345\255\220.png" differ diff --git a/src/server/async_chunk.rs b/src/server/async_chunk.rs index eac319a..2520bcf 100644 --- a/src/server/async_chunk.rs +++ b/src/server/async_chunk.rs @@ -154,14 +154,18 @@ 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 { - // 物体时被打下来了 - if let Some(staff) = staff_info_stroge.voxel_to_staff(old_voxel) { - fill_event.send(ObjectFillEvent { - chunk_key, - xyz: pos, - center, - staff: staff.clone(), - }); + // 物体时被打下来了 这里通过配置掉落 + if let Some(staff_list) = + staff_info_stroge.voxel_to_staff_list(old_voxel) + { + for staff in staff_list.into_iter() { + fill_event.send(ObjectFillEvent { + chunk_key, + xyz: pos, + center, + staff: staff, + }); + } } } // 4. 判断 并更新codiller 存在的情况下才更新 diff --git a/src/staff/mod.rs b/src/staff/mod.rs index 5d15603..3a907af 100644 --- a/src/staff/mod.rs +++ b/src/staff/mod.rs @@ -5,6 +5,7 @@ use bevy::{ }, utils::HashMap, }; +use rand::Rng; use serde::{Deserialize, Serialize}; use crate::voxel_world::voxel::Voxel; @@ -41,9 +42,15 @@ pub enum StaffType { pub struct StaffInfoStroge { pub data: HashMap, pub voxel_staff: HashMap, + // 灵活的 体素和物品掉落的关系 + pub filled_map: HashMap, } impl StaffInfoStroge { + fn register_filled(&mut self, fill_mate: FilledMeta) { + self.filled_map + .insert(fill_mate.voxel_id, fill_mate.clone()); + } fn register(&mut self, staff: Staff) { if self.data.contains_key(&staff.id) { warn!("{} is already registered", staff.id); @@ -57,6 +64,38 @@ impl StaffInfoStroge { pub fn voxel_to_staff(&self, voxel: Voxel) -> Option<&Staff> { self.voxel_staff.get(&voxel.id) } + + // 通过体素获取掉落物 + pub fn voxel_to_staff_list(&self, voxel: Voxel) -> Option> { + let mut ret: Vec = Vec::new(); + let voxle_id = voxel.id; + if let Some(mate) = self.filled_map.get(&(voxle_id as usize)) { + for FilledPair { + possible, + staff_id, + times, + } in mate.filled_config.iter() + { + if let Some(staff) = self.get(*staff_id) { + for _ in 0..*times { + let mut rng = rand::thread_rng(); + if rng.gen_bool(*possible as f64) { + ret.push(staff.clone()); + } + } + } + } + } else { + if let Some(staff) = self.voxel_staff.get(&voxel.id) { + ret.push(staff.clone()); + } + } + if ret.len() > 0 { + return Some(ret); + } + None + } + // 通过 物品点 获取物品id pub fn get(&self, staff_id: usize) -> Option { self.data.get(&staff_id).map(|a| a.clone()) @@ -76,6 +115,7 @@ impl Plugin for StaffInfoPlugin { app.insert_resource(StaffInfoStroge { data: HashMap::default(), voxel_staff: HashMap::default(), + filled_map: HashMap::default(), }); app.add_systems(Startup, setup.in_set(StaffSet::Init)); } @@ -93,6 +133,7 @@ impl Plugin for ServerStaffInfoPlugin { app.insert_resource(StaffInfoStroge { data: HashMap::default(), voxel_staff: HashMap::default(), + filled_map: HashMap::default(), }); app.add_systems(Startup, server_setup.in_set(StaffSet::Init)); } @@ -110,9 +151,25 @@ pub struct StaffMeta { staff_type: StaffType, } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct FilledMeta { + // 体素类型的id + voxel_id: usize, + filled_config: Vec, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct FilledPair { + possible: f32, + staff_id: usize, + times: usize, +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct StaffConfigs { pub configs: Vec, + // 掉落物的特性 + pub filled_configs: Vec, } fn load_staff_configs( @@ -141,6 +198,9 @@ fn load_staff_configs( }); } } + for mate in res.filled_configs { + staff_info_stroge.register_filled(mate); + } } Err(_) => { error!("读取Staff配置数据失败"); diff --git a/staff.ron b/staff.ron index 70c7384..5b02c00 100644 --- a/staff.ron +++ b/staff.ron @@ -10,5 +10,15 @@ (id:7,name:"BlueGrass",icon_string:"textures/苍翠地.png",staff_type:Voxel((id:9))), (id:8,name:"AppleWood",icon_string:"textures/苹果树A面.png",staff_type:Voxel((id:10))), (id:9,name:"AppleLeaf",icon_string:"textures/苹果叶子.png",staff_type:Voxel((id:11))), - ] + (id:10,name:"Apple",icon_string:"textures/苹果.png",staff_type:Consumable(0)), + (id:11,name:"AppleLog",icon_string:"textures/棍子.png",staff_type:Consumable(0)), + ], + // 掉落配置 + filled_configs:[ + // 叶子有 20% 概率掉 一个苹果 + (voxel_id:11,filled_config:[ + (possible: 0.2,staff_id: 10,times: 1), + (possible: 0.6,staff_id: 11,times: 2), + ]), + ], ) \ No newline at end of file