@esotericsoftware/spine-webgl 4.3.10 → 4.3.11

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.
@@ -172,6 +172,7 @@ var spine = (() => {
172
172
  SkeletonData: () => SkeletonData,
173
173
  SkeletonDebugRenderer: () => SkeletonDebugRenderer,
174
174
  SkeletonJson: () => SkeletonJson,
175
+ SkeletonPhysicsMovement: () => SkeletonPhysicsMovement,
175
176
  SkeletonRenderer: () => SkeletonRenderer,
176
177
  SkeletonRendererCore: () => SkeletonRendererCore,
177
178
  Skin: () => Skin,
@@ -13070,6 +13071,151 @@ ${error}` : ""}`);
13070
13071
  return map[property] !== void 0 ? map[property] : defaultValue;
13071
13072
  }
13072
13073
 
13074
+ // spine-core/src/SkeletonPhysicsMovement.ts
13075
+ var SkeletonPhysicsMovement = class {
13076
+ /**
13077
+ * Creates a movement tracker for a skeleton displayed by a host runtime object.
13078
+ * @param skeleton The skeleton whose physics constraints receive inherited movement.
13079
+ * @param adapter Runtime-specific hooks used to read and convert the host object's transform.
13080
+ * @param options Initial movement inheritance values.
13081
+ */
13082
+ constructor(skeleton, adapter, options = {}) {
13083
+ this.skeleton = skeleton;
13084
+ this.adapter = adapter;
13085
+ this.positionInheritanceFactorX = options.positionInheritanceX ?? 0;
13086
+ this.positionInheritanceFactorY = options.positionInheritanceY ?? 0;
13087
+ this.rotationInheritanceFactor = options.rotationInheritance ?? 0;
13088
+ }
13089
+ positionInheritanceFactorX = 0;
13090
+ positionInheritanceFactorY = 0;
13091
+ rotationInheritanceFactor = 0;
13092
+ hasLastTransform = false;
13093
+ lastX = 0;
13094
+ lastY = 0;
13095
+ lastZ = 0;
13096
+ lastRotation = 0;
13097
+ currentTransform = { x: 0, y: 0, z: 0, rotation: 0 };
13098
+ currentPosition = { x: 0, y: 0, z: 0 };
13099
+ lastPosition = { x: 0, y: 0, z: 0 };
13100
+ /** Horizontal position inheritance factor. `0` disables horizontal position inheritance. */
13101
+ get positionInheritanceX() {
13102
+ return this.positionInheritanceFactorX;
13103
+ }
13104
+ /** Vertical position inheritance factor. `0` disables vertical position inheritance. */
13105
+ get positionInheritanceY() {
13106
+ return this.positionInheritanceFactorY;
13107
+ }
13108
+ /**
13109
+ * Sets how much host object translation is inherited by skeleton physics constraints.
13110
+ * Use `(1, 1)` for normal inheritance, or `(0, 0)` to disable position inheritance.
13111
+ * @param x The horizontal position inheritance factor.
13112
+ * @param y The vertical position inheritance factor.
13113
+ */
13114
+ setPositionInheritance(x, y) {
13115
+ const wasDisabled = this.positionInheritanceFactorX === 0 && this.positionInheritanceFactorY === 0;
13116
+ const isEnabled = x !== 0 || y !== 0;
13117
+ this.positionInheritanceFactorX = x;
13118
+ this.positionInheritanceFactorY = y;
13119
+ if (wasDisabled && isEnabled) this.resetPosition();
13120
+ }
13121
+ /** Rotation inheritance factor. `0` disables rotation inheritance. */
13122
+ get rotationInheritance() {
13123
+ return this.rotationInheritanceFactor;
13124
+ }
13125
+ /**
13126
+ * Sets how much host object rotation is inherited by skeleton physics constraints.
13127
+ * @param value The rotation inheritance factor.
13128
+ */
13129
+ set rotationInheritance(value) {
13130
+ const wasDisabled = this.rotationInheritanceFactor === 0;
13131
+ this.rotationInheritanceFactor = value;
13132
+ if (wasDisabled && value !== 0) this.resetRotation();
13133
+ }
13134
+ /** Resets the previous position used to calculate inherited translation. */
13135
+ resetPosition() {
13136
+ const transform = this.currentTransform;
13137
+ const readRotation = !this.hasLastTransform && this.rotationInheritanceFactor !== 0;
13138
+ this.adapter.readTransform(transform, readRotation);
13139
+ this.lastX = transform.x;
13140
+ this.lastY = transform.y;
13141
+ this.lastZ = transform.z;
13142
+ if (readRotation) this.lastRotation = transform.rotation;
13143
+ this.hasLastTransform = true;
13144
+ }
13145
+ /** Resets the previous rotation used to calculate inherited rotation. */
13146
+ resetRotation() {
13147
+ const transform = this.currentTransform;
13148
+ this.adapter.readTransform(transform, true);
13149
+ this.lastRotation = transform.rotation;
13150
+ if (!this.hasLastTransform) {
13151
+ this.lastX = transform.x;
13152
+ this.lastY = transform.y;
13153
+ this.lastZ = transform.z;
13154
+ }
13155
+ this.hasLastTransform = true;
13156
+ }
13157
+ /** Resets both previous position and previous rotation. */
13158
+ resetTransform() {
13159
+ const transform = this.currentTransform;
13160
+ this.adapter.readTransform(transform, true);
13161
+ this.setLastTransform(transform.x, transform.y, transform.z, transform.rotation);
13162
+ }
13163
+ /**
13164
+ * Applies host object transform movement since the previous call to the skeleton's physics constraints.
13165
+ *
13166
+ * The first call records the current transform as the baseline and does not apply movement.
13167
+ */
13168
+ applyTransformMovement() {
13169
+ const inheritPosition = this.positionInheritanceFactorX !== 0 || this.positionInheritanceFactorY !== 0;
13170
+ const inheritRotation = this.rotationInheritanceFactor !== 0;
13171
+ if (!inheritPosition && !inheritRotation) return;
13172
+ const transform = this.currentTransform;
13173
+ this.adapter.readTransform(transform, inheritRotation);
13174
+ const { x, y, z } = transform;
13175
+ const currentRotation = inheritRotation ? transform.rotation : this.lastRotation;
13176
+ const positionChanged = x !== this.lastX || y !== this.lastY || z !== this.lastZ;
13177
+ if (this.hasLastTransform) {
13178
+ if (!positionChanged && currentRotation === this.lastRotation) return;
13179
+ if (inheritPosition && positionChanged) this.applyPositionMovement(x, y, z);
13180
+ if (inheritRotation && currentRotation !== this.lastRotation) this.applyRotationMovement(currentRotation);
13181
+ }
13182
+ this.setLastTransform(x, y, z, currentRotation);
13183
+ }
13184
+ applyPositionMovement(currentX, currentY, currentZ) {
13185
+ const currentPosition = this.currentPosition;
13186
+ currentPosition.x = currentX;
13187
+ currentPosition.y = currentY;
13188
+ currentPosition.z = currentZ;
13189
+ this.adapter.worldToSkeleton(currentPosition);
13190
+ const lastPosition = this.lastPosition;
13191
+ lastPosition.x = this.lastX;
13192
+ lastPosition.y = this.lastY;
13193
+ lastPosition.z = this.lastZ;
13194
+ this.adapter.worldToSkeleton(lastPosition);
13195
+ this.skeleton.physicsTranslate(
13196
+ (currentPosition.x - lastPosition.x) * this.positionInheritanceFactorX,
13197
+ (currentPosition.y - lastPosition.y) * this.positionInheritanceFactorY
13198
+ );
13199
+ }
13200
+ applyRotationMovement(currentRotation) {
13201
+ const rotationFactor = this.rotationInheritanceFactor;
13202
+ if (rotationFactor === 0) return;
13203
+ this.skeleton.physicsRotate(0, 0, this.getRotationDelta(currentRotation, this.lastRotation) * rotationFactor);
13204
+ }
13205
+ setLastTransform(x, y, z, rotation) {
13206
+ this.lastX = x;
13207
+ this.lastY = y;
13208
+ this.lastZ = z;
13209
+ this.lastRotation = rotation;
13210
+ this.hasLastTransform = true;
13211
+ }
13212
+ getRotationDelta(current, previous) {
13213
+ let delta = current - previous;
13214
+ delta = (delta + 180) % 360 - 180;
13215
+ return delta < -180 ? delta + 360 : delta;
13216
+ }
13217
+ };
13218
+
13073
13219
  // spine-core/src/SkeletonRendererCore.ts
13074
13220
  var SkeletonRendererCore = class {
13075
13221
  commandPool = new CommandPool();