added effects for adding intrinsics
This commit is contained in:
parent
fa4612cf1f
commit
46bbe14bea
3 changed files with 90 additions and 24 deletions
11
src/effects/intrinsics.rs
Normal file
11
src/effects/intrinsics.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
use super::{ EffectSpawner, EffectType };
|
||||
use specs::prelude::*;
|
||||
|
||||
pub fn add_intrinsic(ecs: &mut World, effect: &EffectSpawner, target: Entity) {
|
||||
let intrinsic = if let EffectType::AddIntrinsic { intrinsic } = &effect.effect_type {
|
||||
intrinsic
|
||||
} else {
|
||||
unreachable!("add_intrinsic() called with the wrong EffectType")
|
||||
};
|
||||
add_intr!(ecs, target, *intrinsic);
|
||||
}
|
||||
|
|
@ -4,13 +4,14 @@ use bracket_lib::prelude::*;
|
|||
use specs::prelude::*;
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::Mutex;
|
||||
use crate::components::DamageType;
|
||||
use crate::components::*;
|
||||
|
||||
mod damage;
|
||||
mod hunger;
|
||||
mod particles;
|
||||
mod targeting;
|
||||
mod triggers;
|
||||
mod intrinsics;
|
||||
|
||||
pub use targeting::aoe_tiles;
|
||||
|
||||
|
|
@ -51,6 +52,9 @@ pub enum EffectType {
|
|||
ModifyNutrition {
|
||||
amount: i32,
|
||||
},
|
||||
AddIntrinsic {
|
||||
intrinsic: Intrinsic,
|
||||
},
|
||||
TriggerFire {
|
||||
trigger: Entity,
|
||||
},
|
||||
|
|
@ -153,6 +157,7 @@ fn tile_effect_hits_entities(effect: &EffectType) -> bool {
|
|||
EffectType::Healing { .. } => true,
|
||||
EffectType::ModifyNutrition { .. } => true,
|
||||
EffectType::Confusion { .. } => true,
|
||||
EffectType::AddIntrinsic { .. } => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
|
@ -175,6 +180,7 @@ fn affect_entity(ecs: &mut World, effect: &EffectSpawner, target: Entity) {
|
|||
}
|
||||
EffectType::EntityDeath => damage::entity_death(ecs, effect, target),
|
||||
EffectType::ModifyNutrition { .. } => hunger::modify_nutrition(ecs, effect, target),
|
||||
EffectType::AddIntrinsic { .. } => intrinsics::add_intrinsic(ecs, effect, target),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
// macros/mod.rs
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! player {
|
||||
/// Used to check if the player has a given component.
|
||||
macro_rules! player_has_component {
|
||||
($ecs:expr, $component:ty) => {
|
||||
{
|
||||
let player = $ecs.fetch::<Entity>();
|
||||
|
|
@ -16,29 +17,77 @@ macro_rules! player {
|
|||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! add_intr {
|
||||
($ecs:expr, $intrinsic:expr) => {
|
||||
let player = $ecs.fetch::<Entity>();
|
||||
let mut intrinsics = $ecs.write_storage::<crate::Intrinsics>();
|
||||
if let Some(player_intrinsics) = intrinsics.get_mut(*player) {
|
||||
if !player_intrinsics.list.contains(&$intrinsic) {
|
||||
player_intrinsics.list.insert($intrinsic);
|
||||
let mut intrinsic_changed = $ecs.write_storage::<crate::IntrinsicChanged>();
|
||||
if let Some(this_intrinsic_changed) = intrinsic_changed.get_mut(*player) {
|
||||
this_intrinsic_changed.gained.insert($intrinsic);
|
||||
} else {
|
||||
intrinsic_changed.insert(*player, crate::IntrinsicChanged {
|
||||
gained: {
|
||||
let mut m = std::collections::HashSet::new();
|
||||
m.insert($intrinsic);
|
||||
m
|
||||
},
|
||||
lost: std::collections::HashSet::new()
|
||||
}).expect("Failed to insert IntrinsicChanged component.");
|
||||
}
|
||||
/// Used to check if a given entity has a given Intrinsic.
|
||||
macro_rules! has {
|
||||
($ecs:expr, $entity:expr, $intrinsic:expr) => {
|
||||
{
|
||||
let intrinsics = $ecs.read_storage::<crate::Intrinsics>();
|
||||
if let Some(has_intrinsics) = intrinsics.get($entity) {
|
||||
has_intrinsics.list.contains(&$intrinsic)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
/// Used to check if the player has a given Intrinsic.
|
||||
macro_rules! player_has {
|
||||
($ecs:expr, $intrinsic:expr) => {
|
||||
{
|
||||
let player = $ecs.fetch::<Entity>();
|
||||
let intrinsics = $ecs.read_storage::<crate::Intrinsics>();
|
||||
if let Some(player_intrinsics) = intrinsics.get(*player) {
|
||||
player_intrinsics.list.contains(&$intrinsic)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
/// Handles adding an Intrinsic to the player, and adding it to the IntrinsicChanged component.
|
||||
macro_rules! add_intr {
|
||||
($ecs:expr, $entity:expr, $intrinsic:expr) => {
|
||||
{
|
||||
let mut intrinsics = $ecs.write_storage::<crate::Intrinsics>();
|
||||
if let Some(player_intrinsics) = intrinsics.get_mut($entity) {
|
||||
if !player_intrinsics.list.contains(&$intrinsic) {
|
||||
player_intrinsics.list.insert($intrinsic);
|
||||
let mut intrinsic_changed = $ecs.write_storage::<crate::IntrinsicChanged>();
|
||||
if let Some(this_intrinsic_changed) = intrinsic_changed.get_mut($entity) {
|
||||
this_intrinsic_changed.gained.insert($intrinsic);
|
||||
} else {
|
||||
intrinsic_changed.insert($entity, crate::IntrinsicChanged {
|
||||
gained: {
|
||||
let mut m = std::collections::HashSet::new();
|
||||
m.insert($intrinsic);
|
||||
m
|
||||
},
|
||||
lost: std::collections::HashSet::new()
|
||||
}).expect("Failed to insert IntrinsicChanged component.");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
intrinsics.insert($entity, crate::Intrinsics {
|
||||
list: {
|
||||
let mut m = std::collections::HashSet::new();
|
||||
m.insert($intrinsic);
|
||||
m
|
||||
}
|
||||
}).expect("Failed to insert Intrinsics component.");
|
||||
let mut intrinsic_changed = $ecs.write_storage::<crate::IntrinsicChanged>();
|
||||
intrinsic_changed.insert($entity, crate::IntrinsicChanged {
|
||||
gained: {
|
||||
let mut m = std::collections::HashSet::new();
|
||||
m.insert($intrinsic);
|
||||
m
|
||||
},
|
||||
lost: std::collections::HashSet::new()
|
||||
}).expect("Failed to insert IntrinsicChanged component.");
|
||||
}
|
||||
} else {
|
||||
unreachable!("add_intr!(): The player should always have an Intrinsics component.");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue