summaryrefslogtreecommitdiff
path: root/src/components/messages.rs
blob: 5e27cc68b531cd813824a39ed312f336fa7a04c0 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

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



pub trait Message: Send + Sync + Any + PartialEq {}

#[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);
	}
	
	pub fn has_message(&self, messages: &[M]) -> bool {
		for message in self.messages.iter() {
			for asked in messages {
				if message == asked {
					return true;
				}
			}
		}
		false
	}
}


#[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, PartialEq)]
pub struct AttackMessage {
	pub attacker: Option<Entity>,
	pub typ: AttackType
}

impl Message for AttackMessage {}

pub type AttackInbox = Inbox<AttackMessage>;








#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Trigger {
	// basic triggers
	Loot,
	Remove,
	Build,
	Spawn,
	// combination triggers
	Die, // Remove + Loot
	Change // Remove + Build
}

impl Trigger {
	pub fn from_str(txt: &str) -> Option<Self> {
		Some(match txt {
			"loot" => Self::Loot,
			"remove" => Self::Remove,
			"build" => Self::Build,
			"spawn" => Self::Spawn,
			"die" => Self::Die,
			"change" => Self::Change,
			_ => {return None}
		})
	}
}

impl Message for Trigger {}

pub type TriggerBox = Inbox<Trigger>;