2020-08-10 22:54:45 +00:00
|
|
|
use crate::sync::Uid;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use specs::{Component, FlaggedStorage};
|
|
|
|
use specs_idvs::IdvStorage;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
/// De/buff ID.
|
2020-10-01 00:40:46 +00:00
|
|
|
/// ID can be independant of an actual type/config of a `BuffEffect`.
|
2020-08-10 22:54:45 +00:00
|
|
|
/// Therefore, information provided by `BuffId` can be incomplete/incorrect.
|
|
|
|
///
|
|
|
|
/// For example, there could be two regeneration buffs, each with
|
|
|
|
/// different strength, but they could use the same `BuffId`,
|
|
|
|
/// making it harder to recognize which is which.
|
|
|
|
///
|
|
|
|
/// Also, this should be dehardcoded eventually.
|
2020-10-02 17:15:10 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
|
2020-08-10 22:54:45 +00:00
|
|
|
pub enum BuffId {
|
|
|
|
/// Restores health/time for some period
|
2020-10-03 18:48:56 +00:00
|
|
|
Regeneration { strength: f32 },
|
2020-10-01 00:40:46 +00:00
|
|
|
/// Lowers health over time for some duration
|
2020-10-03 18:48:56 +00:00
|
|
|
Bleeding { strength: f32 },
|
2020-10-02 17:15:10 +00:00
|
|
|
/// Prefixes an entity's name with "Cursed"
|
2020-10-01 17:33:35 +00:00
|
|
|
/// Currently placeholder buff to show other stuff is possible
|
2020-10-02 17:15:10 +00:00
|
|
|
Cursed,
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// De/buff category ID.
|
|
|
|
/// Similar to `BuffId`, but to mark a category (for more generic usage, like
|
|
|
|
/// positive/negative buffs).
|
2020-10-02 19:09:19 +00:00
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Debug, Serialize, Deserialize)]
|
2020-08-10 22:54:45 +00:00
|
|
|
pub enum BuffCategoryId {
|
|
|
|
Natural,
|
2020-10-01 00:40:46 +00:00
|
|
|
Physical,
|
2020-08-10 22:54:45 +00:00
|
|
|
Magical,
|
|
|
|
Divine,
|
2020-10-02 19:09:19 +00:00
|
|
|
Debuff,
|
|
|
|
Buff,
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Data indicating and configuring behaviour of a de/buff.
|
|
|
|
///
|
|
|
|
/// NOTE: Contents of this enum are WIP/Placeholder
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2020-10-01 00:40:46 +00:00
|
|
|
pub enum BuffEffect {
|
|
|
|
/// Periodically damages or heals entity
|
2020-10-01 01:35:57 +00:00
|
|
|
HealthChangeOverTime { rate: f32, accumulated: f32 },
|
2020-08-10 22:54:45 +00:00
|
|
|
/// Changes name on_add/on_remove
|
|
|
|
NameChange { prefix: String },
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Actual de/buff.
|
|
|
|
/// Buff can timeout after some time if `time` is Some. If `time` is None,
|
|
|
|
/// Buff will last indefinitely, until removed manually (by some action, like
|
|
|
|
/// uncursing). The `time` field might be moved into the `Buffs` component
|
|
|
|
/// (so that `Buff` does not own this information).
|
|
|
|
///
|
|
|
|
/// Buff has an id and data, which can be independent on each other.
|
|
|
|
/// This makes it hard to create buff stacking "helpers", as the system
|
|
|
|
/// does not assume that the same id is always the same behaviour (data).
|
|
|
|
/// Therefore id=behaviour relationship has to be enforced elsewhere (if
|
|
|
|
/// desired).
|
|
|
|
///
|
|
|
|
/// To provide more classification info when needed,
|
|
|
|
/// buff can be in one or more buff category.
|
|
|
|
///
|
|
|
|
/// `data` is separate, to make this system more flexible
|
|
|
|
/// (at the cost of the fact that id=behaviour relationship might not apply).
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Buff {
|
|
|
|
pub id: BuffId,
|
|
|
|
pub cat_ids: Vec<BuffCategoryId>,
|
|
|
|
pub time: Option<Duration>,
|
2020-10-01 00:40:46 +00:00
|
|
|
pub effects: Vec<BuffEffect>,
|
2020-10-03 18:48:56 +00:00
|
|
|
pub source: BuffSource,
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Information about whether buff addition or removal was requested.
|
|
|
|
/// This to implement "on_add" and "on_remove" hooks for constant buffs.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum BuffChange {
|
|
|
|
/// Adds this buff.
|
|
|
|
Add(Buff),
|
|
|
|
/// Removes all buffs with this ID.
|
2020-10-01 02:32:38 +00:00
|
|
|
RemoveById(BuffId),
|
2020-10-02 19:09:19 +00:00
|
|
|
/// Removes buffs of these indices (first vec is for active buffs, second is
|
|
|
|
/// for inactive buffs)
|
2020-10-02 17:15:10 +00:00
|
|
|
RemoveByIndex(Vec<usize>, Vec<usize>),
|
2020-10-02 19:09:19 +00:00
|
|
|
/// Removes buffs of these categories (first vec is of categories of which
|
|
|
|
/// all are required, second vec is of categories of which at least one is
|
|
|
|
/// required) Note that this functionality is currently untested and
|
|
|
|
/// should be tested when doing so is possible
|
|
|
|
RemoveByCategory(Vec<BuffCategoryId>, Vec<BuffCategoryId>),
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Source of the de/buff
|
|
|
|
#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum BuffSource {
|
|
|
|
/// Applied by a character
|
|
|
|
Character { by: Uid },
|
|
|
|
/// Applied by world, like a poisonous fumes from a swamp
|
|
|
|
World,
|
|
|
|
/// Applied by command
|
|
|
|
Command,
|
|
|
|
/// Applied by an item
|
|
|
|
Item,
|
|
|
|
/// Applied by another buff (like an after-effect)
|
|
|
|
Buff,
|
|
|
|
/// Some other source
|
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Component holding all de/buffs that gets resolved each tick.
|
|
|
|
/// On each tick, remaining time of buffs get lowered and
|
2020-10-01 00:40:46 +00:00
|
|
|
/// buff effect of each buff is applied or not, depending on the `BuffEffect`
|
2020-10-01 01:35:57 +00:00
|
|
|
/// (specs system will decide based on `BuffEffect`, to simplify
|
|
|
|
/// implementation). TODO: Something like `once` flag for `Buff` to remove the
|
|
|
|
/// dependence on `BuffEffect` enum?
|
2020-08-10 22:54:45 +00:00
|
|
|
///
|
|
|
|
/// In case of one-time buffs, buff effects will be applied on addition
|
|
|
|
/// and undone on removal of the buff (by the specs system).
|
|
|
|
/// Example could be decreasing max health, which, if repeated each tick,
|
|
|
|
/// would be probably an undesired effect).
|
|
|
|
///
|
|
|
|
/// TODO: Make this net/sync-friendly. Events could help there
|
|
|
|
/// (probably replacing `changes`). Also, the "buff ticking" is really
|
|
|
|
/// not needed to be synced often, only in case that a buff begins/ends
|
|
|
|
/// (as the buff ECS system will probably run on a client too).
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
|
|
|
|
pub struct Buffs {
|
|
|
|
/// Active de/buffs.
|
2020-10-02 17:15:10 +00:00
|
|
|
pub active_buffs: Vec<Buff>,
|
|
|
|
/// Inactive de/buffs (used so that only 1 buff of a particular type is
|
|
|
|
/// active at any time)
|
|
|
|
pub inactive_buffs: Vec<Buff>,
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Buffs {
|
2020-10-02 17:15:10 +00:00
|
|
|
/// This is a primitive check if a specific buff is present and active.
|
2020-08-10 22:54:45 +00:00
|
|
|
/// (for purposes like blocking usage of abilities or something like this).
|
2020-10-02 17:15:10 +00:00
|
|
|
pub fn has_buff_id(&self, id: &BuffId) -> bool {
|
|
|
|
self.active_buffs.iter().any(|buff| buff.id == *id)
|
|
|
|
}
|
2020-08-10 22:54:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 17:33:35 +00:00
|
|
|
impl Buff {
|
2020-10-03 18:48:56 +00:00
|
|
|
pub fn new(
|
|
|
|
id: BuffId,
|
|
|
|
time: Option<Duration>,
|
|
|
|
cat_ids: Vec<BuffCategoryId>,
|
|
|
|
source: BuffSource,
|
|
|
|
) -> Self {
|
2020-10-01 17:33:35 +00:00
|
|
|
let effects = match id {
|
2020-10-03 18:48:56 +00:00
|
|
|
BuffId::Bleeding { strength } => vec![
|
2020-10-01 17:33:35 +00:00
|
|
|
BuffEffect::HealthChangeOverTime {
|
|
|
|
rate: -strength,
|
|
|
|
accumulated: 0.0,
|
|
|
|
},
|
|
|
|
// This effect is for testing purposes
|
|
|
|
BuffEffect::NameChange {
|
|
|
|
prefix: String::from("Injured "),
|
|
|
|
},
|
|
|
|
],
|
2020-10-03 18:48:56 +00:00
|
|
|
BuffId::Regeneration { strength } => vec![BuffEffect::HealthChangeOverTime {
|
2020-10-01 17:33:35 +00:00
|
|
|
rate: strength,
|
|
|
|
accumulated: 0.0,
|
|
|
|
}],
|
2020-10-02 17:15:10 +00:00
|
|
|
BuffId::Cursed => vec![BuffEffect::NameChange {
|
|
|
|
prefix: String::from("Cursed "),
|
|
|
|
}],
|
2020-10-01 17:33:35 +00:00
|
|
|
};
|
|
|
|
Buff {
|
2020-10-02 17:15:10 +00:00
|
|
|
id,
|
2020-10-01 17:33:35 +00:00
|
|
|
cat_ids,
|
|
|
|
time,
|
|
|
|
effects,
|
2020-10-03 18:48:56 +00:00
|
|
|
source,
|
2020-10-01 17:33:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 22:54:45 +00:00
|
|
|
impl Component for Buffs {
|
|
|
|
type Storage = FlaggedStorage<Self, IdvStorage<Self>>;
|
|
|
|
}
|