@rpgjs/common 5.0.0-alpha.25 → 5.0.0-alpha.26

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.
@@ -1,4 +1,11 @@
1
1
  import { RpgCommonPlayer } from '../Player';
2
+ interface ItemData {
3
+ name: string;
4
+ description: string;
5
+ price: number;
6
+ quantity: number;
7
+ onAdd: (player: RpgCommonPlayer) => void;
8
+ }
2
9
  export declare class Item {
3
10
  id: import('@signe/reactive').WritableSignal<string>;
4
11
  name: import('@signe/reactive').WritableSignal<string>;
@@ -6,5 +13,6 @@ export declare class Item {
6
13
  price: import('@signe/reactive').WritableSignal<number>;
7
14
  quantity: import('@signe/reactive').WritableSignal<number>;
8
15
  onAdd: (player: RpgCommonPlayer) => void;
9
- constructor(data: any);
16
+ constructor(data?: ItemData);
10
17
  }
18
+ export {};
package/dist/index.js CHANGED
@@ -2442,10 +2442,10 @@ class Item {
2442
2442
  this.quantity = signal(1);
2443
2443
  this.onAdd = () => {
2444
2444
  };
2445
- this.description.set(data.description);
2446
- this.price.set(data.price);
2447
- this.name.set(data.name);
2448
- this.onAdd = data.onAdd?.bind(this) ?? (() => {
2445
+ this.description.set(data?.description ?? "");
2446
+ this.price.set(data?.price ?? 0);
2447
+ this.name.set(data?.name ?? "");
2448
+ this.onAdd = data?.onAdd?.bind(this) ?? (() => {
2449
2449
  });
2450
2450
  }
2451
2451
  }
@@ -9482,6 +9482,10 @@ class RpgCommonMap {
9482
9482
  });
9483
9483
  this.moveManager = new MovementManager(() => this.physic);
9484
9484
  this.speedScalar = 50;
9485
+ // Default speed scalar for movement
9486
+ // World Maps properties
9487
+ this.tileWidth = 32;
9488
+ this.tileHeight = 32;
9485
9489
  this.physicsAccumulatorMs = 0;
9486
9490
  this.physicsSyncDepth = 0;
9487
9491
  /**
@@ -9536,6 +9540,88 @@ class RpgCommonMap {
9536
9540
  get isStandalone() {
9537
9541
  return typeof window !== "undefined";
9538
9542
  }
9543
+ /**
9544
+ * Get the width of the map in pixels
9545
+ *
9546
+ * @returns The width of the map in pixels, or 0 if not loaded
9547
+ *
9548
+ * @example
9549
+ * ```ts
9550
+ * const width = map.widthPx;
9551
+ * console.log(`Map width: ${width}px`);
9552
+ * ```
9553
+ */
9554
+ get widthPx() {
9555
+ return this.data()?.width ?? 0;
9556
+ }
9557
+ /**
9558
+ * Get the height of the map in pixels
9559
+ *
9560
+ * @returns The height of the map in pixels, or 0 if not loaded
9561
+ *
9562
+ * @example
9563
+ * ```ts
9564
+ * const height = map.heightPx;
9565
+ * console.log(`Map height: ${height}px`);
9566
+ * ```
9567
+ */
9568
+ get heightPx() {
9569
+ return this.data()?.height ?? 0;
9570
+ }
9571
+ /**
9572
+ * Get the unique identifier of the map
9573
+ *
9574
+ * @returns The map ID, or empty string if not loaded
9575
+ *
9576
+ * @example
9577
+ * ```ts
9578
+ * const mapId = map.id;
9579
+ * console.log(`Current map: ${mapId}`);
9580
+ * ```
9581
+ */
9582
+ get id() {
9583
+ return this.data()?.id ?? "";
9584
+ }
9585
+ /**
9586
+ * Get the X position of this map in the world coordinate system
9587
+ *
9588
+ * This is used when maps are part of a larger world map. The world position
9589
+ * indicates where this map is located relative to other maps.
9590
+ *
9591
+ * @returns The X position in world coordinates, or 0 if not in a world
9592
+ *
9593
+ * @example
9594
+ * ```ts
9595
+ * const worldX = map.worldX;
9596
+ * console.log(`Map is at world position (${worldX}, ${map.worldY})`);
9597
+ * ```
9598
+ */
9599
+ get worldX() {
9600
+ const worldMaps = this.getWorldMapsManager?.();
9601
+ if (!worldMaps) return 0;
9602
+ const mapId = this.id.startsWith("map-") ? this.id.slice(4) : this.id;
9603
+ return worldMaps.getMapInfo(mapId)?.worldX ?? 0;
9604
+ }
9605
+ /**
9606
+ * Get the Y position of this map in the world coordinate system
9607
+ *
9608
+ * This is used when maps are part of a larger world map. The world position
9609
+ * indicates where this map is located relative to other maps.
9610
+ *
9611
+ * @returns The Y position in world coordinates, or 0 if not in a world
9612
+ *
9613
+ * @example
9614
+ * ```ts
9615
+ * const worldY = map.worldY;
9616
+ * console.log(`Map is at world position (${map.worldX}, ${worldY})`);
9617
+ * ```
9618
+ */
9619
+ get worldY() {
9620
+ const worldMaps = this.getWorldMapsManager?.();
9621
+ if (!worldMaps) return 0;
9622
+ const mapId = this.id.startsWith("map-") ? this.id.slice(4) : this.id;
9623
+ return worldMaps.getMapInfo(mapId)?.worldY ?? 0;
9624
+ }
9539
9625
  /**
9540
9626
  * Clear all physics content and reset to initial state
9541
9627
  *
@@ -10627,17 +10713,19 @@ class WorldMapsManager {
10627
10713
  if (typeof search === "number") {
10628
10714
  const src = map;
10629
10715
  return maps.filter((m) => {
10630
- const horizontallyOverlaps = Math.max(src.worldX, m.worldX) < Math.min(src.worldX + src.width, m.worldX + m.width);
10631
- const verticallyOverlaps = Math.max(src.worldY, m.worldY) < Math.min(src.worldY + src.height, m.worldY + m.height);
10716
+ const horizontallyOverlapsOrTouches = Math.max(src.worldX, m.worldX) <= Math.min(src.worldX + src.widthPx, m.worldX + m.widthPx);
10717
+ const verticallyOverlapsOrTouches = Math.max(src.worldY, m.worldY) <= Math.min(src.worldY + src.heightPx, m.worldY + m.heightPx);
10718
+ const marginLeftRight = src.tileWidth / 2;
10719
+ const marginTopDown = src.tileHeight / 2;
10632
10720
  switch (search) {
10633
10721
  case 0:
10634
- return verticallyOverlaps && m.worldY + m.height === src.worldY;
10722
+ return verticallyOverlapsOrTouches && m.worldY + m.heightPx - marginTopDown === src.worldY;
10635
10723
  case 1:
10636
- return verticallyOverlaps && m.worldY === src.worldY + src.height;
10724
+ return verticallyOverlapsOrTouches && m.worldY + marginTopDown === src.worldY + src.heightPx;
10637
10725
  case 2:
10638
- return horizontallyOverlaps && m.worldX + m.width === src.worldX;
10726
+ return horizontallyOverlapsOrTouches && m.worldX + m.widthPx - marginLeftRight === src.worldX;
10639
10727
  case 3:
10640
- return horizontallyOverlaps && m.worldX === src.worldX + src.width;
10728
+ return horizontallyOverlapsOrTouches && m.worldX + marginLeftRight === src.worldX + src.widthPx;
10641
10729
  default:
10642
10730
  return false;
10643
10731
  }
@@ -10645,7 +10733,7 @@ class WorldMapsManager {
10645
10733
  }
10646
10734
  if ("x" in search && "y" in search) {
10647
10735
  const found = maps.find(
10648
- (m) => search.x >= m.worldX && search.x < m.worldX + m.width && search.y >= m.worldY && search.y < m.worldY + m.height
10736
+ (m) => search.x >= m.worldX && search.x < m.worldX + m.widthPx && search.y >= m.worldY && search.y < m.worldY + m.heightPx
10649
10737
  );
10650
10738
  return found ? [found] : [];
10651
10739
  }
@@ -10653,9 +10741,9 @@ class WorldMapsManager {
10653
10741
  const { minX, minY, maxX, maxY } = search;
10654
10742
  return maps.filter((m) => {
10655
10743
  const aLeft = m.worldX;
10656
- const aRight = m.worldX + m.width;
10744
+ const aRight = m.worldX + m.widthPx;
10657
10745
  const aTop = m.worldY;
10658
- const aBottom = m.worldY + m.height;
10746
+ const aBottom = m.worldY + m.heightPx;
10659
10747
  const bLeft = minX;
10660
10748
  const bRight = maxX;
10661
10749
  const bTop = minY;