@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.
@@ -12841,6 +12841,151 @@ function getValue(map, property, defaultValue) {
12841
12841
  return map[property] !== void 0 ? map[property] : defaultValue;
12842
12842
  }
12843
12843
 
12844
+ // spine-core/src/SkeletonPhysicsMovement.ts
12845
+ var SkeletonPhysicsMovement = class {
12846
+ /**
12847
+ * Creates a movement tracker for a skeleton displayed by a host runtime object.
12848
+ * @param skeleton The skeleton whose physics constraints receive inherited movement.
12849
+ * @param adapter Runtime-specific hooks used to read and convert the host object's transform.
12850
+ * @param options Initial movement inheritance values.
12851
+ */
12852
+ constructor(skeleton, adapter, options = {}) {
12853
+ this.skeleton = skeleton;
12854
+ this.adapter = adapter;
12855
+ this.positionInheritanceFactorX = options.positionInheritanceX ?? 0;
12856
+ this.positionInheritanceFactorY = options.positionInheritanceY ?? 0;
12857
+ this.rotationInheritanceFactor = options.rotationInheritance ?? 0;
12858
+ }
12859
+ positionInheritanceFactorX = 0;
12860
+ positionInheritanceFactorY = 0;
12861
+ rotationInheritanceFactor = 0;
12862
+ hasLastTransform = false;
12863
+ lastX = 0;
12864
+ lastY = 0;
12865
+ lastZ = 0;
12866
+ lastRotation = 0;
12867
+ currentTransform = { x: 0, y: 0, z: 0, rotation: 0 };
12868
+ currentPosition = { x: 0, y: 0, z: 0 };
12869
+ lastPosition = { x: 0, y: 0, z: 0 };
12870
+ /** Horizontal position inheritance factor. `0` disables horizontal position inheritance. */
12871
+ get positionInheritanceX() {
12872
+ return this.positionInheritanceFactorX;
12873
+ }
12874
+ /** Vertical position inheritance factor. `0` disables vertical position inheritance. */
12875
+ get positionInheritanceY() {
12876
+ return this.positionInheritanceFactorY;
12877
+ }
12878
+ /**
12879
+ * Sets how much host object translation is inherited by skeleton physics constraints.
12880
+ * Use `(1, 1)` for normal inheritance, or `(0, 0)` to disable position inheritance.
12881
+ * @param x The horizontal position inheritance factor.
12882
+ * @param y The vertical position inheritance factor.
12883
+ */
12884
+ setPositionInheritance(x, y) {
12885
+ const wasDisabled = this.positionInheritanceFactorX === 0 && this.positionInheritanceFactorY === 0;
12886
+ const isEnabled = x !== 0 || y !== 0;
12887
+ this.positionInheritanceFactorX = x;
12888
+ this.positionInheritanceFactorY = y;
12889
+ if (wasDisabled && isEnabled) this.resetPosition();
12890
+ }
12891
+ /** Rotation inheritance factor. `0` disables rotation inheritance. */
12892
+ get rotationInheritance() {
12893
+ return this.rotationInheritanceFactor;
12894
+ }
12895
+ /**
12896
+ * Sets how much host object rotation is inherited by skeleton physics constraints.
12897
+ * @param value The rotation inheritance factor.
12898
+ */
12899
+ set rotationInheritance(value) {
12900
+ const wasDisabled = this.rotationInheritanceFactor === 0;
12901
+ this.rotationInheritanceFactor = value;
12902
+ if (wasDisabled && value !== 0) this.resetRotation();
12903
+ }
12904
+ /** Resets the previous position used to calculate inherited translation. */
12905
+ resetPosition() {
12906
+ const transform = this.currentTransform;
12907
+ const readRotation = !this.hasLastTransform && this.rotationInheritanceFactor !== 0;
12908
+ this.adapter.readTransform(transform, readRotation);
12909
+ this.lastX = transform.x;
12910
+ this.lastY = transform.y;
12911
+ this.lastZ = transform.z;
12912
+ if (readRotation) this.lastRotation = transform.rotation;
12913
+ this.hasLastTransform = true;
12914
+ }
12915
+ /** Resets the previous rotation used to calculate inherited rotation. */
12916
+ resetRotation() {
12917
+ const transform = this.currentTransform;
12918
+ this.adapter.readTransform(transform, true);
12919
+ this.lastRotation = transform.rotation;
12920
+ if (!this.hasLastTransform) {
12921
+ this.lastX = transform.x;
12922
+ this.lastY = transform.y;
12923
+ this.lastZ = transform.z;
12924
+ }
12925
+ this.hasLastTransform = true;
12926
+ }
12927
+ /** Resets both previous position and previous rotation. */
12928
+ resetTransform() {
12929
+ const transform = this.currentTransform;
12930
+ this.adapter.readTransform(transform, true);
12931
+ this.setLastTransform(transform.x, transform.y, transform.z, transform.rotation);
12932
+ }
12933
+ /**
12934
+ * Applies host object transform movement since the previous call to the skeleton's physics constraints.
12935
+ *
12936
+ * The first call records the current transform as the baseline and does not apply movement.
12937
+ */
12938
+ applyTransformMovement() {
12939
+ const inheritPosition = this.positionInheritanceFactorX !== 0 || this.positionInheritanceFactorY !== 0;
12940
+ const inheritRotation = this.rotationInheritanceFactor !== 0;
12941
+ if (!inheritPosition && !inheritRotation) return;
12942
+ const transform = this.currentTransform;
12943
+ this.adapter.readTransform(transform, inheritRotation);
12944
+ const { x, y, z } = transform;
12945
+ const currentRotation = inheritRotation ? transform.rotation : this.lastRotation;
12946
+ const positionChanged = x !== this.lastX || y !== this.lastY || z !== this.lastZ;
12947
+ if (this.hasLastTransform) {
12948
+ if (!positionChanged && currentRotation === this.lastRotation) return;
12949
+ if (inheritPosition && positionChanged) this.applyPositionMovement(x, y, z);
12950
+ if (inheritRotation && currentRotation !== this.lastRotation) this.applyRotationMovement(currentRotation);
12951
+ }
12952
+ this.setLastTransform(x, y, z, currentRotation);
12953
+ }
12954
+ applyPositionMovement(currentX, currentY, currentZ) {
12955
+ const currentPosition = this.currentPosition;
12956
+ currentPosition.x = currentX;
12957
+ currentPosition.y = currentY;
12958
+ currentPosition.z = currentZ;
12959
+ this.adapter.worldToSkeleton(currentPosition);
12960
+ const lastPosition = this.lastPosition;
12961
+ lastPosition.x = this.lastX;
12962
+ lastPosition.y = this.lastY;
12963
+ lastPosition.z = this.lastZ;
12964
+ this.adapter.worldToSkeleton(lastPosition);
12965
+ this.skeleton.physicsTranslate(
12966
+ (currentPosition.x - lastPosition.x) * this.positionInheritanceFactorX,
12967
+ (currentPosition.y - lastPosition.y) * this.positionInheritanceFactorY
12968
+ );
12969
+ }
12970
+ applyRotationMovement(currentRotation) {
12971
+ const rotationFactor = this.rotationInheritanceFactor;
12972
+ if (rotationFactor === 0) return;
12973
+ this.skeleton.physicsRotate(0, 0, this.getRotationDelta(currentRotation, this.lastRotation) * rotationFactor);
12974
+ }
12975
+ setLastTransform(x, y, z, rotation) {
12976
+ this.lastX = x;
12977
+ this.lastY = y;
12978
+ this.lastZ = z;
12979
+ this.lastRotation = rotation;
12980
+ this.hasLastTransform = true;
12981
+ }
12982
+ getRotationDelta(current, previous) {
12983
+ let delta = current - previous;
12984
+ delta = (delta + 180) % 360 - 180;
12985
+ return delta < -180 ? delta + 360 : delta;
12986
+ }
12987
+ };
12988
+
12844
12989
  // spine-core/src/SkeletonRendererCore.ts
12845
12990
  var SkeletonRendererCore = class {
12846
12991
  commandPool = new CommandPool();
@@ -13495,6 +13640,7 @@ export {
13495
13640
  SkeletonClipping,
13496
13641
  SkeletonData,
13497
13642
  SkeletonJson,
13643
+ SkeletonPhysicsMovement,
13498
13644
  SkeletonRenderer,
13499
13645
  SkeletonRendererCore,
13500
13646
  Skin,