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
use anymap::AnyMap;
use std::collections::HashMap;
#[derive(Debug)]
pub struct Entity {
pub components: AnyMap,
pub tags: HashMap<&'static str, String>,
}
impl Entity {
pub fn new(label: String) -> Entity {
let mut tags = HashMap::new();
tags.insert("label", label);
Entity {
components: AnyMap::new(),
tags: tags,
}
}
pub fn label(&self) -> &str {
self.tags.get("label").unwrap()
}
}
impl PartialEq for Entity {
fn eq(&self, other: &Entity) -> bool {
self.label() == other.label()
}
}
#[cfg(test)]
mod tests {
use ::EntityBuilder;
#[derive(Debug, PartialEq)]
struct TestComponent(pub u8);
#[derive(Debug, PartialEq)]
struct AnotherComponent;
#[test]
fn entity_can_contain_components() {
let entity = EntityBuilder::create_str("test")
.add(TestComponent(1))
.build();
assert_eq!(entity.tags.get("label"), Some(&String::from("test")));
assert_eq!(entity.components.get::<TestComponent>(),
Some(&TestComponent(1)));
}
#[test]
fn entity_has_components_works() {
let entity = EntityBuilder::create_str("test")
.add(TestComponent(1))
.build();
assert!(entity.components.contains::<TestComponent>());
assert!(!entity.components.contains::<AnotherComponent>());
let entity = EntityBuilder::create_str("test")
.add(AnotherComponent)
.build();
assert!(!entity.components.contains::<TestComponent>());
assert!(entity.components.contains::<AnotherComponent>());
}
}