From f57c2ec45379588e86b22e0ed719e30504bd5355 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Wed, 25 Sep 2019 16:52:58 +0100 Subject: [PATCH] Made consumable items have an effect, better damage animation --- common/src/comp/inventory/item.rs | 18 ++++++++---------- common/src/comp/stats.rs | 3 ++- common/src/effect.rs | 8 ++++++++ common/src/lib.rs | 1 + server/src/lib.rs | 22 ++++++++++++++++++++++ voxygen/src/scene/figure/mod.rs | 2 +- 6 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 common/src/effect.rs diff --git a/common/src/comp/inventory/item.rs b/common/src/comp/inventory/item.rs index 4958350100..2305318770 100644 --- a/common/src/comp/inventory/item.rs +++ b/common/src/comp/inventory/item.rs @@ -1,4 +1,8 @@ -use crate::terrain::{Block, BlockKind}; +use crate::{ + comp, + effect::Effect, + terrain::{Block, BlockKind}, +}; use specs::{Component, FlaggedStorage}; use specs_idvs::IDVStorage; @@ -88,12 +92,6 @@ impl Consumable { } } -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub enum ConsumptionEffect { - Health(i32), - Xp(i32), -} - #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum Debug { Boost, @@ -112,7 +110,7 @@ pub enum Item { }, Consumable { kind: Consumable, - effect: ConsumptionEffect, + effect: Effect, }, Ingredient, Debug(Debug), @@ -156,14 +154,14 @@ impl Item { pub fn apple() -> Self { Item::Consumable { kind: Consumable::Apple, - effect: ConsumptionEffect::Health(3), + effect: Effect::Health(20, comp::HealthSource::Item), } } pub fn mushroom() -> Self { Item::Consumable { kind: Consumable::Mushroom, - effect: ConsumptionEffect::Health(1), + effect: Effect::Health(10, comp::HealthSource::Item), } } } diff --git a/common/src/comp/stats.rs b/common/src/comp/stats.rs index e1e77f0549..1a97e59ae8 100644 --- a/common/src/comp/stats.rs +++ b/common/src/comp/stats.rs @@ -2,7 +2,7 @@ use crate::{comp, state::Uid}; use specs::{Component, FlaggedStorage}; use specs_idvs::IDVStorage; -#[derive(Clone, Copy, Debug, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)] pub enum HealthSource { Attack { by: Uid }, // TODO: Implement weapon Suicide, @@ -10,6 +10,7 @@ pub enum HealthSource { Revive, Command, LevelUp, + Item, Unknown, } #[derive(Clone, Copy, Debug, Serialize, Deserialize)] diff --git a/common/src/effect.rs b/common/src/effect.rs new file mode 100644 index 0000000000..358f6429e1 --- /dev/null +++ b/common/src/effect.rs @@ -0,0 +1,8 @@ +use crate::comp; + +/// An effect that may be applied to an entity +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum Effect { + Health(i32, comp::HealthSource), + Xp(i64), +} diff --git a/common/src/lib.rs b/common/src/lib.rs index 9a16148edf..eec8b7542f 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -16,6 +16,7 @@ extern crate log; pub mod assets; pub mod clock; pub mod comp; +pub mod effect; pub mod event; pub mod figure; pub mod msg; diff --git a/server/src/lib.rs b/server/src/lib.rs index c67fe80ea8..2a286db8f2 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -19,6 +19,7 @@ use crate::{ }; use common::{ comp, + effect::Effect, event::{EventBus, ServerEvent}, msg::{ClientMsg, ClientState, RequestStateError, ServerError, ServerInfo, ServerMsg}, net::PostOffice, @@ -866,6 +867,9 @@ impl Server { stats.equipment.main = item; } } + Some(comp::Item::Consumable { effect, .. }) => { + state.apply_effect(entity, effect); + } _ => {} } state.write_component(entity, comp::InventoryUpdate); @@ -1388,6 +1392,7 @@ impl Drop for Server { trait StateExt { fn give_item(&mut self, entity: EcsEntity, item: comp::Item); + fn apply_effect(&mut self, entity: EcsEntity, effect: Effect); } impl StateExt for State { @@ -1398,4 +1403,21 @@ impl StateExt for State { .map(|inv| inv.push(item)); self.write_component(entity, comp::InventoryUpdate); } + + fn apply_effect(&mut self, entity: EcsEntity, effect: Effect) { + match effect { + Effect::Health(hp, source) => { + self.ecs_mut() + .write_storage::() + .get_mut(entity) + .map(|stats| stats.health.change_by(hp, source)); + } + Effect::Xp(xp) => { + self.ecs_mut() + .write_storage::() + .get_mut(entity) + .map(|stats| stats.exp.change_by(xp)); + } + } + } } diff --git a/voxygen/src/scene/figure/mod.rs b/voxygen/src/scene/figure/mod.rs index 5dbd3768a0..46915fd864 100644 --- a/voxygen/src/scene/figure/mod.rs +++ b/voxygen/src/scene/figure/mod.rs @@ -108,7 +108,7 @@ impl FigureMgr { .and_then(|stats| stats.health.last_change) .map(|(_, time, _)| { Rgba::broadcast(1.0) - + Rgba::new(0.0, -1.0, -1.0, 0.0) + + Rgba::new(1.0, 1.0, 1.0, 0.0) .map(|c| (c / (1.0 + DAMAGE_FADE_COEFFICIENT * time)) as f32) }) .unwrap_or(Rgba::broadcast(1.0));