Added functionality to remove buffs by category.

This commit is contained in:
Sam 2020-10-02 14:09:19 -05:00
parent ccad1fa0b8
commit de7191b985
2 changed files with 62 additions and 7 deletions

View File

@ -29,14 +29,14 @@ pub enum BuffId {
/// De/buff category ID. /// De/buff category ID.
/// Similar to `BuffId`, but to mark a category (for more generic usage, like /// Similar to `BuffId`, but to mark a category (for more generic usage, like
/// positive/negative buffs). /// positive/negative buffs).
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)] #[derive(Clone, Copy, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub enum BuffCategoryId { pub enum BuffCategoryId {
Natural, Natural,
Physical, Physical,
Magical, Magical,
Divine, Divine,
Negative, Debuff,
Positive, Buff,
} }
/// Data indicating and configuring behaviour of a de/buff. /// Data indicating and configuring behaviour of a de/buff.
@ -83,8 +83,14 @@ pub enum BuffChange {
Add(Buff), Add(Buff),
/// Removes all buffs with this ID. /// Removes all buffs with this ID.
RemoveById(BuffId), RemoveById(BuffId),
/// Removes buff of this index /// Removes buffs of these indices (first vec is for active buffs, second is
/// for inactive buffs)
RemoveByIndex(Vec<usize>, Vec<usize>), RemoveByIndex(Vec<usize>, Vec<usize>),
/// 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>),
} }
/// Source of the de/buff /// Source of the de/buff

View File

@ -684,8 +684,9 @@ pub fn handle_buff(server: &mut Server, uid: Uid, buff_change: buff::BuffChange)
let mut stats = ecs.write_storage::<comp::Stats>(); let mut stats = ecs.write_storage::<comp::Stats>();
let (mut active_buff_indices_for_removal, mut inactive_buff_indices_for_removal) = let (mut active_buff_indices_for_removal, mut inactive_buff_indices_for_removal) =
(Vec::new(), Vec::new()); (Vec::new(), Vec::new());
use buff::BuffChange;
match buff_change { match buff_change {
buff::BuffChange::Add(new_buff) => { BuffChange::Add(new_buff) => {
if buffs.active_buffs.is_empty() { if buffs.active_buffs.is_empty() {
add_buff_effects(new_buff.clone(), stats.get_mut(entity)); add_buff_effects(new_buff.clone(), stats.get_mut(entity));
buffs.active_buffs.push(new_buff); buffs.active_buffs.push(new_buff);
@ -716,11 +717,11 @@ pub fn handle_buff(server: &mut Server, uid: Uid, buff_change: buff::BuffChange)
} }
} }
}, },
buff::BuffChange::RemoveByIndex(active_indices, inactive_indices) => { BuffChange::RemoveByIndex(active_indices, inactive_indices) => {
active_buff_indices_for_removal = active_indices; active_buff_indices_for_removal = active_indices;
inactive_buff_indices_for_removal = inactive_indices; inactive_buff_indices_for_removal = inactive_indices;
}, },
buff::BuffChange::RemoveById(id) => { BuffChange::RemoveById(id) => {
let some_predicate = |current_id: &buff::BuffId| *current_id == id; let some_predicate = |current_id: &buff::BuffId| *current_id == id;
for i in 0..buffs.active_buffs.len() { for i in 0..buffs.active_buffs.len() {
if some_predicate(&mut buffs.active_buffs[i].id) { if some_predicate(&mut buffs.active_buffs[i].id) {
@ -733,6 +734,54 @@ pub fn handle_buff(server: &mut Server, uid: Uid, buff_change: buff::BuffChange)
} }
} }
}, },
BuffChange::RemoveByCategory(all_required, any_required) => {
for i in 0..buffs.active_buffs.len() {
let mut required_met = true;
for required in &all_required {
if !buffs.active_buffs[i]
.cat_ids
.iter()
.any(|cat| cat == required)
{
required_met = false;
break;
}
}
let mut any_met = any_required.is_empty();
for any in &any_required {
if !buffs.active_buffs[i].cat_ids.iter().any(|cat| cat == any) {
any_met = true;
break;
}
}
if required_met && any_met {
active_buff_indices_for_removal.push(i);
}
}
for i in 0..buffs.inactive_buffs.len() {
let mut required_met = true;
for required in &all_required {
if !buffs.inactive_buffs[i]
.cat_ids
.iter()
.any(|cat| cat == required)
{
required_met = false;
break;
}
}
let mut any_met = any_required.is_empty();
for any in &any_required {
if !buffs.inactive_buffs[i].cat_ids.iter().any(|cat| cat == any) {
any_met = true;
break;
}
}
if required_met && any_met {
inactive_buff_indices_for_removal.push(i);
}
}
},
} }
let mut removed_active_buff_ids = Vec::new(); let mut removed_active_buff_ids = Vec::new();
while !active_buff_indices_for_removal.is_empty() { while !active_buff_indices_for_removal.is_empty() {