1
|
|
|
<?php |
2
|
|
|
/** @formatter:off |
3
|
|
|
* ****************************************************************** |
4
|
|
|
* Created by Marko Kungla on Jun 20, 2016 - 11:21:15 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 Position.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\Components; |
25
|
|
|
|
26
|
|
|
use \AframeVR\Interfaces\ComponentInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The position component defines where an entity is placed in the scene’s world space. |
30
|
|
|
* It takes a coordinate value as three space-delimited numbers. |
31
|
|
|
* |
32
|
|
|
* All entities inherently have the position component. |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
class Position implements ComponentInterface |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Negative X axis extends left. |
39
|
|
|
* Positive X Axis extends right. |
40
|
|
|
* |
41
|
|
|
* @var int $x |
42
|
|
|
*/ |
43
|
|
|
protected $x; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Negative Y axis extends up. |
47
|
|
|
* Positive Y Axis extends down. |
48
|
|
|
* |
49
|
|
|
* @var int $y |
50
|
|
|
*/ |
51
|
|
|
protected $y; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Negative Z axis extends in. |
55
|
|
|
* Positive Z Axis extends out. |
56
|
|
|
* |
57
|
|
|
* @var int $z |
58
|
|
|
*/ |
59
|
|
|
protected $z; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set initial coordinates |
63
|
|
|
* |
64
|
|
|
* @param string $coordinates |
|
|
|
|
65
|
|
|
*/ |
66
|
8 |
|
public function __construct($x = 0, $y = 0, $z = 0) |
67
|
|
|
{ |
68
|
8 |
|
$this->update($x, $y, $z); |
69
|
8 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get Component scripts |
73
|
|
|
* |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function getScripts(): array |
79
|
|
|
{ |
80
|
|
|
return array(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Does component have DOM Atributes |
85
|
|
|
* |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function hasDOMAttributes(): bool |
91
|
|
|
{ |
92
|
|
|
return ! empty(get_object_vars($this)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Remove default DOMElement Attributes which are not required |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
public function removeDefaultDOMAttributes() |
101
|
|
|
{ |
102
|
|
|
if ($this->x === 0 && $this->y === 0 && $this->z === 0) { |
103
|
|
|
unset($this->x); |
104
|
|
|
unset($this->y); |
105
|
|
|
unset($this->z); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get DOMAttr for the entity |
111
|
|
|
* |
112
|
|
|
* @return DOMAttr |
|
|
|
|
113
|
|
|
*/ |
114
|
|
|
public function getDOMAttributes(): \DOMAttr |
115
|
|
|
{ |
116
|
|
|
return new \DOMAttr('position', sprintf('%s %s %s', $this->x, $this->y, $this->z)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Update coordinates |
121
|
|
|
* |
122
|
|
|
* @param string $coordinates |
|
|
|
|
123
|
|
|
*/ |
124
|
8 |
|
public function update($x = 0, $y = 0, $z = 0) |
125
|
|
|
{ |
126
|
8 |
|
$this->x = $x ?? 0; |
127
|
8 |
|
$this->y = $y ?? 0; |
128
|
8 |
|
$this->z = $z ?? 0; |
129
|
|
|
} |
130
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.