Add ability to learn spells

This commit is contained in:
Timothy Warren 2022-01-25 13:39:43 -05:00
parent 868bbade36
commit bd7fa27794
5 changed files with 40 additions and 2 deletions

View File

@ -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,
}

View File

@ -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<Entity>, 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::<TeachesSpell>().get(entity) {
if let Some(known) = ecs.write_storage::<KnownSpells>().get_mut(creator.unwrap()) {
if let Some(spell_entity) = find_spell_entity(ecs, &spell.spell) {
if let Some(spell_info) = ecs.read_storage::<SpellTemplate>().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
}

View File

@ -140,6 +140,7 @@ fn init_state() -> State {
SpawnParticleLine,
SpellTemplate,
StatusEffect,
TeachesSpell,
TeleportTo,
TownPortal,
Vendor,

View File

@ -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.",

View File

@ -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,