roguelike-game/src/effects/triggers.rs

140 lines
3.9 KiB
Rust
Raw Normal View History

use ::specs::prelude::*;
use super::{add_effect, EffectType, Targets};
use crate::components::{
Confusion, Consumable, Hidden, InflictsDamage, MagicMapper, Name, ProvidesFood,
ProvidesHealing, SingleActivation, TeleportTo, TownPortal,
};
use crate::{GameLog, Map, RunState};
pub fn item_trigger(creator: Option<Entity>, item: Entity, targets: &Targets, ecs: &mut World) {
// Use the item via the generic system
let did_something = event_trigger(creator, item, targets, ecs);
// If it was a consumable, then it gets deleted
if did_something && ecs.read_storage::<Consumable>().get(item).is_some() {
ecs.entities()
.delete(item)
.expect("Failed to delete consumable item");
}
}
pub fn trigger(creator: Option<Entity>, trigger: Entity, targets: &Targets, ecs: &mut World) {
// The triggering item is no longer hidden
ecs.write_storage::<Hidden>().remove(trigger);
// Use the item via the generic system
let did_something = event_trigger(creator, trigger, targets, ecs);
// If it was a single activation, then it gets deleted
if did_something
&& ecs
.read_storage::<SingleActivation>()
.get(trigger)
.is_some()
{
ecs.entities()
.delete(trigger)
.expect("Failed to delete SingleActivation tag");
}
}
fn event_trigger(
creator: Option<Entity>,
entity: Entity,
targets: &Targets,
ecs: &mut World,
) -> bool {
let mut did_something = false;
let mut gamelog = ecs.fetch_mut::<GameLog>();
// Providing food
if ecs.read_storage::<ProvidesFood>().get(entity).is_some() {
add_effect(creator, EffectType::WellFed, targets.clone());
let names = ecs.read_storage::<Name>();
gamelog.append(format!("You eat the {}.", names.get(entity).unwrap().name));
did_something = true;
}
// Magic mapper
if ecs.read_storage::<MagicMapper>().get(entity).is_some() {
let mut runstate = ecs.fetch_mut::<RunState>();
gamelog.append("The map is revealed to you!");
*runstate = RunState::MagicMapReveal { row: 0 };
did_something = true;
}
// Town Portal
if ecs.read_storage::<TownPortal>().get(entity).is_some() {
let map = ecs.fetch::<Map>();
if map.depth == 1 {
gamelog.append("You are already in town, so the scroll does nothing.");
} else {
gamelog.append("You are teleported back to town!");
let mut runstate = ecs.fetch_mut::<RunState>();
*runstate = RunState::TownPortal;
did_something = true;
}
}
// Healing
if let Some(heal) = ecs.read_storage::<ProvidesHealing>().get(entity) {
add_effect(
creator,
EffectType::Healing {
amount: heal.heal_amount,
},
targets.clone(),
);
did_something = true;
}
// Damage
if let Some(damage) = ecs.read_storage::<InflictsDamage>().get(entity) {
add_effect(
creator,
EffectType::Damage {
amount: damage.damage,
},
targets.clone(),
);
did_something = true;
}
// Confusion
if let Some(confusion) = ecs.read_storage::<Confusion>().get(entity) {
add_effect(
creator,
EffectType::Confusion {
turns: confusion.turns,
},
targets.clone(),
);
did_something = true;
}
// Teleport
if let Some(teleport) = ecs.read_storage::<TeleportTo>().get(entity) {
add_effect(
creator,
EffectType::TeleportTo {
x: teleport.x,
y: teleport.y,
depth: teleport.depth,
player_only: teleport.player_only,
},
targets.clone(),
);
did_something = true;
}
did_something
}