@rpgjs/client 5.0.0-beta.22 → 5.0.0-beta.24

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 (49) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/Game/Object.d.ts +6 -0
  3. package/dist/Game/Object.js +58 -2
  4. package/dist/Game/Object.js.map +1 -1
  5. package/dist/RpgClientEngine.d.ts +2 -0
  6. package/dist/RpgClientEngine.js +15 -6
  7. package/dist/RpgClientEngine.js.map +1 -1
  8. package/dist/components/character-hitbox.d.ts +13 -0
  9. package/dist/components/character-hitbox.js +38 -0
  10. package/dist/components/character-hitbox.js.map +1 -0
  11. package/dist/components/character-hitbox.spec.d.ts +1 -0
  12. package/dist/components/character.ce.js +38 -29
  13. package/dist/components/character.ce.js.map +1 -1
  14. package/dist/components/gui/dialogbox/index.ce.js +31 -2
  15. package/dist/components/gui/dialogbox/index.ce.js.map +1 -1
  16. package/dist/components/gui/hud/hud.ce.js +2 -2
  17. package/dist/components/gui/hud/hud.ce.js.map +1 -1
  18. package/dist/components/gui/menu/items-menu.ce.js +27 -4
  19. package/dist/components/gui/menu/items-menu.ce.js.map +1 -1
  20. package/dist/components/gui/menu/main-menu.ce.js +210 -155
  21. package/dist/components/gui/menu/main-menu.ce.js.map +1 -1
  22. package/dist/components/gui/save-load.ce.js +33 -4
  23. package/dist/components/gui/save-load.ce.js.map +1 -1
  24. package/dist/components/gui/shop/shop.ce.js +47 -4
  25. package/dist/components/gui/shop/shop.ce.js.map +1 -1
  26. package/dist/services/standalone.d.ts +6 -1
  27. package/dist/services/standalone.js +35 -17
  28. package/dist/services/standalone.js.map +1 -1
  29. package/dist/utils/syncHitbox.d.ts +1 -0
  30. package/dist/utils/syncHitbox.js +69 -0
  31. package/dist/utils/syncHitbox.js.map +1 -0
  32. package/dist/utils/syncHitbox.spec.d.ts +1 -0
  33. package/package.json +4 -4
  34. package/src/Game/Object.spec.ts +70 -6
  35. package/src/Game/Object.ts +90 -2
  36. package/src/RpgClientEngine.ts +19 -6
  37. package/src/components/character-hitbox.spec.ts +33 -0
  38. package/src/components/character-hitbox.ts +72 -0
  39. package/src/components/character.ce +52 -45
  40. package/src/components/gui/dialogbox/index.ce +56 -40
  41. package/src/components/gui/hud/hud.ce +2 -2
  42. package/src/components/gui/menu/items-menu.ce +12 -2
  43. package/src/components/gui/menu/main-menu.ce +37 -6
  44. package/src/components/gui/save-load.ce +48 -32
  45. package/src/components/gui/shop/shop.ce +28 -3
  46. package/src/services/standalone.spec.ts +47 -0
  47. package/src/services/standalone.ts +53 -20
  48. package/src/utils/syncHitbox.spec.ts +79 -0
  49. package/src/utils/syncHitbox.ts +104 -0
@@ -8,6 +8,7 @@ import { RpgGui } from "../Gui/Gui";
8
8
  import { provideKeyboardControls } from "./keyboardControls";
9
9
  import { provideSaveClient } from "./save";
10
10
  import { normalizeStandaloneMessage } from "./standalone-message";
11
+ import type { SocketQuery } from "./AbstractSocket";
11
12
 
12
13
  type ServerIo = any;
13
14
  type ClientIo = any;
@@ -22,6 +23,9 @@ class BridgeWebsocket extends AbstractWebsocket {
22
23
  private room: ServerIo;
23
24
  private socket: ClientIo;
24
25
  private socketRoom?: ServerIo;
26
+ private roomId = "lobby-1";
27
+ private query?: SocketQuery;
28
+ private readonly privateId = "player-client-id";
25
29
  private listeners: Array<{
26
30
  event: string;
27
31
  callback: (data: any) => void;
@@ -29,11 +33,10 @@ class BridgeWebsocket extends AbstractWebsocket {
29
33
  }> = [];
30
34
  private rooms = {
31
35
  partyFn: async (roomId: string) => {
32
- this.room = new ServerIo(roomId, this.rooms);
33
- const server = new this.server(this.room)
36
+ const room = new ServerIo(roomId, this.rooms);
37
+ const server = new this.server(room)
34
38
  await server.onStart();
35
39
  await server.subRoom.onStart()
36
- this.context.set('server', server)
37
40
  return server
38
41
  },
39
42
  env: {}
@@ -57,22 +60,8 @@ class BridgeWebsocket extends AbstractWebsocket {
57
60
 
58
61
  private async _connection(listeners?: (data: any) => void) {
59
62
  this.detachCurrentSocket();
60
- this.serverInstance = this.context.get('server')
61
- this.socket = new ClientIo(this.serverInstance, 'player-client-id');
62
- const url = new URL('http://localhost')
63
- const request = new Request(url.toString(), {
64
- method: 'GET',
65
- headers: {
66
- 'Content-Type': 'application/json'
67
- }
68
- })
63
+ await this.connectToRoom();
69
64
  listeners?.(this.socket)
70
- this.room.clients.set(this.socket.id, this.socket);
71
- this.socketRoom = this.room;
72
- this.listeners.forEach(({ handler }) => {
73
- this.socket.addEventListener("message", handler);
74
- });
75
- await this.serverInstance.onConnect(this.socket.conn as any, { request } as any);
76
65
  return this.socket
77
66
  }
78
67
 
@@ -129,8 +118,9 @@ class BridgeWebsocket extends AbstractWebsocket {
129
118
  * await websocket.reconnect()
130
119
  * ```
131
120
  */
132
- updateProperties(_params: SocketUpdateProperties) {
133
- // empty
121
+ updateProperties({ room, query }: SocketUpdateProperties) {
122
+ this.roomId = room;
123
+ this.query = query;
134
124
  }
135
125
 
136
126
  /**
@@ -156,6 +146,49 @@ class BridgeWebsocket extends AbstractWebsocket {
156
146
  })
157
147
  }
158
148
 
149
+ private async connectToRoom() {
150
+ if (this.serverInstance?.room?.id !== this.roomId) {
151
+ const party = await this.room.context.parties.main.get(this.roomId);
152
+ this.serverInstance = party.server;
153
+ this.context.set('server', this.serverInstance);
154
+ this.room = this.serverInstance.room;
155
+ }
156
+ else {
157
+ this.serverInstance = this.context.get('server');
158
+ }
159
+
160
+ this.socket = new ClientIo(this.serverInstance, this.privateId);
161
+ const url = new URL('http://localhost');
162
+ const query = this.normalizeQuery(this.query);
163
+ if (query) {
164
+ for (const [key, value] of Object.entries(query)) {
165
+ url.searchParams.set(key, value);
166
+ }
167
+ }
168
+ const request = new Request(url.toString(), {
169
+ method: 'GET',
170
+ headers: {
171
+ 'Content-Type': 'application/json'
172
+ }
173
+ });
174
+ this.room.clients.set(this.socket.id, this.socket);
175
+ this.socketRoom = this.room;
176
+ this.listeners.forEach(({ handler }) => {
177
+ this.socket.addEventListener("message", handler);
178
+ });
179
+ await this.serverInstance.onConnect(this.socket.conn as any, { request } as any);
180
+ }
181
+
182
+ private normalizeQuery(query?: SocketQuery): Record<string, string> | undefined {
183
+ if (!query) {
184
+ return undefined;
185
+ }
186
+ return Object.fromEntries(
187
+ Object.entries(query)
188
+ .filter((entry): entry is [string, string] => typeof entry[1] === "string")
189
+ );
190
+ }
191
+
159
192
  private detachCurrentSocket() {
160
193
  if (!this.socket) return;
161
194
  this.listeners.forEach(({ handler }) => {
@@ -0,0 +1,79 @@
1
+ import { signal } from "canvasengine";
2
+ import { describe, expect, test, vi } from "vitest";
3
+ import { load } from "@signe/sync";
4
+ import { applySyncedHitboxPayload } from "./syncHitbox";
5
+
6
+ const createObject = (id: string) => ({
7
+ id,
8
+ x: signal(10),
9
+ y: signal(20),
10
+ hitbox: signal({ w: 32, h: 32 }),
11
+ });
12
+
13
+ describe("applySyncedHitboxPayload", () => {
14
+ test("applies player hitboxes from sync object payloads", () => {
15
+ const player = createObject("player-1");
16
+ const sceneMap = {
17
+ players: () => ({ "player-1": player }),
18
+ events: () => ({}),
19
+ getObjectById: (id: string) => (id === "player-1" ? player : undefined),
20
+ updateHitbox: vi.fn(),
21
+ };
22
+ const payload = {
23
+ players: {
24
+ "player-1": {
25
+ hitbox: { w: 56, h: 50 },
26
+ },
27
+ },
28
+ };
29
+
30
+ load(sceneMap, payload, true);
31
+ expect(player.hitbox()).toEqual({ w: 32, h: 32 });
32
+
33
+ applySyncedHitboxPayload(sceneMap, payload);
34
+
35
+ expect(player.hitbox()).toEqual({ w: 56, h: 50 });
36
+ expect(sceneMap.updateHitbox).toHaveBeenCalledWith("player-1", 10, 20, 56, 50);
37
+ });
38
+
39
+ test("accepts Studio width and height hitbox payloads for events", () => {
40
+ const event = createObject("event-1");
41
+ const sceneMap = {
42
+ players: () => ({}),
43
+ events: () => ({ "event-1": event }),
44
+ getObjectById: (id: string) => (id === "event-1" ? event : undefined),
45
+ updateHitbox: vi.fn(),
46
+ };
47
+
48
+ applySyncedHitboxPayload(sceneMap, {
49
+ events: {
50
+ "event-1": {
51
+ hitbox: { width: "80", height: 40 },
52
+ },
53
+ },
54
+ });
55
+
56
+ expect(event.hitbox()).toEqual({ w: 80, h: 40 });
57
+ expect(sceneMap.updateHitbox).toHaveBeenCalledWith("event-1", 10, 20, 80, 40);
58
+ });
59
+
60
+ test("ignores invalid hitbox payloads", () => {
61
+ const player = createObject("player-1");
62
+ const sceneMap = {
63
+ players: () => ({ "player-1": player }),
64
+ events: () => ({}),
65
+ updateHitbox: vi.fn(),
66
+ };
67
+
68
+ applySyncedHitboxPayload(sceneMap, {
69
+ players: {
70
+ "player-1": {
71
+ hitbox: { w: 0, h: 40 },
72
+ },
73
+ },
74
+ });
75
+
76
+ expect(player.hitbox()).toEqual({ w: 32, h: 32 });
77
+ expect(sceneMap.updateHitbox).not.toHaveBeenCalled();
78
+ });
79
+ });
@@ -0,0 +1,104 @@
1
+ interface HitboxSize {
2
+ width: number;
3
+ height: number;
4
+ }
5
+
6
+ const toPositiveNumber = (value: unknown): number | null => {
7
+ const numberValue = typeof value === "string" ? Number(value) : value;
8
+ return typeof numberValue === "number" && Number.isFinite(numberValue) && numberValue > 0
9
+ ? Math.round(numberValue)
10
+ : null;
11
+ };
12
+
13
+ const readSignalValue = (value: any): any => {
14
+ if (typeof value === "function") {
15
+ try {
16
+ return value();
17
+ }
18
+ catch {
19
+ return undefined;
20
+ }
21
+ }
22
+ return value;
23
+ };
24
+
25
+ const normalizeHitbox = (source: unknown): HitboxSize | null => {
26
+ if (!source || typeof source !== "object") {
27
+ return null;
28
+ }
29
+
30
+ const hitbox = source as any;
31
+ const width = toPositiveNumber(hitbox.w ?? hitbox.width);
32
+ const height = toPositiveNumber(hitbox.h ?? hitbox.height);
33
+
34
+ return width && height ? { width, height } : null;
35
+ };
36
+
37
+ const readObjectHitbox = (object: any): HitboxSize | null => {
38
+ return normalizeHitbox(readSignalValue(object?.hitbox));
39
+ };
40
+
41
+ const readCoordinate = (object: any, key: "x" | "y"): number | null => {
42
+ const value = readSignalValue(object?.[key]);
43
+ const numberValue = typeof value === "string" ? Number(value) : value;
44
+ return typeof numberValue === "number" && Number.isFinite(numberValue)
45
+ ? numberValue
46
+ : null;
47
+ };
48
+
49
+ const getObjectById = (sceneMap: any, id: string): any => {
50
+ return sceneMap?.getObjectById?.(id)
51
+ ?? sceneMap?.players?.()?.[id]
52
+ ?? sceneMap?.events?.()?.[id];
53
+ };
54
+
55
+ const applyHitboxToObject = (
56
+ sceneMap: any,
57
+ id: string,
58
+ object: any,
59
+ hitbox: HitboxSize,
60
+ ): void => {
61
+ if (!object) {
62
+ return;
63
+ }
64
+
65
+ const current = readObjectHitbox(object);
66
+ if (current?.width !== hitbox.width || current?.height !== hitbox.height) {
67
+ if (typeof object.hitbox?.set === "function") {
68
+ object.hitbox.set({ w: hitbox.width, h: hitbox.height });
69
+ }
70
+ else if (typeof object.setHitbox === "function") {
71
+ object.setHitbox(hitbox.width, hitbox.height);
72
+ return;
73
+ }
74
+ }
75
+
76
+ const objectId = typeof object.id === "string" && object.id ? object.id : id;
77
+ const x = readCoordinate(object, "x");
78
+ const y = readCoordinate(object, "y");
79
+ if (objectId && x !== null && y !== null) {
80
+ sceneMap?.updateHitbox?.(objectId, x, y, hitbox.width, hitbox.height);
81
+ }
82
+ };
83
+
84
+ const applyHitboxCollection = (sceneMap: any, collection: unknown): void => {
85
+ if (!collection || typeof collection !== "object") {
86
+ return;
87
+ }
88
+
89
+ for (const [id, patch] of Object.entries(collection)) {
90
+ if (!patch || typeof patch !== "object") {
91
+ continue;
92
+ }
93
+ const hitbox = normalizeHitbox((patch as any).hitbox);
94
+ if (!hitbox) {
95
+ continue;
96
+ }
97
+ applyHitboxToObject(sceneMap, id, getObjectById(sceneMap, id), hitbox);
98
+ }
99
+ };
100
+
101
+ export const applySyncedHitboxPayload = (sceneMap: any, payload: any): void => {
102
+ applyHitboxCollection(sceneMap, payload?.players);
103
+ applyHitboxCollection(sceneMap, payload?.events);
104
+ };