forked from chrisb88/php-ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.php
More file actions
149 lines (125 loc) · 3.27 KB
/
Copy pathEntity.php
File metadata and controls
149 lines (125 loc) · 3.27 KB
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
149
<?php
namespace ecs;
use ecs\events\EventManager;
use ecs\events\messages\ComponentAddedMessage;
use ecs\events\messages\ComponentRemovedMessage;
class Entity
{
/**
* @var int
*/
private $id;
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var EventManager
*/
private $eventManager;
/**
* @var Component[]
*/
private $components;
/**
* @param EntityManager $entityManager
* @param EventManager $eventManager
* @param int $id
*/
public function __construct(EntityManager $entityManager, EventManager $eventManager, $id) {
$this->id = $id;
$this->components = [];
$this->entityManager = $entityManager;
$this->eventManager = $eventManager;
}
/**
* @return int
*/
public function getId() {
return $this->id;
}
/**
* @throws \Exception
*/
public function destroy() {
throw new \Exception("Not implemented.");
}
/**
* @param Component $component
* @return $this
* @throws \Exception
*/
public function addComponent(Component $component) {
$id = get_class($component);
if (isset($this->components[$id])) {
throw new \Exception(sprintf('Component "%s" is already registered.', $id));
}
$this->components[$id] = $component;
$this->eventManager->emit(new ComponentAddedMessage($component));
return $this;
}
public function addComponentIfNotExist(Component $component)
{
$id = get_class($component);
if (!isset($this->components[$id])) {
$this->addComponent($component);
}
return $this;
}
/**
* @param string $className
* @return Component
* @throws \Exception
*/
public function getComponent($className) {
if (isset($this->components[$className])) {
return $this->components[$className];
}
throw new \Exception(sprintf('Component "%s" is not registered.', $className));
}
/**
* @param string $className
* @return $this
* @throws \Exception
*/
public function removeComponent($className)
{
if (isset($this->components[$className])) {
unset($this->components[$className]);
$this->eventManager->emit(new ComponentRemovedMessage($className, $this));
return $this;
}
throw new \Exception(sprintf('Component "%s" is not registered.', $className));
}
/**
* @param string $className
* @return bool
*/
public function hasComponent($className) {
return isset($this->components[$className]);
}
/**
* @param string[] $classNames
* @return bool
*/
public function hasAllComponents(array $classNames) {
foreach ($classNames as $name) {
if (!$this->hasComponent($name)) {
return false;
}
}
return true;
}
/**
* @param string[] $classNames
* @return bool
*/
public function hasAnyComponent(array $classNames) {
foreach ($classNames as $name) {
if ($this->hasComponent($name)) {
return true;
}
}
return false;
}
}