summaryrefslogtreecommitdiff
path: root/src/components/messages.rs
blob: ae615f117ceb12caa47067ddc3b0c167a8d0553c (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

use std::any::Any;
use specs::{
	Component,
	DenseVecStorage,
	Entity,
	WriteStorage
};



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
		}
	}
}

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

impl Message for AttackMessage {}

pub type AttackInbox = Inbox<AttackMessage>;