@rpgjs/server 5.0.0-alpha.2 → 5.0.0-alpha.21

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 (53) hide show
  1. package/dist/Gui/DialogGui.d.ts +4 -0
  2. package/dist/Gui/index.d.ts +1 -0
  3. package/dist/Player/BattleManager.d.ts +32 -22
  4. package/dist/Player/ClassManager.d.ts +31 -18
  5. package/dist/Player/ComponentManager.d.ts +123 -0
  6. package/dist/Player/Components.d.ts +345 -0
  7. package/dist/Player/EffectManager.d.ts +40 -0
  8. package/dist/Player/ElementManager.d.ts +31 -0
  9. package/dist/Player/GoldManager.d.ts +22 -0
  10. package/dist/Player/GuiManager.d.ts +176 -0
  11. package/dist/Player/ItemFixture.d.ts +6 -0
  12. package/dist/Player/ItemManager.d.ts +164 -10
  13. package/dist/Player/MoveManager.d.ts +32 -44
  14. package/dist/Player/ParameterManager.d.ts +343 -14
  15. package/dist/Player/Player.d.ts +266 -8
  16. package/dist/Player/SkillManager.d.ts +27 -19
  17. package/dist/Player/StateManager.d.ts +28 -35
  18. package/dist/Player/VariableManager.d.ts +30 -0
  19. package/dist/RpgServer.d.ts +227 -1
  20. package/dist/decorators/event.d.ts +46 -0
  21. package/dist/decorators/map.d.ts +177 -0
  22. package/dist/index.d.ts +6 -0
  23. package/dist/index.js +17472 -18167
  24. package/dist/index.js.map +1 -1
  25. package/dist/rooms/map.d.ts +486 -8
  26. package/package.json +17 -15
  27. package/src/Gui/DialogGui.ts +7 -2
  28. package/src/Gui/index.ts +3 -1
  29. package/src/Player/BattleManager.ts +97 -38
  30. package/src/Player/ClassManager.ts +95 -35
  31. package/src/Player/ComponentManager.ts +425 -19
  32. package/src/Player/Components.ts +380 -0
  33. package/src/Player/EffectManager.ts +110 -27
  34. package/src/Player/ElementManager.ts +126 -25
  35. package/src/Player/GoldManager.ts +32 -35
  36. package/src/Player/GuiManager.ts +187 -140
  37. package/src/Player/ItemFixture.ts +4 -5
  38. package/src/Player/ItemManager.ts +363 -48
  39. package/src/Player/MoveManager.ts +323 -308
  40. package/src/Player/ParameterManager.ts +499 -99
  41. package/src/Player/Player.ts +719 -80
  42. package/src/Player/SkillManager.ts +44 -23
  43. package/src/Player/StateManager.ts +210 -95
  44. package/src/Player/VariableManager.ts +180 -48
  45. package/src/RpgServer.ts +236 -1
  46. package/src/core/context.ts +1 -0
  47. package/src/decorators/event.ts +61 -0
  48. package/src/decorators/map.ts +198 -0
  49. package/src/index.ts +7 -1
  50. package/src/module.ts +24 -0
  51. package/src/rooms/map.ts +1054 -54
  52. package/dist/Player/Event.d.ts +0 -0
  53. package/src/Player/Event.ts +0 -0
@@ -1,29 +1,435 @@
1
- import { type Constructor } from "@rpgjs/common";
1
+ import { Constructor } from "@rpgjs/common";
2
2
  import { RpgCommonPlayer } from "@rpgjs/common";
3
+ import { ComponentInput, ComponentLayout } from "./Components";
3
4
 
4
- /**
5
- * Interface defining what ComponentManager adds to a class
6
- */
7
- export interface IComponentManager {
8
- setGraphic(graphic: string | string[]): void;
9
- }
5
+ type ComponentPosition = 'top' | 'center' | 'bottom' | 'left' | 'right';
10
6
 
11
7
  /**
12
- * Component Manager mixin
8
+ * Component Manager Mixin
9
+ *
10
+ * Provides graphic and component management capabilities to any class. This mixin allows
11
+ * setting single or multiple graphics for player representation, enabling
12
+ * dynamic visual changes and animation sequences. It also provides methods to
13
+ * display UI components around the player graphic (top, bottom, center, left, right).
13
14
  *
14
- * Adds methods to manage player graphics
15
+ * Components are stored as JSON strings for efficient synchronization.
15
16
  *
16
- * @param Base - The base class to extend
17
- * @returns A new class with component management capabilities
17
+ * @param Base - The base class to extend with component management
18
+ * @returns Extended class with component management methods
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * class MyPlayer extends WithComponentManager(BasePlayer) {
23
+ * constructor() {
24
+ * super();
25
+ * this.setGraphic("hero");
26
+ * }
27
+ * }
28
+ *
29
+ * const player = new MyPlayer();
30
+ * player.setGraphic(["hero_idle", "hero_walk"]);
31
+ * player.setComponentsTop(Components.text('{name}'));
32
+ * ```
18
33
  */
19
- export function WithComponentManager<TBase extends Constructor<RpgCommonPlayer>>(Base: TBase) {
20
- return class extends Base implements IComponentManager {
21
- setGraphic(graphic: string | string[]) {
22
- if (Array.isArray(graphic)) {
23
- this.graphics.set(graphic);
24
- } else {
25
- this.graphics.set([graphic]);
34
+ export function WithComponentManager<TBase extends Constructor<RpgCommonPlayer>>(Base: TBase): new (...args: ConstructorParameters<TBase>) => InstanceType<TBase> & IComponentManager {
35
+ return class extends Base {
36
+ setGraphic(graphic: string | string[]): void {
37
+ if (Array.isArray(graphic)) {
38
+ this.graphics.set(graphic);
39
+ } else {
40
+ this.graphics.set([graphic]);
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Set components to display above the player graphic
46
+ *
47
+ * Components are displayed above the player's sprite and can include
48
+ * text, bars, shapes, or any combination. The components are synchronized
49
+ * to all clients on the map.
50
+ *
51
+ * @param layout - Component(s) to display, can be single, array, or 2D array
52
+ * @param options - Optional layout options for positioning and sizing
53
+ * @returns void
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * // Single text component
58
+ * player.setComponentsTop(Components.text('{name}'));
59
+ *
60
+ * // Multiple components vertically
61
+ * player.setComponentsTop([
62
+ * Components.text('HP: {hp}'),
63
+ * Components.text('{name}')
64
+ * ]);
65
+ *
66
+ * // Table layout (columns)
67
+ * player.setComponentsTop([
68
+ * [Components.text('{hp}'), Components.text('{name}')]
69
+ * ]);
70
+ *
71
+ * // With layout options
72
+ * player.setComponentsTop([
73
+ * Components.text('HP: {hp}'),
74
+ * Components.text('{name}')
75
+ * ], {
76
+ * width: 100,
77
+ * height: 30,
78
+ * marginBottom: -10
79
+ * });
80
+ * ```
81
+ */
82
+ setComponentsTop(layout: ComponentInput, options?: ComponentLayout): void {
83
+ const normalized = this.normalizeComponents(layout);
84
+ const data = {
85
+ components: normalized,
86
+ layout: options || {}
87
+ };
88
+ this.componentsTop.set(JSON.stringify(data));
89
+ }
90
+
91
+ /**
92
+ * Set components to display below the player graphic
93
+ *
94
+ * Components are displayed below the player's sprite and can include
95
+ * text, bars, shapes, or any combination. The components are synchronized
96
+ * to all clients on the map.
97
+ *
98
+ * @param layout - Component(s) to display, can be single, array, or 2D array
99
+ * @param options - Optional layout options for positioning and sizing
100
+ * @returns void
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * player.setComponentsBottom(Components.shape({
105
+ * fill: '#ff0000',
106
+ * type: 'rectangle',
107
+ * width: 32,
108
+ * height: 32
109
+ * }), {
110
+ * marginBottom: 16
111
+ * });
112
+ * ```
113
+ */
114
+ setComponentsBottom(layout: ComponentInput, options?: ComponentLayout): void {
115
+ const normalized = this.normalizeComponents(layout);
116
+ const data = {
117
+ components: normalized,
118
+ layout: options || {}
119
+ };
120
+ this.componentsBottom.set(JSON.stringify(data));
121
+ }
122
+
123
+ /**
124
+ * Set components to display at the center of the player graphic
125
+ *
126
+ * Components are displayed at the center of the player's sprite.
127
+ * Be careful: if you assign, it deletes the graphics and if the lines are superimposed.
128
+ *
129
+ * @param layout - Component(s) to display, can be single, array, or 2D array
130
+ * @param options - Optional layout options for positioning and sizing
131
+ * @returns void
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * player.setComponentsCenter([
136
+ * Components.text('{name}'),
137
+ * Components.hpBar()
138
+ * ]);
139
+ * ```
140
+ */
141
+ setComponentsCenter(layout: ComponentInput, options?: ComponentLayout): void {
142
+ const normalized = this.normalizeComponents(layout);
143
+ const data = {
144
+ components: normalized,
145
+ layout: options || {}
146
+ };
147
+ this.componentsCenter.set(JSON.stringify(data));
148
+ }
149
+
150
+ /**
151
+ * Set components to display to the left of the player graphic
152
+ *
153
+ * Components are displayed to the left of the player's sprite.
154
+ *
155
+ * @param layout - Component(s) to display, can be single, array, or 2D array
156
+ * @param options - Optional layout options for positioning and sizing
157
+ * @returns void
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * player.setComponentsLeft([
162
+ * Components.text('{name}'),
163
+ * Components.hpBar()
164
+ * ]);
165
+ * ```
166
+ */
167
+ setComponentsLeft(layout: ComponentInput, options?: ComponentLayout): void {
168
+ const normalized = this.normalizeComponents(layout);
169
+ const data = {
170
+ components: normalized,
171
+ layout: options || {}
172
+ };
173
+ this.componentsLeft.set(JSON.stringify(data));
174
+ }
175
+
176
+ /**
177
+ * Set components to display to the right of the player graphic
178
+ *
179
+ * Components are displayed to the right of the player's sprite.
180
+ *
181
+ * @param layout - Component(s) to display, can be single, array, or 2D array
182
+ * @param options - Optional layout options for positioning and sizing
183
+ * @returns void
184
+ *
185
+ * @example
186
+ * ```ts
187
+ * player.setComponentsRight([
188
+ * Components.text('{name}'),
189
+ * Components.hpBar()
190
+ * ]);
191
+ * ```
192
+ */
193
+ setComponentsRight(layout: ComponentInput, options?: ComponentLayout): void {
194
+ const normalized = this.normalizeComponents(layout);
195
+ const data = {
196
+ components: normalized,
197
+ layout: options || {}
198
+ };
199
+ this.componentsRight.set(JSON.stringify(data));
200
+ }
201
+
202
+ /**
203
+ * Remove components from a specific position
204
+ *
205
+ * Deletes all components at the specified position.
206
+ *
207
+ * @param position - Position of the components: 'top', 'center', 'bottom', 'left', or 'right'
208
+ * @returns void
209
+ *
210
+ * @example
211
+ * ```ts
212
+ * player.removeComponents('top');
213
+ * ```
214
+ */
215
+ removeComponents(position: ComponentPosition): void {
216
+ switch (position) {
217
+ case 'top':
218
+ this.componentsTop.set(null);
219
+ break;
220
+ case 'center':
221
+ this.componentsCenter.set(null);
222
+ break;
223
+ case 'bottom':
224
+ this.componentsBottom.set(null);
225
+ break;
226
+ case 'left':
227
+ this.componentsLeft.set(null);
228
+ break;
229
+ case 'right':
230
+ this.componentsRight.set(null);
231
+ break;
232
+ }
233
+ }
234
+
235
+ /**
236
+ * Merge components with existing components at a specific position
237
+ *
238
+ * Merges new components with existing components at the specified position.
239
+ *
240
+ * @param position - Position of the components: 'top', 'center', 'bottom', 'left', or 'right'
241
+ * @param layout - Component(s) to merge, can be single, array, or 2D array
242
+ * @param options - Optional layout options for positioning and sizing
243
+ * @returns void
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * // First set some components
248
+ * player.setComponentsTop([Components.text('{name}')]);
249
+ *
250
+ * // Then merge additional components
251
+ * player.mergeComponents('top', [Components.hpBar()], {
252
+ * width: 100
253
+ * });
254
+ * ```
255
+ */
256
+ mergeComponents(position: ComponentPosition, layout: ComponentInput, options?: ComponentLayout): void {
257
+ const normalized = this.normalizeComponents(layout);
258
+
259
+ // Get existing components
260
+ let existingData: any = null;
261
+ let signal: any = null;
262
+
263
+ switch (position) {
264
+ case 'top':
265
+ signal = this.componentsTop;
266
+ break;
267
+ case 'center':
268
+ signal = this.componentsCenter;
269
+ break;
270
+ case 'bottom':
271
+ signal = this.componentsBottom;
272
+ break;
273
+ case 'left':
274
+ signal = this.componentsLeft;
275
+ break;
276
+ case 'right':
277
+ signal = this.componentsRight;
278
+ break;
26
279
  }
280
+
281
+ const existingJson = signal();
282
+ if (existingJson) {
283
+ try {
284
+ existingData = JSON.parse(existingJson);
285
+ } catch (e) {
286
+ existingData = null;
287
+ }
288
+ }
289
+
290
+ // Merge components
291
+ const existingComponents = existingData?.components || [];
292
+ const mergedComponents = [...existingComponents, ...normalized];
293
+
294
+ // Merge layout options
295
+ const mergedLayout = {
296
+ ...(existingData?.layout || {}),
297
+ ...(options || {})
298
+ };
299
+
300
+ const data = {
301
+ components: mergedComponents,
302
+ layout: mergedLayout
303
+ };
304
+
305
+ signal.set(JSON.stringify(data));
27
306
  }
28
- };
307
+
308
+ /**
309
+ * Normalize component input to a consistent structure
310
+ *
311
+ * Converts various input formats (single component, array, 2D array)
312
+ * into a normalized 2D array structure for consistent rendering.
313
+ *
314
+ * @param components - Component input in any format
315
+ * @returns Normalized 2D array of components
316
+ */
317
+ private normalizeComponents(components: ComponentInput): any[][] {
318
+ if (!components) {
319
+ return [];
320
+ }
321
+
322
+ // Single component
323
+ if (!Array.isArray(components)) {
324
+ return [[components]];
325
+ }
326
+
327
+ // Check if it's a 2D array (table layout)
328
+ if (components.length > 0 && Array.isArray(components[0])) {
329
+ return components as any[][];
330
+ }
331
+
332
+ // 1D array (vertical layout)
333
+ return components.map(comp => [comp]);
334
+ }
335
+ } as unknown as any;
336
+ }
337
+
338
+ /**
339
+ * Interface for component management capabilities
340
+ * Defines the method signatures that will be available on the player
341
+ */
342
+ export interface IComponentManager {
343
+ /**
344
+ * Set the graphic(s) for this player
345
+ *
346
+ * Allows setting either a single graphic or multiple graphics for the player.
347
+ * When multiple graphics are provided, they are used for animation sequences.
348
+ * The graphics system provides flexible visual representation that can be
349
+ * dynamically changed during gameplay for different states, equipment, or animations.
350
+ *
351
+ * @param graphic - Single graphic name or array of graphic names for animation sequences
352
+ * @returns void
353
+ *
354
+ * @example
355
+ * ```ts
356
+ * // Set a single graphic for static representation
357
+ * player.setGraphic("hero");
358
+ *
359
+ * // Set multiple graphics for animation sequences
360
+ * player.setGraphic(["hero_idle", "hero_walk", "hero_run"]);
361
+ *
362
+ * // Dynamic graphic changes based on equipment
363
+ * if (player.hasArmor('platemail')) {
364
+ * player.setGraphic("hero_armored");
365
+ * }
366
+ *
367
+ * // Animation sequences for different actions
368
+ * player.setGraphic(["mage_cast_1", "mage_cast_2", "mage_cast_3"]);
369
+ * ```
370
+ */
371
+ setGraphic(graphic: string | string[]): void;
372
+
373
+ /**
374
+ * Set components to display above the player graphic
375
+ *
376
+ * @param layout - Component(s) to display, can be single, array, or 2D array
377
+ * @param options - Optional layout options for positioning and sizing
378
+ * @returns void
379
+ */
380
+ setComponentsTop(layout: ComponentInput, options?: ComponentLayout): void;
381
+
382
+ /**
383
+ * Set components to display below the player graphic
384
+ *
385
+ * @param layout - Component(s) to display, can be single, array, or 2D array
386
+ * @param options - Optional layout options for positioning and sizing
387
+ * @returns void
388
+ */
389
+ setComponentsBottom(layout: ComponentInput, options?: ComponentLayout): void;
390
+
391
+ /**
392
+ * Set components to display at the center of the player graphic
393
+ *
394
+ * @param layout - Component(s) to display, can be single, array, or 2D array
395
+ * @param options - Optional layout options for positioning and sizing
396
+ * @returns void
397
+ */
398
+ setComponentsCenter(layout: ComponentInput, options?: ComponentLayout): void;
399
+
400
+ /**
401
+ * Set components to display to the left of the player graphic
402
+ *
403
+ * @param layout - Component(s) to display, can be single, array, or 2D array
404
+ * @param options - Optional layout options for positioning and sizing
405
+ * @returns void
406
+ */
407
+ setComponentsLeft(layout: ComponentInput, options?: ComponentLayout): void;
408
+
409
+ /**
410
+ * Set components to display to the right of the player graphic
411
+ *
412
+ * @param layout - Component(s) to display, can be single, array, or 2D array
413
+ * @param options - Optional layout options for positioning and sizing
414
+ * @returns void
415
+ */
416
+ setComponentsRight(layout: ComponentInput, options?: ComponentLayout): void;
417
+
418
+ /**
419
+ * Remove components from a specific position
420
+ *
421
+ * @param position - Position of the components: 'top', 'center', 'bottom', 'left', or 'right'
422
+ * @returns void
423
+ */
424
+ removeComponents(position: ComponentPosition): void;
425
+
426
+ /**
427
+ * Merge components with existing components at a specific position
428
+ *
429
+ * @param position - Position of the components: 'top', 'center', 'bottom', 'left', or 'right'
430
+ * @param layout - Component(s) to merge, can be single, array, or 2D array
431
+ * @param options - Optional layout options for positioning and sizing
432
+ * @returns void
433
+ */
434
+ mergeComponents(position: ComponentPosition, layout: ComponentInput, options?: ComponentLayout): void;
29
435
  }