@rpgjs/common 5.0.0-alpha.42 → 5.0.0-alpha.44

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
@@ -10104,6 +10104,7 @@ class RpgCommonMap {
10104
10104
  kind: this.resolvePhysicsEntityKind(owner, entity.uuid)
10105
10105
  });
10106
10106
  }
10107
+ this.unbindCharacterSignalSync(entity);
10107
10108
  this.physic.removeEntity(entity);
10108
10109
  }
10109
10110
  this.physic.getMovementManager().clearAll();
@@ -10410,6 +10411,7 @@ class RpgCommonMap {
10410
10411
  const existingEntity = this.physic.getEntityByUUID(owner.id);
10411
10412
  if (existingEntity) {
10412
10413
  existingEntity.owner = owner;
10414
+ this.bindCharacterSignalSync(existingEntity, owner);
10413
10415
  this.updateCharacterHitbox(owner);
10414
10416
  return;
10415
10417
  }
@@ -10440,6 +10442,7 @@ class RpgCommonMap {
10440
10442
  const entity = this.physic.getEntityByUUID(owner.id);
10441
10443
  if (!entity) return;
10442
10444
  entity.owner = owner;
10445
+ this.bindCharacterSignalSync(entity, owner);
10443
10446
  const hitbox = typeof owner.hitbox === "function" ? owner.hitbox() : owner.hitbox;
10444
10447
  const width = hitbox?.w ?? 32;
10445
10448
  const height = hitbox?.h ?? 32;
@@ -10461,6 +10464,50 @@ class RpgCommonMap {
10461
10464
  }
10462
10465
  return fallback;
10463
10466
  }
10467
+ bindCharacterSignalSync(entity, owner) {
10468
+ const entityAny = entity;
10469
+ if (entityAny.__rpgSignalSyncOwner === owner && typeof entityAny.__rpgSignalSyncUnsubscribe === "function") {
10470
+ return;
10471
+ }
10472
+ this.unbindCharacterSignalSync(entity);
10473
+ entityAny.__rpgSignalSyncOwner = owner;
10474
+ const subscriptions = [];
10475
+ entityAny.__rpgSignalSyncUnsubscribe = () => {
10476
+ subscriptions.forEach((subscription) => subscription.unsubscribe());
10477
+ delete entityAny.__rpgSignalSyncUnsubscribe;
10478
+ delete entityAny.__rpgSignalSyncOwner;
10479
+ };
10480
+ const syncFromSignals = () => {
10481
+ if (this.isPhysicsSyncingSignals) {
10482
+ return;
10483
+ }
10484
+ const currentOwner = entityAny.owner;
10485
+ if (!currentOwner?.id || currentOwner.id !== entity.uuid) {
10486
+ return;
10487
+ }
10488
+ this.updateCharacterHitbox(currentOwner);
10489
+ };
10490
+ const subscribeSignal = (signalLike) => {
10491
+ const observable = signalLike?.observable;
10492
+ if (!observable || typeof observable.subscribe !== "function") {
10493
+ return;
10494
+ }
10495
+ subscriptions.push(observable.subscribe(() => syncFromSignals()));
10496
+ };
10497
+ subscribeSignal(owner.x);
10498
+ subscribeSignal(owner.y);
10499
+ subscribeSignal(owner.hitbox);
10500
+ subscribeSignal(owner._through);
10501
+ }
10502
+ unbindCharacterSignalSync(entity) {
10503
+ const entityAny = entity;
10504
+ const unsubscribe = entityAny.__rpgSignalSyncUnsubscribe;
10505
+ if (typeof unsubscribe === "function") {
10506
+ unsubscribe();
10507
+ return;
10508
+ }
10509
+ delete entityAny.__rpgSignalSyncOwner;
10510
+ }
10464
10511
  shouldDisableCharacterCollisions(owner) {
10465
10512
  if (typeof owner._through === "function") {
10466
10513
  try {
@@ -10822,6 +10869,7 @@ class RpgCommonMap {
10822
10869
  entity.unfreeze();
10823
10870
  }
10824
10871
  entity.owner = owner;
10872
+ this.bindCharacterSignalSync(entity, owner);
10825
10873
  entity.onDirectionChange(({ cardinalDirection }) => {
10826
10874
  if (!("$send" in this)) return;
10827
10875
  const owner2 = entity.owner;
@@ -10863,14 +10911,16 @@ class RpgCommonMap {
10863
10911
  const topLeftX2 = x - entityWidth / 2;
10864
10912
  const topLeftY2 = y - entityHeight / 2;
10865
10913
  let changed = false;
10866
- if (typeof currentOwner.x === "function" && typeof currentOwner.x.set === "function") {
10867
- currentOwner.x.set(Math.round(topLeftX2));
10868
- changed = true;
10869
- }
10870
- if (typeof currentOwner.y === "function" && typeof currentOwner.y.set === "function") {
10871
- currentOwner.y.set(Math.round(topLeftY2));
10872
- changed = true;
10873
- }
10914
+ this.withPhysicsSync(() => {
10915
+ if (typeof currentOwner.x === "function" && typeof currentOwner.x.set === "function") {
10916
+ currentOwner.x.set(Math.round(topLeftX2));
10917
+ changed = true;
10918
+ }
10919
+ if (typeof currentOwner.y === "function" && typeof currentOwner.y.set === "function") {
10920
+ currentOwner.y.set(Math.round(topLeftY2));
10921
+ changed = true;
10922
+ }
10923
+ });
10874
10924
  if (changed) {
10875
10925
  currentOwner.applyFrames?.();
10876
10926
  }
@@ -10983,6 +11033,7 @@ class RpgCommonMap {
10983
11033
  if (!entity) {
10984
11034
  return false;
10985
11035
  }
11036
+ this.unbindCharacterSignalSync(entity);
10986
11037
  const resolvedOwner = owner ?? entity.owner;
10987
11038
  if (resolvedOwner) {
10988
11039
  this.emitPhysicsEntityRemove({