@rpgjs/server 5.0.0-alpha.31 → 5.0.0-alpha.32

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.
@@ -659,10 +659,10 @@ export interface RpgServer {
659
659
  * })
660
660
  * ```
661
661
  *
662
- * @prop { { [dataName]: data } } [database]
662
+ * @prop { { [dataName]: data } | (engine: RpgMap) => { [dataName]: data } | Promise<{ [dataName]: data }> } [database]
663
663
  * @memberof RpgServer
664
664
  * */
665
- database?: object | any[];
665
+ database?: object | any[] | ((engine: RpgMap) => object | any[] | Promise<object | any[]>);
666
666
  /**
667
667
  * Array of all maps. Each element can be either a class (decorated with `@MapData` or not) or a `MapOptions` object
668
668
  *
package/dist/index.js CHANGED
@@ -19476,6 +19476,9 @@ function WithMoveManager(Base) {
19476
19476
  this.waitingForPromise = false;
19477
19477
  this.promiseStartTime = 0;
19478
19478
  this.promiseDuration = 0;
19479
+ this.remainingDistance = 0;
19480
+ this.segmentDirection = null;
19481
+ this.segmentStep = 0;
19479
19482
  // Frequency wait state
19480
19483
  this.waitingForFrequency = false;
19481
19484
  this.frequencyWaitStartTime = 0;
@@ -19502,6 +19505,9 @@ function WithMoveManager(Base) {
19502
19505
  processNextRoute() {
19503
19506
  this.waitingForFrequency = false;
19504
19507
  this.frequencyWaitStartTime = 0;
19508
+ this.remainingDistance = 0;
19509
+ this.segmentDirection = null;
19510
+ this.segmentStep = 0;
19505
19511
  if (this.routeIndex >= this.routes.length) {
19506
19512
  this.debugLog("COMPLETE all routes finished");
19507
19513
  this.finished = true;
@@ -19578,41 +19584,13 @@ function WithMoveManager(Base) {
19578
19584
  break;
19579
19585
  }
19580
19586
  }
19581
- let targetTopLeftX = currentTopLeftX;
19582
- let targetTopLeftY = currentTopLeftY;
19583
- switch (moveDirection) {
19584
- case Direction.Right:
19585
- case "right":
19586
- targetTopLeftX = currentTopLeftX + distance;
19587
- break;
19588
- case Direction.Left:
19589
- case "left":
19590
- targetTopLeftX = currentTopLeftX - distance;
19591
- break;
19592
- case Direction.Down:
19593
- case "down":
19594
- targetTopLeftY = currentTopLeftY + distance;
19595
- break;
19596
- case Direction.Up:
19597
- case "up":
19598
- targetTopLeftY = currentTopLeftY - distance;
19599
- break;
19587
+ this.remainingDistance = distance;
19588
+ this.segmentDirection = moveDirection;
19589
+ this.segmentStep = this.getTileStepDistance(playerSpeed);
19590
+ this.setNextSegmentTarget(currentTopLeftX, currentTopLeftY);
19591
+ if (this.currentTargetTopLeft) {
19592
+ this.debugLog(`MOVE direction=${moveDirection} from=(${currentTopLeftX.toFixed(1)}, ${currentTopLeftY.toFixed(1)}) to=(${this.currentTargetTopLeft.x.toFixed(1)}, ${this.currentTargetTopLeft.y.toFixed(1)}) dist=${distance.toFixed(1)}`);
19600
19593
  }
19601
- const entity = map.physic.getEntityByUUID(this.player.id);
19602
- if (!entity) {
19603
- this.finished = true;
19604
- this.onComplete(false);
19605
- return;
19606
- }
19607
- const hitbox = this.player.hitbox();
19608
- const hitboxWidth = hitbox?.w ?? 32;
19609
- const hitboxHeight = hitbox?.h ?? 32;
19610
- const targetX = targetTopLeftX + hitboxWidth / 2;
19611
- const targetY = targetTopLeftY + hitboxHeight / 2;
19612
- this.currentTarget = { x: targetX, y: targetY };
19613
- this.currentTargetTopLeft = { x: targetTopLeftX, y: targetTopLeftY };
19614
- this.currentDirection = { x: 0, y: 0 };
19615
- this.debugLog(`MOVE direction=${moveDirection} from=(${currentTopLeftX.toFixed(1)}, ${currentTopLeftY.toFixed(1)}) to=(${targetTopLeftX.toFixed(1)}, ${targetTopLeftY.toFixed(1)}) dist=${distance.toFixed(1)}`);
19616
19594
  this.lastPosition = null;
19617
19595
  this.isCurrentlyStuck = false;
19618
19596
  this.stuckCheckStartTime = 0;
@@ -19648,7 +19626,13 @@ function WithMoveManager(Base) {
19648
19626
  const frequencyMs = playerFrequency || 0;
19649
19627
  if (frequencyMs > 0 && Date.now() - this.frequencyWaitStartTime >= frequencyMs * this.ratioFrequency) {
19650
19628
  this.waitingForFrequency = false;
19651
- this.processNextRoute();
19629
+ if (this.remainingDistance > 0) {
19630
+ const currentTopLeftX2 = this.player.x();
19631
+ const currentTopLeftY2 = this.player.y();
19632
+ this.setNextSegmentTarget(currentTopLeftX2, currentTopLeftY2);
19633
+ } else {
19634
+ this.processNextRoute();
19635
+ }
19652
19636
  }
19653
19637
  return;
19654
19638
  }
@@ -19696,6 +19680,10 @@ function WithMoveManager(Base) {
19696
19680
  if (playerFrequency && playerFrequency > 0) {
19697
19681
  this.waitingForFrequency = true;
19698
19682
  this.frequencyWaitStartTime = Date.now();
19683
+ } else if (this.remainingDistance > 0) {
19684
+ const nextTopLeftX = this.player.x();
19685
+ const nextTopLeftY = this.player.y();
19686
+ this.setNextSegmentTarget(nextTopLeftX, nextTopLeftY);
19699
19687
  } else {
19700
19688
  this.processNextRoute();
19701
19689
  }
@@ -19720,6 +19708,10 @@ function WithMoveManager(Base) {
19720
19708
  if (playerFrequency && playerFrequency > 0) {
19721
19709
  this.waitingForFrequency = true;
19722
19710
  this.frequencyWaitStartTime = Date.now();
19711
+ } else if (this.remainingDistance > 0) {
19712
+ const nextTopLeftX = this.player.x();
19713
+ const nextTopLeftY = this.player.y();
19714
+ this.setNextSegmentTarget(nextTopLeftX, nextTopLeftY);
19723
19715
  } else {
19724
19716
  this.processNextRoute();
19725
19717
  }
@@ -19794,6 +19786,10 @@ function WithMoveManager(Base) {
19794
19786
  if (playerFrequency && playerFrequency > 0) {
19795
19787
  this.waitingForFrequency = true;
19796
19788
  this.frequencyWaitStartTime = Date.now();
19789
+ } else if (this.remainingDistance > 0) {
19790
+ const nextTopLeftX = this.player.x();
19791
+ const nextTopLeftY = this.player.y();
19792
+ this.setNextSegmentTarget(nextTopLeftX, nextTopLeftY);
19797
19793
  } else {
19798
19794
  this.processNextRoute();
19799
19795
  }
@@ -19816,6 +19812,60 @@ function WithMoveManager(Base) {
19816
19812
  onFinished() {
19817
19813
  this.onComplete(true);
19818
19814
  }
19815
+ getTileStepDistance(playerSpeed) {
19816
+ if (!Number.isFinite(playerSpeed) || playerSpeed <= 0) {
19817
+ return this.tileSize;
19818
+ }
19819
+ const stepsPerTile = Math.max(1, Math.floor(this.tileSize / playerSpeed));
19820
+ return stepsPerTile * playerSpeed;
19821
+ }
19822
+ setNextSegmentTarget(currentTopLeftX, currentTopLeftY) {
19823
+ if (!this.segmentDirection || this.remainingDistance <= 0) {
19824
+ return;
19825
+ }
19826
+ const map = this.player.getCurrentMap();
19827
+ if (!map) {
19828
+ this.finished = true;
19829
+ this.onComplete(false);
19830
+ return;
19831
+ }
19832
+ const entity = map.physic.getEntityByUUID(this.player.id);
19833
+ if (!entity) {
19834
+ this.finished = true;
19835
+ this.onComplete(false);
19836
+ return;
19837
+ }
19838
+ const segmentDistance = Math.min(this.segmentStep || this.remainingDistance, this.remainingDistance);
19839
+ let targetTopLeftX = currentTopLeftX;
19840
+ let targetTopLeftY = currentTopLeftY;
19841
+ switch (this.segmentDirection) {
19842
+ case Direction.Right:
19843
+ case "right":
19844
+ targetTopLeftX = currentTopLeftX + segmentDistance;
19845
+ break;
19846
+ case Direction.Left:
19847
+ case "left":
19848
+ targetTopLeftX = currentTopLeftX - segmentDistance;
19849
+ break;
19850
+ case Direction.Down:
19851
+ case "down":
19852
+ targetTopLeftY = currentTopLeftY + segmentDistance;
19853
+ break;
19854
+ case Direction.Up:
19855
+ case "up":
19856
+ targetTopLeftY = currentTopLeftY - segmentDistance;
19857
+ break;
19858
+ }
19859
+ const hitbox = this.player.hitbox();
19860
+ const hitboxWidth = hitbox?.w ?? 32;
19861
+ const hitboxHeight = hitbox?.h ?? 32;
19862
+ const targetX = targetTopLeftX + hitboxWidth / 2;
19863
+ const targetY = targetTopLeftY + hitboxHeight / 2;
19864
+ this.currentTarget = { x: targetX, y: targetY };
19865
+ this.currentTargetTopLeft = { x: targetTopLeftX, y: targetTopLeftY };
19866
+ this.currentDirection = { x: 0, y: 0 };
19867
+ this.remainingDistance -= segmentDistance;
19868
+ }
19819
19869
  }
19820
19870
  const routeStrategy = new RouteMovementStrategy(
19821
19871
  finalRoutes,
@@ -20178,7 +20228,7 @@ class MenuGui extends Gui {
20178
20228
  spCost: skill?.spCost() ?? 0
20179
20229
  }));
20180
20230
  const saveLoad = this.buildSaveLoad(options);
20181
- return { menus, items, equips: menuEquips, skills, saveLoad, playerStats: buildStats() };
20231
+ return { menus, items, equips: menuEquips, skills, saveLoad, playerStats: buildStats(), expForNextlevel: player2.expForNextlevel };
20182
20232
  }
20183
20233
  refreshMenu(clientActionId) {
20184
20234
  const data = this.buildMenuData(this.menuOptions);
@@ -20667,10 +20717,10 @@ function WithParameterManager(Base) {
20667
20717
  this._param = type(computed(() => {
20668
20718
  const obj = {};
20669
20719
  const parameters = this._parametersSignal();
20720
+ const allModifiers = this._getAggregatedModifiers();
20670
20721
  const level = this._level();
20671
20722
  for (const [name, paramConfig] of Object.entries(parameters)) {
20672
20723
  let curveVal = Math.floor((paramConfig.end - paramConfig.start) * ((level - 1) / (this.finalLevel - this.initialLevel))) + paramConfig.start;
20673
- const allModifiers = this._getAggregatedModifiers();
20674
20724
  const modifier = allModifiers[name];
20675
20725
  if (modifier) {
20676
20726
  if (modifier.rate) curveVal *= modifier.rate;
@@ -29874,14 +29924,18 @@ function provideServerModules(modules) {
29874
29924
  }
29875
29925
  };
29876
29926
  }
29877
- if (module.database && typeof module.database === "object") {
29878
- const database = { ...module.database };
29927
+ if (module.database) {
29928
+ const database = module.database;
29879
29929
  module = {
29880
29930
  ...module,
29881
29931
  databaseHooks: {
29882
- load: (engine) => {
29883
- for (const key in database) {
29884
- engine.addInDatabase(key, database[key]);
29932
+ load: async (engine) => {
29933
+ const data = typeof database === "function" ? await database(engine) : database;
29934
+ if (!data || typeof data !== "object") {
29935
+ return;
29936
+ }
29937
+ for (const key in data) {
29938
+ engine.addInDatabase(key, data[key]);
29885
29939
  }
29886
29940
  }
29887
29941
  }