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

use std::collections::HashMap;
use specs::{Builder, EntityBuilder};

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


#[derive(Clone)]
pub enum ComponentWrapper{
	Visible(Visible),
	Blocking(Blocking),
	Player(Player)
}

impl ComponentWrapper {

	pub fn build<'a>(&self, builder: EntityBuilder<'a>) -> EntityBuilder<'a> {
		match self.clone() {
			Self::Visible(c) => builder.with(c),
			Self::Blocking(c) => builder.with(c),
			Self::Player(c) => builder.with(c)
		}
	}

	pub fn load_component(comptype: ComponentType, mut parameters: HashMap<&str, Parameter>) -> Option<Self> {
		match comptype {
			ComponentType::Visible => Some(Self::Visible(Visible{
				sprite: parameters.remove("sprite")?.as_str()?.to_string(),
				height: parameters.remove("height")?.as_f64()?
			})),
			ComponentType::Blocking => Some(Self::Blocking(Blocking)),
			ComponentType::Player => Some(Self::Player(Player::new(
				parameters.remove("name")?.as_str()?.to_string()
			)))
		}
	}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ComponentType {
	Visible,
	Blocking,
	Player
}

impl ComponentType {
	
	pub fn from_str(typename: &str) -> Option<ComponentType>{
		match typename {
			"Visible" => Some(ComponentType::Visible),
			"Blocking" => Some(ComponentType::Blocking),
			"Player" => Some(ComponentType::Player),
			_ => None
		}
	}
	
	pub fn parameters(&self) -> HashMap<&str, ParameterType> {
		match self {
			ComponentType::Visible => hashmap!("sprite" => ParameterType::String, "height" => ParameterType::Float),
			ComponentType::Blocking => HashMap::new(),
			ComponentType::Player => hashmap!("name" => ParameterType::String)
		}
	}
}