summaryrefslogtreecommitdiff
path: root/src/components/ear.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/components/ear.rs
parente7b4ed0f044c3ada82155f81d3b61c0c5ce36583 (diff)
send out notifications when fighting
Diffstat (limited to 'src/components/ear.rs')
-rw-r--r--src/components/ear.rs87
1 files changed, 80 insertions, 7 deletions
diff --git a/src/components/ear.rs b/src/components/ear.rs
index 4bfc87b..1432164 100644
--- a/src/components/ear.rs
+++ b/src/components/ear.rs
@@ -2,23 +2,96 @@
use specs::{
HashMapStorage,
Component,
+ Entity,
+ WriteStorage
};
#[derive(Debug, Clone)]
-pub struct Sound {
- pub source: Option<String>,
- pub text: String
+pub enum HealthNotification {
+ Attack,
+ Damage,
+ Heal
}
-impl Sound {
- pub fn as_message(self) -> (Option<String>, String) {
- (None, format!("{}: {}", self.source.unwrap_or("".to_string()), self.text))
+use HealthNotification::*;
+
+#[derive(Debug, Clone)]
+pub enum Notification {
+ Sound{
+ source: Option<String>,
+ text: String
+ },
+ Health {
+ actor: String,
+ target: String,
+ amount: i64,
+ typ: HealthNotification
+ },
+ Kill {
+ actor: String,
+ target: String
+ },
+ Die {
+ actor: String,
+ target: String
+ }
+}
+
+use Notification::*;
+
+
+impl Notification {
+
+
+ pub fn type_name(&self) -> String {
+ (match self {
+ Sound{source: _, text: _} => "sound",
+ Health{actor: _, target: _, amount: _, typ} => match typ {
+ Attack => "attack",
+ Damage => "damage",
+ Heal => "heal"
+ },
+ Kill{actor: _, target: _} => "kill",
+ Die{actor: _, target: _} => "die"
+ }).to_string()
+ }
+
+ pub fn as_message(&self) -> (String, String) {
+ let body = match self {
+ Sound{source, text} => {
+ if let Some(name) = &source {
+ format!("{}: {}", name, &text)
+ } else {
+ text.clone()
+ }
+ }
+ Health{actor, target, amount, typ} => {
+ match typ {
+ Attack | Damage => format!("{} attacks {} for {} damage", actor, target, amount),
+ Heal => format!("{} heals {} for {} health", actor, target, amount)
+ }
+ },
+ Kill{actor, target} => {
+ format!("{} kills {}", actor, target)
+ },
+ Die{actor, target} => {
+ format!("{} was killed by {}", target, actor)
+ }
+ };
+ (self.type_name(), body)
}
}
#[derive(Component, Debug, Clone, Default)]
#[storage(HashMapStorage)]
pub struct Ear{
- pub sounds: Vec<Sound>
+ pub sounds: Vec<Notification>
+}
+
+pub fn say(ears: &mut WriteStorage<Ear>, ent: Entity, msg: Notification){
+ if let Some(ear) = ears.get_mut(ent) {
+ ear.sounds.push(msg);
+ }
}
+