Skip to content

Commit

Permalink
dev-掉落物品配置和随机掉落支撑
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhgithub committed Sep 9, 2023
1 parent b1d8391 commit 918eeda
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
Binary file added assets/textures/苹果.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 12 additions & 8 deletions src/server/async_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 存在的情况下才更新
Expand Down
60 changes: 60 additions & 0 deletions src/staff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bevy::{
},
utils::HashMap,
};
use rand::Rng;
use serde::{Deserialize, Serialize};

use crate::voxel_world::voxel::Voxel;
Expand Down Expand Up @@ -41,9 +42,15 @@ pub enum StaffType {
pub struct StaffInfoStroge {
pub data: HashMap<usize, Staff>,
pub voxel_staff: HashMap<u8, Staff>,
// 灵活的 体素和物品掉落的关系
pub filled_map: HashMap<usize, FilledMeta>,
}

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);
Expand All @@ -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<Vec<Staff>> {
let mut ret: Vec<Staff> = 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<Staff> {
self.data.get(&staff_id).map(|a| a.clone())
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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<FilledPair>,
}

#[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<StaffMeta>,
// 掉落物的特性
pub filled_configs: Vec<FilledMeta>,
}

fn load_staff_configs(
Expand Down Expand Up @@ -141,6 +198,9 @@ fn load_staff_configs(
});
}
}
for mate in res.filled_configs {
staff_info_stroge.register_filled(mate);
}
}
Err(_) => {
error!("读取Staff配置数据失败");
Expand Down
10 changes: 9 additions & 1 deletion staff.ron
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@
(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)),
],
// 掉落配置
filled_configs:[
// 叶子有 20% 概率掉 一个苹果
(voxel_id:11,filled_config:[
(possible: 0.2,staff_id: 10,times: 1),
]),
],
)

0 comments on commit 918eeda

Please sign in to comment.