Added item drops

This commit is contained in:
Joshua Barretto 2019-07-26 22:01:41 +01:00
parent b3cae2f3dd
commit 995090d2d4
5 changed files with 22 additions and 5 deletions

View File

@ -1,4 +1,4 @@
use specs::Component;
use specs::{Component, FlaggedStorage};
use specs_idvs::IDVStorage;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
@ -62,5 +62,5 @@ impl Default for Item {
}
impl Component for Item {
type Storage = IDVStorage<Self>;
type Storage = FlaggedStorage<Self, IDVStorage<Self>>;
}

View File

@ -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, InventoryUpdate};
pub use inventory::{item, Inventory, InventoryUpdate, Item};
pub use last::Last;
pub use phys::{ForceUpdate, Ori, Pos, Vel};
pub use player::Player;

View File

@ -25,6 +25,7 @@ sphynx::sum_type! {
CanBuild(comp::CanBuild),
Stats(comp::Stats),
LightEmitter(comp::LightEmitter),
Item(comp::Item),
}
}
// Automatically derive From<T> for EcsCompPhantom
@ -40,6 +41,7 @@ sphynx::sum_type! {
CanBuild(PhantomData<comp::CanBuild>),
Stats(PhantomData<comp::Stats>),
LightEmitter(PhantomData<comp::LightEmitter>),
Item(PhantomData<comp::Item>),
}
}
impl sphynx::CompPacket for EcsCompPacket {

View File

@ -131,6 +131,7 @@ impl State {
ecs.register_synced::<comp::Stats>();
ecs.register_synced::<comp::CanBuild>();
ecs.register_synced::<comp::LightEmitter>();
ecs.register_synced::<comp::Item>();
// Register components send from clients -> server
ecs.register::<comp::Controller>();

View File

@ -419,6 +419,7 @@ impl Server {
let mut disconnected_clients = Vec::new();
let mut requested_chunks = Vec::new();
let mut modified_blocks = Vec::new();
let mut dropped_items = Vec::new();
self.clients.remove_if(|entity, client| {
let mut disconnect = false;
@ -499,12 +500,19 @@ impl Server {
state.write_component(entity, comp::InventoryUpdate);
}
ClientMsg::DropInventorySlot(x) => {
state
let item = state
.ecs()
.write_storage::<comp::Inventory>()
.get_mut(entity)
.map(|inv| inv.remove(x)); // TODO: Spawn an item drop entity
.and_then(|inv| inv.remove(x));
state.write_component(entity, comp::InventoryUpdate);
if let (Some(pos), Some(item)) =
(state.ecs().read_storage::<comp::Pos>().get(entity), item)
{
dropped_items.push((*pos, item));
}
}
ClientMsg::Character { name, body } => match client.client_state {
// Become Registered first.
@ -682,6 +690,12 @@ impl Server {
self.state.set_block(pos, block);
}
for (pos, item) in dropped_items {
self.create_object(pos, comp::object::Body::Crate)
.with(item)
.build();
}
Ok(frontend_events)
}