@react-shimeji/core 0.3.1 → 0.3.2

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/index.js CHANGED
@@ -1590,6 +1590,7 @@ function environmentRectangle(bounds) {
1590
1590
  };
1591
1591
  }
1592
1592
  var PLATFORM_NEARBY_DISTANCE = 400;
1593
+ var PLATFORM_EDGE_TOLERANCE = 2;
1593
1594
  function distanceToRectangle(point, rectangle) {
1594
1595
  const dx = Math.max(rectangle.x - point.x, 0, point.x - rectangle.x - rectangle.width);
1595
1596
  const dy = Math.max(rectangle.y - point.y, 0, point.y - rectangle.y - rectangle.height);
@@ -1709,7 +1710,7 @@ var Mascot = class {
1709
1710
  if (result === "lost-ground") {
1710
1711
  this.state.dragging = false;
1711
1712
  this.actions.cancel();
1712
- this.currentBehavior = this.findFallBehavior();
1713
+ this.currentBehavior = this.selectEdgeBehavior(bounds, platforms);
1713
1714
  if (this.currentBehavior) this.startBehavior(this.createEnvironment(bounds, platforms), bounds, platforms);
1714
1715
  } else if (result === "complete") {
1715
1716
  this.currentBehavior = this.selectNextBehavior(this.createEnvironment(bounds, platforms), bounds);
@@ -1735,14 +1736,37 @@ var Mascot = class {
1735
1736
  findFallBehavior() {
1736
1737
  return this.behavior.force("Fall") ?? this.behavior.force("\u843D\u4E0B\u3059\u308B");
1737
1738
  }
1738
- selectNextBehavior(environment, bounds) {
1739
+ selectNextBehavior(environment, bounds, relocateOnFallback = true) {
1739
1740
  const selected = this.behavior.selectNext(environment);
1740
- if (this.behavior.usedFallback()) {
1741
+ if (relocateOnFallback && this.behavior.usedFallback()) {
1741
1742
  this.state.x = Math.trunc(bounds.x + this.random() * bounds.width);
1742
1743
  this.state.y = bounds.y - 256;
1743
1744
  }
1744
1745
  return selected;
1745
1746
  }
1747
+ selectEdgeBehavior(bounds, platforms) {
1748
+ const original = { x: this.state.x, y: this.state.y };
1749
+ const platform = platforms.find((candidate) => candidate.element === this.activePlatformElement);
1750
+ if (platform) {
1751
+ const left = platform.x;
1752
+ const right = platform.x + platform.width;
1753
+ const top = platform.y;
1754
+ const bottom = platform.y + platform.height;
1755
+ if (Math.abs(this.state.y - top) <= PLATFORM_EDGE_TOLERANCE || Math.abs(this.state.y - bottom) <= PLATFORM_EDGE_TOLERANCE) {
1756
+ if (this.state.x < left) this.state.x = left;
1757
+ else if (this.state.x > right) this.state.x = right;
1758
+ } else if (Math.abs(this.state.x - left) <= PLATFORM_EDGE_TOLERANCE || Math.abs(this.state.x - right) <= PLATFORM_EDGE_TOLERANCE) {
1759
+ if (this.state.y < top) this.state.y = top;
1760
+ else if (this.state.y > bottom) this.state.y = bottom;
1761
+ }
1762
+ }
1763
+ const selected = this.selectNextBehavior(this.createEnvironment(bounds, platforms), bounds, false);
1764
+ if (this.behavior.usedFallback() || selected?.name === "Fall" || selected?.name === "\u843D\u4E0B\u3059\u308B") {
1765
+ this.state.x = original.x;
1766
+ this.state.y = original.y;
1767
+ }
1768
+ return selected ?? this.findFallBehavior();
1769
+ }
1746
1770
  createEnvironment(bounds, platforms = this.platforms) {
1747
1771
  const workArea = environmentRectangle(bounds);
1748
1772
  const inactive = environmentRectangle({ x: -100, y: -100, width: 0, height: 0 });
@@ -1773,7 +1797,7 @@ var Mascot = class {
1773
1797
  }
1774
1798
  const anchor = this.state;
1775
1799
  const current = platforms.find((platform) => platform.element === this.activePlatformElement);
1776
- if (current && (isOnTop(anchor, current, 2) || isOnBottom(anchor, current, 2) || isOnLeft(anchor, current, 2) || isOnRight(anchor, current, 2))) return current;
1800
+ if (current && (isOnTop(anchor, current, PLATFORM_EDGE_TOLERANCE) || isOnBottom(anchor, current, PLATFORM_EDGE_TOLERANCE) || isOnLeft(anchor, current, PLATFORM_EDGE_TOLERANCE) || isOnRight(anchor, current, PLATFORM_EDGE_TOLERANCE))) return current;
1777
1801
  if (this.state.vy >= 0) {
1778
1802
  const landingPlatform = platforms.filter((platform) => anchor.x >= platform.x && anchor.x <= platform.x + platform.width && platform.y >= anchor.y - 1).sort((left, right) => left.y - right.y)[0];
1779
1803
  if (landingPlatform) {