@rpgjs/server 5.0.0-alpha.17 → 5.0.0-alpha.19

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,10 +1,15 @@
1
- import { PlayerCtor } from '@rpgjs/common';
1
+ import { Constructor, RpgCommonPlayer } from '@rpgjs/common';
2
+ import { ComponentInput, ComponentLayout } from './Components';
3
+ type ComponentPosition = 'top' | 'center' | 'bottom' | 'left' | 'right';
2
4
  /**
3
5
  * Component Manager Mixin
4
6
  *
5
- * Provides graphic management capabilities to any class. This mixin allows
7
+ * Provides graphic and component management capabilities to any class. This mixin allows
6
8
  * setting single or multiple graphics for player representation, enabling
7
- * dynamic visual changes and animation sequences.
9
+ * dynamic visual changes and animation sequences. It also provides methods to
10
+ * display UI components around the player graphic (top, bottom, center, left, right).
11
+ *
12
+ * Components are stored as JSON strings for efficient synchronization.
8
13
  *
9
14
  * @param Base - The base class to extend with component management
10
15
  * @returns Extended class with component management methods
@@ -20,41 +25,99 @@ import { PlayerCtor } from '@rpgjs/common';
20
25
  *
21
26
  * const player = new MyPlayer();
22
27
  * player.setGraphic(["hero_idle", "hero_walk"]);
28
+ * player.setComponentsTop(Components.text('{name}'));
23
29
  * ```
24
30
  */
25
- export declare function WithComponentManager<TBase extends PlayerCtor>(Base: TBase): new (...args: ConstructorParameters<TBase>) => InstanceType<TBase> & IComponentManager;
31
+ export declare function WithComponentManager<TBase extends Constructor<RpgCommonPlayer>>(Base: TBase): new (...args: ConstructorParameters<TBase>) => InstanceType<TBase> & IComponentManager;
26
32
  /**
27
33
  * Interface for component management capabilities
28
- * Defines the method signature that will be available on the player
34
+ * Defines the method signatures that will be available on the player
29
35
  */
30
36
  export interface IComponentManager {
31
37
  /**
32
- * Set the graphic(s) for this player
33
- *
34
- * Allows setting either a single graphic or multiple graphics for the player.
35
- * When multiple graphics are provided, they are used for animation sequences.
36
- * The graphics system provides flexible visual representation that can be
37
- * dynamically changed during gameplay for different states, equipment, or animations.
38
- *
39
- * @param graphic - Single graphic name or array of graphic names for animation sequences
40
- * @returns void
41
- *
42
- * @example
43
- * ```ts
44
- * // Set a single graphic for static representation
45
- * player.setGraphic("hero");
46
- *
47
- * // Set multiple graphics for animation sequences
48
- * player.setGraphic(["hero_idle", "hero_walk", "hero_run"]);
49
- *
50
- * // Dynamic graphic changes based on equipment
51
- * if (player.hasArmor('platemail')) {
52
- * player.setGraphic("hero_armored");
53
- * }
54
- *
55
- * // Animation sequences for different actions
56
- * player.setGraphic(["mage_cast_1", "mage_cast_2", "mage_cast_3"]);
57
- * ```
58
- */
38
+ * Set the graphic(s) for this player
39
+ *
40
+ * Allows setting either a single graphic or multiple graphics for the player.
41
+ * When multiple graphics are provided, they are used for animation sequences.
42
+ * The graphics system provides flexible visual representation that can be
43
+ * dynamically changed during gameplay for different states, equipment, or animations.
44
+ *
45
+ * @param graphic - Single graphic name or array of graphic names for animation sequences
46
+ * @returns void
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * // Set a single graphic for static representation
51
+ * player.setGraphic("hero");
52
+ *
53
+ * // Set multiple graphics for animation sequences
54
+ * player.setGraphic(["hero_idle", "hero_walk", "hero_run"]);
55
+ *
56
+ * // Dynamic graphic changes based on equipment
57
+ * if (player.hasArmor('platemail')) {
58
+ * player.setGraphic("hero_armored");
59
+ * }
60
+ *
61
+ * // Animation sequences for different actions
62
+ * player.setGraphic(["mage_cast_1", "mage_cast_2", "mage_cast_3"]);
63
+ * ```
64
+ */
59
65
  setGraphic(graphic: string | string[]): void;
66
+ /**
67
+ * Set components to display above the player graphic
68
+ *
69
+ * @param layout - Component(s) to display, can be single, array, or 2D array
70
+ * @param options - Optional layout options for positioning and sizing
71
+ * @returns void
72
+ */
73
+ setComponentsTop(layout: ComponentInput, options?: ComponentLayout): void;
74
+ /**
75
+ * Set components to display below the player graphic
76
+ *
77
+ * @param layout - Component(s) to display, can be single, array, or 2D array
78
+ * @param options - Optional layout options for positioning and sizing
79
+ * @returns void
80
+ */
81
+ setComponentsBottom(layout: ComponentInput, options?: ComponentLayout): void;
82
+ /**
83
+ * Set components to display at the center of the player graphic
84
+ *
85
+ * @param layout - Component(s) to display, can be single, array, or 2D array
86
+ * @param options - Optional layout options for positioning and sizing
87
+ * @returns void
88
+ */
89
+ setComponentsCenter(layout: ComponentInput, options?: ComponentLayout): void;
90
+ /**
91
+ * Set components to display to the left of the player graphic
92
+ *
93
+ * @param layout - Component(s) to display, can be single, array, or 2D array
94
+ * @param options - Optional layout options for positioning and sizing
95
+ * @returns void
96
+ */
97
+ setComponentsLeft(layout: ComponentInput, options?: ComponentLayout): void;
98
+ /**
99
+ * Set components to display to the right of the player graphic
100
+ *
101
+ * @param layout - Component(s) to display, can be single, array, or 2D array
102
+ * @param options - Optional layout options for positioning and sizing
103
+ * @returns void
104
+ */
105
+ setComponentsRight(layout: ComponentInput, options?: ComponentLayout): void;
106
+ /**
107
+ * Remove components from a specific position
108
+ *
109
+ * @param position - Position of the components: 'top', 'center', 'bottom', 'left', or 'right'
110
+ * @returns void
111
+ */
112
+ removeComponents(position: ComponentPosition): void;
113
+ /**
114
+ * Merge components with existing components at a specific position
115
+ *
116
+ * @param position - Position of the components: 'top', 'center', 'bottom', 'left', or 'right'
117
+ * @param layout - Component(s) to merge, can be single, array, or 2D array
118
+ * @param options - Optional layout options for positioning and sizing
119
+ * @returns void
120
+ */
121
+ mergeComponents(position: ComponentPosition, layout: ComponentInput, options?: ComponentLayout): void;
60
122
  }
123
+ export {};
@@ -0,0 +1,345 @@
1
+ /**
2
+ * Component definitions for player UI elements
3
+ *
4
+ * This module provides factory functions to create component definitions
5
+ * that can be displayed above or below player graphics. Components are
6
+ * synchronized from server to client and rendered using CanvasEngine.
7
+ *
8
+ * ## Design
9
+ *
10
+ * Components are defined as data structures that describe UI elements
11
+ * (text, bars, shapes) with their properties and layout options. The
12
+ * server creates these definitions and they are automatically synchronized
13
+ * to all clients on the map.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { Components } from '@rpgjs/server';
18
+ *
19
+ * // Create a text component
20
+ * const nameComponent = Components.text('{name}');
21
+ *
22
+ * // Create an HP bar
23
+ * const hpBar = Components.hpBar();
24
+ *
25
+ * // Set components on player
26
+ * player.setComponentsTop([nameComponent, hpBar]);
27
+ * ```
28
+ */
29
+ export interface ComponentLayout {
30
+ /** Width of the component block in pixels */
31
+ width?: number;
32
+ /** Height of the component block in pixels */
33
+ height?: number;
34
+ /** Margin from the top of the player in pixels */
35
+ marginTop?: number;
36
+ /** Margin from the bottom of the player in pixels */
37
+ marginBottom?: number;
38
+ /** Margin from the left of the player in pixels */
39
+ marginLeft?: number;
40
+ /** Margin from the right of the player in pixels */
41
+ marginRight?: number;
42
+ }
43
+ export interface TextComponentOptions {
44
+ /** Text color in hexadecimal format (e.g., '#000000') */
45
+ fill?: string;
46
+ /** Font size in pixels */
47
+ fontSize?: number;
48
+ /** Font family */
49
+ fontFamily?: string;
50
+ /** Font style: 'normal', 'italic', 'oblique' */
51
+ fontStyle?: 'normal' | 'italic' | 'oblique';
52
+ /** Font weight: 'normal', 'bold', 'bolder', 'lighter', or numeric values */
53
+ fontWeight?: 'normal' | 'bold' | 'bolder' | 'lighter' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900' | number;
54
+ /** Stroke color in hexadecimal format */
55
+ stroke?: string;
56
+ /** Opacity between 0 and 1 */
57
+ opacity?: number;
58
+ /** Word wrap */
59
+ wordWrap?: boolean;
60
+ /** Text alignment */
61
+ align?: 'left' | 'center' | 'right' | 'justify';
62
+ }
63
+ export interface BarComponentOptions {
64
+ /** Background color in hexadecimal format */
65
+ bgColor?: string;
66
+ /** Fill color in hexadecimal format */
67
+ fillColor?: string;
68
+ /** Border color in hexadecimal format */
69
+ borderColor?: string;
70
+ /** Border width */
71
+ borderWidth?: number;
72
+ /** Height of the bar in pixels */
73
+ height?: number;
74
+ /** Width of the bar in pixels */
75
+ width?: number;
76
+ /** Border radius */
77
+ borderRadius?: number;
78
+ /** Opacity between 0 and 1 */
79
+ opacity?: number;
80
+ }
81
+ export interface ShapeComponentOptions {
82
+ /** Fill color in hexadecimal format */
83
+ fill: string;
84
+ /** Type of shape */
85
+ type: 'circle' | 'rectangle' | 'ellipse' | 'polygon' | 'line' | 'rounded-rectangle';
86
+ /** Radius (for circle) */
87
+ radius?: number | string;
88
+ /** Width (for rectangle, ellipse) */
89
+ width?: number | string;
90
+ /** Height (for rectangle, ellipse) */
91
+ height?: number | string;
92
+ /** X1 position (for line) */
93
+ x1?: number | string;
94
+ /** Y1 position (for line) */
95
+ y1?: number | string;
96
+ /** X2 position (for line) */
97
+ x2?: number | string;
98
+ /** Y2 position (for line) */
99
+ y2?: number | string;
100
+ /** Points array (for polygon) */
101
+ points?: number[];
102
+ /** Opacity between 0 and 1 */
103
+ opacity?: number | string;
104
+ /** Line/border style */
105
+ line?: {
106
+ color?: string;
107
+ width?: number;
108
+ alpha?: number;
109
+ };
110
+ }
111
+ export type ComponentDefinition = {
112
+ type: 'text';
113
+ value: string;
114
+ style?: TextComponentOptions;
115
+ } | {
116
+ type: 'hpBar';
117
+ style?: BarComponentOptions;
118
+ text?: string | null;
119
+ } | {
120
+ type: 'spBar';
121
+ style?: BarComponentOptions;
122
+ text?: string | null;
123
+ } | {
124
+ type: 'bar';
125
+ current: string;
126
+ max: string;
127
+ style?: BarComponentOptions;
128
+ text?: string | null;
129
+ } | {
130
+ type: 'shape';
131
+ value: ShapeComponentOptions;
132
+ } | {
133
+ type: 'image';
134
+ value: string;
135
+ } | {
136
+ type: 'tile';
137
+ value: number | string;
138
+ };
139
+ export type ComponentInput = ComponentDefinition | ComponentDefinition[] | ComponentDefinition[][];
140
+ /**
141
+ * Components factory for creating component definitions
142
+ *
143
+ * Provides factory methods to create various UI components that can be
144
+ * displayed above or below player graphics. Components support template
145
+ * strings with placeholders like {name}, {hp}, etc. that are replaced
146
+ * with actual player property values on the client.
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * // Create a text component
151
+ * Components.text('Player: {name}');
152
+ *
153
+ * // Create an HP bar with custom text
154
+ * Components.hpBar({}, '{$percent}%');
155
+ *
156
+ * // Create a custom bar
157
+ * Components.bar('wood', 'param.maxWood');
158
+ * ```
159
+ */
160
+ export declare const Components: {
161
+ /**
162
+ * Create a text component
163
+ *
164
+ * Creates a text component that displays text with optional styling.
165
+ * Supports template strings with placeholders like {name}, {hp}, etc.
166
+ * that are replaced with actual player property values.
167
+ *
168
+ * ## Design
169
+ *
170
+ * Text components use template strings to allow dynamic content without
171
+ * resending the entire component structure when values change. Only the
172
+ * property values are synchronized, reducing bandwidth usage.
173
+ *
174
+ * @param text - Text to display, can include placeholders like {name}, {hp}
175
+ * @param options - Text styling options
176
+ * @returns Component definition for text
177
+ *
178
+ * @example
179
+ * ```ts
180
+ * // Simple text
181
+ * Components.text('Player Name');
182
+ *
183
+ * // Text with placeholder
184
+ * Components.text('{name}');
185
+ *
186
+ * // Text with styling
187
+ * Components.text('{name}', {
188
+ * fill: '#000000',
189
+ * fontSize: 20
190
+ * });
191
+ * ```
192
+ */
193
+ text(value: string, style?: TextComponentOptions): ComponentDefinition;
194
+ /**
195
+ * Create an HP bar component
196
+ *
197
+ * Creates a health point bar that automatically displays the player's
198
+ * current HP relative to their maximum HP. The bar updates automatically
199
+ * as HP changes.
200
+ *
201
+ * ## Design
202
+ *
203
+ * HP bars read from the player's hp and param.maxHp properties. The
204
+ * bar can optionally display text above it showing current, max, or
205
+ * percentage values.
206
+ *
207
+ * @param options - Bar styling options
208
+ * @param text - Optional text to display above the bar. Can use placeholders:
209
+ * - {$current} - Current HP value
210
+ * - {$max} - Maximum HP value
211
+ * - {$percent} - Percentage value
212
+ * Set to null to hide text
213
+ * @returns Component definition for HP bar
214
+ *
215
+ * @example
216
+ * ```ts
217
+ * // Simple HP bar
218
+ * Components.hpBar();
219
+ *
220
+ * // HP bar with percentage text
221
+ * Components.hpBar({}, '{$percent}%');
222
+ *
223
+ * // HP bar with custom styling
224
+ * Components.hpBar({
225
+ * fillColor: '#ff0000',
226
+ * height: 8
227
+ * });
228
+ * ```
229
+ */
230
+ hpBar(style?: BarComponentOptions, text?: string | null): ComponentDefinition;
231
+ /**
232
+ * Create an SP bar component
233
+ *
234
+ * Creates a skill point bar that automatically displays the player's
235
+ * current SP relative to their maximum SP. The bar updates automatically
236
+ * as SP changes.
237
+ *
238
+ * @param style - Bar styling options
239
+ * @param text - Optional text to display above the bar. Can use placeholders:
240
+ * - {$current} - Current SP value
241
+ * - {$max} - Maximum SP value
242
+ * - {$percent} - Percentage value
243
+ * Set to null to hide text
244
+ * @returns Component definition for SP bar
245
+ *
246
+ * @example
247
+ * ```ts
248
+ * // Simple SP bar
249
+ * Components.spBar();
250
+ *
251
+ * // SP bar with text
252
+ * Components.spBar({}, 'SP: {$current}/{$max}');
253
+ * ```
254
+ */
255
+ spBar(style?: BarComponentOptions, text?: string | null): ComponentDefinition;
256
+ /**
257
+ * Create a custom bar component
258
+ *
259
+ * Creates a bar that displays a custom property value relative to a maximum.
260
+ * Useful for displaying custom resources like wood, mana, energy, etc.
261
+ *
262
+ * @param current - Property path for current value (e.g., 'wood', 'mana')
263
+ * @param max - Property path for maximum value (e.g., 'param.maxWood', 'param.maxMana')
264
+ * @param style - Bar styling options
265
+ * @param text - Optional text to display above the bar. Can use placeholders:
266
+ * - {$current} - Current value
267
+ * - {$max} - Maximum value
268
+ * - {$percent} - Percentage value
269
+ * Set to null to hide text
270
+ * @returns Component definition for custom bar
271
+ *
272
+ * @example
273
+ * ```ts
274
+ * // Bar for custom property
275
+ * Components.bar('wood', 'param.maxWood');
276
+ *
277
+ * // Bar with text
278
+ * Components.bar('mana', 'param.maxMana', {}, 'Mana: {$current}/{$max}');
279
+ * ```
280
+ */
281
+ bar(current: string, max: string, style?: BarComponentOptions, text?: string | null): ComponentDefinition;
282
+ /**
283
+ * Create a shape component
284
+ *
285
+ * Creates a geometric shape that can be displayed above or below the player.
286
+ * Useful for visual indicators, backgrounds, or decorative elements.
287
+ *
288
+ * @param value - Shape configuration options
289
+ * @returns Component definition for shape
290
+ *
291
+ * @example
292
+ * ```ts
293
+ * // Circle shape
294
+ * Components.shape({
295
+ * fill: '#ffffff',
296
+ * type: 'circle',
297
+ * radius: 10
298
+ * });
299
+ *
300
+ * // Rectangle shape
301
+ * Components.shape({
302
+ * fill: '#ff0000',
303
+ * type: 'rectangle',
304
+ * width: 32,
305
+ * height: 32
306
+ * });
307
+ *
308
+ * // Using parameters
309
+ * Components.shape({
310
+ * fill: '#ffffff',
311
+ * type: 'circle',
312
+ * radius: 'hp' // radius will be the same as hp value
313
+ * });
314
+ * ```
315
+ */
316
+ shape(value: ShapeComponentOptions): ComponentDefinition;
317
+ /**
318
+ * Create an image component
319
+ *
320
+ * Displays an image from a URL or spritesheet identifier.
321
+ *
322
+ * @param value - Image source URL or spritesheet identifier
323
+ * @returns Component definition for image
324
+ *
325
+ * @example
326
+ * ```ts
327
+ * Components.image('mygraphic.png');
328
+ * ```
329
+ */
330
+ image(value: string): ComponentDefinition;
331
+ /**
332
+ * Create a tile component
333
+ *
334
+ * Displays a tile from a tileset by ID.
335
+ *
336
+ * @param value - Tile ID in the tileset
337
+ * @returns Component definition for tile
338
+ *
339
+ * @example
340
+ * ```ts
341
+ * Components.tile(3); // Use tile #3
342
+ * ```
343
+ */
344
+ tile(value: number | string): ComponentDefinition;
345
+ };
@@ -1,4 +1,144 @@
1
1
  import { PlayerCtor } from '@rpgjs/common';
2
+ import { RpgPlayer } from './Player';
3
+ /**
4
+ * Interface defining the hooks that can be implemented on item classes or objects
5
+ *
6
+ * These hooks are called at specific moments during the item lifecycle:
7
+ * - `onAdd`: When the item is added to the player's inventory
8
+ * - `onUse`: When the item is successfully used
9
+ * - `onUseFailed`: When the item usage fails (e.g., chance roll failed)
10
+ * - `onRemove`: When the item is removed from the inventory
11
+ * - `onEquip`: When the item is equipped or unequipped
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const itemHooks: ItemHooks = {
16
+ * onAdd(player) {
17
+ * console.log('Item added to inventory');
18
+ * },
19
+ * onUse(player) {
20
+ * player.hp += 100;
21
+ * }
22
+ * };
23
+ * ```
24
+ */
25
+ export interface ItemHooks {
26
+ /**
27
+ * Called when the item is added to the player's inventory
28
+ *
29
+ * @param player - The player receiving the item
30
+ */
31
+ onAdd?: (player: RpgPlayer) => void | Promise<void>;
32
+ /**
33
+ * Called when the item is successfully used
34
+ *
35
+ * @param player - The player using the item
36
+ */
37
+ onUse?: (player: RpgPlayer) => void | Promise<void>;
38
+ /**
39
+ * Called when the item usage fails (e.g., chance roll failed)
40
+ *
41
+ * @param player - The player attempting to use the item
42
+ */
43
+ onUseFailed?: (player: RpgPlayer) => void | Promise<void>;
44
+ /**
45
+ * Called when the item is removed from the inventory
46
+ *
47
+ * @param player - The player losing the item
48
+ */
49
+ onRemove?: (player: RpgPlayer) => void | Promise<void>;
50
+ /**
51
+ * Called when the item is equipped or unequipped
52
+ *
53
+ * @param player - The player equipping/unequipping the item
54
+ * @param equip - true if equipping, false if unequipping
55
+ */
56
+ onEquip?: (player: RpgPlayer, equip: boolean) => void | Promise<void>;
57
+ }
58
+ /**
59
+ * Base properties that can be included in an item object
60
+ *
61
+ * This interface defines the common properties that items can have.
62
+ * Use this as a base and extend it with specific item types.
63
+ *
64
+ * @template T - Additional properties specific to the item type
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * interface PotionData extends ItemData {
69
+ * hpValue: number;
70
+ * mpValue: number;
71
+ * }
72
+ *
73
+ * const potion: ItemObject<PotionData> = {
74
+ * name: 'Health Potion',
75
+ * description: 'Restores 100 HP',
76
+ * price: 200,
77
+ * hpValue: 100,
78
+ * mpValue: 0,
79
+ * onUse(player) {
80
+ * player.hp += this.hpValue;
81
+ * }
82
+ * };
83
+ * ```
84
+ */
85
+ export interface ItemData {
86
+ /** Item name */
87
+ name?: string;
88
+ /** Item description */
89
+ description?: string;
90
+ /** Item price */
91
+ price?: number;
92
+ /** HP value restored when used */
93
+ hpValue?: number;
94
+ /** MP/SP value restored when used */
95
+ mpValue?: number;
96
+ /** Chance to successfully use the item (0-1) */
97
+ hitRate?: number;
98
+ /** Whether the item is consumable */
99
+ consumable?: boolean;
100
+ /** States to add when used */
101
+ addStates?: any[];
102
+ /** States to remove when used */
103
+ removeStates?: any[];
104
+ /** Elemental properties */
105
+ elements?: any[];
106
+ /** Parameter modifiers */
107
+ paramsModifier?: Record<string, any>;
108
+ /** Item type (for equipment validation) */
109
+ _type?: 'item' | 'weapon' | 'armor';
110
+ }
111
+ /**
112
+ * Item object type that combines data properties with hooks
113
+ *
114
+ * This type allows you to create item objects directly without needing a class.
115
+ * The object can contain both item data properties and lifecycle hooks.
116
+ *
117
+ * @template T - Additional properties specific to the item type (extends ItemData)
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * const potion: ItemObject = {
122
+ * name: 'Health Potion',
123
+ * description: 'Restores 100 HP',
124
+ * price: 200,
125
+ * hpValue: 100,
126
+ * consumable: true,
127
+ * onAdd(player) {
128
+ * console.log('Potion added!');
129
+ * },
130
+ * onUse(player) {
131
+ * player.hp += 100;
132
+ * }
133
+ * };
134
+ *
135
+ * player.addItem(potion);
136
+ * ```
137
+ */
138
+ export type ItemObject<T extends ItemData = ItemData> = T & ItemHooks & {
139
+ /** Item identifier (required if not using class or string) */
140
+ id?: string;
141
+ };
2
142
  /**
3
143
  * Item Manager Mixin
4
144
  *