@reldens/skills 0.54.0 → 0.58.0
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/.github/workflows/npm-publish.yml +135 -0
- package/CLAUDE.md +619 -619
- package/README.md +353 -353
- package/index.js +19 -19
- package/lib/class-path.js +143 -143
- package/lib/client/receiver.js +86 -86
- package/lib/constants.js +67 -67
- package/lib/level.js +31 -31
- package/lib/levels-set.js +276 -276
- package/lib/server/sender.js +180 -180
- package/lib/server.js +53 -53
- package/lib/skill.js +345 -345
- package/lib/skills-events.js +45 -45
- package/lib/types/attack.js +202 -202
- package/lib/types/effect.js +52 -52
- package/lib/types/physical-attack.js +46 -46
- package/lib/types/physical-effect.js +46 -46
- package/lib/types/physical-properties-validator.js +35 -35
- package/lib/types/physical-skill-runner.js +41 -41
- package/package.json +52 -52
- package/tests/unit/test-class-path.js +4 -3
- package/tests/unit/test-levels-set.js +4 -3
- package/tests/unit/test-skill.js +4 -3
- package/tests/unit/types/test-attack.js +4 -3
- package/tests/unit/types/test-effect.js +4 -3
package/lib/skill.js
CHANGED
|
@@ -1,345 +1,345 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
* Reldens - Skills - Skill
|
|
4
|
-
*
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const SkillsConst = require('./constants');
|
|
8
|
-
const SkillsEvents = require('./skills-events');
|
|
9
|
-
const { PropertyManager, Condition, Calculator } = require('@reldens/modifiers');
|
|
10
|
-
const { InteractionArea, EventsManagerSingleton, Logger, sc } = require('@reldens/utils');
|
|
11
|
-
|
|
12
|
-
class Skill
|
|
13
|
-
{
|
|
14
|
-
|
|
15
|
-
constructor(props)
|
|
16
|
-
{
|
|
17
|
-
this.isReady = true;
|
|
18
|
-
if(!sc.hasOwn(props, 'key')){
|
|
19
|
-
Logger.error('Missing skill key.');
|
|
20
|
-
this.isReady = false;
|
|
21
|
-
}
|
|
22
|
-
if(!sc.hasOwn(props, 'owner')){
|
|
23
|
-
Logger.error('Missing skill owner.');
|
|
24
|
-
this.isReady = false;
|
|
25
|
-
}
|
|
26
|
-
if(typeof props.owner.getPosition !== 'function'){
|
|
27
|
-
Logger.error('Undefined owner position method.');
|
|
28
|
-
this.isReady = false;
|
|
29
|
-
}
|
|
30
|
-
this.key = props.key;
|
|
31
|
-
this.owner = props.owner;
|
|
32
|
-
this.ownerIdProperty = sc.get(props, 'ownerIdProperty', 'id');
|
|
33
|
-
this.type = SkillsConst.SKILL.TYPE.BASE;
|
|
34
|
-
this.customData = sc.get(props, 'customData', false);
|
|
35
|
-
this.autoValidation = sc.get(props, 'autoValidation', false);
|
|
36
|
-
this.skillDelay = sc.get(props, 'skillDelay', 0);
|
|
37
|
-
// @TODO - BETA - Include the following properties and behaviors for skills.
|
|
38
|
-
// this.targetConditions = sc.hasOwn(props, 'targetConditions') ? props.targetConditions : [];
|
|
39
|
-
// this.skillDelayModifiers = []; // modifiers array to reduce or increase the skillDelay time.
|
|
40
|
-
// this.repeat = 0; // the effects or the skill damage will be re-applied X times.
|
|
41
|
-
// this.repeatTime = 0; // the time between repetitions, if not specified the skill will repeat continuously.
|
|
42
|
-
// this.afterSkill = new Skill(props); // a linked skill will be executed after the current skill ends.
|
|
43
|
-
// this.afterSkillTime = 0; // time between the skill end and linkedSkill starts, if 0, will run automatically.
|
|
44
|
-
// this.failChance = 0; // the 0-100% chance of the skill execution fails.
|
|
45
|
-
// this.failChanceCallback = () => {}; // a callback to customize the fail chance calculation.
|
|
46
|
-
this.castTime = sc.get(props, 'castTime', 0);
|
|
47
|
-
this.owner.isCasting = false;
|
|
48
|
-
this.owner.castingTimer = false;
|
|
49
|
-
this.isValid = true;
|
|
50
|
-
this.usesLimit = sc.get(props, 'usesLimit', 0);
|
|
51
|
-
this.uses = 0;
|
|
52
|
-
this.canActivate = sc.get(props, 'canActivate', true);
|
|
53
|
-
this.range = sc.get(props, 'range', 0);
|
|
54
|
-
this.skillActivationTimer = false;
|
|
55
|
-
this.lastState = '';
|
|
56
|
-
this.groups = sc.get(props, 'groups', []);
|
|
57
|
-
this.lastAppliedModifiers = {};
|
|
58
|
-
// if enabled, range will be validated just before execution to get the owner and target realtime positions:
|
|
59
|
-
this.rangeAutomaticValidation = sc.get(props, 'rangeAutomaticValidation', false);
|
|
60
|
-
// @NOTE: if automatic range validation is enabled, the rangePropertyX and rangePropertyY will be required and
|
|
61
|
-
// used for both (owner and target) by default.
|
|
62
|
-
this.rangePropertyX = sc.get(props, 'rangePropertyX', false);
|
|
63
|
-
this.rangePropertyY = sc.get(props, 'rangePropertyY', false);
|
|
64
|
-
// specify if you like to use a different range property for the target:
|
|
65
|
-
this.rangeTargetPropertyX = sc.get(props, 'rangeTargetPropertyX', false);
|
|
66
|
-
this.rangeTargetPropertyY = sc.get(props, 'rangeTargetPropertyY', false);
|
|
67
|
-
// allow targeting the same owner:
|
|
68
|
-
this.allowSelfTarget = sc.get(props, 'allowSelfTarget', false);
|
|
69
|
-
// we can use a fixed target or pass the target on execution:
|
|
70
|
-
this.target = sc.get(props, 'target', false);
|
|
71
|
-
this.events = sc.get(props, 'events', EventsManagerSingleton);
|
|
72
|
-
this.ownerConditions = sc.get(props, 'ownerConditions', []);
|
|
73
|
-
// owner effects will be applied when the skill is executed:
|
|
74
|
-
this.ownerEffects = sc.get(props, 'ownerEffects', []);
|
|
75
|
-
// critical multiplier and critical chance are to specify how and if a skill hit is critical:
|
|
76
|
-
this.criticalChance = sc.get(props, 'criticalChance', 0);
|
|
77
|
-
this.criticalMultiplier = sc.get(props, 'criticalMultiplier', 1);
|
|
78
|
-
this.criticalFixedValue = sc.get(props, 'criticalFixedValue', 0);
|
|
79
|
-
// helper:
|
|
80
|
-
this.propertyManager = new PropertyManager();
|
|
81
|
-
this.calculator = new Calculator();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
validate()
|
|
85
|
-
{
|
|
86
|
-
if(!this.isReady){
|
|
87
|
-
Logger.error('Skill is not ready for validation.', this.key);
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
// @NOTE: the following is tricky, we use the isValid = true so developers can change it and return a different
|
|
91
|
-
// isValid value after the event "validateSuccess".
|
|
92
|
-
this.isValid = true;
|
|
93
|
-
// @TODO - BETA - Replace catch.
|
|
94
|
-
this.fireEvent(SkillsEvents.VALIDATE_BEFORE, this).catch((err) => {
|
|
95
|
-
Logger.error(err);
|
|
96
|
-
});
|
|
97
|
-
// the delay is the time in milliseconds until the player can use the skill again:
|
|
98
|
-
if(!this.canActivate || this.owner.isCasting){
|
|
99
|
-
this.lastState = SkillsConst.SKILL_STATES.CAN_NOT_ACTIVATE;
|
|
100
|
-
// @NOTE: player could be running an attack already.
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
// validate conditions:
|
|
104
|
-
if(!this.validateConditions()){
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
if(0 < this.usesLimit && this.uses >= this.usesLimit){
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
this.canActivate = true;
|
|
111
|
-
if(0 < this.skillDelay){
|
|
112
|
-
this.canActivate = false;
|
|
113
|
-
this.skillActivationTimer = setTimeout(()=> {
|
|
114
|
-
this.canActivate = true;
|
|
115
|
-
}, this.skillDelay);
|
|
116
|
-
}
|
|
117
|
-
// with this event we could modify the isValid property if required:
|
|
118
|
-
// @TODO - BETA - Replace catch.
|
|
119
|
-
this.fireEvent(SkillsEvents.VALIDATE_SUCCESS, this).catch((err) => {
|
|
120
|
-
Logger.error(err);
|
|
121
|
-
});
|
|
122
|
-
return this.isValid;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
validateConditions()
|
|
126
|
-
{
|
|
127
|
-
for(let condition of this.ownerConditions){
|
|
128
|
-
if(!(condition instanceof Condition)){
|
|
129
|
-
Logger.critical('Wrong Condition instance was specified.', (typeof condition));
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
if(!condition.isValidOn(this.owner)){
|
|
133
|
-
// @TODO - BETA - Replace catch.
|
|
134
|
-
this.fireEvent(SkillsEvents.VALIDATE_FAIL, this, condition).catch((err) => {
|
|
135
|
-
Logger.error(err);
|
|
136
|
-
});
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return true;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
validateRange(target)
|
|
144
|
-
{
|
|
145
|
-
if(!this.rangePropertyX || !this.rangePropertyY){
|
|
146
|
-
Logger.error('Missing range properties for validation.');
|
|
147
|
-
return false;
|
|
148
|
-
}
|
|
149
|
-
let ownerPosition = {
|
|
150
|
-
x: this.propertyManager.getPropertyValue(this.owner, this.rangePropertyX),
|
|
151
|
-
y: this.propertyManager.getPropertyValue(this.owner, this.rangePropertyY)
|
|
152
|
-
};
|
|
153
|
-
let targetPosition = {
|
|
154
|
-
x: this.propertyManager.getPropertyValue(target, (this.rangeTargetPropertyX || this.rangePropertyX)),
|
|
155
|
-
y: this.propertyManager.getPropertyValue(target, (this.rangeTargetPropertyY || this.rangePropertyY))
|
|
156
|
-
};
|
|
157
|
-
return this.isInRange(ownerPosition, targetPosition);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
isInRange(ownerPosition, targetPosition)
|
|
161
|
-
{
|
|
162
|
-
// @TODO - BETA - Replace catch.
|
|
163
|
-
this.fireEvent(SkillsEvents.SKILL_BEFORE_IN_RANGE, this).catch((err) => {
|
|
164
|
-
Logger.error(err);
|
|
165
|
-
});
|
|
166
|
-
// if range is 0 then the attack range is infinity:
|
|
167
|
-
if(this.range === 0){
|
|
168
|
-
return true;
|
|
169
|
-
}
|
|
170
|
-
// validate attack range:
|
|
171
|
-
let interactionArea = new InteractionArea();
|
|
172
|
-
interactionArea.setupInteractionArea(this.range, targetPosition.x, targetPosition.y);
|
|
173
|
-
let interactionResult = interactionArea.isValidInteraction(ownerPosition.x, ownerPosition.y);
|
|
174
|
-
// @TODO - BETA - Replace catch.
|
|
175
|
-
this.fireEvent(SkillsEvents.SKILL_AFTER_IN_RANGE, this, interactionResult).catch((err) => {
|
|
176
|
-
Logger.error(err);
|
|
177
|
-
});
|
|
178
|
-
return interactionResult;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
async execute(target)
|
|
182
|
-
{
|
|
183
|
-
if(!this.isReady){
|
|
184
|
-
Logger.error('Skill is not ready to be executed.', this.key);
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
await this.fireEvent(SkillsEvents.SKILL_BEFORE_EXECUTE, this, target);
|
|
188
|
-
if(target){
|
|
189
|
-
this.target = target;
|
|
190
|
-
}
|
|
191
|
-
if(!this.target){
|
|
192
|
-
Logger.error('Target undefined.');
|
|
193
|
-
return false;
|
|
194
|
-
}
|
|
195
|
-
if(
|
|
196
|
-
// custom conditions:
|
|
197
|
-
!this.onExecuteConditions()
|
|
198
|
-
// range:
|
|
199
|
-
|| await this.isValidRange(target)
|
|
200
|
-
// cast validation:
|
|
201
|
-
|| (this.autoValidation && !this.validate())
|
|
202
|
-
// @TODO - BETA - Auto validation doesn't work, check ActionsMessageActions.parseMessageAndRunActions.
|
|
203
|
-
){
|
|
204
|
-
return false;
|
|
205
|
-
}
|
|
206
|
-
if(this.ownerEffects){
|
|
207
|
-
this.applyModifiers(this.ownerEffects, this.owner, true);
|
|
208
|
-
await this.fireEvent(SkillsEvents.SKILL_APPLY_OWNER_EFFECTS, this, target);
|
|
209
|
-
}
|
|
210
|
-
let skillLogicResult = await this.applySkillLogicOnTarget(target);
|
|
211
|
-
this.uses++;
|
|
212
|
-
await this.onExecuteRewards();
|
|
213
|
-
await this.fireEvent(SkillsEvents.SKILL_AFTER_EXECUTE, this, target);
|
|
214
|
-
return skillLogicResult;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
async applySkillLogicOnTarget(target)
|
|
218
|
-
{
|
|
219
|
-
if(0 < this.castTime){
|
|
220
|
-
await this.fireEvent(SkillsEvents.SKILL_BEFORE_CAST, this, target);
|
|
221
|
-
this.owner.isCasting = true;
|
|
222
|
-
this.owner.castingTimer = setTimeout(async () => {
|
|
223
|
-
let skillLogicResult = await this.finishExecution(target);
|
|
224
|
-
this.owner.isCasting = false;
|
|
225
|
-
await this.fireEvent(SkillsEvents.SKILL_AFTER_CAST, this, target, skillLogicResult);
|
|
226
|
-
}, this.castTime);
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
return await this.finishExecution(target);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
async isValidRange(target)
|
|
233
|
-
{
|
|
234
|
-
return this.rangeAutomaticValidation
|
|
235
|
-
&& this.rangePropertyX
|
|
236
|
-
&& this.rangePropertyY
|
|
237
|
-
&& !(await this.validateRange(target));
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
async finishExecution(target)
|
|
241
|
-
{
|
|
242
|
-
await this.fireEvent(SkillsEvents.SKILL_BEFORE_RUN_LOGIC, this, target);
|
|
243
|
-
let skillLogicResult = await this.runSkillLogic();
|
|
244
|
-
await this.fireEvent(SkillsEvents.SKILL_AFTER_RUN_LOGIC, this, target);
|
|
245
|
-
return skillLogicResult;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
onExecuteConditions()
|
|
249
|
-
{
|
|
250
|
-
// implement any custom conditions here.
|
|
251
|
-
return true;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
async runSkillLogic()
|
|
255
|
-
{
|
|
256
|
-
// run the skill logic here.
|
|
257
|
-
return true;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
async onExecuteRewards()
|
|
261
|
-
{
|
|
262
|
-
// implement any custom rewards here.
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
applyCriticalValue(normalValue)
|
|
266
|
-
{
|
|
267
|
-
if(!this.isCritical()){
|
|
268
|
-
return normalValue;
|
|
269
|
-
}
|
|
270
|
-
if(sc.isNumber(this.criticalMultiplier)){
|
|
271
|
-
normalValue = normalValue * this.criticalMultiplier;
|
|
272
|
-
}
|
|
273
|
-
if(sc.isNumber(this.criticalFixedValue)){
|
|
274
|
-
normalValue = normalValue + this.criticalFixedValue;
|
|
275
|
-
}
|
|
276
|
-
return normalValue;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
getCriticalDiff(normalValue)
|
|
280
|
-
{
|
|
281
|
-
return this.applyCriticalValue(normalValue) - normalValue;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
isCritical()
|
|
285
|
-
{
|
|
286
|
-
if(this.criticalChance <= 0){
|
|
287
|
-
return false;
|
|
288
|
-
}
|
|
289
|
-
return sc.randomInteger(1, 100) <= this.criticalChance;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
applyModifiers(modifiersObjectList, target, avoidCritical = false)
|
|
293
|
-
{
|
|
294
|
-
this.lastAppliedModifiers = {};
|
|
295
|
-
for(let i of Object.keys(modifiersObjectList)){
|
|
296
|
-
let modifier = modifiersObjectList[i];
|
|
297
|
-
modifier.target = target;
|
|
298
|
-
let newValue = modifier.getModifiedValue();
|
|
299
|
-
if(!avoidCritical){
|
|
300
|
-
newValue = newValue + this.getCriticalDiff(modifier.value);
|
|
301
|
-
}
|
|
302
|
-
modifier.setOwnerProperty(modifier.propertyKey, newValue);
|
|
303
|
-
this.lastAppliedModifiers[modifier.propertyKey] = newValue;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
getOwnerId()
|
|
308
|
-
{
|
|
309
|
-
return this.owner[this.ownerIdProperty];
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
getOwnerEventKey()
|
|
313
|
-
{
|
|
314
|
-
return sc.get(this.owner, 'eventsPrefix', 'skill.ownerId.'+this.getOwnerId());
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
getOwnerUniqueEventKey(suffix)
|
|
318
|
-
{
|
|
319
|
-
let uniqueKey = sc.isFunction(this.owner.eventUniqueKey)
|
|
320
|
-
? this.owner.eventUniqueKey()
|
|
321
|
-
: 'skills.ownerId.' + this.getOwnerId() + '.uKey.' + sc.getTime();
|
|
322
|
-
return uniqueKey+(suffix ? '.'+suffix : '');
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
async fireEvent(eventName, ...args)
|
|
327
|
-
{
|
|
328
|
-
// @NOTE: we append the current level set owner ID to the event name, so we can pick up this event only for
|
|
329
|
-
// this owner automatically:
|
|
330
|
-
return await this.events.emit(this.eventFullName(eventName), ...args);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
listenEvent(eventName, callback, removeKey, masterKey)
|
|
334
|
-
{
|
|
335
|
-
return this.events.onWithKey(this.eventFullName(eventName), callback, removeKey, masterKey);
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
eventFullName(eventName)
|
|
339
|
-
{
|
|
340
|
-
return this.getOwnerEventKey()+'.'+eventName;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
module.exports = Skill;
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Reldens - Skills - Skill
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const SkillsConst = require('./constants');
|
|
8
|
+
const SkillsEvents = require('./skills-events');
|
|
9
|
+
const { PropertyManager, Condition, Calculator } = require('@reldens/modifiers');
|
|
10
|
+
const { InteractionArea, EventsManagerSingleton, Logger, sc } = require('@reldens/utils');
|
|
11
|
+
|
|
12
|
+
class Skill
|
|
13
|
+
{
|
|
14
|
+
|
|
15
|
+
constructor(props)
|
|
16
|
+
{
|
|
17
|
+
this.isReady = true;
|
|
18
|
+
if(!sc.hasOwn(props, 'key')){
|
|
19
|
+
Logger.error('Missing skill key.');
|
|
20
|
+
this.isReady = false;
|
|
21
|
+
}
|
|
22
|
+
if(!sc.hasOwn(props, 'owner')){
|
|
23
|
+
Logger.error('Missing skill owner.');
|
|
24
|
+
this.isReady = false;
|
|
25
|
+
}
|
|
26
|
+
if(typeof props.owner.getPosition !== 'function'){
|
|
27
|
+
Logger.error('Undefined owner position method.');
|
|
28
|
+
this.isReady = false;
|
|
29
|
+
}
|
|
30
|
+
this.key = props.key;
|
|
31
|
+
this.owner = props.owner;
|
|
32
|
+
this.ownerIdProperty = sc.get(props, 'ownerIdProperty', 'id');
|
|
33
|
+
this.type = SkillsConst.SKILL.TYPE.BASE;
|
|
34
|
+
this.customData = sc.get(props, 'customData', false);
|
|
35
|
+
this.autoValidation = sc.get(props, 'autoValidation', false);
|
|
36
|
+
this.skillDelay = sc.get(props, 'skillDelay', 0);
|
|
37
|
+
// @TODO - BETA - Include the following properties and behaviors for skills.
|
|
38
|
+
// this.targetConditions = sc.hasOwn(props, 'targetConditions') ? props.targetConditions : [];
|
|
39
|
+
// this.skillDelayModifiers = []; // modifiers array to reduce or increase the skillDelay time.
|
|
40
|
+
// this.repeat = 0; // the effects or the skill damage will be re-applied X times.
|
|
41
|
+
// this.repeatTime = 0; // the time between repetitions, if not specified the skill will repeat continuously.
|
|
42
|
+
// this.afterSkill = new Skill(props); // a linked skill will be executed after the current skill ends.
|
|
43
|
+
// this.afterSkillTime = 0; // time between the skill end and linkedSkill starts, if 0, will run automatically.
|
|
44
|
+
// this.failChance = 0; // the 0-100% chance of the skill execution fails.
|
|
45
|
+
// this.failChanceCallback = () => {}; // a callback to customize the fail chance calculation.
|
|
46
|
+
this.castTime = sc.get(props, 'castTime', 0);
|
|
47
|
+
this.owner.isCasting = false;
|
|
48
|
+
this.owner.castingTimer = false;
|
|
49
|
+
this.isValid = true;
|
|
50
|
+
this.usesLimit = sc.get(props, 'usesLimit', 0);
|
|
51
|
+
this.uses = 0;
|
|
52
|
+
this.canActivate = sc.get(props, 'canActivate', true);
|
|
53
|
+
this.range = sc.get(props, 'range', 0);
|
|
54
|
+
this.skillActivationTimer = false;
|
|
55
|
+
this.lastState = '';
|
|
56
|
+
this.groups = sc.get(props, 'groups', []);
|
|
57
|
+
this.lastAppliedModifiers = {};
|
|
58
|
+
// if enabled, range will be validated just before execution to get the owner and target realtime positions:
|
|
59
|
+
this.rangeAutomaticValidation = sc.get(props, 'rangeAutomaticValidation', false);
|
|
60
|
+
// @NOTE: if automatic range validation is enabled, the rangePropertyX and rangePropertyY will be required and
|
|
61
|
+
// used for both (owner and target) by default.
|
|
62
|
+
this.rangePropertyX = sc.get(props, 'rangePropertyX', false);
|
|
63
|
+
this.rangePropertyY = sc.get(props, 'rangePropertyY', false);
|
|
64
|
+
// specify if you like to use a different range property for the target:
|
|
65
|
+
this.rangeTargetPropertyX = sc.get(props, 'rangeTargetPropertyX', false);
|
|
66
|
+
this.rangeTargetPropertyY = sc.get(props, 'rangeTargetPropertyY', false);
|
|
67
|
+
// allow targeting the same owner:
|
|
68
|
+
this.allowSelfTarget = sc.get(props, 'allowSelfTarget', false);
|
|
69
|
+
// we can use a fixed target or pass the target on execution:
|
|
70
|
+
this.target = sc.get(props, 'target', false);
|
|
71
|
+
this.events = sc.get(props, 'events', EventsManagerSingleton);
|
|
72
|
+
this.ownerConditions = sc.get(props, 'ownerConditions', []);
|
|
73
|
+
// owner effects will be applied when the skill is executed:
|
|
74
|
+
this.ownerEffects = sc.get(props, 'ownerEffects', []);
|
|
75
|
+
// critical multiplier and critical chance are to specify how and if a skill hit is critical:
|
|
76
|
+
this.criticalChance = sc.get(props, 'criticalChance', 0);
|
|
77
|
+
this.criticalMultiplier = sc.get(props, 'criticalMultiplier', 1);
|
|
78
|
+
this.criticalFixedValue = sc.get(props, 'criticalFixedValue', 0);
|
|
79
|
+
// helper:
|
|
80
|
+
this.propertyManager = new PropertyManager();
|
|
81
|
+
this.calculator = new Calculator();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
validate()
|
|
85
|
+
{
|
|
86
|
+
if(!this.isReady){
|
|
87
|
+
Logger.error('Skill is not ready for validation.', this.key);
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
// @NOTE: the following is tricky, we use the isValid = true so developers can change it and return a different
|
|
91
|
+
// isValid value after the event "validateSuccess".
|
|
92
|
+
this.isValid = true;
|
|
93
|
+
// @TODO - BETA - Replace catch.
|
|
94
|
+
this.fireEvent(SkillsEvents.VALIDATE_BEFORE, this).catch((err) => {
|
|
95
|
+
Logger.error(err);
|
|
96
|
+
});
|
|
97
|
+
// the delay is the time in milliseconds until the player can use the skill again:
|
|
98
|
+
if(!this.canActivate || this.owner.isCasting){
|
|
99
|
+
this.lastState = SkillsConst.SKILL_STATES.CAN_NOT_ACTIVATE;
|
|
100
|
+
// @NOTE: player could be running an attack already.
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
// validate conditions:
|
|
104
|
+
if(!this.validateConditions()){
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
if(0 < this.usesLimit && this.uses >= this.usesLimit){
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
this.canActivate = true;
|
|
111
|
+
if(0 < this.skillDelay){
|
|
112
|
+
this.canActivate = false;
|
|
113
|
+
this.skillActivationTimer = setTimeout(()=> {
|
|
114
|
+
this.canActivate = true;
|
|
115
|
+
}, this.skillDelay);
|
|
116
|
+
}
|
|
117
|
+
// with this event we could modify the isValid property if required:
|
|
118
|
+
// @TODO - BETA - Replace catch.
|
|
119
|
+
this.fireEvent(SkillsEvents.VALIDATE_SUCCESS, this).catch((err) => {
|
|
120
|
+
Logger.error(err);
|
|
121
|
+
});
|
|
122
|
+
return this.isValid;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
validateConditions()
|
|
126
|
+
{
|
|
127
|
+
for(let condition of this.ownerConditions){
|
|
128
|
+
if(!(condition instanceof Condition)){
|
|
129
|
+
Logger.critical('Wrong Condition instance was specified.', (typeof condition));
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
if(!condition.isValidOn(this.owner)){
|
|
133
|
+
// @TODO - BETA - Replace catch.
|
|
134
|
+
this.fireEvent(SkillsEvents.VALIDATE_FAIL, this, condition).catch((err) => {
|
|
135
|
+
Logger.error(err);
|
|
136
|
+
});
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
validateRange(target)
|
|
144
|
+
{
|
|
145
|
+
if(!this.rangePropertyX || !this.rangePropertyY){
|
|
146
|
+
Logger.error('Missing range properties for validation.');
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
let ownerPosition = {
|
|
150
|
+
x: this.propertyManager.getPropertyValue(this.owner, this.rangePropertyX),
|
|
151
|
+
y: this.propertyManager.getPropertyValue(this.owner, this.rangePropertyY)
|
|
152
|
+
};
|
|
153
|
+
let targetPosition = {
|
|
154
|
+
x: this.propertyManager.getPropertyValue(target, (this.rangeTargetPropertyX || this.rangePropertyX)),
|
|
155
|
+
y: this.propertyManager.getPropertyValue(target, (this.rangeTargetPropertyY || this.rangePropertyY))
|
|
156
|
+
};
|
|
157
|
+
return this.isInRange(ownerPosition, targetPosition);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
isInRange(ownerPosition, targetPosition)
|
|
161
|
+
{
|
|
162
|
+
// @TODO - BETA - Replace catch.
|
|
163
|
+
this.fireEvent(SkillsEvents.SKILL_BEFORE_IN_RANGE, this).catch((err) => {
|
|
164
|
+
Logger.error(err);
|
|
165
|
+
});
|
|
166
|
+
// if range is 0 then the attack range is infinity:
|
|
167
|
+
if(this.range === 0){
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
// validate attack range:
|
|
171
|
+
let interactionArea = new InteractionArea();
|
|
172
|
+
interactionArea.setupInteractionArea(this.range, targetPosition.x, targetPosition.y);
|
|
173
|
+
let interactionResult = interactionArea.isValidInteraction(ownerPosition.x, ownerPosition.y);
|
|
174
|
+
// @TODO - BETA - Replace catch.
|
|
175
|
+
this.fireEvent(SkillsEvents.SKILL_AFTER_IN_RANGE, this, interactionResult).catch((err) => {
|
|
176
|
+
Logger.error(err);
|
|
177
|
+
});
|
|
178
|
+
return interactionResult;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async execute(target)
|
|
182
|
+
{
|
|
183
|
+
if(!this.isReady){
|
|
184
|
+
Logger.error('Skill is not ready to be executed.', this.key);
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
await this.fireEvent(SkillsEvents.SKILL_BEFORE_EXECUTE, this, target);
|
|
188
|
+
if(target){
|
|
189
|
+
this.target = target;
|
|
190
|
+
}
|
|
191
|
+
if(!this.target){
|
|
192
|
+
Logger.error('Target undefined.');
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
if(
|
|
196
|
+
// custom conditions:
|
|
197
|
+
!this.onExecuteConditions()
|
|
198
|
+
// range:
|
|
199
|
+
|| await this.isValidRange(target)
|
|
200
|
+
// cast validation:
|
|
201
|
+
|| (this.autoValidation && !this.validate())
|
|
202
|
+
// @TODO - BETA - Auto validation doesn't work, check ActionsMessageActions.parseMessageAndRunActions.
|
|
203
|
+
){
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
if(this.ownerEffects){
|
|
207
|
+
this.applyModifiers(this.ownerEffects, this.owner, true);
|
|
208
|
+
await this.fireEvent(SkillsEvents.SKILL_APPLY_OWNER_EFFECTS, this, target);
|
|
209
|
+
}
|
|
210
|
+
let skillLogicResult = await this.applySkillLogicOnTarget(target);
|
|
211
|
+
this.uses++;
|
|
212
|
+
await this.onExecuteRewards();
|
|
213
|
+
await this.fireEvent(SkillsEvents.SKILL_AFTER_EXECUTE, this, target);
|
|
214
|
+
return skillLogicResult;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async applySkillLogicOnTarget(target)
|
|
218
|
+
{
|
|
219
|
+
if(0 < this.castTime){
|
|
220
|
+
await this.fireEvent(SkillsEvents.SKILL_BEFORE_CAST, this, target);
|
|
221
|
+
this.owner.isCasting = true;
|
|
222
|
+
this.owner.castingTimer = setTimeout(async () => {
|
|
223
|
+
let skillLogicResult = await this.finishExecution(target);
|
|
224
|
+
this.owner.isCasting = false;
|
|
225
|
+
await this.fireEvent(SkillsEvents.SKILL_AFTER_CAST, this, target, skillLogicResult);
|
|
226
|
+
}, this.castTime);
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
return await this.finishExecution(target);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async isValidRange(target)
|
|
233
|
+
{
|
|
234
|
+
return this.rangeAutomaticValidation
|
|
235
|
+
&& this.rangePropertyX
|
|
236
|
+
&& this.rangePropertyY
|
|
237
|
+
&& !(await this.validateRange(target));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async finishExecution(target)
|
|
241
|
+
{
|
|
242
|
+
await this.fireEvent(SkillsEvents.SKILL_BEFORE_RUN_LOGIC, this, target);
|
|
243
|
+
let skillLogicResult = await this.runSkillLogic();
|
|
244
|
+
await this.fireEvent(SkillsEvents.SKILL_AFTER_RUN_LOGIC, this, target);
|
|
245
|
+
return skillLogicResult;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
onExecuteConditions()
|
|
249
|
+
{
|
|
250
|
+
// implement any custom conditions here.
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async runSkillLogic()
|
|
255
|
+
{
|
|
256
|
+
// run the skill logic here.
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async onExecuteRewards()
|
|
261
|
+
{
|
|
262
|
+
// implement any custom rewards here.
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
applyCriticalValue(normalValue)
|
|
266
|
+
{
|
|
267
|
+
if(!this.isCritical()){
|
|
268
|
+
return normalValue;
|
|
269
|
+
}
|
|
270
|
+
if(sc.isNumber(this.criticalMultiplier)){
|
|
271
|
+
normalValue = normalValue * this.criticalMultiplier;
|
|
272
|
+
}
|
|
273
|
+
if(sc.isNumber(this.criticalFixedValue)){
|
|
274
|
+
normalValue = normalValue + this.criticalFixedValue;
|
|
275
|
+
}
|
|
276
|
+
return normalValue;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
getCriticalDiff(normalValue)
|
|
280
|
+
{
|
|
281
|
+
return this.applyCriticalValue(normalValue) - normalValue;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
isCritical()
|
|
285
|
+
{
|
|
286
|
+
if(this.criticalChance <= 0){
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
return sc.randomInteger(1, 100) <= this.criticalChance;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
applyModifiers(modifiersObjectList, target, avoidCritical = false)
|
|
293
|
+
{
|
|
294
|
+
this.lastAppliedModifiers = {};
|
|
295
|
+
for(let i of Object.keys(modifiersObjectList)){
|
|
296
|
+
let modifier = modifiersObjectList[i];
|
|
297
|
+
modifier.target = target;
|
|
298
|
+
let newValue = modifier.getModifiedValue();
|
|
299
|
+
if(!avoidCritical){
|
|
300
|
+
newValue = newValue + this.getCriticalDiff(modifier.value);
|
|
301
|
+
}
|
|
302
|
+
modifier.setOwnerProperty(modifier.propertyKey, newValue);
|
|
303
|
+
this.lastAppliedModifiers[modifier.propertyKey] = newValue;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
getOwnerId()
|
|
308
|
+
{
|
|
309
|
+
return this.owner[this.ownerIdProperty];
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
getOwnerEventKey()
|
|
313
|
+
{
|
|
314
|
+
return sc.get(this.owner, 'eventsPrefix', 'skill.ownerId.'+this.getOwnerId());
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
getOwnerUniqueEventKey(suffix)
|
|
318
|
+
{
|
|
319
|
+
let uniqueKey = sc.isFunction(this.owner.eventUniqueKey)
|
|
320
|
+
? this.owner.eventUniqueKey()
|
|
321
|
+
: 'skills.ownerId.' + this.getOwnerId() + '.uKey.' + sc.getTime();
|
|
322
|
+
return uniqueKey+(suffix ? '.'+suffix : '');
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
async fireEvent(eventName, ...args)
|
|
327
|
+
{
|
|
328
|
+
// @NOTE: we append the current level set owner ID to the event name, so we can pick up this event only for
|
|
329
|
+
// this owner automatically:
|
|
330
|
+
return await this.events.emit(this.eventFullName(eventName), ...args);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
listenEvent(eventName, callback, removeKey, masterKey)
|
|
334
|
+
{
|
|
335
|
+
return this.events.onWithKey(this.eventFullName(eventName), callback, removeKey, masterKey);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
eventFullName(eventName)
|
|
339
|
+
{
|
|
340
|
+
return this.getOwnerEventKey()+'.'+eventName;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
module.exports = Skill;
|