@rpgjs/client 5.0.0-beta.12 → 5.0.0-beta.13

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.
Files changed (88) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/Game/Object.d.ts +2 -0
  3. package/dist/Game/Object.js +20 -6
  4. package/dist/Game/Object.js.map +1 -1
  5. package/dist/Gui/Gui.d.ts +3 -2
  6. package/dist/Gui/Gui.js +18 -6
  7. package/dist/Gui/Gui.js.map +1 -1
  8. package/dist/RpgClient.d.ts +21 -1
  9. package/dist/RpgClientEngine.d.ts +20 -2
  10. package/dist/RpgClientEngine.js +180 -17
  11. package/dist/RpgClientEngine.js.map +1 -1
  12. package/dist/components/character.ce.js +82 -7
  13. package/dist/components/character.ce.js.map +1 -1
  14. package/dist/components/gui/dialogbox/index.ce.js +27 -12
  15. package/dist/components/gui/dialogbox/index.ce.js.map +1 -1
  16. package/dist/components/gui/gameover.ce.js +4 -3
  17. package/dist/components/gui/gameover.ce.js.map +1 -1
  18. package/dist/components/gui/menu/equip-menu.ce.js +9 -8
  19. package/dist/components/gui/menu/equip-menu.ce.js.map +1 -1
  20. package/dist/components/gui/menu/exit-menu.ce.js +7 -5
  21. package/dist/components/gui/menu/exit-menu.ce.js.map +1 -1
  22. package/dist/components/gui/menu/items-menu.ce.js +8 -7
  23. package/dist/components/gui/menu/items-menu.ce.js.map +1 -1
  24. package/dist/components/gui/menu/main-menu.ce.js +12 -11
  25. package/dist/components/gui/menu/main-menu.ce.js.map +1 -1
  26. package/dist/components/gui/menu/options-menu.ce.js +7 -5
  27. package/dist/components/gui/menu/options-menu.ce.js.map +1 -1
  28. package/dist/components/gui/menu/skills-menu.ce.js +4 -2
  29. package/dist/components/gui/menu/skills-menu.ce.js.map +1 -1
  30. package/dist/components/gui/notification/notification.ce.js +4 -1
  31. package/dist/components/gui/notification/notification.ce.js.map +1 -1
  32. package/dist/components/gui/save-load.ce.js +10 -9
  33. package/dist/components/gui/save-load.ce.js.map +1 -1
  34. package/dist/components/gui/shop/shop.ce.js +17 -16
  35. package/dist/components/gui/shop/shop.ce.js.map +1 -1
  36. package/dist/components/gui/title-screen.ce.js +4 -3
  37. package/dist/components/gui/title-screen.ce.js.map +1 -1
  38. package/dist/components/interaction-components.ce.js +20 -0
  39. package/dist/components/interaction-components.ce.js.map +1 -0
  40. package/dist/components/scenes/canvas.ce.js +12 -7
  41. package/dist/components/scenes/canvas.ce.js.map +1 -1
  42. package/dist/components/scenes/draw-map.ce.js +18 -13
  43. package/dist/components/scenes/draw-map.ce.js.map +1 -1
  44. package/dist/i18n.d.ts +55 -0
  45. package/dist/i18n.js +60 -0
  46. package/dist/i18n.js.map +1 -0
  47. package/dist/i18n.spec.d.ts +1 -0
  48. package/dist/index.d.ts +2 -0
  49. package/dist/index.js +3 -1
  50. package/dist/module.js +23 -3
  51. package/dist/module.js.map +1 -1
  52. package/dist/services/interactions.d.ts +159 -0
  53. package/dist/services/interactions.js +460 -0
  54. package/dist/services/interactions.js.map +1 -0
  55. package/dist/services/interactions.spec.d.ts +1 -0
  56. package/dist/services/keyboardControls.d.ts +1 -0
  57. package/dist/services/keyboardControls.js +1 -0
  58. package/dist/services/keyboardControls.js.map +1 -1
  59. package/package.json +4 -4
  60. package/src/Game/Object.spec.ts +14 -1
  61. package/src/Game/Object.ts +34 -10
  62. package/src/Gui/Gui.spec.ts +67 -0
  63. package/src/Gui/Gui.ts +24 -7
  64. package/src/RpgClient.ts +28 -1
  65. package/src/RpgClientEngine.ts +248 -29
  66. package/src/components/character.ce +90 -7
  67. package/src/components/gui/dialogbox/index.ce +35 -14
  68. package/src/components/gui/gameover.ce +4 -3
  69. package/src/components/gui/menu/equip-menu.ce +9 -8
  70. package/src/components/gui/menu/exit-menu.ce +4 -3
  71. package/src/components/gui/menu/items-menu.ce +8 -7
  72. package/src/components/gui/menu/main-menu.ce +12 -11
  73. package/src/components/gui/menu/options-menu.ce +4 -3
  74. package/src/components/gui/menu/skills-menu.ce +2 -1
  75. package/src/components/gui/notification/notification.ce +7 -1
  76. package/src/components/gui/save-load.ce +11 -10
  77. package/src/components/gui/shop/shop.ce +17 -16
  78. package/src/components/gui/title-screen.ce +4 -3
  79. package/src/components/interaction-components.ce +23 -0
  80. package/src/components/scenes/canvas.ce +12 -7
  81. package/src/components/scenes/draw-map.ce +16 -5
  82. package/src/i18n.spec.ts +39 -0
  83. package/src/i18n.ts +59 -0
  84. package/src/index.ts +2 -0
  85. package/src/module.ts +32 -10
  86. package/src/services/interactions.spec.ts +175 -0
  87. package/src/services/interactions.ts +722 -0
  88. package/src/services/keyboardControls.ts +2 -1
@@ -23,12 +23,68 @@ import { normalizeRoomMapId } from "./utils/mapId.js";
23
23
  import { ProjectileManager } from "./Game/ProjectileManager.js";
24
24
  import { ClientVisualRegistry } from "./Game/ClientVisuals.js";
25
25
  import { createClientPointerContext } from "./services/pointerContext.js";
26
+ import { RpgClientInteractions } from "./services/interactions.js";
26
27
  import { EventComponentResolverRegistry } from "./Game/EventComponentResolver.js";
28
+ import { RpgClientBuiltinI18n } from "./i18n.js";
27
29
  import { Howl, bootstrapCanvas, signal, trigger } from "canvasengine";
28
- import { Direction, ModulesToken, PredictionController, Vector2, normalizeLightingState } from "@rpgjs/common";
30
+ import { Direction, ModulesToken, PredictionController, Vector2, getOrCreateI18nService, normalizeLightingState } from "@rpgjs/common";
29
31
  import { BehaviorSubject, combineLatest, filter, lastValueFrom, switchMap, take } from "rxjs";
30
32
  import * as PIXI from "pixi.js";
31
33
  //#region src/RpgClientEngine.ts
34
+ var DEFAULT_DASH_ADDITIONAL_SPEED = 8;
35
+ var DEFAULT_DASH_DURATION_MS = 180;
36
+ var DEFAULT_DASH_COOLDOWN_MS = 450;
37
+ var isDashInput = (input) => typeof input === "object" && input !== null && input.type === "dash";
38
+ var isMoveInput = (input) => typeof input === "object" && input !== null && input.type === "move";
39
+ var resolveMoveDirection = (input) => {
40
+ if (isMoveInput(input)) return input.direction;
41
+ if (typeof input === "string" || typeof input === "number") return input;
42
+ };
43
+ var directionToVector = (direction) => {
44
+ switch (direction) {
45
+ case Direction.Left: return {
46
+ x: -1,
47
+ y: 0
48
+ };
49
+ case Direction.Right: return {
50
+ x: 1,
51
+ y: 0
52
+ };
53
+ case Direction.Up: return {
54
+ x: 0,
55
+ y: -1
56
+ };
57
+ case Direction.Down:
58
+ default: return {
59
+ x: 0,
60
+ y: 1
61
+ };
62
+ }
63
+ };
64
+ var vectorToDirection = (direction) => {
65
+ if (Math.abs(direction.x) > Math.abs(direction.y)) return direction.x < 0 ? Direction.Left : Direction.Right;
66
+ return direction.y < 0 ? Direction.Up : Direction.Down;
67
+ };
68
+ var normalizeDashInput = (input, fallbackDirection) => {
69
+ const rawDirection = input.direction ?? directionToVector(fallbackDirection);
70
+ const rawX = Number(rawDirection?.x ?? 0);
71
+ const rawY = Number(rawDirection?.y ?? 0);
72
+ const magnitude = Math.hypot(rawX, rawY);
73
+ if (!Number.isFinite(magnitude) || magnitude <= 0) return null;
74
+ const additionalSpeed = typeof input.additionalSpeed === "number" && Number.isFinite(input.additionalSpeed) ? Math.max(0, Math.min(input.additionalSpeed, 64)) : DEFAULT_DASH_ADDITIONAL_SPEED;
75
+ const duration = typeof input.duration === "number" && Number.isFinite(input.duration) ? Math.max(1, Math.min(input.duration, 1e3)) : DEFAULT_DASH_DURATION_MS;
76
+ const cooldown = typeof input.cooldown === "number" && Number.isFinite(input.cooldown) ? Math.max(0, Math.min(input.cooldown, 5e3)) : DEFAULT_DASH_COOLDOWN_MS;
77
+ return {
78
+ type: "dash",
79
+ direction: {
80
+ x: rawX / magnitude,
81
+ y: rawY / magnitude
82
+ },
83
+ additionalSpeed,
84
+ duration,
85
+ cooldown
86
+ };
87
+ };
32
88
  var RpgClientEngine = class {
33
89
  constructor(context) {
34
90
  this.context = context;
@@ -37,10 +93,12 @@ var RpgClientEngine = class {
37
93
  this.width = signal("100%");
38
94
  this.height = signal("100%");
39
95
  this.spritesheets = /* @__PURE__ */ new Map();
96
+ this.spritesheetPromises = /* @__PURE__ */ new Map();
40
97
  this.sounds = /* @__PURE__ */ new Map();
41
98
  this.componentAnimations = [];
42
99
  this.clientVisuals = new ClientVisualRegistry();
43
100
  this.pointer = createClientPointerContext();
101
+ this.interactions = new RpgClientInteractions(this);
44
102
  this.particleSettings = { emitters: [] };
45
103
  this.playerIdSignal = signal(null);
46
104
  this.spriteComponentsBehind = signal([]);
@@ -58,6 +116,7 @@ var RpgClientEngine = class {
58
116
  this.lastClientPhysicsStepAt = 0;
59
117
  this.frameOffset = 0;
60
118
  this.latestServerTickAt = 0;
119
+ this.dashLockedUntil = 0;
61
120
  this.rtt = 0;
62
121
  this.pingInterval = null;
63
122
  this.PING_INTERVAL_MS = 5e3;
@@ -80,6 +139,8 @@ var RpgClientEngine = class {
80
139
  this.guiService = inject(RpgGui);
81
140
  this.loadMapService = inject(LoadMapToken);
82
141
  this.hooks = inject(ModulesToken);
142
+ this.i18nService = getOrCreateI18nService(context);
143
+ this.i18nService.addMessages(RpgClientBuiltinI18n, "rpgjs-client", 0);
83
144
  this.projectiles = new ProjectileManager(this.hooks, (projectile) => this.predictProjectileImpact(projectile));
84
145
  this.globalConfig = inject(GlobalConfigToken);
85
146
  if (!this.globalConfig) this.globalConfig = {};
@@ -103,6 +164,21 @@ var RpgClientEngine = class {
103
164
  this.predictionEnabled = this.globalConfig?.prediction?.enabled !== false;
104
165
  this.initializePredictionController();
105
166
  }
167
+ setLocale(locale) {
168
+ this.locale = locale;
169
+ }
170
+ getLocale() {
171
+ return this.locale || this.i18nService.defaultLocale;
172
+ }
173
+ t(key, params) {
174
+ return this.i18nService.t(key, params, this.getLocale());
175
+ }
176
+ i18n() {
177
+ return {
178
+ locale: this.getLocale(),
179
+ t: (key, params) => this.t(key, params)
180
+ };
181
+ }
106
182
  /**
107
183
  * Assigns a CanvasEngine KeyboardControls instance to the dependency injection context
108
184
  *
@@ -167,7 +243,6 @@ var RpgClientEngine = class {
167
243
  this.renderer = app.renderer;
168
244
  this.setupPointerTracking();
169
245
  this.tick = canvasElement?.propObservables?.context["tick"].observable;
170
- this.flushPendingSyncPackets();
171
246
  const inputCheckSubscription = this.tick.subscribe(() => {
172
247
  if (Date.now() - this.lastInputTime > 100) {
173
248
  const player = this.getCurrentPlayer();
@@ -178,6 +253,7 @@ var RpgClientEngine = class {
178
253
  this.tickSubscriptions.push(inputCheckSubscription);
179
254
  this.hooks.callHooks("client-spritesheets-load", this).subscribe();
180
255
  this.hooks.callHooks("client-spritesheetResolver-load", this).subscribe();
256
+ this.flushPendingSyncPackets();
181
257
  this.hooks.callHooks("client-sounds-load", this).subscribe();
182
258
  this.hooks.callHooks("client-soundResolver-load", this).subscribe();
183
259
  RpgSound.init(this);
@@ -187,6 +263,7 @@ var RpgClientEngine = class {
187
263
  this.hooks.callHooks("client-componentAnimations-load", this).subscribe();
188
264
  this.hooks.callHooks("client-clientVisuals-load", this).subscribe();
189
265
  this.hooks.callHooks("client-projectiles-load", this).subscribe();
266
+ this.hooks.callHooks("client-interactions-load", this).subscribe();
190
267
  this.hooks.callHooks("client-sprite-load", this).subscribe();
191
268
  await lastValueFrom(this.hooks.callHooks("client-engine-onStart", this));
192
269
  this.resizeHandler = () => {
@@ -218,7 +295,7 @@ var RpgClientEngine = class {
218
295
  const canvas = renderer?.canvas ?? renderer?.view ?? this.canvasApp?.canvas;
219
296
  if (!canvas || typeof canvas.addEventListener !== "function") return;
220
297
  this.pointerCanvas = canvas;
221
- this.pointerMoveHandler = (event) => {
298
+ const updatePointer = (event) => {
222
299
  const rect = canvas.getBoundingClientRect();
223
300
  const screen = {
224
301
  x: event.clientX - rect.left,
@@ -241,11 +318,59 @@ var RpgClientEngine = class {
241
318
  }
242
319
  this.pointer.update(screen, world);
243
320
  };
321
+ this.pointerMoveHandler = (event) => {
322
+ updatePointer(event);
323
+ this.interactions.handlePointerMove(event);
324
+ };
325
+ this.pointerUpHandler = (event) => {
326
+ updatePointer(event);
327
+ this.interactions.handlePointerUp(event);
328
+ };
329
+ this.pointerCancelHandler = (event) => {
330
+ updatePointer(event);
331
+ this.interactions.cancelDrag(event);
332
+ };
244
333
  canvas.addEventListener("pointermove", this.pointerMoveHandler);
245
334
  canvas.addEventListener("pointerdown", this.pointerMoveHandler);
335
+ canvas.addEventListener("pointerup", this.pointerUpHandler);
336
+ canvas.addEventListener("pointercancel", this.pointerCancelHandler);
337
+ canvas.addEventListener("pointerleave", this.pointerCancelHandler);
338
+ }
339
+ updatePointerFromInteractionEvent(event) {
340
+ const global = event?.global ?? event?.data?.global;
341
+ if (!global) {
342
+ this.pointer.updateFromEvent(event);
343
+ return;
344
+ }
345
+ const screen = {
346
+ x: Number(global.x),
347
+ y: Number(global.y)
348
+ };
349
+ if (!Number.isFinite(screen.x) || !Number.isFinite(screen.y)) {
350
+ this.pointer.updateFromEvent(event);
351
+ return;
352
+ }
353
+ const viewport = this.findViewportInstance();
354
+ if (viewport && typeof viewport.toWorld === "function") {
355
+ const point = viewport.toWorld(screen.x, screen.y);
356
+ this.pointer.update(screen, {
357
+ x: Number(point.x),
358
+ y: Number(point.y)
359
+ });
360
+ return;
361
+ }
362
+ this.pointer.update(screen);
246
363
  }
247
364
  findViewportInstance() {
248
- return (this.canvasApp?.stage?.children ?? []).find((child) => typeof child?.toWorld === "function" || child?.constructor?.name === "Viewport");
365
+ const find = (node) => {
366
+ if (!node) return void 0;
367
+ if (typeof node?.toWorld === "function" || node?.constructor?.name === "Viewport") return node;
368
+ for (const child of node.children ?? []) {
369
+ const viewport = find(child);
370
+ if (viewport) return viewport;
371
+ }
372
+ };
373
+ return find(this.canvasApp?.stage);
249
374
  }
250
375
  prepareSyncPayload(data) {
251
376
  const payload = { ...data ?? {} };
@@ -637,12 +762,20 @@ var RpgClientEngine = class {
637
762
  getSpriteSheet(id) {
638
763
  if (this.spritesheets.has(id)) return this.spritesheets.get(id);
639
764
  if (this.spritesheetResolver) {
765
+ if (this.spritesheetPromises.has(id)) return this.spritesheetPromises.get(id);
640
766
  const result = this.spritesheetResolver(id);
641
- if (result instanceof Promise) return result.then((spritesheet) => {
642
- if (spritesheet) this.spritesheets.set(id, spritesheet);
643
- return spritesheet;
644
- });
645
- else {
767
+ if (result instanceof Promise) {
768
+ const promise = result.then((spritesheet) => {
769
+ if (spritesheet) this.spritesheets.set(id, spritesheet);
770
+ this.spritesheetPromises.delete(id);
771
+ return spritesheet;
772
+ }).catch((error) => {
773
+ this.spritesheetPromises.delete(id);
774
+ throw error;
775
+ });
776
+ this.spritesheetPromises.set(id, promise);
777
+ return promise;
778
+ } else {
646
779
  if (result) this.spritesheets.set(id, result);
647
780
  return result;
648
781
  }
@@ -1163,10 +1296,17 @@ var RpgClientEngine = class {
1163
1296
  return;
1164
1297
  }
1165
1298
  const timestamp = Date.now();
1299
+ const movementInput = isDashInput(input) ? normalizeDashInput(input, currentPlayer?.direction?.()) : input;
1300
+ if (!movementInput) return;
1301
+ if (isDashInput(movementInput)) {
1302
+ const cooldown = movementInput.cooldown ?? DEFAULT_DASH_COOLDOWN_MS;
1303
+ if (timestamp < this.dashLockedUntil) return;
1304
+ this.dashLockedUntil = timestamp + cooldown;
1305
+ }
1166
1306
  let frame;
1167
1307
  let tick;
1168
1308
  if (this.predictionEnabled && this.prediction) {
1169
- const meta = this.prediction.recordInput(input, timestamp);
1309
+ const meta = this.prediction.recordInput(movementInput, timestamp);
1170
1310
  frame = meta.frame;
1171
1311
  tick = meta.tick;
1172
1312
  } else {
@@ -1175,20 +1315,25 @@ var RpgClientEngine = class {
1175
1315
  }
1176
1316
  this.inputFrameCounter = frame;
1177
1317
  this.hooks.callHooks("client-engine-onInput", this, {
1178
- input,
1318
+ input: movementInput,
1179
1319
  playerId: this.playerId
1180
1320
  }).subscribe();
1181
1321
  const bodyReady = this.ensureCurrentPlayerBody();
1182
1322
  if (currentPlayer && bodyReady) {
1183
- currentPlayer.changeDirection(input);
1184
- this.sceneMap.moveBody(currentPlayer, input);
1323
+ this.applyPredictedMovementInput(currentPlayer, movementInput);
1185
1324
  if (this.predictionEnabled && this.prediction) {
1186
1325
  this.pendingPredictionFrames.push(frame);
1187
1326
  if (this.pendingPredictionFrames.length > 240) this.pendingPredictionFrames = this.pendingPredictionFrames.slice(-240);
1188
1327
  }
1189
1328
  }
1190
- this.emitMovePacket(input, frame, tick, timestamp, true);
1191
- this.lastInputTime = Date.now();
1329
+ this.emitMovePacket(movementInput, frame, tick, timestamp, true);
1330
+ this.lastInputTime = isDashInput(movementInput) ? Date.now() + (movementInput.duration ?? DEFAULT_DASH_DURATION_MS) : Date.now();
1331
+ }
1332
+ async processDash(input = {}) {
1333
+ const currentPlayer = this.sceneMap.getCurrentPlayer();
1334
+ const dashInput = normalizeDashInput(input, typeof currentPlayer?.direction === "function" ? currentPlayer.direction() : currentPlayer?.direction);
1335
+ if (!dashInput) return;
1336
+ await this.processInput({ input: dashInput });
1192
1337
  }
1193
1338
  processAction(action, data) {
1194
1339
  if (this.stopProcessingInput) return;
@@ -1297,7 +1442,7 @@ var RpgClientEngine = class {
1297
1442
  input: entry.direction,
1298
1443
  x: state.x,
1299
1444
  y: state.y,
1300
- direction: state.direction ?? entry.direction
1445
+ direction: state.direction ?? resolveMoveDirection(entry.direction)
1301
1446
  });
1302
1447
  }
1303
1448
  if (trajectory.length > this.MAX_MOVE_TRAJECTORY_POINTS) return trajectory.slice(-this.MAX_MOVE_TRAJECTORY_POINTS);
@@ -1332,6 +1477,17 @@ var RpgClientEngine = class {
1332
1477
  if (now - this.lastMovePathSentAt < this.MOVE_PATH_RESEND_INTERVAL_MS) return;
1333
1478
  this.emitMovePacket(latest.direction, latest.frame, latest.tick, now, false);
1334
1479
  }
1480
+ applyPredictedMovementInput(player, input) {
1481
+ if (isDashInput(input)) {
1482
+ const direction = vectorToDirection(input.direction);
1483
+ player.changeDirection(direction);
1484
+ return Boolean(this.sceneMap.dashBody?.(player, input));
1485
+ }
1486
+ const direction = resolveMoveDirection(input);
1487
+ if (!direction) return false;
1488
+ player.changeDirection(direction);
1489
+ return Boolean(this.sceneMap.moveBody?.(player, direction));
1490
+ }
1335
1491
  getLocalPlayerState() {
1336
1492
  const currentPlayer = this.sceneMap?.getCurrentPlayer();
1337
1493
  if (!currentPlayer) return {
@@ -1557,7 +1713,7 @@ var RpgClientEngine = class {
1557
1713
  const replayInputs = pendingInputs.slice(-600);
1558
1714
  for (const entry of replayInputs) {
1559
1715
  if (!entry?.direction) continue;
1560
- this.sceneMap.moveBody(player, entry.direction);
1716
+ this.applyPredictedMovementInput(player, entry.direction);
1561
1717
  this.sceneMap.stepPredictionTick();
1562
1718
  this.prediction?.attachPredictedState(entry.frame, this.getLocalPlayerState());
1563
1719
  }
@@ -1626,7 +1782,14 @@ var RpgClientEngine = class {
1626
1782
  if (this.pointerMoveHandler && this.pointerCanvas) {
1627
1783
  this.pointerCanvas.removeEventListener("pointermove", this.pointerMoveHandler);
1628
1784
  this.pointerCanvas.removeEventListener("pointerdown", this.pointerMoveHandler);
1785
+ if (this.pointerUpHandler) this.pointerCanvas.removeEventListener("pointerup", this.pointerUpHandler);
1786
+ if (this.pointerCancelHandler) {
1787
+ this.pointerCanvas.removeEventListener("pointercancel", this.pointerCancelHandler);
1788
+ this.pointerCanvas.removeEventListener("pointerleave", this.pointerCancelHandler);
1789
+ }
1629
1790
  this.pointerMoveHandler = void 0;
1791
+ this.pointerUpHandler = void 0;
1792
+ this.pointerCancelHandler = void 0;
1630
1793
  this.pointerCanvas = void 0;
1631
1794
  }
1632
1795
  const rendererStillExists = this.renderer && typeof this.renderer.destroy === "function";