1
|
|
|
<?php |
2
|
|
|
/** @formatter:off |
3
|
|
|
* ****************************************************************** |
4
|
|
|
* Created by Marko Kungla on Jun 25, 2016 - 7:51:42 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 PositionComponent.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\Components\Position; |
25
|
|
|
|
26
|
|
|
use \AframeVR\Interfaces\Core\Components\PositionCMPTIF; |
27
|
|
|
use \AframeVR\Core\Helpers\ComponentAbstract; |
28
|
|
|
use \AframeVR\Core\Helpers\ComponentHelper; |
29
|
|
|
|
30
|
|
View Code Duplication |
class PositionComponent extends ComponentAbstract implements PositionCMPTIF |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
use ComponentHelper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initialize Position Component |
36
|
|
|
* |
37
|
|
|
* The position component defines where an entity is placed in the scene's world space. |
38
|
|
|
* It takes a coordinate value as three space-delimited numbers. |
39
|
|
|
* |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
* |
44
|
|
|
*/ |
45
|
67 |
|
public function initializeComponent(): bool |
46
|
|
|
{ |
47
|
67 |
|
$this->setDomAttribute('position'); |
48
|
|
|
|
49
|
67 |
|
return true; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return DOM attribute contents |
54
|
|
|
* |
55
|
|
|
* Position Components dom atribute contains coordinates to point in world |
56
|
|
|
* Ex: position="1 1 1" |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
6 |
|
public function getDomAttributeString(): string |
61
|
|
|
{ |
62
|
6 |
|
$attrs = $this->getDOMAttributesArray(); |
63
|
|
|
|
64
|
6 |
|
return $this->createCoordinateString($attrs['x'], $attrs['y'], $attrs['z']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get current position coordinates |
69
|
|
|
* |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
1 |
|
public function getPosition(): string |
75
|
|
|
{ |
76
|
1 |
|
return $this->getDomAttributeString(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Negative X axis extends left. |
81
|
|
|
* Positive X Axis extends right. |
82
|
|
|
* |
83
|
|
|
* @param float $x_axis |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
15 |
|
public function positionX(float $x_axis) |
87
|
|
|
{ |
88
|
15 |
|
$this->init(); |
89
|
15 |
|
$this->dom_attributes['x'] = $x_axis; |
90
|
15 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Negative Y axis extends up. |
94
|
|
|
* Positive Y Axis extends down. |
95
|
|
|
* |
96
|
|
|
* @param float $y_axis |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
14 |
|
public function positionY(float $y_axis) |
100
|
|
|
{ |
101
|
14 |
|
$this->init(); |
102
|
14 |
|
$this->dom_attributes['y'] = $y_axis; |
103
|
14 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Negative Z axis extends in. |
107
|
|
|
* Positive Z Axis extends out. |
108
|
|
|
* |
109
|
|
|
* @param float $z_axis |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
14 |
|
public function positionZ(float $z_axis) |
113
|
|
|
{ |
114
|
14 |
|
$this->init(); |
115
|
14 |
|
$this->dom_attributes['z'] = $z_axis; |
116
|
14 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* When any position component methods are called then init others |
120
|
|
|
* |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
15 |
|
private function init() |
124
|
|
|
{ |
125
|
15 |
|
$this->dom_attributes['x'] = $this->dom_attributes['x'] ?? 0; |
126
|
15 |
|
$this->dom_attributes['y'] = $this->dom_attributes['y'] ?? 0; |
127
|
15 |
|
$this->dom_attributes['z'] = $this->dom_attributes['z'] ?? 0; |
128
|
|
|
} |
129
|
|
|
} |
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.