summaryrefslogtreecommitdiff
path: root/src/systems/attacking.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-04-10 00:16:39 +0200
committertroido <troido@protonmail.com>2020-04-10 00:16:39 +0200
commitafd1317197d1346626c58736defbbdf8aee7da68 (patch)
tree56781223a2664f292ee6f470d992b47c6b1b8609 /src/systems/attacking.rs
parente7b4ed0f044c3ada82155f81d3b61c0c5ce36583 (diff)
send out notifications when fighting
Diffstat (limited to 'src/systems/attacking.rs')
-rw-r--r--src/systems/attacking.rs40
1 files changed, 34 insertions, 6 deletions
diff --git a/src/systems/attacking.rs b/src/systems/attacking.rs
index e4001c9..804fac1 100644
--- a/src/systems/attacking.rs
+++ b/src/systems/attacking.rs
@@ -11,7 +11,17 @@ use specs::{
};
use crate::{
- components::{Health, AttackInbox, AttackType, Dead, Position, Autofight},
+ components::{
+ Health,
+ AttackInbox,
+ AttackType,
+ Dead,
+ Position,
+ Autofight,
+ Ear,
+ ear::{Notification, HealthNotification::{Attack, Damage, Heal}, say},
+ Visible
+ },
resources::NewEntities,
Template,
util
@@ -27,9 +37,11 @@ impl <'a> System<'a> for Attacking {
WriteStorage<'a, Dead>,
ReadStorage<'a, Position>,
Write<'a, NewEntities>,
- WriteStorage<'a, Autofight>
+ WriteStorage<'a, Autofight>,
+ WriteStorage<'a, Ear>,
+ ReadStorage<'a, Visible>
);
- fn run(&mut self, (entities, mut attackeds, mut healths, mut deads, positions, mut new, mut autofighters): Self::SystemData) {
+ fn run(&mut self, (entities, mut attackeds, mut healths, mut deads, positions, mut new, mut autofighters, mut ears, visibles): Self::SystemData) {
for (entity, attacked, autofighter) in (&entities, &attackeds, &mut autofighters).join() {
for attack in &attacked.messages {
@@ -42,27 +54,43 @@ impl <'a> System<'a> for Attacking {
}
}
}
- for (ent, health, attacked) in (&entities, &mut healths, &mut attackeds).join() {
+ for (target, health, attacked) in (&entities, &mut healths, &mut attackeds).join() {
+ let target_name = visibles.get(target).map(|v| v.name.as_str()).unwrap_or("?").to_string();
let mut wounded = false;
+ let mut attackers = Vec::new();
+ let mut attacker_names = Vec::new();
for attack in attacked.messages.drain(..) {
+ let actor_name = attack.attacker.map(|ae| visibles.get(ae)).flatten().map(|v| v.name.as_str()).unwrap_or("?").to_string();
match attack.typ {
AttackType::Attack(strength) => {
let damage = rand::thread_rng().gen_range(0, strength+1);
health.health -= damage;
if damage > 0 {
wounded = true;
+ if let Some(actor) = attack.attacker {
+ say(&mut ears, actor, Notification::Health{actor: actor_name.clone(), target: target_name.clone(), amount: damage, typ: Attack});
+ attackers.push(actor);
+ attacker_names.push(actor_name.clone());
+ }
+ say(&mut ears, target, Notification::Health{actor: actor_name.clone(), target: target_name.clone(), amount: damage, typ: Damage});
}
}
AttackType::Heal(healthdiff) => {
+ say(&mut ears, target, Notification::Health{actor: actor_name.clone(), target: target_name.clone(), amount: healthdiff, typ: Heal});
health.health += healthdiff;
}
}
}
health.health = util::clamp(health.health, 0, health.maxhealth);
if health.health == 0 {
- deads.insert(ent, Dead).unwrap();
+ deads.insert(target, Dead).unwrap();
+ let killers = attacker_names.join(" and ");
+ say(&mut ears, target, Notification::Die{actor: killers.clone(), target: target_name.clone()});
+ for actor in attackers {
+ say(&mut ears, actor, Notification::Kill{actor: killers.clone(), target: target_name.clone()});
+ }
}
- if let Some(position) = positions.get(ent){
+ if let Some(position) = positions.get(target){
if wounded {
new.create(position.pos, &Template::empty("wound")).unwrap();
}