@woosh/meep-engine 2.31.1 → 2.31.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/binary/BitSet.js +5 -1
- package/core/collection/RingBuffer.js +16 -1
- package/core/geom/Vector1.js +228 -219
- package/core/geom/Vector2.js +547 -502
- package/core/geom/Vector3.js +8 -0
- package/core/geom/Vector4.js +322 -311
- package/core/graph/v2/Graph.js +6 -6
- package/core/model/ObservedBoolean.js +12 -4
- package/core/process/worker/WorkerBuilder.js +27 -9
- package/core/process/worker/WorkerProxy.js +6 -0
- package/editor/Editor.js +10 -1
- package/editor/process/symbolic/makePositionedIconDisplaySymbol.js +3 -1
- package/engine/EngineHarness.js +1 -1
- package/engine/ecs/parent/ParentEntitySystem.js +1 -1
- package/engine/ecs/terrain/ecs/Terrain.js +11 -2
- package/engine/ecs/terrain/ecs/makeTerrainWorkerProxy.js +10 -10
- package/engine/graphics/camera/makeOrbitalCameraController.js +7 -1
- package/engine/graphics/ecs/camera/topdown/TopDownCameraControllerSystem.js +4 -31
- package/engine/graphics/ecs/mesh/Mesh.js +9 -3
- package/engine/graphics/ecs/path/entity/EntityPath.js +7 -7
- package/engine/graphics/ecs/trail2d/Trail2DSystem.js +31 -16
- package/engine/graphics/micron/build/buildMicronGeometryFromBufferGeometry.js +17 -8
- package/engine/graphics/micron/build/graph/metis/metis.js +23 -33
- package/engine/graphics/micron/render/v1/MaterialContext.js +21 -2
- package/engine/graphics/particles/particular/engine/renderers/billboard/prototypeBillboardRenderer.js +1 -1
- package/engine/graphics/particles/particular/engine/simulator/SimulationStepFixedPhysics.js +1 -1
- package/engine/graphics/render/buffer/buffers/NormalFrameBuffer.js +25 -16
- package/engine/graphics/render/buffer/buffers/prototypeNormalFrameBuffer.js +6 -5
- package/engine/graphics/trail/x/RibbonMaterialX.js +7 -2
- package/engine/grid/components/GridPosition.js +22 -122
- package/engine/grid/components/GridPositionSerializationAdapter.js +58 -0
- package/engine/grid/components/GridPositionSerializationUpdater_0_1.js +37 -0
- package/engine/intelligence/mcts/MonteCarlo.js +12 -15
- package/engine/intelligence/mcts/StateNode.js +3 -3
- package/generation/GridTags.js +3 -2
- package/generation/example/SampleGenerator0.js +1 -1
- package/generation/example/generators/{mir_generator_place_buff_objects.js → interactive/mir_generator_place_buff_objects.js} +97 -26
- package/generation/example/generators/mir_generator_place_starting_point.js +12 -5
- package/generation/example/grid/configureMirGrid.js +1 -1
- package/generation/filtering/CellFilter.js +14 -0
- package/generation/filtering/numeric/util/CellFilterLogToConsole.js +2 -1
- package/generation/grid/GridData.js +18 -14
- package/generation/grid/MarkerMatchCounter.js +23 -0
- package/generation/grid/generation/GridTaskApplyActionToCells.js +86 -0
- package/generation/grid/generation/grid/select/CellSupplier.js +28 -0
- package/generation/grid/generation/grid/select/CellSupplierBestN.js +188 -0
- package/generation/grid/generation/grid/select/CellSupplierPathUpHill.js +187 -0
- package/generation/markers/GridActionRuleSet.js +1 -1
- package/generation/markers/MarkerNode.js +18 -0
- package/generation/markers/debug/visualizeMarkers.js +8 -1
- package/generation/markers/transform/MarkerNodeTransformerSequence.js +47 -0
- package/generation/placement/GridCellActionTransformNearbyMarkers.js +9 -17
- package/generation/rules/CellMatcherFromFilter.js +38 -0
- package/generation/rules/cell/CellMatcherContainsMarkerWithinRadius.js +1 -1
- package/package.json +3 -3
package/core/geom/Vector2.js
CHANGED
|
@@ -10,444 +10,619 @@ import { lerp } from "../math/lerp.js";
|
|
|
10
10
|
import { max2 } from "../math/max2.js";
|
|
11
11
|
import { min2 } from "../math/min2.js";
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
class Vector2 {
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* @param {number} [x=0
|
|
17
|
+
* @param {number} [y=0]
|
|
18
|
+
* @property {number} x
|
|
19
|
+
* @property {number} y
|
|
20
|
+
* @constructor
|
|
21
|
+
*/
|
|
22
|
+
constructor(x = 0, y = 0) {
|
|
23
|
+
assert.isNumber(x, 'x');
|
|
24
|
+
assert.isNumber(y, 'y');
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
assert.notNaN(x, 'x');
|
|
27
|
+
assert.notNaN(y, 'y');
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
*
|
|
31
|
+
* @type {number}
|
|
32
|
+
*/
|
|
33
|
+
this.x = x;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
*
|
|
37
|
+
* @type {number}
|
|
38
|
+
*/
|
|
39
|
+
this.y = y;
|
|
40
|
+
|
|
41
|
+
this.onChanged = new Signal();
|
|
42
|
+
}
|
|
27
43
|
|
|
28
44
|
/**
|
|
29
45
|
*
|
|
30
|
-
* @
|
|
46
|
+
* @param {number[]} array
|
|
47
|
+
* @param {number} offset
|
|
31
48
|
*/
|
|
32
|
-
|
|
49
|
+
readFromArray(array, offset) {
|
|
50
|
+
this.set(
|
|
51
|
+
array[offset],
|
|
52
|
+
array[offset + 1]
|
|
53
|
+
);
|
|
54
|
+
}
|
|
33
55
|
|
|
34
56
|
/**
|
|
35
57
|
*
|
|
36
|
-
* @
|
|
58
|
+
* @param {number[]} array
|
|
59
|
+
* @param {number} offset
|
|
37
60
|
*/
|
|
38
|
-
|
|
61
|
+
writeToArray(array, offset) {
|
|
62
|
+
array[offset] = this.x;
|
|
63
|
+
array[offset + 1] = this.y;
|
|
64
|
+
}
|
|
39
65
|
|
|
40
|
-
|
|
41
|
-
|
|
66
|
+
/**
|
|
67
|
+
*
|
|
68
|
+
* @param {number} x
|
|
69
|
+
* @param {number} y
|
|
70
|
+
* @returns {Vector2}
|
|
71
|
+
*/
|
|
72
|
+
set(x, y) {
|
|
73
|
+
assert.isNumber(x, 'x');
|
|
74
|
+
assert.isNumber(y, 'y');
|
|
42
75
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
* @type {Vector2}
|
|
46
|
-
*/
|
|
47
|
-
Vector2.up = Object.freeze(new Vector2(0, 1));
|
|
76
|
+
assert.notNaN(x, 'x');
|
|
77
|
+
assert.notNaN(y, 'y');
|
|
48
78
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
* @type {Vector2}
|
|
52
|
-
*/
|
|
53
|
-
Vector2.down = Object.freeze(new Vector2(0, -1));
|
|
79
|
+
const oldX = this.x;
|
|
80
|
+
const oldY = this.y;
|
|
54
81
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
*/
|
|
59
|
-
Vector2.left = Object.freeze(new Vector2(-1, 0));
|
|
82
|
+
if (oldX !== x || oldY !== y) {
|
|
83
|
+
this.x = x;
|
|
84
|
+
this.y = y;
|
|
60
85
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
86
|
+
if (this.onChanged.hasHandlers()) {
|
|
87
|
+
this.onChanged.send4(x, y, oldX, oldY);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
66
92
|
|
|
67
|
-
/**
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
* @readonly
|
|
74
|
-
* @type {Vector2}
|
|
75
|
-
*/
|
|
76
|
-
Vector2.one = Object.freeze(new Vector2(1, 1));
|
|
93
|
+
/**
|
|
94
|
+
*
|
|
95
|
+
* @param {number} x
|
|
96
|
+
* @param {number} y
|
|
97
|
+
*/
|
|
98
|
+
setSilent(x, y) {
|
|
77
99
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
* @type {boolean}
|
|
81
|
-
*/
|
|
82
|
-
Vector2.prototype.isVector2 = true;
|
|
100
|
+
this.x = x;
|
|
101
|
+
this.y = y;
|
|
83
102
|
|
|
84
|
-
|
|
85
|
-
*
|
|
86
|
-
* @param {number[]} array
|
|
87
|
-
* @param {number} offset
|
|
88
|
-
*/
|
|
89
|
-
Vector2.prototype.readFromArray = function (array, offset) {
|
|
90
|
-
this.set(
|
|
91
|
-
array[offset],
|
|
92
|
-
array[offset + 1]
|
|
93
|
-
);
|
|
94
|
-
};
|
|
103
|
+
}
|
|
95
104
|
|
|
96
|
-
/**
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
};
|
|
105
|
+
/**
|
|
106
|
+
*
|
|
107
|
+
* @param {number} x
|
|
108
|
+
* @returns {Vector2}
|
|
109
|
+
*/
|
|
110
|
+
setX(x) {
|
|
111
|
+
return this.set(x, this.y);
|
|
112
|
+
}
|
|
105
113
|
|
|
106
|
-
/**
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
assert.isNumber(y, 'y');
|
|
114
|
+
/**
|
|
115
|
+
*
|
|
116
|
+
* @param {number} y
|
|
117
|
+
* @returns {Vector2}
|
|
118
|
+
*/
|
|
119
|
+
setY(y) {
|
|
120
|
+
return this.set(this.x, y);
|
|
121
|
+
}
|
|
115
122
|
|
|
116
|
-
|
|
117
|
-
|
|
123
|
+
/**
|
|
124
|
+
*
|
|
125
|
+
* @param {number} x
|
|
126
|
+
* @param {number} y
|
|
127
|
+
* @returns {Vector2}
|
|
128
|
+
*/
|
|
129
|
+
_sub(x, y) {
|
|
130
|
+
return this.set(this.x - x, this.y - y);
|
|
131
|
+
}
|
|
118
132
|
|
|
119
|
-
|
|
120
|
-
|
|
133
|
+
/**
|
|
134
|
+
*
|
|
135
|
+
* @param {Vector2} other
|
|
136
|
+
* @returns {Vector2}
|
|
137
|
+
*/
|
|
138
|
+
sub(other) {
|
|
139
|
+
return this._sub(other.x, other.y);
|
|
140
|
+
}
|
|
121
141
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
142
|
+
/**
|
|
143
|
+
*
|
|
144
|
+
* @param {Vector2} a
|
|
145
|
+
* @param {Vector2} b
|
|
146
|
+
*/
|
|
147
|
+
subVectors(a, b) {
|
|
148
|
+
this.set(a.x - b.x, a.y - b.y);
|
|
149
|
+
}
|
|
125
150
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
151
|
+
/**
|
|
152
|
+
* performs Math.floor operation on x and y
|
|
153
|
+
* @returns {Vector2}
|
|
154
|
+
*/
|
|
155
|
+
floor() {
|
|
156
|
+
return this.set(Math.floor(this.x), Math.floor(this.y));
|
|
129
157
|
}
|
|
130
|
-
return this;
|
|
131
|
-
};
|
|
132
158
|
|
|
133
|
-
/**
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
159
|
+
/**
|
|
160
|
+
* performs Math.ceil operation on x and y
|
|
161
|
+
* @returns {Vector2}
|
|
162
|
+
*/
|
|
163
|
+
ceil() {
|
|
164
|
+
return this.set(Math.ceil(this.x), Math.ceil(this.y));
|
|
165
|
+
}
|
|
139
166
|
|
|
140
|
-
|
|
141
|
-
|
|
167
|
+
/**
|
|
168
|
+
* Round both components to nearest integer
|
|
169
|
+
*/
|
|
170
|
+
round() {
|
|
171
|
+
const x = Math.round(this.x);
|
|
172
|
+
const y = Math.round(this.y);
|
|
173
|
+
this.set(x, y);
|
|
174
|
+
}
|
|
142
175
|
|
|
143
|
-
|
|
176
|
+
/**
|
|
177
|
+
* performs Math.abs operation on x and y
|
|
178
|
+
* @returns {Vector2}
|
|
179
|
+
*/
|
|
180
|
+
abs() {
|
|
181
|
+
return this.set(Math.abs(this.x), Math.abs(this.y));
|
|
182
|
+
}
|
|
144
183
|
|
|
145
|
-
/**
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
184
|
+
/**
|
|
185
|
+
*
|
|
186
|
+
* @param {number} x
|
|
187
|
+
* @param {number} y
|
|
188
|
+
* @returns {Vector2}
|
|
189
|
+
*/
|
|
190
|
+
_mod(x, y) {
|
|
191
|
+
return this.set(this.x % x, this.y % y);
|
|
192
|
+
}
|
|
153
193
|
|
|
154
|
-
/**
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
194
|
+
/**
|
|
195
|
+
*
|
|
196
|
+
* @param {Vector2} other
|
|
197
|
+
* @returns {Vector2}
|
|
198
|
+
*/
|
|
199
|
+
mod(other) {
|
|
200
|
+
return this._mod(other.x, other.y);
|
|
201
|
+
}
|
|
162
202
|
|
|
163
|
-
/**
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
};
|
|
203
|
+
/**
|
|
204
|
+
*
|
|
205
|
+
* @param {Vector2} other
|
|
206
|
+
* @returns {Vector2}
|
|
207
|
+
*/
|
|
208
|
+
divide(other) {
|
|
209
|
+
return this.set(this.x / other.x, this.y / other.y);
|
|
210
|
+
}
|
|
172
211
|
|
|
173
|
-
/**
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
212
|
+
/**
|
|
213
|
+
*
|
|
214
|
+
* @param {Vector2} other
|
|
215
|
+
* @returns {Vector2}
|
|
216
|
+
*/
|
|
217
|
+
multiply(other) {
|
|
218
|
+
return this._multiply(other.x, other.y);
|
|
219
|
+
}
|
|
181
220
|
|
|
182
|
-
/**
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
221
|
+
/**
|
|
222
|
+
*
|
|
223
|
+
* @param {number} x
|
|
224
|
+
* @param {number} y
|
|
225
|
+
* @returns {Vector2}
|
|
226
|
+
*/
|
|
227
|
+
_multiply(x, y) {
|
|
228
|
+
return this.set(this.x * x, this.y * y);
|
|
229
|
+
}
|
|
189
230
|
|
|
190
|
-
/**
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
231
|
+
/**
|
|
232
|
+
*
|
|
233
|
+
* @param {Vector2} other
|
|
234
|
+
* @returns {Vector2}
|
|
235
|
+
*/
|
|
236
|
+
max(other) {
|
|
237
|
+
const x = max2(this.x, other.x);
|
|
238
|
+
const y = max2(this.y, other.y);
|
|
239
|
+
return this.set(x, y);
|
|
240
|
+
}
|
|
197
241
|
|
|
198
|
-
/**
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
242
|
+
/**
|
|
243
|
+
*
|
|
244
|
+
* @param {Vector2} other
|
|
245
|
+
* @returns {number}
|
|
246
|
+
*/
|
|
247
|
+
dot(other) {
|
|
248
|
+
return v2_dot(this.x, this.y, other.x, other.y);
|
|
249
|
+
}
|
|
206
250
|
|
|
207
|
-
/**
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
251
|
+
/**
|
|
252
|
+
*
|
|
253
|
+
* @param {Vector2} other
|
|
254
|
+
* @returns {Vector2}
|
|
255
|
+
*/
|
|
256
|
+
copy(other) {
|
|
257
|
+
return this.set(other.x, other.y);
|
|
258
|
+
}
|
|
214
259
|
|
|
215
|
-
/**
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return this.set(this.x % x, this.y % y);
|
|
223
|
-
};
|
|
260
|
+
/**
|
|
261
|
+
*
|
|
262
|
+
* @returns {Vector2}
|
|
263
|
+
*/
|
|
264
|
+
clone() {
|
|
265
|
+
return new Vector2(this.x, this.y);
|
|
266
|
+
}
|
|
224
267
|
|
|
225
|
-
/**
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
};
|
|
268
|
+
/**
|
|
269
|
+
*
|
|
270
|
+
* @returns {Vector2}
|
|
271
|
+
*/
|
|
272
|
+
negate() {
|
|
273
|
+
return this.set(-this.x, -this.y);
|
|
274
|
+
}
|
|
233
275
|
|
|
234
|
-
/**
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
276
|
+
/**
|
|
277
|
+
*
|
|
278
|
+
* @param {number} x
|
|
279
|
+
* @param {number} y
|
|
280
|
+
* @returns {Vector2}
|
|
281
|
+
*/
|
|
282
|
+
_add(x, y) {
|
|
283
|
+
return this.set(this.x + x, this.y + y);
|
|
284
|
+
}
|
|
242
285
|
|
|
243
|
-
/**
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
286
|
+
/**
|
|
287
|
+
*
|
|
288
|
+
* @param {Vector2} other
|
|
289
|
+
* @returns {Vector2}
|
|
290
|
+
*/
|
|
291
|
+
add(other) {
|
|
292
|
+
return this._add(other.x, other.y);
|
|
293
|
+
}
|
|
251
294
|
|
|
252
|
-
/**
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
return this.set(this.x * x, this.y * y);
|
|
260
|
-
};
|
|
295
|
+
/**
|
|
296
|
+
*
|
|
297
|
+
* @param {Number} val
|
|
298
|
+
*/
|
|
299
|
+
addScalar(val) {
|
|
300
|
+
return this._add(val, val);
|
|
301
|
+
}
|
|
261
302
|
|
|
262
|
-
/**
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
const y = max2(this.y, other.y);
|
|
270
|
-
return this.set(x, y);
|
|
271
|
-
};
|
|
303
|
+
/**
|
|
304
|
+
*
|
|
305
|
+
* @param {number} val
|
|
306
|
+
*/
|
|
307
|
+
setScalar(val) {
|
|
308
|
+
this.set(val, val);
|
|
309
|
+
}
|
|
272
310
|
|
|
311
|
+
/**
|
|
312
|
+
*
|
|
313
|
+
* @param {number} val
|
|
314
|
+
*/
|
|
315
|
+
divideScalar(val) {
|
|
316
|
+
this.multiplyScalar(1 / val);
|
|
317
|
+
}
|
|
273
318
|
|
|
274
|
-
/**
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
319
|
+
/**
|
|
320
|
+
*
|
|
321
|
+
* @param {Number} val
|
|
322
|
+
* @returns {Vector2}
|
|
323
|
+
*/
|
|
324
|
+
multiplyScalar(val) {
|
|
325
|
+
assert.isNumber(val, 'val');
|
|
326
|
+
assert.notNaN(val, 'val');
|
|
282
327
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
* @returns {Vector2}
|
|
286
|
-
*/
|
|
287
|
-
Vector2.prototype.clone = function () {
|
|
288
|
-
return new Vector2(this.x, this.y);
|
|
289
|
-
};
|
|
328
|
+
return this.set(this.x * val, this.y * val);
|
|
329
|
+
}
|
|
290
330
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
*/
|
|
295
|
-
Vector2.prototype.negate = function () {
|
|
296
|
-
return this.set(-this.x, -this.y);
|
|
297
|
-
};
|
|
331
|
+
toJSON() {
|
|
332
|
+
return { x: this.x, y: this.y };
|
|
333
|
+
}
|
|
298
334
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
* @param {number} y
|
|
303
|
-
* @returns {Vector2}
|
|
304
|
-
*/
|
|
305
|
-
Vector2.prototype._add = function (x, y) {
|
|
306
|
-
return this.set(this.x + x, this.y + y);
|
|
307
|
-
};
|
|
308
|
-
/**
|
|
309
|
-
*
|
|
310
|
-
* @param {Vector2} other
|
|
311
|
-
* @returns {Vector2}
|
|
312
|
-
*/
|
|
313
|
-
Vector2.prototype.add = function (other) {
|
|
314
|
-
return this._add(other.x, other.y);
|
|
315
|
-
};
|
|
316
|
-
/**
|
|
317
|
-
*
|
|
318
|
-
* @param {Number} val
|
|
319
|
-
*/
|
|
320
|
-
Vector2.prototype.addScalar = function (val) {
|
|
321
|
-
return this._add(val, val);
|
|
322
|
-
};
|
|
335
|
+
fromJSON({ x = 0, y = 0 }) {
|
|
336
|
+
this.set(x, y);
|
|
337
|
+
}
|
|
323
338
|
|
|
324
|
-
/**
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
339
|
+
/**
|
|
340
|
+
*
|
|
341
|
+
* @param {BinaryBuffer} buffer
|
|
342
|
+
*/
|
|
343
|
+
toBinaryBuffer(buffer) {
|
|
344
|
+
buffer.writeFloat64(this.x);
|
|
345
|
+
buffer.writeFloat64(this.y);
|
|
346
|
+
}
|
|
331
347
|
|
|
332
|
-
/**
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
348
|
+
/**
|
|
349
|
+
*
|
|
350
|
+
* @param {BinaryBuffer} buffer
|
|
351
|
+
*/
|
|
352
|
+
fromBinaryBuffer(buffer) {
|
|
353
|
+
const x = buffer.readFloat64();
|
|
354
|
+
const y = buffer.readFloat64();
|
|
339
355
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
356
|
+
this.set(x, y);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
*
|
|
361
|
+
* @param {BinaryBuffer} buffer
|
|
362
|
+
*/
|
|
363
|
+
toBinaryBufferFloat32(buffer) {
|
|
364
|
+
buffer.writeFloat32(this.x);
|
|
365
|
+
buffer.writeFloat32(this.y);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
*
|
|
370
|
+
* @param {BinaryBuffer} buffer
|
|
371
|
+
*/
|
|
372
|
+
fromBinaryBufferFloat32(buffer) {
|
|
373
|
+
const x = buffer.readFloat32();
|
|
374
|
+
const y = buffer.readFloat32();
|
|
348
375
|
|
|
349
|
-
|
|
350
|
-
}
|
|
376
|
+
this.set(x, y);
|
|
377
|
+
}
|
|
351
378
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
}
|
|
379
|
+
/**
|
|
380
|
+
*
|
|
381
|
+
* @returns {boolean}
|
|
382
|
+
*/
|
|
383
|
+
isZero() {
|
|
384
|
+
return this.x === 0 && this.y === 0;
|
|
385
|
+
}
|
|
355
386
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
}
|
|
387
|
+
/**
|
|
388
|
+
*
|
|
389
|
+
* @param {Number} minX
|
|
390
|
+
* @param {Number} minY
|
|
391
|
+
* @param {Number} maxX
|
|
392
|
+
* @param {Number} maxY
|
|
393
|
+
*/
|
|
394
|
+
clamp(minX, minY, maxX, maxY) {
|
|
395
|
+
const x = clamp(this.x, minX, maxX);
|
|
396
|
+
const y = clamp(this.y, minY, maxY);
|
|
397
|
+
return this.set(x, y);
|
|
398
|
+
}
|
|
359
399
|
|
|
360
|
-
/**
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
400
|
+
/**
|
|
401
|
+
*
|
|
402
|
+
* @param {Number} lowX
|
|
403
|
+
* @param {Number} lowY
|
|
404
|
+
*/
|
|
405
|
+
clampLow(lowX, lowY) {
|
|
406
|
+
const x = max2(this.x, lowX);
|
|
407
|
+
const y = max2(this.y, lowY);
|
|
408
|
+
return this.set(x, y);
|
|
409
|
+
}
|
|
368
410
|
|
|
369
|
-
/**
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
411
|
+
/**
|
|
412
|
+
*
|
|
413
|
+
* @param {Number} highX
|
|
414
|
+
* @param {Number} highY
|
|
415
|
+
*/
|
|
416
|
+
clampHigh(highX, highY) {
|
|
417
|
+
const x = min2(this.x, highX);
|
|
418
|
+
const y = min2(this.y, highY);
|
|
419
|
+
return this.set(x, y);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
*
|
|
424
|
+
* @param {Vector2} other
|
|
425
|
+
* @returns {number}
|
|
426
|
+
*/
|
|
427
|
+
distanceSqrTo(other) {
|
|
428
|
+
return this._distanceSqrTo(other.x, other.y);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
*
|
|
433
|
+
* @param {Vector2} a
|
|
434
|
+
* @param {Vector2} b
|
|
435
|
+
* @param {number} fraction
|
|
436
|
+
*/
|
|
437
|
+
lerpVectors(a, b, fraction) {
|
|
438
|
+
v2Lerp(this, a, b, fraction);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
*
|
|
443
|
+
* @param {number[]} matrix3
|
|
444
|
+
*/
|
|
445
|
+
applyMatrix3(matrix3) {
|
|
446
|
+
const x = this.x;
|
|
447
|
+
const y = this.y;
|
|
448
|
+
|
|
449
|
+
const _x = matrix3[0] * x + matrix3[3] * y + matrix3[6];
|
|
450
|
+
const _y = matrix3[1] * x + matrix3[4] * y + matrix3[7];
|
|
451
|
+
|
|
452
|
+
this.set(_x, _y);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
*
|
|
457
|
+
* @param {Vector2} other
|
|
458
|
+
* @returns {number}
|
|
459
|
+
*/
|
|
460
|
+
distanceTo(other) {
|
|
461
|
+
return this._distanceTo(other.x, other.y);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
*
|
|
466
|
+
* @param {number} x
|
|
467
|
+
* @param {number} y
|
|
468
|
+
* @returns {number}
|
|
469
|
+
*/
|
|
470
|
+
_distanceSqrTo(x, y) {
|
|
471
|
+
const dx = this.x - x;
|
|
472
|
+
const dy = this.y - y;
|
|
473
|
+
return magnitudeSqr(dx, dy);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
*
|
|
478
|
+
* @param {number} x
|
|
479
|
+
* @param {number} y
|
|
480
|
+
* @returns {number}
|
|
481
|
+
*/
|
|
482
|
+
_distanceTo(x, y) {
|
|
483
|
+
return Math.sqrt(this._distanceSqrTo(x, y));
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
*
|
|
488
|
+
* @param {Vector2} other
|
|
489
|
+
* @returns {number}
|
|
490
|
+
*/
|
|
491
|
+
manhattanDistanceTo(other) {
|
|
492
|
+
const dx = Math.abs(this.x - other.x);
|
|
493
|
+
const dy = Math.abs(this.y - other.y);
|
|
494
|
+
|
|
495
|
+
return dx + dy;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* @returns {number}
|
|
500
|
+
*/
|
|
501
|
+
length() {
|
|
502
|
+
return v2_magnitude(this.x, this.y);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Normalizes the vector, preserving its direction, but making magnitude equal to 1
|
|
507
|
+
*/
|
|
508
|
+
normalize() {
|
|
509
|
+
const l = this.length();
|
|
510
|
+
|
|
511
|
+
if (l === 0) {
|
|
512
|
+
//special case, can't normalize 0 length vector
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const m = 1 / l;
|
|
517
|
+
|
|
518
|
+
this.multiplyScalar(m);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
*
|
|
524
|
+
* @returns {number}
|
|
525
|
+
*/
|
|
526
|
+
hashCode() {
|
|
527
|
+
const x = Math.sin(this.x) * 1367130550;
|
|
528
|
+
const y = Math.sin(this.y) * 1367130550;
|
|
529
|
+
|
|
530
|
+
let hash = ((x << 5) - x) + y;
|
|
531
|
+
|
|
532
|
+
hash |= 0; //convert to 32bit int
|
|
533
|
+
|
|
534
|
+
return hash;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
*
|
|
539
|
+
* @param {number} angle in radians
|
|
540
|
+
*/
|
|
541
|
+
rotate(angle) {
|
|
542
|
+
const sin = Math.sin(angle);
|
|
543
|
+
const cos = Math.cos(angle);
|
|
544
|
+
|
|
545
|
+
const x = this.x * cos - this.y * sin
|
|
546
|
+
const y = this.x * sin - this.y * cos;
|
|
547
|
+
|
|
548
|
+
this.set(x, y);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
*
|
|
553
|
+
* @param {function} processor
|
|
554
|
+
* @param {*} [thisArg]
|
|
555
|
+
* @returns {Vector2}
|
|
556
|
+
*/
|
|
557
|
+
process(processor, thisArg) {
|
|
558
|
+
processor.call(thisArg, this.x, this.y);
|
|
559
|
+
|
|
560
|
+
this.onChanged.add(processor, thisArg);
|
|
561
|
+
|
|
562
|
+
return this;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
toString() {
|
|
566
|
+
return `Vector2{x:${this.x}, y:${this.y}}`;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
*
|
|
571
|
+
* @param {Vector2} other
|
|
572
|
+
* @returns {boolean}
|
|
573
|
+
*/
|
|
574
|
+
equals(other) {
|
|
575
|
+
return this.x === other.x && this.y === other.y;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
* [Symbol.iterator]() {
|
|
579
|
+
|
|
580
|
+
yield this.x;
|
|
581
|
+
yield this.y;
|
|
582
|
+
|
|
583
|
+
}
|
|
584
|
+
}
|
|
376
585
|
|
|
377
|
-
this.set(x, y);
|
|
378
|
-
};
|
|
379
586
|
/**
|
|
380
|
-
*
|
|
381
|
-
* @
|
|
587
|
+
* @readonly
|
|
588
|
+
* @type {Vector2}
|
|
382
589
|
*/
|
|
383
|
-
Vector2.
|
|
384
|
-
buffer.writeFloat32(this.x);
|
|
385
|
-
buffer.writeFloat32(this.y);
|
|
386
|
-
};
|
|
590
|
+
Vector2.up = Object.freeze(new Vector2(0, 1));
|
|
387
591
|
|
|
388
592
|
/**
|
|
389
|
-
*
|
|
390
|
-
* @
|
|
593
|
+
* @readonly
|
|
594
|
+
* @type {Vector2}
|
|
391
595
|
*/
|
|
392
|
-
Vector2.
|
|
393
|
-
const x = buffer.readFloat32();
|
|
394
|
-
const y = buffer.readFloat32();
|
|
395
|
-
|
|
396
|
-
this.set(x, y);
|
|
397
|
-
};
|
|
596
|
+
Vector2.down = Object.freeze(new Vector2(0, -1));
|
|
398
597
|
|
|
399
598
|
/**
|
|
400
|
-
*
|
|
401
|
-
* @
|
|
599
|
+
* @readonly
|
|
600
|
+
* @type {Vector2}
|
|
402
601
|
*/
|
|
403
|
-
Vector2.
|
|
404
|
-
return this.x === 0 && this.y === 0;
|
|
405
|
-
};
|
|
406
|
-
|
|
602
|
+
Vector2.left = Object.freeze(new Vector2(-1, 0));
|
|
407
603
|
|
|
408
604
|
/**
|
|
409
|
-
*
|
|
410
|
-
* @
|
|
411
|
-
* @param {Number} minY
|
|
412
|
-
* @param {Number} maxX
|
|
413
|
-
* @param {Number} maxY
|
|
605
|
+
* @readonly
|
|
606
|
+
* @type {Vector2}
|
|
414
607
|
*/
|
|
415
|
-
Vector2.
|
|
416
|
-
const x = clamp(this.x, minX, maxX);
|
|
417
|
-
const y = clamp(this.y, minY, maxY);
|
|
418
|
-
return this.set(x, y);
|
|
419
|
-
};
|
|
608
|
+
Vector2.right = Object.freeze(new Vector2(1, 0));
|
|
420
609
|
|
|
421
610
|
/**
|
|
422
|
-
*
|
|
423
|
-
* @
|
|
424
|
-
* @param {Number} lowY
|
|
611
|
+
* @readonly
|
|
612
|
+
* @type {Vector2}
|
|
425
613
|
*/
|
|
426
|
-
Vector2.
|
|
427
|
-
const x = max2(this.x, lowX);
|
|
428
|
-
const y = max2(this.y, lowY);
|
|
429
|
-
return this.set(x, y);
|
|
430
|
-
};
|
|
431
|
-
|
|
614
|
+
Vector2.zero = Object.freeze(new Vector2(0, 0));
|
|
432
615
|
/**
|
|
433
|
-
*
|
|
434
|
-
* @
|
|
435
|
-
* @param {Number} highY
|
|
616
|
+
* @readonly
|
|
617
|
+
* @type {Vector2}
|
|
436
618
|
*/
|
|
437
|
-
Vector2.
|
|
438
|
-
const x = min2(this.x, highX);
|
|
439
|
-
const y = min2(this.y, highY);
|
|
440
|
-
return this.set(x, y);
|
|
441
|
-
};
|
|
619
|
+
Vector2.one = Object.freeze(new Vector2(1, 1));
|
|
442
620
|
|
|
443
621
|
/**
|
|
444
|
-
*
|
|
445
|
-
* @
|
|
446
|
-
* @returns {number}
|
|
622
|
+
* @readonly
|
|
623
|
+
* @type {boolean}
|
|
447
624
|
*/
|
|
448
|
-
Vector2.prototype.
|
|
449
|
-
return this._distanceSqrTo(other.x, other.y);
|
|
450
|
-
};
|
|
625
|
+
Vector2.prototype.isVector2 = true;
|
|
451
626
|
|
|
452
627
|
/**
|
|
453
628
|
*
|
|
@@ -464,30 +639,6 @@ function v2Lerp(result, a, b, fraction) {
|
|
|
464
639
|
result.set(x, y);
|
|
465
640
|
}
|
|
466
641
|
|
|
467
|
-
/**
|
|
468
|
-
*
|
|
469
|
-
* @param {Vector2} a
|
|
470
|
-
* @param {Vector2} b
|
|
471
|
-
* @param {number} fraction
|
|
472
|
-
*/
|
|
473
|
-
Vector2.prototype.lerpVectors = function (a, b, fraction) {
|
|
474
|
-
v2Lerp(this, a, b, fraction);
|
|
475
|
-
};
|
|
476
|
-
|
|
477
|
-
/**
|
|
478
|
-
*
|
|
479
|
-
* @param {number[]} matrix3
|
|
480
|
-
*/
|
|
481
|
-
Vector2.prototype.applyMatrix3 = function (matrix3) {
|
|
482
|
-
const x = this.x;
|
|
483
|
-
const y = this.y;
|
|
484
|
-
|
|
485
|
-
const _x = matrix3[0] * x + matrix3[3] * y + matrix3[6];
|
|
486
|
-
const _y = matrix3[1] * x + matrix3[4] * y + matrix3[7];
|
|
487
|
-
|
|
488
|
-
this.set(_x, _y);
|
|
489
|
-
};
|
|
490
|
-
|
|
491
642
|
|
|
492
643
|
/**
|
|
493
644
|
*
|
|
@@ -515,10 +666,10 @@ export function v2_angleBetween(x0, y0, x1, y1) {
|
|
|
515
666
|
* @returns {number}
|
|
516
667
|
*/
|
|
517
668
|
export function v2_dot(x0, y0, x1, y1) {
|
|
518
|
-
assert.
|
|
519
|
-
assert.
|
|
520
|
-
assert.
|
|
521
|
-
assert.
|
|
669
|
+
assert.isNumber(x0, 'x0');
|
|
670
|
+
assert.isNumber(y0, 'y0');
|
|
671
|
+
assert.isNumber(x1, 'x1');
|
|
672
|
+
assert.isNumber(y1, 'y1');
|
|
522
673
|
|
|
523
674
|
return (x0 * x1 + y0 * y1);
|
|
524
675
|
}
|
|
@@ -553,120 +704,14 @@ export function v2_magnitude(x, y) {
|
|
|
553
704
|
*/
|
|
554
705
|
export function v2_distance(x0, y0, x1, y1) {
|
|
555
706
|
|
|
556
|
-
assert.
|
|
557
|
-
assert.
|
|
558
|
-
assert.
|
|
559
|
-
assert.
|
|
707
|
+
assert.isNumber(x0, 'x0');
|
|
708
|
+
assert.isNumber(y0, 'x0');
|
|
709
|
+
assert.isNumber(x1, 'x0');
|
|
710
|
+
assert.isNumber(y1, 'x0');
|
|
560
711
|
|
|
561
712
|
return v2_magnitude(x1 - x0, y1 - y0);
|
|
562
713
|
}
|
|
563
714
|
|
|
564
715
|
Vector2._distance = v2_distance;
|
|
565
716
|
|
|
566
|
-
/**
|
|
567
|
-
*
|
|
568
|
-
* @param {Vector2} other
|
|
569
|
-
* @returns {number}
|
|
570
|
-
*/
|
|
571
|
-
Vector2.prototype.distanceTo = function (other) {
|
|
572
|
-
return this._distanceTo(other.x, other.y);
|
|
573
|
-
};
|
|
574
|
-
|
|
575
|
-
/**
|
|
576
|
-
*
|
|
577
|
-
* @param {number} x
|
|
578
|
-
* @param {number} y
|
|
579
|
-
* @returns {number}
|
|
580
|
-
*/
|
|
581
|
-
Vector2.prototype._distanceSqrTo = function (x, y) {
|
|
582
|
-
const dx = this.x - x;
|
|
583
|
-
const dy = this.y - y;
|
|
584
|
-
return magnitudeSqr(dx, dy);
|
|
585
|
-
};
|
|
586
|
-
|
|
587
|
-
/**
|
|
588
|
-
*
|
|
589
|
-
* @param {number} x
|
|
590
|
-
* @param {number} y
|
|
591
|
-
* @returns {number}
|
|
592
|
-
*/
|
|
593
|
-
Vector2.prototype._distanceTo = function (x, y) {
|
|
594
|
-
return Math.sqrt(this._distanceSqrTo(x, y));
|
|
595
|
-
};
|
|
596
|
-
|
|
597
|
-
/**
|
|
598
|
-
*
|
|
599
|
-
* @param {Vector2} other
|
|
600
|
-
* @returns {number}
|
|
601
|
-
*/
|
|
602
|
-
Vector2.prototype.manhattanDistanceTo = function (other) {
|
|
603
|
-
const dx = Math.abs(this.x - other.x);
|
|
604
|
-
const dy = Math.abs(this.y - other.y);
|
|
605
|
-
|
|
606
|
-
return dx + dy;
|
|
607
|
-
};
|
|
608
|
-
|
|
609
|
-
/**
|
|
610
|
-
* @returns {number}
|
|
611
|
-
*/
|
|
612
|
-
Vector2.prototype.length = function () {
|
|
613
|
-
return v2_magnitude(this.x, this.y);
|
|
614
|
-
};
|
|
615
|
-
|
|
616
|
-
/**
|
|
617
|
-
*
|
|
618
|
-
* @returns {number}
|
|
619
|
-
*/
|
|
620
|
-
Vector2.prototype.hashCode = function () {
|
|
621
|
-
const x = Math.sin(this.x) * 1367130550;
|
|
622
|
-
const y = Math.sin(this.y) * 1367130550;
|
|
623
|
-
|
|
624
|
-
let hash = ((x << 5) - x) + y;
|
|
625
|
-
|
|
626
|
-
hash |= 0; //convert to 32bit int
|
|
627
|
-
|
|
628
|
-
return hash;
|
|
629
|
-
};
|
|
630
|
-
|
|
631
|
-
/**
|
|
632
|
-
*
|
|
633
|
-
* @param {number} angle in radians
|
|
634
|
-
*/
|
|
635
|
-
Vector2.prototype.rotate = function (angle) {
|
|
636
|
-
const sin = Math.sin(angle);
|
|
637
|
-
const cos = Math.cos(angle);
|
|
638
|
-
|
|
639
|
-
const x = this.x * cos - this.y * sin
|
|
640
|
-
const y = this.x * sin - this.y * cos;
|
|
641
|
-
|
|
642
|
-
this.set(x, y);
|
|
643
|
-
};
|
|
644
|
-
|
|
645
|
-
/**
|
|
646
|
-
*
|
|
647
|
-
* @param {function} processor
|
|
648
|
-
* @param {*} [thisArg]
|
|
649
|
-
* @returns {Vector2}
|
|
650
|
-
*/
|
|
651
|
-
Vector2.prototype.process = function (processor, thisArg) {
|
|
652
|
-
processor.call(thisArg, this.x, this.y);
|
|
653
|
-
|
|
654
|
-
this.onChanged.add(processor, thisArg);
|
|
655
|
-
|
|
656
|
-
return this;
|
|
657
|
-
};
|
|
658
|
-
|
|
659
|
-
Vector2.prototype.toString = function () {
|
|
660
|
-
return `Vector2{x:${this.x}, y:${this.y}}`;
|
|
661
|
-
};
|
|
662
|
-
|
|
663
|
-
/**
|
|
664
|
-
*
|
|
665
|
-
* @param {Vector2} other
|
|
666
|
-
* @returns {boolean}
|
|
667
|
-
*/
|
|
668
|
-
Vector2.prototype.equals = function (other) {
|
|
669
|
-
return this.x === other.x && this.y === other.y;
|
|
670
|
-
};
|
|
671
|
-
|
|
672
717
|
export default Vector2;
|