@esotericsoftware/spine-canvas 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.
@@ -138,6 +138,7 @@ var spine = (() => {
138
138
  SkeletonClipping: () => SkeletonClipping,
139
139
  SkeletonData: () => SkeletonData,
140
140
  SkeletonJson: () => SkeletonJson,
141
+ SkeletonPhysicsMovement: () => SkeletonPhysicsMovement,
141
142
  SkeletonRenderer: () => SkeletonRenderer,
142
143
  SkeletonRendererCore: () => SkeletonRendererCore,
143
144
  Skin: () => Skin,
@@ -13030,6 +13031,151 @@ ${error}` : ""}`);
13030
13031
  return map[property] !== void 0 ? map[property] : defaultValue;
13031
13032
  }
13032
13033
 
13034
+ // spine-core/src/SkeletonPhysicsMovement.ts
13035
+ var SkeletonPhysicsMovement = class {
13036
+ /**
13037
+ * Creates a movement tracker for a skeleton displayed by a host runtime object.
13038
+ * @param skeleton The skeleton whose physics constraints receive inherited movement.
13039
+ * @param adapter Runtime-specific hooks used to read and convert the host object's transform.
13040
+ * @param options Initial movement inheritance values.
13041
+ */
13042
+ constructor(skeleton, adapter, options = {}) {
13043
+ this.skeleton = skeleton;
13044
+ this.adapter = adapter;
13045
+ this.positionInheritanceFactorX = options.positionInheritanceX ?? 0;
13046
+ this.positionInheritanceFactorY = options.positionInheritanceY ?? 0;
13047
+ this.rotationInheritanceFactor = options.rotationInheritance ?? 0;
13048
+ }
13049
+ positionInheritanceFactorX = 0;
13050
+ positionInheritanceFactorY = 0;
13051
+ rotationInheritanceFactor = 0;
13052
+ hasLastTransform = false;
13053
+ lastX = 0;
13054
+ lastY = 0;
13055
+ lastZ = 0;
13056
+ lastRotation = 0;
13057
+ currentTransform = { x: 0, y: 0, z: 0, rotation: 0 };
13058
+ currentPosition = { x: 0, y: 0, z: 0 };
13059
+ lastPosition = { x: 0, y: 0, z: 0 };
13060
+ /** Horizontal position inheritance factor. `0` disables horizontal position inheritance. */
13061
+ get positionInheritanceX() {
13062
+ return this.positionInheritanceFactorX;
13063
+ }
13064
+ /** Vertical position inheritance factor. `0` disables vertical position inheritance. */
13065
+ get positionInheritanceY() {
13066
+ return this.positionInheritanceFactorY;
13067
+ }
13068
+ /**
13069
+ * Sets how much host object translation is inherited by skeleton physics constraints.
13070
+ * Use `(1, 1)` for normal inheritance, or `(0, 0)` to disable position inheritance.
13071
+ * @param x The horizontal position inheritance factor.
13072
+ * @param y The vertical position inheritance factor.
13073
+ */
13074
+ setPositionInheritance(x, y) {
13075
+ const wasDisabled = this.positionInheritanceFactorX === 0 && this.positionInheritanceFactorY === 0;
13076
+ const isEnabled = x !== 0 || y !== 0;
13077
+ this.positionInheritanceFactorX = x;
13078
+ this.positionInheritanceFactorY = y;
13079
+ if (wasDisabled && isEnabled) this.resetPosition();
13080
+ }
13081
+ /** Rotation inheritance factor. `0` disables rotation inheritance. */
13082
+ get rotationInheritance() {
13083
+ return this.rotationInheritanceFactor;
13084
+ }
13085
+ /**
13086
+ * Sets how much host object rotation is inherited by skeleton physics constraints.
13087
+ * @param value The rotation inheritance factor.
13088
+ */
13089
+ set rotationInheritance(value) {
13090
+ const wasDisabled = this.rotationInheritanceFactor === 0;
13091
+ this.rotationInheritanceFactor = value;
13092
+ if (wasDisabled && value !== 0) this.resetRotation();
13093
+ }
13094
+ /** Resets the previous position used to calculate inherited translation. */
13095
+ resetPosition() {
13096
+ const transform = this.currentTransform;
13097
+ const readRotation = !this.hasLastTransform && this.rotationInheritanceFactor !== 0;
13098
+ this.adapter.readTransform(transform, readRotation);
13099
+ this.lastX = transform.x;
13100
+ this.lastY = transform.y;
13101
+ this.lastZ = transform.z;
13102
+ if (readRotation) this.lastRotation = transform.rotation;
13103
+ this.hasLastTransform = true;
13104
+ }
13105
+ /** Resets the previous rotation used to calculate inherited rotation. */
13106
+ resetRotation() {
13107
+ const transform = this.currentTransform;
13108
+ this.adapter.readTransform(transform, true);
13109
+ this.lastRotation = transform.rotation;
13110
+ if (!this.hasLastTransform) {
13111
+ this.lastX = transform.x;
13112
+ this.lastY = transform.y;
13113
+ this.lastZ = transform.z;
13114
+ }
13115
+ this.hasLastTransform = true;
13116
+ }
13117
+ /** Resets both previous position and previous rotation. */
13118
+ resetTransform() {
13119
+ const transform = this.currentTransform;
13120
+ this.adapter.readTransform(transform, true);
13121
+ this.setLastTransform(transform.x, transform.y, transform.z, transform.rotation);
13122
+ }
13123
+ /**
13124
+ * Applies host object transform movement since the previous call to the skeleton's physics constraints.
13125
+ *
13126
+ * The first call records the current transform as the baseline and does not apply movement.
13127
+ */
13128
+ applyTransformMovement() {
13129
+ const inheritPosition = this.positionInheritanceFactorX !== 0 || this.positionInheritanceFactorY !== 0;
13130
+ const inheritRotation = this.rotationInheritanceFactor !== 0;
13131
+ if (!inheritPosition && !inheritRotation) return;
13132
+ const transform = this.currentTransform;
13133
+ this.adapter.readTransform(transform, inheritRotation);
13134
+ const { x, y, z } = transform;
13135
+ const currentRotation = inheritRotation ? transform.rotation : this.lastRotation;
13136
+ const positionChanged = x !== this.lastX || y !== this.lastY || z !== this.lastZ;
13137
+ if (this.hasLastTransform) {
13138
+ if (!positionChanged && currentRotation === this.lastRotation) return;
13139
+ if (inheritPosition && positionChanged) this.applyPositionMovement(x, y, z);
13140
+ if (inheritRotation && currentRotation !== this.lastRotation) this.applyRotationMovement(currentRotation);
13141
+ }
13142
+ this.setLastTransform(x, y, z, currentRotation);
13143
+ }
13144
+ applyPositionMovement(currentX, currentY, currentZ) {
13145
+ const currentPosition = this.currentPosition;
13146
+ currentPosition.x = currentX;
13147
+ currentPosition.y = currentY;
13148
+ currentPosition.z = currentZ;
13149
+ this.adapter.worldToSkeleton(currentPosition);
13150
+ const lastPosition = this.lastPosition;
13151
+ lastPosition.x = this.lastX;
13152
+ lastPosition.y = this.lastY;
13153
+ lastPosition.z = this.lastZ;
13154
+ this.adapter.worldToSkeleton(lastPosition);
13155
+ this.skeleton.physicsTranslate(
13156
+ (currentPosition.x - lastPosition.x) * this.positionInheritanceFactorX,
13157
+ (currentPosition.y - lastPosition.y) * this.positionInheritanceFactorY
13158
+ );
13159
+ }
13160
+ applyRotationMovement(currentRotation) {
13161
+ const rotationFactor = this.rotationInheritanceFactor;
13162
+ if (rotationFactor === 0) return;
13163
+ this.skeleton.physicsRotate(0, 0, this.getRotationDelta(currentRotation, this.lastRotation) * rotationFactor);
13164
+ }
13165
+ setLastTransform(x, y, z, rotation) {
13166
+ this.lastX = x;
13167
+ this.lastY = y;
13168
+ this.lastZ = z;
13169
+ this.lastRotation = rotation;
13170
+ this.hasLastTransform = true;
13171
+ }
13172
+ getRotationDelta(current, previous) {
13173
+ let delta = current - previous;
13174
+ delta = (delta + 180) % 360 - 180;
13175
+ return delta < -180 ? delta + 360 : delta;
13176
+ }
13177
+ };
13178
+
13033
13179
  // spine-core/src/SkeletonRendererCore.ts
13034
13180
  var SkeletonRendererCore = class {
13035
13181
  commandPool = new CommandPool();