summaryrefslogtreecommitdiff
path: root/src/componentwrapper.rs
blob: 23e104b1b995c738e955a5446f738a257547d87c (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

use std::collections::HashMap;
use specs::{Builder, world::LazyBuilder};

use crate::components::{Visible, Blocking, Player, Floor};
use crate::parameter::{Parameter, ParameterType};



macro_rules! components {
	($($comp: ident [$($paramname: ident : $paramtype: ident),*] $paramlist: ident {$creation: expr});*) => {
	
	#[derive(Clone)]
	pub enum ComponentWrapper{
		$(
			$comp($comp),
		)*
	}

	impl ComponentWrapper {

		pub fn build<'a>(&self, builder: LazyBuilder<'a>) -> LazyBuilder<'a> {
			match self.clone() {
				$(
					Self::$comp(c) => builder.with(c),
				)*
			}
		}
		
		pub fn load_component(comptype: ComponentType, parameters_: HashMap<&str, Parameter>) -> Option<Self> {
			
			match comptype {
				$(
					ComponentType::$comp => Some(Self::$comp({
						#[allow(unused_mut)]
						let mut $paramlist = parameters_;
						$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 [sprite: String, height: Float] parameters {
		Visible {
			sprite: parameters.remove("sprite")?.as_string()?,
			height: parameters.remove("height")?.as_f64()?
		}
	};
	Blocking [] _p {Blocking};
	Floor [] _p {Floor};
	Player [name: String] parameters {Player::new(parameters.remove("name")?.as_string()?)}
);

pub type PreEntity = Vec<ComponentWrapper>;