Some code clarity tweaks

This commit is contained in:
Timothy Warren 2022-02-04 09:53:30 -05:00
parent 351b149b27
commit 9b5adaad9c
2 changed files with 29 additions and 26 deletions

View File

@ -424,65 +424,66 @@ fn parse_particle(n: &str) -> SpawnParticleBurst {
macro_rules! apply_effects { macro_rules! apply_effects {
($effects:expr, $eb:expr) => { ($effects:expr, $eb:expr) => {
for effect in $effects.iter() { // Effects are defined in the raws file as name => value pairs
let effect_name = effect.0.as_str(); for (effect_name, value) in $effects.iter() {
match effect_name { match effect_name.as_str() {
"provides_healing" => { "provides_healing" => {
$eb = $eb.with(ProvidesHealing { $eb = $eb.with(ProvidesHealing {
heal_amount: effect.1.parse::<i32>().unwrap(), heal_amount: value.parse::<i32>().unwrap(),
}); });
} }
"provides_mana" => { "provides_mana" => {
$eb = $eb.with(ProvidesMana { $eb = $eb.with(ProvidesMana {
mana_amount: effect.1.parse::<i32>().unwrap(), mana_amount: value.parse::<i32>().unwrap(),
}) })
} }
"teach_spell" => { "teach_spell" => {
$eb = $eb.with(TeachesSpell { $eb = $eb.with(TeachesSpell {
spell: effect.1.to_string(), spell: value.to_string(),
}) })
} }
"ranged" => { "ranged" => {
$eb = $eb.with(Ranged { $eb = $eb.with(Ranged {
range: effect.1.parse::<i32>().unwrap(), range: value.parse::<i32>().unwrap(),
}); });
} }
"damage" => { "damage" => {
$eb = $eb.with(InflictsDamage { $eb = $eb.with(InflictsDamage {
damage: effect.1.parse::<i32>().unwrap(), damage: value.parse::<i32>().unwrap(),
}); });
} }
"area_of_effect" => { "area_of_effect" => {
$eb = $eb.with(AreaOfEffect { $eb = $eb.with(AreaOfEffect {
radius: effect.1.parse::<i32>().unwrap(), radius: value.parse::<i32>().unwrap(),
}); });
} }
"confusion" => { "confusion" => {
$eb = $eb.with(Confusion {}); $eb = $eb.with(Confusion {});
$eb = $eb.with(Duration { $eb = $eb.with(Duration {
turns: effect.1.parse::<i32>().unwrap(), turns: value.parse::<i32>().unwrap(),
}); });
} }
"magic_mapping" => $eb = $eb.with(MagicMapper {}), "magic_mapping" => $eb = $eb.with(MagicMapper {}),
"town_portal" => $eb = $eb.with(TownPortal {}), "town_portal" => $eb = $eb.with(TownPortal {}),
"food" => $eb = $eb.with(ProvidesFood {}), "food" => $eb = $eb.with(ProvidesFood {}),
"single_activation" => $eb = $eb.with(SingleActivation {}), "single_activation" => $eb = $eb.with(SingleActivation {}),
"particle_line" => $eb = $eb.with(parse_particle_line(&effect.1)), "particle_line" => $eb = $eb.with(parse_particle_line(&value)),
"particle" => $eb = $eb.with(parse_particle(&effect.1)), "particle" => $eb = $eb.with(parse_particle(&value)),
"remove_curse" => $eb = $eb.with(ProvidesRemoveCurse {}), "remove_curse" => $eb = $eb.with(ProvidesRemoveCurse {}),
"identify" => $eb = $eb.with(ProvidesIdentification {}), "identify" => $eb = $eb.with(ProvidesIdentification {}),
"slow" => { "slow" => {
$eb = $eb.with(Slow { $eb = $eb.with(Slow {
initiative_penalty: effect.1.parse::<f32>().unwrap(), initiative_penalty: value.parse::<f32>().unwrap(),
}) })
} }
"damage_over_time" => { "damage_over_time" => {
$eb = $eb.with(DamageOverTime { $eb = $eb.with(DamageOverTime {
damage: effect.1.parse::<i32>().unwrap(), damage: value.parse::<i32>().unwrap(),
}) })
} }
"target_self" => $eb = $eb.with(AlwaysTargetsSelf {}), "target_self" => $eb = $eb.with(AlwaysTargetsSelf {}),
_ => { _ => {
#[cfg(feature = "debug")]
console::log(format!( console::log(format!(
"WARNING: consumable effect '{}' not implemented.", "WARNING: consumable effect '{}' not implemented.",
effect_name effect_name

View File

@ -79,6 +79,8 @@ impl<'a> System<'a> for VisibleAI {
reaction.0 as i32 / map.width, reaction.0 as i32 / map.width,
), ),
); );
// First, see if we are attempting to cast a spell
if let Some(abilities) = abilities.get(entity) { if let Some(abilities) = abilities.get(entity) {
for ability in abilities.abilities.iter() { for ability in abilities.abilities.iter() {
if range >= ability.min_range if range >= ability.min_range
@ -103,34 +105,33 @@ impl<'a> System<'a> for VisibleAI {
}, },
) )
.expect("Unable to insert intent to cast spell"); .expect("Unable to insert intent to cast spell");
done = true; done = true;
} }
} }
} }
// Check if there are targets if a ranged weapon is wielded
if !done { if !done {
for (weapon, equip) in (&weapons, &equipped).join() { for (weapon, equip) in (&weapons, &equipped).join() {
if let Some(wrange) = weapon.range { if let Some(wrange) = weapon.range {
if equip.owner == entity { if equip.owner == entity && wrange >= range as i32 {
#[cfg(feature = "debug")]
console::log(format!( console::log(format!(
"Owner found. Ranges: {}/{}", "Owner found. Ranges: {}/{}. Inserting shoot",
wrange, range wrange, range
)); ));
if wrange >= range as i32 { wants_shoot
console::log("Inserting shoot"); .insert(entity, WantsToShoot { target: reaction.2 })
wants_shoot .expect("Unable to insert intent to shoot");
.insert(
entity, done = true;
WantsToShoot { target: reaction.2 },
)
.expect("Unable to insert intent to shoot");
done = true;
}
} }
} }
} }
} }
// The target is not in range (yet), so approach/chase the target
if !done { if !done {
want_approach want_approach
.insert( .insert(
@ -143,6 +144,7 @@ impl<'a> System<'a> for VisibleAI {
chasing chasing
.insert(entity, Chasing { target: reaction.2 }) .insert(entity, Chasing { target: reaction.2 })
.expect("Unable to insert intent to chase"); .expect("Unable to insert intent to chase");
done = true; done = true;
} }
} }