1
|
|
|
<?php |
2
|
|
|
/** @formatter:off |
3
|
|
|
* ****************************************************************** |
4
|
|
|
* Created by Marko Kungla on Jun 20, 2016 - 9:11:10 PM |
5
|
|
|
* Contact [email protected] |
6
|
|
|
* @copyright 2016 Marko Kungla - https://github.com/mkungla |
7
|
|
|
* @license The MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* @category AframeVR |
10
|
|
|
* @package aframe-php |
11
|
|
|
* |
12
|
|
|
* Lang PHP (php version >= 7) |
13
|
|
|
* Encoding UTF-8 |
14
|
|
|
* File Entity.php |
15
|
|
|
* Code format PSR-2 and 12 |
16
|
|
|
* @link https://github.com/mkungla/aframe-php |
17
|
|
|
^ @issues https://github.com/mkungla/aframe-php/issues |
18
|
|
|
* ******************************************************************** |
19
|
|
|
* Contributors: |
20
|
|
|
* @author Marko Kungla <[email protected]> |
21
|
|
|
* ******************************************************************** |
22
|
|
|
* Comments: |
23
|
|
|
* @formatter:on */ |
24
|
|
|
namespace AframeVR\Core; |
25
|
|
|
|
26
|
|
|
use \AframeVR\Core\Exceptions\BadComponentCallException; |
27
|
|
|
use \AframeVR\Interfaces\{ |
28
|
|
|
ComponentInterface, |
29
|
|
|
EntityInterface |
30
|
|
|
}; |
31
|
|
|
use \DOMElement; |
32
|
|
|
|
33
|
|
|
class Entity implements EntityInterface |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
protected $components = array(); |
37
|
|
|
|
38
|
8 |
|
public function __construct() |
39
|
|
|
{ |
40
|
|
|
/* Components which All entities inherently have */ |
41
|
8 |
|
$this->component('Position'); |
42
|
8 |
|
$this->component('Rotation'); |
43
|
|
|
|
44
|
|
|
/* |
45
|
|
|
* We initialize common entity components here since |
46
|
|
|
* init and defaults are most cases overwritten by extending class |
47
|
|
|
*/ |
48
|
8 |
|
$this->position(); |
49
|
8 |
|
$this->rotation(); |
50
|
|
|
|
51
|
|
|
/* Extending entity components and init */ |
52
|
8 |
|
$this->init(); |
|
|
|
|
53
|
|
|
|
54
|
|
|
/* Extending entity defaults */ |
55
|
8 |
|
$this->defaults(); |
|
|
|
|
56
|
8 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Position component |
60
|
|
|
* |
61
|
|
|
* All entities inherently have the position component. |
62
|
|
|
* |
63
|
|
|
* @param number $x |
|
|
|
|
64
|
|
|
* @param number $y |
|
|
|
|
65
|
|
|
* @param number $z |
|
|
|
|
66
|
|
|
* @return Entity |
67
|
|
|
*/ |
68
|
8 |
|
public function position($x = 0, $y = 0, $z = 0): EntityInterface |
69
|
|
|
{ |
70
|
8 |
|
$this->component('Position')->update($x, $y, $z); |
|
|
|
|
71
|
8 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Rotation component |
76
|
|
|
* |
77
|
|
|
* All entities inherently have the rotation component. |
78
|
|
|
* |
79
|
|
|
* @param number $x |
|
|
|
|
80
|
|
|
* @param number $y |
|
|
|
|
81
|
|
|
* @param number $z |
|
|
|
|
82
|
|
|
* @return EntityInterface |
|
|
|
|
83
|
|
|
*/ |
84
|
8 |
|
public function rotation($x = 0, $y = 0, $z = 0): EntityInterface |
85
|
|
|
{ |
86
|
8 |
|
$this->component('Rotation')->update($x, $y, $z); |
|
|
|
|
87
|
8 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Scale component |
92
|
|
|
* |
93
|
|
|
* All entities inherently have the scale component. |
94
|
|
|
* |
95
|
|
|
* @param number $x |
|
|
|
|
96
|
|
|
* @param number $y |
|
|
|
|
97
|
|
|
* @param number $z |
|
|
|
|
98
|
|
|
* @return EntityInterface |
|
|
|
|
99
|
|
|
*/ |
100
|
1 |
|
public function scale($x = 0, $y = 0, $z = 0): EntityInterface |
101
|
|
|
{ |
102
|
1 |
|
$this->component('Scale')->update($x, $y, $z); |
|
|
|
|
103
|
1 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Load component for this entity |
108
|
|
|
* |
109
|
|
|
* @param string $component_name |
110
|
|
|
* @throws BadComponentCallException |
111
|
|
|
* @return ComponentInterface |
|
|
|
|
112
|
|
|
*/ |
113
|
8 |
|
public function component(string $component_name): ComponentInterface |
114
|
|
|
{ |
115
|
8 |
|
$component_name = strtolower($component_name); |
116
|
|
|
|
117
|
8 |
|
if (! array_key_exists($component_name, $this->components)) { |
118
|
|
|
try { |
119
|
8 |
|
$component = sprintf('\AframeVR\Components\%s', ucfirst($component_name)); |
120
|
8 |
|
if (class_exists($component)) { |
121
|
8 |
|
$this->components[$component_name] = new $component(); |
122
|
|
|
} else { |
123
|
8 |
|
throw new BadComponentCallException($component_name); |
124
|
|
|
} |
125
|
|
|
} catch (BadComponentCallException $e) { |
126
|
|
|
die($e->getMessage()); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
8 |
|
return $this->components[$component_name]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Handle entity components |
135
|
|
|
* |
136
|
|
|
* Since we might need to customize these to have |
137
|
|
|
* custom components loaded as $this->methosd aswell therefore |
138
|
|
|
* we have these placeholder magic methods here |
139
|
|
|
* |
140
|
|
|
* @param string $component_name |
141
|
|
|
* @param array $args |
142
|
|
|
*/ |
143
|
|
|
public function __call(string $component_name, array $args) |
144
|
|
|
{ |
145
|
|
|
return call_user_func_array($this->{$component_name}->bindTo($this), $args); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Create and add DOM element of the entity |
150
|
|
|
* |
151
|
|
|
* @param unknown $aframe_dom |
152
|
|
|
* @return DOMElement |
153
|
|
|
*/ |
154
|
|
|
public function DOMElement(&$aframe_dom): DOMElement |
155
|
|
|
{ |
156
|
|
|
/* Create entity DOMElement */ |
157
|
|
|
$a_entity = $aframe_dom->createElement('a-entity', "\n"); |
158
|
|
|
foreach ($this->components as $component) { |
159
|
|
|
/** |
160
|
|
|
* Remeve component default properties which are not needed |
161
|
|
|
*/ |
162
|
|
|
$component->removeDefaultDOMAttributes(); |
163
|
|
|
/* |
164
|
|
|
* Check does component has any attributes to add to DOM element. |
165
|
|
|
* default attributes most of cases are ommited so we might not have any attributes to add |
166
|
|
|
*/ |
167
|
|
|
if ($component->hasDOMAttributes()) |
168
|
|
|
$a_entity->setAttributeNode($component->getDOMAttributes()); |
169
|
|
|
} |
170
|
|
|
return $a_entity; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: