Inventory manipulations are now only for input from the client, and are no longer directly sent as a server event. Slot manipulations do that instead.

This commit is contained in:
Sam 2021-02-08 13:55:50 -05:00
parent 1c83c5ee6f
commit e033fe6bee
6 changed files with 61 additions and 27 deletions

View File

@ -600,9 +600,14 @@ impl Client {
}
pub fn use_slot(&mut self, slot: Slot) {
self.send_msg(ClientGeneral::ControlEvent(ControlEvent::InventoryManip(
InventoryManip::Use(slot),
)));
match slot {
Slot::Equip(equip) => {
self.control_action(ControlAction::LoadoutManip(LoadoutManip::Use(equip)))
},
Slot::Inventory(inv) => self.send_msg(ClientGeneral::ControlEvent(
ControlEvent::InventoryManip(InventoryManip::Use(inv)),
)),
}
}
pub fn swap_slots(&mut self, a: Slot, b: Slot) {
@ -610,9 +615,11 @@ impl Client {
(Slot::Equip(equip), slot) | (slot, Slot::Equip(equip)) => {
self.control_action(ControlAction::LoadoutManip(LoadoutManip::Swap(equip, slot)))
},
_ => self.send_msg(ClientGeneral::ControlEvent(ControlEvent::InventoryManip(
InventoryManip::Swap(a, b),
))),
(Slot::Inventory(inv1), Slot::Inventory(inv2)) => {
self.send_msg(ClientGeneral::ControlEvent(ControlEvent::InventoryManip(
InventoryManip::Swap(inv1, inv2),
)))
},
}
}
@ -621,9 +628,9 @@ impl Client {
Slot::Equip(equip) => {
self.control_action(ControlAction::LoadoutManip(LoadoutManip::Drop(equip)))
},
_ => self.send_msg(ClientGeneral::ControlEvent(ControlEvent::InventoryManip(
InventoryManip::Drop(slot),
))),
Slot::Inventory(inv) => self.send_msg(ClientGeneral::ControlEvent(
ControlEvent::InventoryManip(InventoryManip::Drop(inv)),
)),
}
}

View File

@ -1,6 +1,6 @@
use crate::{
comp::{
inventory::slot::{EquipSlot, Slot},
inventory::slot::{EquipSlot, InvSlotId, Slot},
BuffKind,
},
uid::Uid,
@ -17,6 +17,23 @@ pub const DEFAULT_HOLD_DURATION: Duration = Duration::from_millis(200);
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum InventoryManip {
Pickup(Uid),
Collect(Vec3<i32>),
Use(InvSlotId),
Swap(InvSlotId, InvSlotId),
Drop(InvSlotId),
CraftRecipe(String),
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum LoadoutManip {
Use(EquipSlot),
Swap(EquipSlot, Slot),
Drop(EquipSlot),
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum SlotManip {
Pickup(Uid),
Collect(Vec3<i32>),
Use(Slot),
@ -25,21 +42,31 @@ pub enum InventoryManip {
CraftRecipe(String),
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum LoadoutManip {
Swap(EquipSlot, Slot),
Drop(EquipSlot),
}
impl From<LoadoutManip> for InventoryManip {
impl From<LoadoutManip> for SlotManip {
fn from(loadout_manip: LoadoutManip) -> Self {
match loadout_manip {
LoadoutManip::Use(equip) => Self::Use(Slot::Equip(equip)),
LoadoutManip::Swap(equip, slot) => Self::Swap(Slot::Equip(equip), slot),
LoadoutManip::Drop(equip) => Self::Drop(Slot::Equip(equip)),
}
}
}
impl From<InventoryManip> for SlotManip {
fn from(inv_manip: InventoryManip) -> Self {
match inv_manip {
InventoryManip::Pickup(pickup) => Self::Pickup(pickup),
InventoryManip::Collect(collect) => Self::Collect(collect),
InventoryManip::Use(inv) => Self::Use(Slot::Inventory(inv)),
InventoryManip::Swap(inv1, inv2) => {
Self::Swap(Slot::Inventory(inv1), Slot::Inventory(inv2))
},
InventoryManip::Drop(inv) => Self::Drop(Slot::Inventory(inv)),
InventoryManip::CraftRecipe(recipe) => Self::CraftRecipe(recipe),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum GroupManip {
Invite(Uid),

View File

@ -46,7 +46,7 @@ pub use chat::{
};
pub use controller::{
Climb, ControlAction, ControlEvent, Controller, ControllerInputs, GroupManip, Input,
InventoryManip, LoadoutManip, MountState, Mounting,
InventoryManip, LoadoutManip, MountState, Mounting, SlotManip,
};
pub use energy::{Energy, EnergyChange, EnergySource};
pub use group::Group;

View File

@ -46,7 +46,7 @@ pub enum ServerEvent {
entity: EcsEntity,
cause: comp::HealthSource,
},
InventoryManip(EcsEntity, comp::InventoryManip),
InventoryManip(EcsEntity, comp::SlotManip),
GroupManip(EcsEntity, comp::GroupManip),
Respawn(EcsEntity),
Shoot {

View File

@ -99,7 +99,7 @@ impl<'a> System<'a> for Sys {
}
},
ControlEvent::InventoryManip(manip) => {
server_emitter.emit(ServerEvent::InventoryManip(entity, manip));
server_emitter.emit(ServerEvent::InventoryManip(entity, manip.into()));
},
ControlEvent::GroupManip(manip) => {
server_emitter.emit(ServerEvent::GroupManip(entity, manip))

View File

@ -37,7 +37,7 @@ pub fn snuff_lantern(storage: &mut WriteStorage<comp::LightEmitter>, entity: Ecs
#[allow(clippy::blocks_in_if_conditions)]
#[allow(clippy::same_item_push)] // TODO: Pending review in #587
pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::InventoryManip) {
pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::SlotManip) {
let state = server.state_mut();
let mut dropped_items = Vec::new();
let mut thrown_items = Vec::new();
@ -60,7 +60,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
};
match manip {
comp::InventoryManip::Pickup(uid) => {
comp::SlotManip::Pickup(uid) => {
let picked_up_item: Option<comp::Item>;
let item_entity = if let (Some((item, item_entity)), Some(mut inv)) = (
state
@ -133,7 +133,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
state.write_component(entity, event);
},
comp::InventoryManip::Collect(pos) => {
comp::SlotManip::Collect(pos) => {
let block = state.terrain().get(pos).ok().copied();
if let Some(block) = block {
@ -204,7 +204,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
}
},
comp::InventoryManip::Use(slot) => {
comp::SlotManip::Use(slot) => {
let mut inventories = state.ecs().write_storage::<comp::Inventory>();
let mut inventory = if let Some(inventory) = inventories.get_mut(entity) {
inventory
@ -405,7 +405,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
}
},
comp::InventoryManip::Swap(a, b) => {
comp::SlotManip::Swap(a, b) => {
let ecs = state.ecs();
if let Some(pos) = ecs.read_storage::<comp::Pos>().get(entity) {
@ -429,7 +429,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
);
},
comp::InventoryManip::Drop(slot) => {
comp::SlotManip::Drop(slot) => {
let item = match slot {
Slot::Inventory(slot) => state
.ecs()
@ -462,7 +462,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
);
},
comp::InventoryManip::CraftRecipe(recipe) => {
comp::SlotManip::CraftRecipe(recipe) => {
if let Some(mut inv) = state
.ecs()
.write_storage::<comp::Inventory>()