From 5dea17984636e01c5b8f898de8906a3425a2fa02 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Thu, 25 Jul 2019 15:48:27 +0100 Subject: [PATCH] Added inventory sync messages and InventoryUpdate component --- client/src/lib.rs | 3 +++ common/src/comp/inventory/mod.rs | 10 +++++++++- common/src/comp/mod.rs | 2 +- common/src/msg/server.rs | 1 + common/src/state.rs | 2 ++ server/src/lib.rs | 17 +++++++++++++++++ 6 files changed, 33 insertions(+), 2 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 70da461859..1a7b0479e6 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -421,6 +421,9 @@ impl Client { self.state.write_component(entity, action_state); } } + ServerMsg::InventoryUpdate(inventory) => { + self.state.write_component(self.entity, inventory) + } ServerMsg::TerrainChunkUpdate { key, chunk } => { self.state.insert_chunk(key, *chunk); self.pending_chunks.remove(&key); diff --git a/common/src/comp/inventory/mod.rs b/common/src/comp/inventory/mod.rs index 3f3e0ba9f1..8e850918cb 100644 --- a/common/src/comp/inventory/mod.rs +++ b/common/src/comp/inventory/mod.rs @@ -4,7 +4,7 @@ pub mod item; // Reexports pub use self::item::Item; -use specs::{Component, HashMapStorage}; +use specs::{Component, NullStorage, HashMapStorage}; use specs_idvs::IDVStorage; #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -41,3 +41,11 @@ impl Default for Inventory { impl Component for Inventory { type Storage = HashMapStorage; } + +// ForceUpdate +#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] +pub struct InventoryUpdate; + +impl Component for InventoryUpdate { + type Storage = NullStorage; +} diff --git a/common/src/comp/mod.rs b/common/src/comp/mod.rs index 86a948dd21..20ec83c0d6 100644 --- a/common/src/comp/mod.rs +++ b/common/src/comp/mod.rs @@ -20,7 +20,7 @@ pub use controller::Controller; pub use inputs::{ Attacking, CanBuild, Gliding, Jumping, MoveDir, OnGround, Respawning, Rolling, Wielding, }; -pub use inventory::{item, Inventory}; +pub use inventory::{item, Inventory, InventoryUpdate}; pub use last::Last; pub use phys::{ForceUpdate, Ori, Pos, Vel}; pub use player::Player; diff --git a/common/src/msg/server.rs b/common/src/msg/server.rs index 26a0f4f248..8686e6f089 100644 --- a/common/src/msg/server.rs +++ b/common/src/msg/server.rs @@ -55,6 +55,7 @@ pub enum ServerMsg { entity: u64, action_state: comp::ActionState, }, + InventoryUpdate(comp::Inventory), TerrainChunkUpdate { key: Vec2, chunk: Box, diff --git a/common/src/state.rs b/common/src/state.rs index 9269d6e101..8934f484e3 100644 --- a/common/src/state.rs +++ b/common/src/state.rs @@ -142,6 +142,7 @@ impl State { ecs.register::(); ecs.register::(); ecs.register::(); + ecs.register::(); // Register client-local components ecs.register::(); @@ -156,6 +157,7 @@ impl State { ecs.register::(); ecs.register::(); ecs.register::(); + ecs.register::(); ecs.register::(); // Controller effects ecs.register::(); diff --git a/server/src/lib.rs b/server/src/lib.rs index b467289571..9ee66db5a4 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -191,6 +191,7 @@ impl Server { state.write_component(entity, comp::Vel(Vec3::zero())); state.write_component(entity, comp::Ori(Vec3::unit_y())); state.write_component(entity, comp::ActionState::default()); + state.write_component(entity, comp::Inventory::default()); // Make sure physics are accepted. state.write_component(entity, comp::ForceUpdate); @@ -919,11 +920,27 @@ impl Server { } } + // Sync inventories + for (entity, inventory, _) in ( + &self.state.ecs().entities(), + &self.state.ecs().read_storage::(), + &self.state.ecs().read_storage::(), + ) + .join() + { + self.clients + .notify(entity, ServerMsg::InventoryUpdate(inventory.clone())); + } + // Remove all force flags. self.state .ecs_mut() .write_storage::() .clear(); + self.state + .ecs_mut() + .write_storage::() + .clear(); } pub fn generate_chunk(&mut self, key: Vec2) {