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

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
@@ -10086,6 +10086,7 @@ class RpgCommonMap {
10086
10086
  }
10087
10087
  this.clearAll();
10088
10088
  this.moveManager.clearAll();
10089
+ this.emitPhysicsReset();
10089
10090
  this.physicsAccumulatorMs = 0;
10090
10091
  }
10091
10092
  /**
@@ -10095,6 +10096,14 @@ class RpgCommonMap {
10095
10096
  clearAll() {
10096
10097
  const entities = this.physic.getEntities();
10097
10098
  for (const entity of entities) {
10099
+ const owner = entity.owner;
10100
+ if (owner) {
10101
+ this.emitPhysicsEntityRemove({
10102
+ owner,
10103
+ entity,
10104
+ kind: this.resolvePhysicsEntityKind(owner, entity.uuid)
10105
+ });
10106
+ }
10098
10107
  this.physic.removeEntity(entity);
10099
10108
  }
10100
10109
  this.physic.getMovementManager().clearAll();
@@ -10120,10 +10129,11 @@ class RpgCommonMap {
10120
10129
  this.addStaticHitbox(staticHitbox.id ?? generateShortUUID(), staticHitbox.points);
10121
10130
  }
10122
10131
  }
10132
+ this.emitPhysicsInit({ mapData });
10123
10133
  this.playersSubscription = this.players.observable.subscribe(
10124
10134
  ({ value: player, type, key }) => {
10125
10135
  if (type === "remove") {
10126
- this.removeHitbox(key);
10136
+ this.removeHitbox(key, player, "hero");
10127
10137
  return;
10128
10138
  }
10129
10139
  if (type == "reset") {
@@ -10163,7 +10173,7 @@ class RpgCommonMap {
10163
10173
  if (eventObj && typeof eventObj._movementUnsubscribe === "function") {
10164
10174
  eventObj._movementUnsubscribe();
10165
10175
  }
10166
- this.removeHitbox(key);
10176
+ this.removeHitbox(key, event, "npc");
10167
10177
  } else if (type === "update") {
10168
10178
  event.id = event.id ?? key;
10169
10179
  if (!this.getBody(key)) {
@@ -10416,6 +10426,14 @@ class RpgCommonMap {
10416
10426
  isStatic: options?.isStatic,
10417
10427
  mass: options?.mass
10418
10428
  });
10429
+ const entity = this.getBody(owner.id);
10430
+ if (entity) {
10431
+ this.emitPhysicsEntityAdd({
10432
+ owner,
10433
+ entity,
10434
+ kind
10435
+ });
10436
+ }
10419
10437
  }
10420
10438
  updateCharacterHitbox(owner) {
10421
10439
  if (!owner?.id) return;
@@ -10456,6 +10474,32 @@ class RpgCommonMap {
10456
10474
  }
10457
10475
  return false;
10458
10476
  }
10477
+ resolvePhysicsEntityKind(owner, id) {
10478
+ if (typeof owner?.isEvent === "function") {
10479
+ try {
10480
+ if (owner.isEvent()) {
10481
+ return "npc";
10482
+ }
10483
+ } catch {
10484
+ }
10485
+ }
10486
+ const ownerId = typeof owner?.id === "string" ? owner.id : id;
10487
+ if (ownerId && this.players()?.[ownerId]) {
10488
+ return "hero";
10489
+ }
10490
+ if (ownerId && this.events()?.[ownerId]) {
10491
+ return "npc";
10492
+ }
10493
+ return "generic";
10494
+ }
10495
+ emitPhysicsInit(_context) {
10496
+ }
10497
+ emitPhysicsEntityAdd(_context) {
10498
+ }
10499
+ emitPhysicsEntityRemove(_context) {
10500
+ }
10501
+ emitPhysicsReset() {
10502
+ }
10459
10503
  withPhysicsSync(run) {
10460
10504
  this.physicsSyncDepth += 1;
10461
10505
  try {
@@ -10934,11 +10978,19 @@ class RpgCommonMap {
10934
10978
  * Remove a hitbox from the physics world
10935
10979
  * @private
10936
10980
  */
10937
- removeHitbox(id) {
10981
+ removeHitbox(id, owner, kind) {
10938
10982
  const entity = this.physic.getEntityByUUID(id);
10939
10983
  if (!entity) {
10940
10984
  return false;
10941
10985
  }
10986
+ const resolvedOwner = owner ?? entity.owner;
10987
+ if (resolvedOwner) {
10988
+ this.emitPhysicsEntityRemove({
10989
+ owner: resolvedOwner,
10990
+ entity,
10991
+ kind: kind ?? this.resolvePhysicsEntityKind(resolvedOwner, id)
10992
+ });
10993
+ }
10942
10994
  this.physic.removeEntity(entity);
10943
10995
  return true;
10944
10996
  }