1 | <?php |
||
33 | final class Scene |
||
34 | { |
||
35 | /* All scenes can use primitives */ |
||
36 | use Primitives; |
||
37 | |||
38 | /** |
||
39 | * Name with what you can retrieve this scene while working with multiple scenes |
||
40 | * |
||
41 | * @var string $name |
||
42 | */ |
||
43 | private $keyword; |
||
44 | |||
45 | /** |
||
46 | * Is scene prepared for rendering |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | private $prepared; |
||
51 | |||
52 | /** |
||
53 | * Assets |
||
54 | * |
||
55 | * @var \AframeVR\Core\Assets |
||
56 | */ |
||
57 | protected $assets; |
||
58 | |||
59 | /** |
||
60 | * Aframe Document Object Model |
||
61 | * |
||
62 | * @var \AframeVR\Core\DOM\AframeDOMDocument |
||
63 | */ |
||
64 | protected $aframeDomObj; |
||
65 | |||
66 | /** |
||
67 | * A-Frame scene entities |
||
68 | * |
||
69 | * @var array $entities |
||
70 | */ |
||
71 | protected $entities = array(); |
||
72 | |||
73 | /** |
||
74 | * Scene components |
||
75 | * |
||
76 | * @var array $components |
||
77 | */ |
||
78 | protected $components = array(); |
||
79 | |||
80 | /** |
||
81 | * Scene constructor |
||
82 | * |
||
83 | * @param string $keyword |
||
84 | * @param Config $config |
||
85 | */ |
||
86 | 98 | public function __construct(string $keyword, Config $config) |
|
93 | |||
94 | /** |
||
95 | * Aframe Document Object Model |
||
96 | * |
||
97 | * @api |
||
98 | * |
||
99 | * @return \AframeVR\Core\DOM\AframeDOMDocument |
||
100 | */ |
||
101 | 2 | public function dom() |
|
105 | |||
106 | /** |
||
107 | * Entity |
||
108 | * |
||
109 | * @api |
||
110 | * |
||
111 | * @param string $id |
||
112 | * @return \AframeVR\Core\Entity |
||
113 | */ |
||
114 | 51 | public function entity(string $id = 'untitled'): Entity |
|
118 | |||
119 | /** |
||
120 | * Assets |
||
121 | * |
||
122 | * @api |
||
123 | * |
||
124 | * @return \AframeVR\Core\Assets |
||
125 | */ |
||
126 | 98 | public function asset(): Assets |
|
130 | |||
131 | /** |
||
132 | * Render the A-Frame scene and return the HTML |
||
133 | * |
||
134 | * @api |
||
135 | * |
||
136 | * @param bool $only_scene |
||
137 | * @return string |
||
138 | */ |
||
139 | 19 | public function save($only_scene = false, string $file = null): string |
|
149 | |||
150 | /** |
||
151 | * Render the A-Frame scene and passthru HTML |
||
152 | * |
||
153 | * @api |
||
154 | * |
||
155 | * @param bool $only_scene |
||
156 | * @return void |
||
157 | */ |
||
158 | 1 | public function render($only_scene = false) |
|
162 | |||
163 | /** |
||
164 | * Alias of AframeDOMDocument::setTitle(string) |
||
165 | * |
||
166 | * Set <title> tag |
||
167 | * $this->dom()->setTitle(string); |
||
168 | * |
||
169 | * @api |
||
170 | * |
||
171 | * @param string $title |
||
172 | */ |
||
173 | 1 | public function title(string $title) |
|
177 | |||
178 | /** |
||
179 | * Alias of AframeDOMDocument::setDescription(string) |
||
180 | * |
||
181 | * Set <meta name="description"> tag |
||
182 | * $this->dom()->setDescription(string); |
||
183 | * |
||
184 | * @api |
||
185 | * |
||
186 | * @param string $description |
||
187 | */ |
||
188 | 1 | public function description(string $description) |
|
192 | |||
193 | /** |
||
194 | * Get scenes keyword |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | 1 | public function getKeyword() |
|
202 | |||
203 | /** |
||
204 | * Load component for this entity |
||
205 | * |
||
206 | * @param string $component_name |
||
207 | * @throws \AframeVR\Core\Exceptions\BadComponentCallException |
||
208 | * @return object|null |
||
209 | */ |
||
210 | 2 | public function component(string $component_name) |
|
211 | { |
||
212 | 2 | if (! array_key_exists($component_name, $this->components)) { |
|
213 | 2 | $component = sprintf('\AframeVR\Core\Components\ascene\%s\%sComponent', ucfirst($component_name), |
|
214 | ucfirst($component_name)); |
||
215 | 2 | if (class_exists($component)) { |
|
216 | 1 | $this->components[$component_name] = new $component(); |
|
217 | } else { |
||
218 | 1 | throw new BadComponentCallException($component_name); |
|
219 | } |
||
220 | } |
||
221 | |||
222 | 1 | return $this->components[$component_name] ?? null; |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * Call |
||
227 | * |
||
228 | * @param string $method |
||
229 | * @param array $args |
||
230 | * @throws BadShaderCallException |
||
231 | * @return Entity|\AframeVR\Interfaces\ComponentInterface |
||
232 | */ |
||
233 | 28 | public function __call(string $method, array $args) |
|
234 | { |
||
235 | 28 | $id = $args[0] ?? 0; |
|
236 | 28 | $primitive = sprintf('\AframeVR\Extras\Primitives\%s', ucfirst($method)); |
|
237 | |||
238 | 28 | if (class_exists($primitive)) { |
|
239 | 26 | return $this->childrens[$id] ?? (is_int($id) ? $this->childrens[array_push($this->childrens, |
|
240 | 26 | new $primitive($id)) - 1] : $this->childrens[$id] = new $primitive($id)); |
|
241 | } else { |
||
242 | 2 | return $this->callComponent($method, $args); |
|
243 | } |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Handle entity components |
||
248 | * |
||
249 | * Since we might need to customize these to have |
||
250 | * custom components loaded as $this->methosd aswell therefore |
||
251 | * we have these placeholder magic methods here |
||
252 | * |
||
253 | * @param string $component_name |
||
254 | * @param array $args |
||
255 | */ |
||
256 | 2 | public function callComponent(string $component_name, array $args) |
|
257 | { |
||
258 | 2 | if (! method_exists($this, $component_name)) { |
|
259 | 2 | $this->{$component_name} = Closure::bind( |
|
260 | 2 | function () use ($component_name) { |
|
261 | 2 | return $this->component($component_name); |
|
262 | 2 | }, $this, get_class()); |
|
263 | } |
||
264 | |||
265 | 2 | return call_user_func($this->{$component_name}, $args); |
|
266 | } |
||
267 | |||
268 | /** |
||
269 | * Add everyting to DOM |
||
270 | * |
||
271 | * @return void |
||
272 | */ |
||
273 | 19 | protected function prepare() |
|
289 | } |
||
290 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.