forked from jaxwilko/wn-game-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.php
170 lines (153 loc) · 3.54 KB
/
Vector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace JaxWilko\Game\Classes\Engine\Core\Utils;
use JaxWilko\Game\Classes\Engine\Core\Contracts\VectorInterface;
class Vector implements VectorInterface
{
public int $x;
public int $y;
/**
* Creates a new instance and passes args to set().
*
* @param int $x
* @param int $y
*/
public function __construct(int $x = 0, int $y = 0)
{
$this->set($x, $y);
}
/**
* Creates a new instance from a string of format `$x,$y`.
*
* @param string $str
* @return static
*/
public static function fromString(string $str): static
{
return new static(...array_map(fn ($i) => (int) $i, explode(',', $str)));
}
/**
* Returns a clone of $this.
*
* @return $this
*/
public function clone(): static
{
return clone $this;
}
/**
* Sets both the X and the Y properties of the Vector.
*
* @param int $x
* @param int $y
* @return void
*/
public function set(int $x = 0, int $y = 0): void
{
$this->x($x ?? 0);
$this->y($y ?? 0);
}
/**
* Returns the Vector as [$x, $y].
*
* @return array
*/
public function get(): array
{
return [$this->x, $this->y];
}
/**
* Used to both get and set X depending on if a value is provided.
*
* @param int|null $x
* @return int
*/
public function x(int $x = null): int
{
return !is_null($x) ? $this->x = $x : $this->x;
}
/**
* Used to both get and set Y depending on if a value is provided.
*
* @param int|null $y
* @return int
*/
public function y(int $y = null): int
{
return !is_null($y) ? $this->y = $y : $this->y;
}
/**
* Increases / decreases X by an amount, limited by the max.
*
* @param int $amount
* @param int|null $max
* @return int
*/
public function tapX(int $amount = 0, int $max = null): int
{
if (
!is_null($max)
&& (
$amount > 0 && $this->x + $amount >= $max
|| $amount < 0 && $this->x + $amount <= $max
)
) {
return $this->x = $max;
}
return $this->x += $amount;
}
/**
* Increases / decreases Y by an amount, limited by the max.
*
* @param int $amount
* @param int|null $max
* @return int
*/
public function tapY(int $amount = 0, int $max = null): int
{
if (
!is_null($max)
&& (
$amount > 0 && $this->y + $amount >= $max
|| $amount < 0 && $this->y + $amount <= $max
)
) {
return $this->y = $max;
}
return $this->y += $amount;
}
/**
* Creates a new Vector with the adjustments provided.
*
* @param string $prop
* @param int $amount
* @return Vector
*/
public function dry(string $prop, int $amount): Vector
{
return new Vector(
$prop === 'x' ? $this->x + $amount : $this->x,
$prop === 'y' ? $this->y + $amount : $this->y
);
}
/**
* Casts the Vector to a string.
*
* @return string
*/
public function toString(): string
{
return $this->x() . ',' . $this->y();
}
/**
* Casts the Vector to an array.
*
* @return array
*/
public function toArray(): array
{
return [
'x' => $this->x(),
'y' => $this->y()
];
}
}