@rpgjs/server 5.0.0-alpha.10 → 5.0.0-alpha.2
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.
- package/dist/Player/BattleManager.d.ts +22 -32
- package/dist/Player/ClassManager.d.ts +18 -31
- package/dist/Player/Event.d.ts +0 -0
- package/dist/Player/ItemManager.d.ts +13 -27
- package/dist/Player/MoveManager.d.ts +43 -31
- package/dist/Player/ParameterManager.d.ts +19 -27
- package/dist/Player/Player.d.ts +8 -123
- package/dist/Player/SkillManager.d.ts +19 -27
- package/dist/Player/StateManager.d.ts +35 -28
- package/dist/RpgServer.d.ts +1 -224
- package/dist/index.js +647 -1108
- package/dist/index.js.map +1 -1
- package/dist/rooms/map.d.ts +1 -70
- package/package.json +8 -8
- package/src/Player/BattleManager.ts +38 -97
- package/src/Player/ClassManager.ts +35 -95
- package/src/Player/ComponentManager.ts +20 -64
- package/src/Player/EffectManager.ts +27 -110
- package/src/Player/ElementManager.ts +25 -126
- package/src/Player/Event.ts +0 -0
- package/src/Player/GoldManager.ts +35 -32
- package/src/Player/GuiManager.ts +140 -187
- package/src/Player/ItemFixture.ts +5 -4
- package/src/Player/ItemManager.ts +26 -39
- package/src/Player/MoveManager.ts +31 -40
- package/src/Player/ParameterManager.ts +25 -35
- package/src/Player/Player.ts +39 -184
- package/src/Player/SkillManager.ts +23 -44
- package/src/Player/StateManager.ts +95 -210
- package/src/Player/VariableManager.ts +48 -180
- package/src/RpgServer.ts +1 -232
- package/src/core/context.ts +0 -1
- package/src/rooms/map.ts +8 -76
- package/dist/Player/ComponentManager.d.ts +0 -60
- package/dist/Player/EffectManager.d.ts +0 -40
- package/dist/Player/ElementManager.d.ts +0 -31
- package/dist/Player/GoldManager.d.ts +0 -22
- package/dist/Player/GuiManager.d.ts +0 -176
- package/dist/Player/ItemFixture.d.ts +0 -6
- package/dist/Player/VariableManager.d.ts +0 -30
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isInstanceOf, isString,
|
|
1
|
+
import { isInstanceOf, isString, type Constructor } from "@rpgjs/common";
|
|
2
2
|
import { RpgCommonPlayer, Matter, SeekAvoid } from "@rpgjs/common";
|
|
3
3
|
import { signal, type WritableArraySignal } from "@signe/reactive";
|
|
4
4
|
import { ATK, PDEF, SDEF } from "../presets";
|
|
@@ -13,51 +13,41 @@ interface StateManagerDependencies {
|
|
|
13
13
|
removeState(stateClass: StateClass | string, chance?: number): void;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Interface defining what MoveManager adds to a class
|
|
18
|
+
*/
|
|
19
|
+
export interface IStateManager {
|
|
20
|
+
statesDefense: { rate: number; state: any }[];
|
|
21
|
+
statesEfficiency: WritableArraySignal<any[]>;
|
|
22
|
+
applyStates(
|
|
23
|
+
player: RpgPlayer,
|
|
24
|
+
states: { addStates?: any[]; removeStates?: any[] }
|
|
25
|
+
): void;
|
|
26
|
+
getState(stateClass: StateClass | string): any;
|
|
27
|
+
addState(stateClass: StateClass | string, chance?: number): object | null;
|
|
28
|
+
removeState(stateClass: StateClass | string, chance?: number): void;
|
|
29
|
+
}
|
|
17
30
|
|
|
18
31
|
type StateClass = { new (...args: any[]) };
|
|
19
32
|
|
|
20
33
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* @param Base - The base class to extend with state management
|
|
29
|
-
* @returns Extended class with state management methods
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```ts
|
|
33
|
-
* class MyPlayer extends WithStateManager(BasePlayer) {
|
|
34
|
-
* constructor() {
|
|
35
|
-
* super();
|
|
36
|
-
* // State system is automatically initialized
|
|
37
|
-
* }
|
|
38
|
-
* }
|
|
39
|
-
*
|
|
40
|
-
* const player = new MyPlayer();
|
|
41
|
-
* player.addState(Paralyze);
|
|
42
|
-
* console.log(player.getState(Paralyze));
|
|
43
|
-
* ```
|
|
34
|
+
* Move Manager mixin
|
|
35
|
+
*
|
|
36
|
+
* Adds methods to manage player movement
|
|
37
|
+
*
|
|
38
|
+
* @param Base - The base class to extend
|
|
39
|
+
* @returns A new class with move management capabilities
|
|
44
40
|
*/
|
|
45
|
-
export function WithStateManager<
|
|
46
|
-
|
|
41
|
+
export function WithStateManager<
|
|
42
|
+
TBase extends Constructor<RpgCommonPlayer & StateManagerDependencies>
|
|
43
|
+
>(Base: TBase): Constructor<IStateManager> & TBase {
|
|
44
|
+
return class extends Base implements IStateManager {
|
|
47
45
|
_statesEfficiency = signal<any[]>([]);
|
|
48
46
|
|
|
49
47
|
/**
|
|
50
48
|
* Recovers the player's states defense on inventory. This list is generated from the `statesDefense` property defined on the weapons or armors equipped.
|
|
51
49
|
* If several items have the same element, only the highest rate will be taken into account.
|
|
52
50
|
*
|
|
53
|
-
* Gets the defensive capabilities against various states from equipped items.
|
|
54
|
-
* The system automatically consolidates multiple defensive items, keeping only
|
|
55
|
-
* the highest protection rate for each state type. This provides comprehensive
|
|
56
|
-
* protection against debuffs and negative status effects.
|
|
57
|
-
*
|
|
58
|
-
* @returns Array of state defense objects with rate and state properties
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
51
|
* ```ts
|
|
62
52
|
* import { Armor, State } from '@rpgjs/server'
|
|
63
53
|
*
|
|
@@ -84,29 +74,19 @@ export function WithStateManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
|
84
74
|
* player.equip(FireShield)
|
|
85
75
|
*
|
|
86
76
|
* console.log(player.statesDefense) // [{ rate: 1, state: instance of Paralyze }]
|
|
87
|
-
*
|
|
88
|
-
* // Check specific state defense
|
|
89
|
-
* const paralyzeDefense = player.statesDefense.find(def => def.state instanceof Paralyze);
|
|
90
|
-
* if (paralyzeDefense) {
|
|
91
|
-
* console.log(`Paralyze defense rate: ${paralyzeDefense.rate}`);
|
|
92
|
-
* }
|
|
93
77
|
* ```
|
|
94
|
-
|
|
78
|
+
* @title Get States Defense
|
|
79
|
+
* @prop {Array<{ rate: number, state: StateClass}>} player.statesDefense
|
|
80
|
+
* @readonly
|
|
81
|
+
* @memberof StateManager
|
|
82
|
+
* */
|
|
95
83
|
get statesDefense(): { rate: number; state: any }[] {
|
|
96
|
-
return
|
|
84
|
+
return this.getFeature("statesDefense", "state");
|
|
97
85
|
}
|
|
98
86
|
|
|
99
87
|
/**
|
|
100
88
|
* Set or retrieves all the states where the player is vulnerable or not.
|
|
101
89
|
*
|
|
102
|
-
* Manages the player's state efficiency modifiers, which determine how
|
|
103
|
-
* effective different states are against this player. Values greater than 1
|
|
104
|
-
* indicate vulnerability, while values less than 1 indicate resistance.
|
|
105
|
-
* This combines both class-based efficiency and player-specific modifiers.
|
|
106
|
-
*
|
|
107
|
-
* @returns Array of state efficiency objects with rate and state properties
|
|
108
|
-
*
|
|
109
|
-
* @example
|
|
110
90
|
* ```ts
|
|
111
91
|
* import { Class, State } from '@rpgjs/server'
|
|
112
92
|
*
|
|
@@ -133,16 +113,11 @@ export function WithStateManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
|
133
113
|
* player.statesEfficiency = [{ rate: 2, state: Sleep }]
|
|
134
114
|
*
|
|
135
115
|
* console.log(player.statesEfficiency) // [{ rate: 1, state: instance of Paralyze }, { rate: 2, state: instance of Sleep }]
|
|
136
|
-
*
|
|
137
|
-
* // Check for vulnerabilities
|
|
138
|
-
* const vulnerabilities = player.statesEfficiency.filter(eff => eff.rate > 1);
|
|
139
|
-
* console.log('Vulnerable to states:', vulnerabilities.map(v => v.state.name));
|
|
140
|
-
*
|
|
141
|
-
* // Check for resistances
|
|
142
|
-
* const resistances = player.statesEfficiency.filter(eff => eff.rate < 1);
|
|
143
|
-
* console.log('Resistant to states:', resistances.map(r => r.state.name));
|
|
144
116
|
* ```
|
|
145
|
-
|
|
117
|
+
* @title Set/Get States Efficiency
|
|
118
|
+
* @prop {Array<{ rate: number, state: StateClass}>} player.statesEfficiency
|
|
119
|
+
* @memberof StateManager
|
|
120
|
+
* */
|
|
146
121
|
get statesEfficiency() {
|
|
147
122
|
return this._statesEfficiency;
|
|
148
123
|
}
|
|
@@ -151,87 +126,38 @@ export function WithStateManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
|
151
126
|
this._statesEfficiency = val;
|
|
152
127
|
}
|
|
153
128
|
|
|
154
|
-
/**
|
|
155
|
-
* Apply states to a player from skill or item effects
|
|
156
|
-
*
|
|
157
|
-
* Processes state application and removal based on skill or item effects.
|
|
158
|
-
* This method handles both adding beneficial states and removing negative ones,
|
|
159
|
-
* with proper chance calculation and resistance checks.
|
|
160
|
-
*
|
|
161
|
-
* @param player - The target player to apply states to
|
|
162
|
-
* @param states - Object containing arrays of states to add or remove
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* ```ts
|
|
166
|
-
* // Apply states from a healing skill
|
|
167
|
-
* const healingStates = {
|
|
168
|
-
* addStates: [{ state: Regeneration, rate: 0.8 }],
|
|
169
|
-
* removeStates: [{ state: Poison, rate: 1.0 }]
|
|
170
|
-
* };
|
|
171
|
-
* player.applyStates(targetPlayer, healingStates);
|
|
172
|
-
*
|
|
173
|
-
* // Apply debuff from an enemy attack
|
|
174
|
-
* const debuffStates = {
|
|
175
|
-
* addStates: [
|
|
176
|
-
* { state: Paralyze, rate: 0.3 },
|
|
177
|
-
* { state: Slow, rate: 0.5 }
|
|
178
|
-
* ]
|
|
179
|
-
* };
|
|
180
|
-
* player.applyStates(targetPlayer, debuffStates);
|
|
181
|
-
* ```
|
|
182
|
-
*/
|
|
183
129
|
applyStates(
|
|
184
|
-
player: RpgPlayer,
|
|
130
|
+
player: RpgPlayer & IStateManager,
|
|
185
131
|
{ addStates, removeStates }
|
|
186
132
|
) {
|
|
187
133
|
if (addStates) {
|
|
188
134
|
for (let { state, rate } of addStates) {
|
|
189
|
-
|
|
135
|
+
player.addState(state, rate);
|
|
190
136
|
}
|
|
191
137
|
}
|
|
192
138
|
if (removeStates) {
|
|
193
139
|
for (let { state, rate } of removeStates) {
|
|
194
|
-
|
|
140
|
+
player.removeState(state, rate);
|
|
195
141
|
}
|
|
196
142
|
}
|
|
197
143
|
}
|
|
198
144
|
|
|
199
145
|
/**
|
|
200
146
|
* Get a state to the player. Returns `null` if the state is not present on the player
|
|
201
|
-
*
|
|
202
|
-
* Retrieves a specific state instance from the player's active states.
|
|
203
|
-
* This is useful for checking state properties, duration, or performing
|
|
204
|
-
* state-specific operations. Returns null if the state is not currently active.
|
|
205
|
-
*
|
|
206
|
-
* @param stateClass - The state class constructor or state ID to search for
|
|
207
|
-
* @returns The state instance if found, null otherwise
|
|
208
|
-
*
|
|
209
|
-
* @example
|
|
210
147
|
* ```ts
|
|
211
148
|
* import Paralyze from 'your-database/states/paralyze'
|
|
212
149
|
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
* }
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
* const poisonState = player.getState('poison');
|
|
222
|
-
* if (poisonState) {
|
|
223
|
-
* console.log('Player is poisoned');
|
|
224
|
-
* }
|
|
225
|
-
*
|
|
226
|
-
* // Use in conditional logic
|
|
227
|
-
* if (player.getState(Sleep)) {
|
|
228
|
-
* console.log('Player cannot act while sleeping');
|
|
229
|
-
* return; // Skip player turn
|
|
230
|
-
* }
|
|
231
|
-
* ```
|
|
150
|
+
* player.getState(Paralyze)
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* @title Get State
|
|
154
|
+
* @method player.getState(stateClass)
|
|
155
|
+
* @param {StateClass | string} stateClass or state id
|
|
156
|
+
* @returns {instance of StateClass | null}
|
|
157
|
+
* @memberof StateManager
|
|
232
158
|
*/
|
|
233
159
|
getState(stateClass: StateClass | string) {
|
|
234
|
-
if (isString(stateClass)) stateClass =
|
|
160
|
+
if (isString(stateClass)) stateClass = this.databaseById(stateClass);
|
|
235
161
|
return this.states().find((state) => {
|
|
236
162
|
if (isString(stateClass)) {
|
|
237
163
|
return state.id == stateClass;
|
|
@@ -242,56 +168,37 @@ export function WithStateManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
|
242
168
|
|
|
243
169
|
/**
|
|
244
170
|
* Adds a state to the player. Set the chance between 0 and 1 that the state can apply
|
|
245
|
-
*
|
|
246
|
-
* Attempts to apply a state to the player with a specified success chance.
|
|
247
|
-
* The method considers state resistance, efficiency modifiers, and random chance
|
|
248
|
-
* to determine if the state is successfully applied. If successful, the state
|
|
249
|
-
* is added to the player's active states list.
|
|
250
|
-
*
|
|
251
|
-
* @param stateClass - The state class constructor or state ID to apply
|
|
252
|
-
* @param chance - Probability of successful application (0-1, default 1)
|
|
253
|
-
* @returns The state instance if successfully applied, null if already present
|
|
254
|
-
* @throws StateLog.addFailed if the chance roll fails
|
|
255
|
-
*
|
|
256
|
-
* @example
|
|
257
171
|
* ```ts
|
|
258
172
|
* import Paralyze from 'your-database/states/paralyze'
|
|
259
173
|
*
|
|
260
174
|
* try {
|
|
261
|
-
*
|
|
262
|
-
* const state = player.addState(Paralyze);
|
|
263
|
-
* if (state) {
|
|
264
|
-
* console.log('Paralyze applied successfully');
|
|
265
|
-
* }
|
|
266
|
-
* } catch (err) {
|
|
267
|
-
* console.log('Failed to apply paralyze:', err.msg);
|
|
175
|
+
* player.addState(Paralyze)
|
|
268
176
|
* }
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
177
|
+
* catch (err) {
|
|
178
|
+
* console.log(err)
|
|
179
|
+
* }
|
|
180
|
+
* ```
|
|
181
|
+
*
|
|
182
|
+
* @title Add State
|
|
183
|
+
* @method player.addState(stateClass,chance=1)
|
|
184
|
+
* @param {StateClass | string} stateClass state class or state id
|
|
185
|
+
* @param {number} [chance] 1 by default
|
|
186
|
+
* @throws {StateLog} addFailed
|
|
187
|
+
* If the chance to add the state has failed (defined with the `chance` param)
|
|
188
|
+
* ```
|
|
189
|
+
* {
|
|
190
|
+
* id: ADD_STATE_FAILED,
|
|
191
|
+
* msg: '...'
|
|
275
192
|
* }
|
|
276
|
-
*
|
|
277
|
-
* // Apply multiple states with different chances
|
|
278
|
-
* const debuffs = [
|
|
279
|
-
* { state: Slow, chance: 0.8 },
|
|
280
|
-
* { state: Weak, chance: 0.6 }
|
|
281
|
-
* ];
|
|
282
|
-
* debuffs.forEach(({ state, chance }) => {
|
|
283
|
-
* try {
|
|
284
|
-
* player.addState(state, chance);
|
|
285
|
-
* } catch (err) {
|
|
286
|
-
* // Handle failed applications
|
|
287
|
-
* }
|
|
288
|
-
* });
|
|
289
193
|
* ```
|
|
194
|
+
* @returns {instance of StateClass}
|
|
195
|
+
* @memberof StateManager
|
|
196
|
+
* @todo
|
|
290
197
|
*/
|
|
291
198
|
addState(stateClass: StateClass | string, chance = 1): object | null {
|
|
292
199
|
const state = this.getState(stateClass);
|
|
293
200
|
if (isString(stateClass)) {
|
|
294
|
-
stateClass =
|
|
201
|
+
stateClass = this.databaseById(stateClass);
|
|
295
202
|
}
|
|
296
203
|
if (!state) {
|
|
297
204
|
if (Math.random() > chance) {
|
|
@@ -308,49 +215,39 @@ export function WithStateManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
|
308
215
|
|
|
309
216
|
/**
|
|
310
217
|
* Remove a state to the player. Set the chance between 0 and 1 that the state can be removed
|
|
311
|
-
*
|
|
312
|
-
* Attempts to remove a state from the player with a specified success chance.
|
|
313
|
-
* This is useful for cure spells, items, or time-based state removal.
|
|
314
|
-
* The method considers removal resistance and random chance.
|
|
315
|
-
*
|
|
316
|
-
* @param stateClass - The state class constructor or state ID to remove
|
|
317
|
-
* @param chance - Probability of successful removal (0-1, default 1)
|
|
318
|
-
* @throws StateLog.removeFailed if the chance roll fails
|
|
319
|
-
* @throws StateLog.notApplied if the state is not currently active
|
|
320
|
-
*
|
|
321
|
-
* @example
|
|
322
218
|
* ```ts
|
|
323
219
|
* import Paralyze from 'your-database/states/paralyze'
|
|
324
220
|
*
|
|
325
221
|
* try {
|
|
326
|
-
*
|
|
327
|
-
* player.removeState(Paralyze);
|
|
328
|
-
* console.log('Paralyze removed successfully');
|
|
329
|
-
* } catch (err) {
|
|
330
|
-
* if (err.id === 'STATE_NOT_APPLIED') {
|
|
331
|
-
* console.log('Player was not paralyzed');
|
|
332
|
-
* } else {
|
|
333
|
-
* console.log('Failed to remove paralyze:', err.msg);
|
|
334
|
-
* }
|
|
222
|
+
* player.removeState(Paralyze)
|
|
335
223
|
* }
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
224
|
+
* catch (err) {
|
|
225
|
+
* console.log(err)
|
|
226
|
+
* }
|
|
227
|
+
* ```
|
|
228
|
+
*
|
|
229
|
+
* @title Remove State
|
|
230
|
+
* @method player.removeState(stateClass,chance=1)
|
|
231
|
+
* @param {StateClass|string} stateClass class state or state id
|
|
232
|
+
* @param {number} [chance] 1 by default
|
|
233
|
+
* @throws {StateLog} removeFailed
|
|
234
|
+
* If the chance to remove the state has failed (defined with the `chance` param)
|
|
235
|
+
* ```
|
|
236
|
+
* {
|
|
237
|
+
* id: REMOVE_STATE_FAILED,
|
|
238
|
+
* msg: '...'
|
|
342
239
|
* }
|
|
343
|
-
*
|
|
344
|
-
* // Remove all negative states (cure-all effect)
|
|
345
|
-
* const negativeStates = [Poison, Paralyze, Sleep, Slow];
|
|
346
|
-
* negativeStates.forEach(state => {
|
|
347
|
-
* try {
|
|
348
|
-
* player.removeState(state);
|
|
349
|
-
* } catch (err) {
|
|
350
|
-
* // State wasn't active, continue
|
|
351
|
-
* }
|
|
352
|
-
* });
|
|
353
240
|
* ```
|
|
241
|
+
* @throws {StateLog} notApplied
|
|
242
|
+
* If the status does not exist
|
|
243
|
+
* ```
|
|
244
|
+
* {
|
|
245
|
+
* id: STATE_NOT_APPLIED,
|
|
246
|
+
* msg: '...'
|
|
247
|
+
* }
|
|
248
|
+
* ```
|
|
249
|
+
* @returns {instance of StateClass}
|
|
250
|
+
* @memberof StateManager
|
|
354
251
|
*/
|
|
355
252
|
removeState(stateClass: StateClass | string, chance = 1) {
|
|
356
253
|
const index = this.states().findIndex((state) => {
|
|
@@ -369,22 +266,10 @@ export function WithStateManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
|
369
266
|
}
|
|
370
267
|
}
|
|
371
268
|
|
|
372
|
-
|
|
373
|
-
* Find state efficiency modifier for a specific state class
|
|
374
|
-
*
|
|
375
|
-
* @param stateClass - The state class to find efficiency for
|
|
376
|
-
* @returns The efficiency object if found, undefined otherwise
|
|
377
|
-
*/
|
|
378
|
-
findStateEfficiency(stateClass) {
|
|
269
|
+
private findStateEfficiency(stateClass) {
|
|
379
270
|
return this.statesEfficiency().find((state) =>
|
|
380
271
|
isInstanceOf(state.state, stateClass)
|
|
381
272
|
);
|
|
382
273
|
}
|
|
383
|
-
}
|
|
274
|
+
};
|
|
384
275
|
}
|
|
385
|
-
|
|
386
|
-
/**
|
|
387
|
-
* Type helper to extract the interface from the WithStateManager mixin
|
|
388
|
-
* This provides the type without duplicating method signatures
|
|
389
|
-
*/
|
|
390
|
-
export type IStateManager = InstanceType<ReturnType<typeof WithStateManager>>;
|
|
@@ -1,207 +1,75 @@
|
|
|
1
|
-
import { Constructor
|
|
1
|
+
import { type Constructor } from "@rpgjs/common";
|
|
2
|
+
import { RpgCommonPlayer } from "@rpgjs/common";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*
|
|
5
|
+
* Interface defining what MoveManager adds to a class
|
|
6
|
+
*/
|
|
7
|
+
export interface IWithVariableManager {
|
|
8
|
+
variables: Map<string, any>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Move Manager mixin
|
|
12
13
|
*
|
|
13
|
-
*
|
|
14
|
-
* ```ts
|
|
15
|
-
* class MyPlayer extends WithVariableManager(BasePlayer) {
|
|
16
|
-
* constructor() {
|
|
17
|
-
* super();
|
|
18
|
-
* // Variables are automatically initialized
|
|
19
|
-
* }
|
|
20
|
-
* }
|
|
14
|
+
* Adds methods to manage player movement
|
|
21
15
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* ```
|
|
16
|
+
* @param Base - The base class to extend
|
|
17
|
+
* @returns A new class with move management capabilities
|
|
25
18
|
*/
|
|
26
|
-
export function WithVariableManager<TBase extends
|
|
27
|
-
return class extends Base {
|
|
28
|
-
variables: Map<string, any> = new Map()
|
|
19
|
+
export function WithVariableManager<TBase extends Constructor<RpgCommonPlayer>>(Base: TBase) {
|
|
20
|
+
return class extends Base implements IWithVariableManager {
|
|
21
|
+
variables: Map<string, any> = new Map()
|
|
29
22
|
|
|
30
23
|
/**
|
|
31
24
|
* Assign a variable to the player
|
|
32
25
|
*
|
|
33
|
-
* Stores a key-value pair in the player's variable map. This is useful for
|
|
34
|
-
* tracking game state, quest progress, flags, and other player-specific data.
|
|
35
|
-
* The variable system provides a flexible way to store any type of data
|
|
36
|
-
* associated with the player that persists throughout the game session.
|
|
37
|
-
*
|
|
38
|
-
* @param key - The variable identifier (string key to reference the variable)
|
|
39
|
-
* @param val - The value to store (can be any type: boolean, number, string, object, array)
|
|
40
|
-
* @returns void
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
26
|
* ```ts
|
|
44
|
-
*
|
|
45
|
-
* player.setVariable('CHEST_OPENED', true);
|
|
46
|
-
* player.setVariable('playerLevel', 5);
|
|
47
|
-
* player.setVariable('questProgress', { step: 1, completed: false });
|
|
48
|
-
* player.setVariable('inventory', ['sword', 'potion', 'key']);
|
|
49
|
-
* player.setVariable('lastSaveTime', new Date().toISOString());
|
|
27
|
+
* player.setVariable('OPEN_CHEST', true)
|
|
50
28
|
* ```
|
|
51
|
-
*/
|
|
52
|
-
setVariable(key: string, val: any): void {
|
|
53
|
-
this.variables.set(key, val);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Get a variable value
|
|
58
29
|
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* @
|
|
64
|
-
* @
|
|
65
|
-
*
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
* // Get variables with type inference
|
|
69
|
-
* const hasKey = player.getVariable('CHEST_OPENED'); // boolean | undefined
|
|
70
|
-
* const level = player.getVariable('playerLevel'); // number | undefined
|
|
71
|
-
* const quest = player.getVariable('questProgress'); // object | undefined
|
|
72
|
-
* const missing = player.getVariable('nonexistent'); // undefined
|
|
73
|
-
*
|
|
74
|
-
* // Use with default values
|
|
75
|
-
* const level = player.getVariable('playerLevel') ?? 1;
|
|
76
|
-
* const isChestOpened = player.getVariable('CHEST_OPENED') ?? false;
|
|
77
|
-
* ```
|
|
78
|
-
*/
|
|
79
|
-
getVariable<U = any>(key: string): U | undefined {
|
|
80
|
-
return this.variables.get(key);
|
|
30
|
+
* @title Set variable
|
|
31
|
+
* @method player.setVariable(key,val)
|
|
32
|
+
* @param {string} key
|
|
33
|
+
* @param {any} val
|
|
34
|
+
* @returns {void}
|
|
35
|
+
* @memberof VariableManager
|
|
36
|
+
* */
|
|
37
|
+
setVariable(key: string, val) {
|
|
38
|
+
this.variables.set(key, val)
|
|
81
39
|
}
|
|
82
40
|
|
|
83
41
|
/**
|
|
84
|
-
*
|
|
42
|
+
* Get a variable
|
|
85
43
|
*
|
|
86
|
-
* Deletes a variable from the player's variable map. This is useful for
|
|
87
|
-
* cleaning up temporary flags, resetting certain game states, or managing
|
|
88
|
-
* memory by removing unused variables. The method returns a boolean indicating
|
|
89
|
-
* whether the variable existed and was successfully removed.
|
|
90
|
-
*
|
|
91
|
-
* @param key - The variable identifier to remove
|
|
92
|
-
* @returns true if a variable existed and has been removed, false if the variable does not exist
|
|
93
|
-
*
|
|
94
|
-
* @example
|
|
95
44
|
* ```ts
|
|
96
|
-
*
|
|
97
|
-
* const removed = player.removeVariable('CHEST_OPENED'); // true if existed
|
|
98
|
-
* const notFound = player.removeVariable('nonexistent'); // false
|
|
99
|
-
*
|
|
100
|
-
* // Clean up temporary variables
|
|
101
|
-
* player.removeVariable('tempQuestFlag');
|
|
102
|
-
* player.removeVariable('battleTempData');
|
|
103
|
-
*
|
|
104
|
-
* // Conditional removal
|
|
105
|
-
* if (player.getVariable('questCompleted')) {
|
|
106
|
-
* player.removeVariable('questProgress');
|
|
107
|
-
* }
|
|
45
|
+
* const val = player.getVariable('OPEN_CHEST')
|
|
108
46
|
* ```
|
|
109
|
-
*/
|
|
110
|
-
removeVariable(key: string): boolean {
|
|
111
|
-
return this.variables.delete(key);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Check if a variable exists
|
|
116
|
-
*
|
|
117
|
-
* Determines whether a variable with the given key exists in the player's
|
|
118
|
-
* variable map, regardless of its value (including falsy values like false, 0, '').
|
|
119
|
-
* This is useful when you need to distinguish between a variable that doesn't
|
|
120
|
-
* exist and one that has a falsy value.
|
|
121
|
-
*
|
|
122
|
-
* @param key - The variable identifier to check
|
|
123
|
-
* @returns true if the variable exists, false otherwise
|
|
124
47
|
*
|
|
125
|
-
* @
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
* if (player.hasVariable('questStarted')) {
|
|
134
|
-
* // Quest has been started, check progress
|
|
135
|
-
* const progress = player.getVariable('questProgress');
|
|
136
|
-
* } else {
|
|
137
|
-
* // Quest not started yet
|
|
138
|
-
* player.setVariable('questStarted', true);
|
|
139
|
-
* }
|
|
140
|
-
* ```
|
|
141
|
-
*/
|
|
142
|
-
hasVariable(key: string): boolean {
|
|
143
|
-
return this.variables.has(key);
|
|
48
|
+
* @title Get variable
|
|
49
|
+
* @method player.setVariable(key,val)
|
|
50
|
+
* @param {string} key
|
|
51
|
+
* @returns {any}
|
|
52
|
+
* @memberof VariableManager
|
|
53
|
+
* */
|
|
54
|
+
getVariable(key: string) {
|
|
55
|
+
return this.variables.get(key)
|
|
144
56
|
}
|
|
145
57
|
|
|
146
|
-
/**
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
* Returns an array of all variable keys currently stored for this player.
|
|
150
|
-
* This is useful for debugging, serialization, or iterating over all variables.
|
|
151
|
-
* The keys are returned in insertion order.
|
|
152
|
-
*
|
|
153
|
-
* @returns Array of all variable keys
|
|
58
|
+
/**
|
|
59
|
+
* Remove a variable
|
|
154
60
|
*
|
|
155
|
-
* @example
|
|
156
61
|
* ```ts
|
|
157
|
-
*
|
|
158
|
-
* const keys = player.getVariableKeys();
|
|
159
|
-
* console.log('Player has variables:', keys);
|
|
160
|
-
*
|
|
161
|
-
* // Iterate over all variables
|
|
162
|
-
* keys.forEach(key => {
|
|
163
|
-
* const value = player.getVariable(key);
|
|
164
|
-
* console.log(`${key}: ${value}`);
|
|
165
|
-
* });
|
|
166
|
-
*
|
|
167
|
-
* // Filter specific variable types
|
|
168
|
-
* const questKeys = keys.filter(key => key.startsWith('quest_'));
|
|
62
|
+
* player.removeVariable('OPEN_CHEST')
|
|
169
63
|
* ```
|
|
170
|
-
*/
|
|
171
|
-
getVariableKeys(): string[] {
|
|
172
|
-
return Array.from(this.variables.keys());
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Clear all variables
|
|
177
64
|
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
* @
|
|
183
|
-
*
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
* // Clear all variables (use with caution)
|
|
187
|
-
* player.clearVariables();
|
|
188
|
-
*
|
|
189
|
-
* // Clear variables conditionally
|
|
190
|
-
* if (gameReset) {
|
|
191
|
-
* player.clearVariables();
|
|
192
|
-
* // Re-initialize essential variables
|
|
193
|
-
* player.setVariable('gameStarted', true);
|
|
194
|
-
* }
|
|
195
|
-
* ```
|
|
196
|
-
*/
|
|
197
|
-
clearVariables(): void {
|
|
198
|
-
this.variables.clear();
|
|
65
|
+
* @title Remove variable
|
|
66
|
+
* @method player.removeVariable(key)
|
|
67
|
+
* @param {string} key
|
|
68
|
+
* @returns {boolean} true if a variable existed and has been removed, or false if the variable does not exist.
|
|
69
|
+
* @memberof VariableManager
|
|
70
|
+
* */
|
|
71
|
+
removeVariable(key: string) {
|
|
72
|
+
return this.variables.delete(key)
|
|
199
73
|
}
|
|
200
|
-
}
|
|
74
|
+
};
|
|
201
75
|
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Type helper to extract the interface from the WithVariableManager mixin
|
|
205
|
-
* This provides the type without duplicating method signatures
|
|
206
|
-
*/
|
|
207
|
-
export type IVariableManager = InstanceType<ReturnType<typeof WithVariableManager>>;
|