summaryrefslogtreecommitdiff
path: root/src/componentwrapper.rs
blob: 7a3d9462fa00e1ca32a963a7e88fde9e30fcb97a (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

use std::collections::HashMap;
use specs::Builder;
use rand::Rng;

use crate::{
	PlayerId,
	RoomId,
	Sprite,
	playerstate::RoomPos,
	components::{
		AttackType,
		Clan
	},
	parameter::{Parameter, ParameterType},
	Timestamp
};



macro_rules! components {
	($($comp: ident ($($paramname: ident : $paramtype: ident),*) $creation: expr);*;) => {
		#[derive(Clone)]
		pub enum ComponentWrapper{
			$(
				$comp(crate::components::$comp),
			)*
		}
		impl ComponentWrapper {
			pub fn build<A: Builder>(&self, builder: A ) -> A {
				match self.clone() {
					$(
						Self::$comp(c) => builder.with(c),
					)*
				}
			}
			pub fn load_component(comptype: ComponentType, mut parameters: HashMap<&str, Parameter>) -> Option<Self> {
				match comptype {
					$(
						ComponentType::$comp => Some(Self::$comp({
							use crate::components::$comp;
							$(
								let $paramname = match parameters.remove(stringify!($paramname))? {
									Parameter::$paramtype(p) => p,
									_ => {return None}
								};
							)*
							$creation
						})),
					)*
				}
			}
		}
		#[derive(Debug, PartialEq, Eq, Clone, Copy)]
		pub enum ComponentType {
			$(
				$comp,
			)*
		}
		impl ComponentType {
			pub fn from_str(typename: &str) -> Option<ComponentType>{
				match typename {
					$(
						stringify!($comp) => Some(Self::$comp),
					)*
					_ => None
				}
			}
			pub fn parameters(&self) -> HashMap<&str, ParameterType> {
				match self {
					$(
						Self::$comp => {
							#[allow(unused_mut)]
							let mut h = HashMap::new();
							$(
								h.insert(stringify!($paramname), ParameterType::$paramtype);
							)*
							h
						},
					)*
				}
			}
		}
	}
}

components!(
	Visible (name: String, sprite: String, height: Float) {
		Visible {
			sprite: Sprite{name: sprite},
			height,
			name
		}
	};
	Movable (cooldown: Int) {Movable {cooldown}};
	Blocking () {Blocking};
	Floor () {Floor};
	Player (name: String) {Player::new(PlayerId{name})};
	Item (ent: Template, name: String, action: Action) {Item{ent, name, action}};
	Inventory (capacity: Int) {Inventory{items: Vec::new(), capacity: capacity as usize}};
	Health (health: Int, maxhealth: Int) {Health{health, maxhealth}};
	Serialise (template: Template) {Serialise{template}};
	RoomExit (destination: String, dest_pos: String) {
		RoomExit {
			destination: RoomId::from_str(&destination),
			dest_pos: if dest_pos.is_empty() {
					RoomPos::Unknown
				} else {
					RoomPos::Name(dest_pos)
				}
		}
	};
	Trap (damage: Int) {Trap{attack: AttackType::Attack(damage)}};
	Fighter (damage: Int, cooldown: Int) {Fighter{attack: AttackType::Attack(damage), cooldown, range: 1}};
	Healing (delay: Int, health: Int) {Healing{delay, health, next_heal: None}};
	Volatile (delay: Int) {Volatile{delay, end_time: None}};
	Autofight () {Autofight::default()};
	MonsterAI (move_chance: Float, homesickness: Float, view_distance: Int) {MonsterAI{move_chance, homesickness, view_distance}};
	Mortal () {Mortal};
	Spawner (amount: Int, delay: Int, clan: String, template: Template, initial_spawn: Bool) {
		Spawner{
			amount: amount as usize,
			delay,
			clan: Clan{name:
				if clan == "" {
					format!("$random({})", rand::thread_rng().gen::<u32>())
				} else {
					clan
				}
			},
			template: template.unsaved(),
			last_spawn: if initial_spawn {Some(Timestamp(-delay))} else {None}
		}
	};
	Clan (name: String) Clan{name};
	Home (home: Pos) Home{home};
	Faction (faction: String) {Faction::from_str(faction.as_str())?};
	Interactable (action: String) {Interactable::from_str(action.as_str())?};
	Loot (loot: LootList) {Loot{loot}};
	Grow (delay: Int, into: Template) {Grow{delay, into, target_time: None}};
);


pub type PreEntity = Vec<ComponentWrapper>;