summaryrefslogtreecommitdiff
path: root/src/components/messages.rs
blob: 8fe38e32735742ec96628026a5482c1c0afa6a75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

use std::collections::HashMap;
use std::any::Any;
use specs::{
	Component,
	DenseVecStorage,
	Entity,
	WriteStorage
};
use super::equipment::Stat;



pub trait Message: Send + Sync + Any {}

#[derive(Debug, Clone, Default)]
pub struct Inbox<M: Message> {
	pub messages: Vec<M>
}

impl <M: Message> Component for Inbox<M> {
	type Storage = DenseVecStorage<Self>;
}

impl <M: Message> Inbox<M> {
	
	pub fn add_message(messages: &mut WriteStorage<Self>, ent: Entity, message: M){
		messages
			.entry(ent)
			.unwrap()
			.or_insert_with(|| Self{messages: Vec::new()})
			.messages
			.push(message);
	}
}


#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AttackType {
	Attack(i64),
	Heal(i64)
}

impl AttackType {
	pub fn is_hostile(&self) -> bool {
		match self {
			Self::Attack(_) => true,
			Self::Heal(_) => false
		}
	}
	pub fn apply_bonuses(self, bonuses: &HashMap<Stat, i64>) -> AttackType {
		match self {
			Self::Attack(strength) => Self::Attack(strength + *bonuses.get(&Stat::Strength).unwrap_or(&0)),
			Self::Heal(_) => self
		}
	}
}

#[derive(Debug, Clone)]
pub struct AttackMessage {
	pub attacker: Option<Entity>,
	pub typ: AttackType
}

impl Message for AttackMessage {}

pub type AttackInbox = Inbox<AttackMessage>;