add_intr!() macro for adding intrinsics to the Player
If needed, player can just be replaced by another arg to the macro so this works on every other entity - but right now the player is the only thing to gain/lose intrinsics.
This commit is contained in:
parent
b5743819ec
commit
4d21bd46d4
5 changed files with 56 additions and 0 deletions
|
|
@ -460,6 +460,12 @@ impl Intrinsics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct IntrinsicChanged {
|
||||||
|
pub gained: HashSet<Intrinsic>,
|
||||||
|
pub lost: HashSet<Intrinsic>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Component, Debug, ConvertSaveload, Clone)]
|
#[derive(Component, Debug, ConvertSaveload, Clone)]
|
||||||
pub struct InflictsDamage {
|
pub struct InflictsDamage {
|
||||||
pub damage_type: DamageType,
|
pub damage_type: DamageType,
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,9 @@ extern crate serde;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
pub mod macros;
|
||||||
|
|
||||||
pub mod camera;
|
pub mod camera;
|
||||||
pub mod components;
|
pub mod components;
|
||||||
pub mod raws;
|
pub mod raws;
|
||||||
|
|
|
||||||
44
src/macros/mod.rs
Normal file
44
src/macros/mod.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
// macros/mod.rs
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! player {
|
||||||
|
($ecs:expr, $component:ty) => {
|
||||||
|
{
|
||||||
|
let player = $ecs.fetch::<Entity>();
|
||||||
|
let component = $ecs.read_storage::<$component>();
|
||||||
|
if let Some(player_component) = component.get(*player) {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unreachable!("add_intr!(): The player should always have an Intrinsics component.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -111,6 +111,7 @@ fn main() -> BError {
|
||||||
gs.ecs.register::<SpawnParticleLine>();
|
gs.ecs.register::<SpawnParticleLine>();
|
||||||
gs.ecs.register::<HasDamageModifiers>();
|
gs.ecs.register::<HasDamageModifiers>();
|
||||||
gs.ecs.register::<Intrinsics>();
|
gs.ecs.register::<Intrinsics>();
|
||||||
|
gs.ecs.register::<IntrinsicChanged>();
|
||||||
gs.ecs.register::<SimpleMarker<SerializeMe>>();
|
gs.ecs.register::<SimpleMarker<SerializeMe>>();
|
||||||
gs.ecs.register::<SerializationHelper>();
|
gs.ecs.register::<SerializationHelper>();
|
||||||
gs.ecs.register::<DMSerializationHelper>();
|
gs.ecs.register::<DMSerializationHelper>();
|
||||||
|
|
|
||||||
|
|
@ -95,6 +95,7 @@ pub fn save_game(ecs: &mut World) {
|
||||||
IdentifiedItem,
|
IdentifiedItem,
|
||||||
InBackpack,
|
InBackpack,
|
||||||
InflictsDamage,
|
InflictsDamage,
|
||||||
|
IntrinsicChanged,
|
||||||
Intrinsics,
|
Intrinsics,
|
||||||
Item,
|
Item,
|
||||||
KnownSpells,
|
KnownSpells,
|
||||||
|
|
@ -227,6 +228,7 @@ pub fn load_game(ecs: &mut World) {
|
||||||
IdentifiedItem,
|
IdentifiedItem,
|
||||||
InBackpack,
|
InBackpack,
|
||||||
InflictsDamage,
|
InflictsDamage,
|
||||||
|
IntrinsicChanged,
|
||||||
Intrinsics,
|
Intrinsics,
|
||||||
Item,
|
Item,
|
||||||
KnownSpells,
|
KnownSpells,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue