1 | <?php |
||
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) |
||
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 |
||
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: