Made consumable items have an effect, better damage animation

This commit is contained in:
Joshua Barretto 2019-09-25 16:52:58 +01:00
parent a961a267f1
commit f57c2ec453
6 changed files with 42 additions and 12 deletions

View File

@ -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),
}
}
}

View File

@ -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)]

8
common/src/effect.rs Normal file
View File

@ -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),
}

View File

@ -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;

View File

@ -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::<comp::Stats>()
.get_mut(entity)
.map(|stats| stats.health.change_by(hp, source));
}
Effect::Xp(xp) => {
self.ecs_mut()
.write_storage::<comp::Stats>()
.get_mut(entity)
.map(|stats| stats.exp.change_by(xp));
}
}
}
}

View File

@ -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));