Skip to content

Commit

Permalink
dev-添加了一次产生多个的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhgithub committed Sep 8, 2023
1 parent 6202305 commit b2dfb9e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/client/message_def/staff_rule_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ use serde::{Deserialize, Serialize};
pub struct StaffRuleMessage {
pub staff_rule_id: u32,
pub need: Vec<(usize, usize, usize)>,
pub times:usize,
}
5 changes: 4 additions & 1 deletion src/client/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use bevy::prelude::{
AssetServer, Commands, Handle, Image, IntoSystemConfigs, Plugin, Res, Resource, Startup,
};
use bevy_egui::EguiContexts;
use bevy_egui::{egui, EguiContexts};

use crate::staff::{StaffInfoStroge, StaffSet};

use self::staff_rules::MyMemory;

// 这里是尝试管理自定义UI的
pub mod staff_rules;
pub mod test;
Expand All @@ -24,6 +26,7 @@ pub struct UiResourcePlugin;
impl Plugin for UiResourcePlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.add_systems(Startup, init_egui_resource.after(StaffSet::Init));
app.insert_resource(MyMemory(egui::Memory::default()));
}
}

Expand Down
28 changes: 24 additions & 4 deletions src/client/ui/staff_rules.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// 合成相关UI
use bevy::{
prelude::{Entity, Query, Res, ResMut, With},
prelude::{Entity, Query, Res, ResMut, Resource, With},
window::{PrimaryWindow, Window},
};
use bevy_easy_localize::Localize;
use bevy_egui::{
egui::{self, Align2, Vec2},
egui::{self, Align2, Id, Vec2},
EguiContext, EguiUserTextures,
};
use bevy_renet::renet::RenetClient;
Expand All @@ -21,6 +21,9 @@ use crate::{

use super::tool_bar::ToolBar;

#[derive(Debug, Resource)]
pub struct MyMemory(pub egui::Memory);

pub fn staff_rules_ui(
mut q: Query<
(
Expand All @@ -37,6 +40,7 @@ pub fn staff_rules_ui(
staff_info_stroge: Res<StaffInfoStroge>,
mut client: ResMut<RenetClient>,
localize: Res<Localize>,
mut memory: ResMut<MyMemory>,
) {
// 这里显示合成列表
if let Ok((_, ctx, _)) = q.get_single_mut() {
Expand All @@ -59,7 +63,7 @@ pub fn staff_rules_ui(
egui::ScrollArea::horizontal().show(ui, |ui| {
let table = TableBuilder::new(ui)
.striped(true)
.resizable(false)
.resizable(true)
.cell_layout(egui::Layout::left_to_right(egui::Align::Center))
.column(Column::auto())
.column(Column::auto())
Expand Down Expand Up @@ -136,9 +140,23 @@ pub fn staff_rules_ui(
}
});
row.col(|ui| {
// 这里数字框
let num = memory
.0
.data
.get_temp_mut_or(Id::new(ele.id), 1);

if ui.button("-").clicked() && *num > 1 {
*num -= 1;
}
ui.label(format!("{}", num));
if ui.button("+").clicked() && *num < 999 {
*num += 1;
}
if let Some(needed) = can_make_by_staff(
ele.clone(),
&tool_bar_data,
num.clone(),
) {
if ui.button("合成").clicked() {
// 判断使用按钮
Expand All @@ -147,6 +165,7 @@ pub fn staff_rules_ui(
&StaffRuleMessage {
staff_rule_id: ele.id.clone(),
need: needed,
times: num.clone(),
},
)
.unwrap();
Expand All @@ -171,11 +190,12 @@ pub fn staff_rules_ui(
fn can_make_by_staff(
staff_rule: StaffRule<u32>,
toolbar: &ToolBar,
num: usize,
) -> Option<Vec<(usize, usize, usize)>> {
// 需要的
let mut needed: Vec<(usize, usize, usize)> = Vec::new();
for pair in staff_rule.input {
if let Some(rs) = toolbar.need_staff(pair.staff_id, pair.num_needed) {
if let Some(rs) = toolbar.need_staff(pair.staff_id, pair.num_needed * num) {
needed.append(&mut rs.clone());
} else {
return None;
Expand Down
3 changes: 2 additions & 1 deletion src/server/staff_rule_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub fn deal_with_staff_rule(
let StaffRuleMessage {
staff_rule_id,
need,
times,
} = bincode::deserialize(&message).unwrap();
if let Some(rule) = staff_rules.rules.get(&staff_rule_id) {
if let Some(entity) = lobby.players.get(&client_id) {
Expand All @@ -53,7 +54,7 @@ pub fn deal_with_staff_rule(
} in rule.output.clone()
{
if let Some(out_staff) = staff_info_stroge.get(staff_id) {
for _ in 0..num_needed {
for _ in 0..num_needed * times {
let center = trf.translation;
let (chunk_key, xyz) = vec3_to_chunk_key_any_xyz(center);
// 生成新的掉落物
Expand Down

0 comments on commit b2dfb9e

Please sign in to comment.