From bd7fa2779491aa768414a4d6a3d15c8df3dba4fa Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 25 Jan 2022 13:39:43 -0500 Subject: [PATCH] Add ability to learn spells --- src/components.rs | 5 +++++ src/effects/triggers.rs | 29 +++++++++++++++++++++++++++-- src/main.rs | 1 + src/raws/rawmaster.rs | 5 +++++ src/saveload_system.rs | 2 ++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/components.rs b/src/components.rs index c36ff1b..97ee2ad 100644 --- a/src/components.rs +++ b/src/components.rs @@ -428,3 +428,8 @@ pub struct WantsToCastSpell { pub struct ProvidesMana { pub mana_amount: i32, } + +#[derive(Component, Debug, Serialize, Deserialize, Clone)] +pub struct TeachesSpell { + pub spell: String, +} diff --git a/src/effects/triggers.rs b/src/effects/triggers.rs index e569af7..1761fd7 100644 --- a/src/effects/triggers.rs +++ b/src/effects/triggers.rs @@ -5,10 +5,11 @@ use crate::components::{ AttributeBonus, Confusion, Consumable, Duration, Hidden, InflictsDamage, MagicMapper, Name, Pools, ProvidesFood, ProvidesHealing, ProvidesIdentification, ProvidesMana, ProvidesRemoveCurse, SingleActivation, SpawnParticleBurst, SpawnParticleLine, SpellTemplate, - TeleportTo, TownPortal, + TeachesSpell, TeleportTo, TownPortal, }; use crate::effects::{entity_position, targeting}; -use crate::{colors, GameLog, Map, RunState}; +use crate::raws::find_spell_entity; +use crate::{colors, GameLog, KnownSpell, KnownSpells, Map, RunState}; pub fn item_trigger(creator: Option, item: Entity, targets: &Targets, ecs: &mut World) { // Check charges @@ -265,6 +266,30 @@ fn event_trigger( did_something = true; } + // Learn spells + if let Some(spell) = ecs.read_storage::().get(entity) { + if let Some(known) = ecs.write_storage::().get_mut(creator.unwrap()) { + if let Some(spell_entity) = find_spell_entity(ecs, &spell.spell) { + if let Some(spell_info) = ecs.read_storage::().get(spell_entity) { + let mut already_known = false; + known.spells.iter().for_each(|s| { + if s.display_name == spell.spell { + already_known = true + } + }); + if !already_known { + known.spells.push(KnownSpell { + display_name: spell.spell.clone(), + mana_cost: spell_info.mana_cost, + }); + } + } + } + } + + did_something = true; + } + did_something } diff --git a/src/main.rs b/src/main.rs index 238f885..2cac31e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -140,6 +140,7 @@ fn init_state() -> State { SpawnParticleLine, SpellTemplate, StatusEffect, + TeachesSpell, TeleportTo, TownPortal, Vendor, diff --git a/src/raws/rawmaster.rs b/src/raws/rawmaster.rs index 0575a91..d9d159c 100644 --- a/src/raws/rawmaster.rs +++ b/src/raws/rawmaster.rs @@ -322,6 +322,11 @@ macro_rules! apply_effects { "particle" => $eb = $eb.with(parse_particle(&effect.1)), "remove_curse" => $eb = $eb.with(ProvidesRemoveCurse {}), "identify" => $eb = $eb.with(ProvidesIdentification {}), + "teach_spell" => { + $eb = $eb.with(TeachesSpell { + spell: effect.1.to_string(), + }) + } _ => { console::log(format!( "WARNING: consumable effect '{}' not implemented.", diff --git a/src/saveload_system.rs b/src/saveload_system.rs index 42097d7..257d210 100644 --- a/src/saveload_system.rs +++ b/src/saveload_system.rs @@ -118,6 +118,7 @@ pub fn save_game(ecs: &mut World) { SpawnParticleLine, SpellTemplate, StatusEffect, + TeachesSpell, TeleportTo, TownPortal, Vendor, @@ -245,6 +246,7 @@ pub fn load_game(ecs: &mut World) { SpawnParticleLine, SpellTemplate, StatusEffect, + TeachesSpell, TeleportTo, TownPortal, Vendor,