dino-ge 1.10.2 → 1.10.3
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/dist/TransformComponent.d.ts +19 -1
- package/dist/TransformComponent.js +37 -13
- package/dist/TransformComponent.js.map +1 -1
- package/dist/Vector2.d.ts +34 -6
- package/dist/Vector2.js +50 -6
- package/dist/Vector2.js.map +1 -1
- package/package.json +1 -1
|
@@ -47,17 +47,35 @@ export default class TransformComponent extends Component {
|
|
|
47
47
|
*/
|
|
48
48
|
removeChild(child: TransformComponent): void;
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
50
|
+
* Gets the world-space position.
|
|
51
|
+
* Note: This returns a clone to prevent accidental cache mutation.
|
|
52
|
+
* For performance, use getWorldPosition(out).
|
|
51
53
|
*/
|
|
52
54
|
get worldPosition(): Vector2;
|
|
55
|
+
/**
|
|
56
|
+
* Writes the world-space position into the provided 'out' vector.
|
|
57
|
+
* Allocation-free alternative to the worldPosition getter.
|
|
58
|
+
* @param out The vector to write into.
|
|
59
|
+
* @returns The 'out' vector for chaining.
|
|
60
|
+
*/
|
|
61
|
+
getWorldPosition(out: Vector2): Vector2;
|
|
53
62
|
/**
|
|
54
63
|
* Calculates the world-space rotation.
|
|
55
64
|
*/
|
|
56
65
|
get worldRotation(): number;
|
|
57
66
|
/**
|
|
58
67
|
* Calculates the world-space scale.
|
|
68
|
+
* Note: This returns a clone to prevent accidental cache mutation.
|
|
69
|
+
* For performance, use getWorldScale(out).
|
|
59
70
|
*/
|
|
60
71
|
get worldScale(): Vector2;
|
|
72
|
+
/**
|
|
73
|
+
* Writes the world-space scale into the provided 'out' vector.
|
|
74
|
+
* Allocation-free alternative to the worldScale getter.
|
|
75
|
+
* @param out The vector to write into.
|
|
76
|
+
* @returns The 'out' vector for chaining.
|
|
77
|
+
*/
|
|
78
|
+
getWorldScale(out: Vector2): Vector2;
|
|
61
79
|
/**
|
|
62
80
|
* Recalculates all world-space properties from the root parent down.
|
|
63
81
|
* @private
|
|
@@ -84,7 +84,9 @@ export default class TransformComponent extends Component {
|
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
/**
|
|
87
|
-
*
|
|
87
|
+
* Gets the world-space position.
|
|
88
|
+
* Note: This returns a clone to prevent accidental cache mutation.
|
|
89
|
+
* For performance, use getWorldPosition(out).
|
|
88
90
|
*/
|
|
89
91
|
get worldPosition() {
|
|
90
92
|
if (this._isDirty) {
|
|
@@ -92,6 +94,18 @@ export default class TransformComponent extends Component {
|
|
|
92
94
|
}
|
|
93
95
|
return this._worldPosition.clone();
|
|
94
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Writes the world-space position into the provided 'out' vector.
|
|
99
|
+
* Allocation-free alternative to the worldPosition getter.
|
|
100
|
+
* @param out The vector to write into.
|
|
101
|
+
* @returns The 'out' vector for chaining.
|
|
102
|
+
*/
|
|
103
|
+
getWorldPosition(out) {
|
|
104
|
+
if (this._isDirty) {
|
|
105
|
+
this.updateWorldTransform();
|
|
106
|
+
}
|
|
107
|
+
return out.copy(this._worldPosition);
|
|
108
|
+
}
|
|
95
109
|
/**
|
|
96
110
|
* Calculates the world-space rotation.
|
|
97
111
|
*/
|
|
@@ -103,6 +117,8 @@ export default class TransformComponent extends Component {
|
|
|
103
117
|
}
|
|
104
118
|
/**
|
|
105
119
|
* Calculates the world-space scale.
|
|
120
|
+
* Note: This returns a clone to prevent accidental cache mutation.
|
|
121
|
+
* For performance, use getWorldScale(out).
|
|
106
122
|
*/
|
|
107
123
|
get worldScale() {
|
|
108
124
|
if (this._isDirty) {
|
|
@@ -110,6 +126,18 @@ export default class TransformComponent extends Component {
|
|
|
110
126
|
}
|
|
111
127
|
return this._worldScale.clone();
|
|
112
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Writes the world-space scale into the provided 'out' vector.
|
|
131
|
+
* Allocation-free alternative to the worldScale getter.
|
|
132
|
+
* @param out The vector to write into.
|
|
133
|
+
* @returns The 'out' vector for chaining.
|
|
134
|
+
*/
|
|
135
|
+
getWorldScale(out) {
|
|
136
|
+
if (this._isDirty) {
|
|
137
|
+
this.updateWorldTransform();
|
|
138
|
+
}
|
|
139
|
+
return out.copy(this._worldScale);
|
|
140
|
+
}
|
|
113
141
|
/**
|
|
114
142
|
* Recalculates all world-space properties from the root parent down.
|
|
115
143
|
* @private
|
|
@@ -127,18 +155,14 @@ export default class TransformComponent extends Component {
|
|
|
127
155
|
const parentWorldRot = this.parent.worldRotation;
|
|
128
156
|
const parentWorldScale = this.parent.worldScale;
|
|
129
157
|
// Apply parent's scale and rotation to local position
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
localY = rotatedY;
|
|
139
|
-
}
|
|
140
|
-
this._worldPosition.x = parentWorldPos.x + localX;
|
|
141
|
-
this._worldPosition.y = parentWorldPos.y + localY;
|
|
158
|
+
const localX = this.position.x * parentWorldScale.x;
|
|
159
|
+
const localY = this.position.y * parentWorldScale.y;
|
|
160
|
+
const cos = Math.cos(parentWorldRot);
|
|
161
|
+
const sin = Math.sin(parentWorldRot);
|
|
162
|
+
const rotatedX = localX * cos - localY * sin;
|
|
163
|
+
const rotatedY = localX * sin + localY * cos;
|
|
164
|
+
this._worldPosition.x = parentWorldPos.x + rotatedX;
|
|
165
|
+
this._worldPosition.y = parentWorldPos.y + rotatedY;
|
|
142
166
|
this._worldRotation = parentWorldRot + this.rotation;
|
|
143
167
|
this._worldScale.x = parentWorldScale.x * this.scale.x;
|
|
144
168
|
this._worldScale.y = parentWorldScale.y * this.scale.y;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransformComponent.js","sourceRoot":"","sources":["../src/TransformComponent.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC;;;GAGG;AACH,MAAM,CAAC,O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AI,CAAC,cAAc,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,GAAG,QAAQ,CAAC;YACpD,IAAI,CAAC,cAAc,GAAG,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC;YACrD,IAAI,CAAC,WAAW,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;YACvD,IAAI,CAAC,WAAW,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;QACzD,CAAC;QACD,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;IACxB,CAAC;CACF"}
|
package/dist/Vector2.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Class representing a 2D vector or point.
|
|
3
|
+
* Implements a fluent, mutating API to reduce object allocations.
|
|
3
4
|
*/
|
|
4
5
|
export default class Vector2 {
|
|
5
6
|
private _x;
|
|
@@ -13,6 +14,19 @@ export default class Vector2 {
|
|
|
13
14
|
get y(): number;
|
|
14
15
|
set y(val: number);
|
|
15
16
|
constructor(x?: number, y?: number);
|
|
17
|
+
/**
|
|
18
|
+
* Sets the components of this vector.
|
|
19
|
+
* @param x New x coordinate.
|
|
20
|
+
* @param y New y coordinate.
|
|
21
|
+
* @returns This vector for chaining.
|
|
22
|
+
*/
|
|
23
|
+
set(x: number, y: number): this;
|
|
24
|
+
/**
|
|
25
|
+
* Copies the components of another vector into this one.
|
|
26
|
+
* @param other The vector to copy from.
|
|
27
|
+
* @returns This vector for chaining.
|
|
28
|
+
*/
|
|
29
|
+
copy(other: Vector2): this;
|
|
16
30
|
/**
|
|
17
31
|
* Calculates the Euclidean distance between two vectors.
|
|
18
32
|
* @param v1 First vector.
|
|
@@ -30,12 +44,26 @@ export default class Vector2 {
|
|
|
30
44
|
get magnitude(): number;
|
|
31
45
|
/** Returns a new normalized (unit length) version of this vector. */
|
|
32
46
|
get normalized(): Vector2;
|
|
33
|
-
/**
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Normalizes this vector in place.
|
|
49
|
+
* @returns This vector for chaining.
|
|
50
|
+
*/
|
|
51
|
+
normalize(): this;
|
|
52
|
+
/**
|
|
53
|
+
* Adds another vector to this one (mutates).
|
|
54
|
+
* @returns This vector for chaining.
|
|
55
|
+
*/
|
|
56
|
+
add(other: Vector2): this;
|
|
57
|
+
/**
|
|
58
|
+
* Subtracts another vector from this one (mutates).
|
|
59
|
+
* @returns This vector for chaining.
|
|
60
|
+
*/
|
|
61
|
+
subtract(other: Vector2): this;
|
|
62
|
+
/**
|
|
63
|
+
* Multiplies this vector by a scalar (mutates).
|
|
64
|
+
* @returns This vector for chaining.
|
|
65
|
+
*/
|
|
66
|
+
multiply(scalar: number): this;
|
|
39
67
|
/** Returns a copy of this vector. */
|
|
40
68
|
clone(): Vector2;
|
|
41
69
|
}
|
package/dist/Vector2.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Class representing a 2D vector or point.
|
|
3
|
+
* Implements a fluent, mutating API to reduce object allocations.
|
|
3
4
|
*/
|
|
4
5
|
export default class Vector2 {
|
|
5
6
|
/** X coordinate. */
|
|
@@ -24,6 +25,29 @@ export default class Vector2 {
|
|
|
24
25
|
this._x = x;
|
|
25
26
|
this._y = y;
|
|
26
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Sets the components of this vector.
|
|
30
|
+
* @param x New x coordinate.
|
|
31
|
+
* @param y New y coordinate.
|
|
32
|
+
* @returns This vector for chaining.
|
|
33
|
+
*/
|
|
34
|
+
set(x, y) {
|
|
35
|
+
var _a;
|
|
36
|
+
if (this._x !== x || this._y !== y) {
|
|
37
|
+
this._x = x;
|
|
38
|
+
this._y = y;
|
|
39
|
+
(_a = this.onChange) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
40
|
+
}
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Copies the components of another vector into this one.
|
|
45
|
+
* @param other The vector to copy from.
|
|
46
|
+
* @returns This vector for chaining.
|
|
47
|
+
*/
|
|
48
|
+
copy(other) {
|
|
49
|
+
return this.set(other.x, other.y);
|
|
50
|
+
}
|
|
27
51
|
/**
|
|
28
52
|
* Calculates the Euclidean distance between two vectors.
|
|
29
53
|
* @param v1 First vector.
|
|
@@ -45,28 +69,48 @@ export default class Vector2 {
|
|
|
45
69
|
}
|
|
46
70
|
/** Returns the length of the vector. */
|
|
47
71
|
get magnitude() {
|
|
48
|
-
return Math.sqrt(this.
|
|
72
|
+
return Math.sqrt(this._x * this._x + this._y * this._y);
|
|
49
73
|
}
|
|
50
74
|
/** Returns a new normalized (unit length) version of this vector. */
|
|
51
75
|
get normalized() {
|
|
52
76
|
const mag = this.magnitude;
|
|
53
77
|
if (mag === 0)
|
|
54
78
|
return new Vector2(0, 0);
|
|
55
|
-
return new Vector2(this.
|
|
79
|
+
return new Vector2(this._x / mag, this._y / mag);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Normalizes this vector in place.
|
|
83
|
+
* @returns This vector for chaining.
|
|
84
|
+
*/
|
|
85
|
+
normalize() {
|
|
86
|
+
const mag = this.magnitude;
|
|
87
|
+
if (mag !== 0) {
|
|
88
|
+
this.multiply(1 / mag);
|
|
89
|
+
}
|
|
90
|
+
return this;
|
|
56
91
|
}
|
|
57
|
-
/**
|
|
92
|
+
/**
|
|
93
|
+
* Adds another vector to this one (mutates).
|
|
94
|
+
* @returns This vector for chaining.
|
|
95
|
+
*/
|
|
58
96
|
add(other) {
|
|
59
97
|
this.x += other.x;
|
|
60
98
|
this.y += other.y;
|
|
61
99
|
return this;
|
|
62
100
|
}
|
|
63
|
-
/**
|
|
101
|
+
/**
|
|
102
|
+
* Subtracts another vector from this one (mutates).
|
|
103
|
+
* @returns This vector for chaining.
|
|
104
|
+
*/
|
|
64
105
|
subtract(other) {
|
|
65
106
|
this.x -= other.x;
|
|
66
107
|
this.y -= other.y;
|
|
67
108
|
return this;
|
|
68
109
|
}
|
|
69
|
-
/**
|
|
110
|
+
/**
|
|
111
|
+
* Multiplies this vector by a scalar (mutates).
|
|
112
|
+
* @returns This vector for chaining.
|
|
113
|
+
*/
|
|
70
114
|
multiply(scalar) {
|
|
71
115
|
this.x *= scalar;
|
|
72
116
|
this.y *= scalar;
|
|
@@ -74,7 +118,7 @@ export default class Vector2 {
|
|
|
74
118
|
}
|
|
75
119
|
/** Returns a copy of this vector. */
|
|
76
120
|
clone() {
|
|
77
|
-
return new Vector2(this.
|
|
121
|
+
return new Vector2(this._x, this._y);
|
|
78
122
|
}
|
|
79
123
|
}
|
|
80
124
|
//# sourceMappingURL=Vector2.js.map
|
package/dist/Vector2.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Vector2.js","sourceRoot":"","sources":["../src/Vector2.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"Vector2.js","sourceRoot":"","sources":["../src/Vector2.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,OAAO;IAO1B,oBAAoB;IACpB,IAAI,CAAC,KAAa,OAAO,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IACnC,IAAI,CAAC,CAAC,GAAW;;QACf,IAAI,IAAI,CAAC,EAAE,KAAK,GAAG,EAAE,CAAC;YACpB,IAAI,CAAC,EAAE,GAAG,GAAG,CAAC;YACd,MAAA,IAAI,CAAC,QAAQ,oDAAI,CAAC;QACpB,CAAC;IACH,CAAC;IAED,oBAAoB;IACpB,IAAI,CAAC,KAAa,OAAO,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IACnC,IAAI,CAAC,CAAC,GAAW;;QACf,IAAI,IAAI,CAAC,EAAE,KAAK,GAAG,EAAE,CAAC;YACpB,IAAI,CAAC,EAAE,GAAG,G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