-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGamePiece.js
248 lines (201 loc) · 6.45 KB
/
GamePiece.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import _ from 'lodash';
import Position from './Position.js';
import Direction from './Direction.js';
import Statuses from './Statuses.js';
import attributes from '../util/Attributes.js';
import loglevel from 'loglevel-decorator';
@loglevel
export default class GamePiece
{
constructor()
{
this.id = null;
this.cardTemplateId = null;
this.name = null;
this.description = null;
this.playerId = null;
this.position = new Position();
this.direction = Direction.South;
this.attack = null;
this.baseAttack = null;
this.health = null;
this.baseHealth = null;
this.movement = null;
this.baseMovement = null;
this.range = null;
this.baseRange = null;
this.spellDamage = null;
this.baseSpellDamage = null;
this.events = null;
this.baseTags = [];
this.tags = [];
this.buffs = [];
this.aura = null;
this.statuses = 0;
this.baseStatuses = 0;
this.abilityCharge = 0;
this.armor = 0;
//piece has moved or attacked this turn?
this.moveCount = 0;
this.attackCount = 0;
this.bornOn = null;
}
get maxBuffedHealth(){
return this.baseHealth + _.sumBy(this.buffs, 'health');
}
get isHero(){
return this.tags.includes('Hero');
}
get isMinion(){
return this.tags.includes('Minion');
}
get isRanged(){
return this.range > 0;
}
get isMelee(){
return !this.range;
}
get canAttack(){
let maxAttacks = (this.statuses & Statuses.DyadStrike) ? 2 : 1;
return this.attack > 0
&& this.attackCount < maxAttacks
&& (this.statuses & Statuses.CantAttack) == 0
&& (this.statuses & Statuses.Petrify) == 0;
}
addBuff(buff, cardEvaluator){
let action = {};
if(buff.enabled){
action = this.addBuffStats(buff, cardEvaluator);
}
this.buffs.push(buff);
return action;
}
//requires you to find the buff instance beforehand
removeBuff(buff, cardEvaluator){
if(this.buffs.length === 0) return null;
let buffIndex = this.buffs.indexOf(buff);
if(buffIndex === -1) return null;
this.buffs.splice(buffIndex, 1);
return this.removeBuffStats(buff, cardEvaluator);
}
enableBuff(buff, cardEvaluator){
if(buff.enabled) return;
buff.enabled = true;
return this.addBuffStats(buff, cardEvaluator);
}
disableBuff(buff, cardEvaluator){
if(!buff.enabled) return;
let buffAction = this.removeBuffStats(buff, cardEvaluator);
buff.enabled = false;
return buffAction;
}
addBuffStats(buff, cardEvaluator){
let action = {};
action = this.changeBuffStats(buff, action, true);
if(buff.addStatus){
let statusAction = this.addStatuses(buff.addStatus, cardEvaluator);
//merge the status action into the buff action, the attribute changes from status should be the most up to date
Object.assign(action, statusAction);
}
if(buff.removeStatus){
let statusAction = this.removeStatuses(buff.removeStatus, cardEvaluator);
Object.assign(action, statusAction);
}
action.statuses = this.statuses;
action.removed = false;
return action
}
removeBuffStats(buff, cardEvaluator){
let action = {};
//Only back out stats if they're actually applied
if(!buff.enabled){
return action;
}
action = this.changeBuffStats(buff, action, false);
//now for statuses, if one was added, remove it, and vice versa
if(buff.addStatus){
let statusAction = this.removeStatuses(buff.addStatus, cardEvaluator);
Object.assign(action, statusAction);
}
//If a buff is removing a status, when that buff gets disable we don't add back the status to the piece
//Just a game rule I suppose and it makes more sense this way
// if(buff.removeStatus){
// }
action.statuses = this.statuses;
action.removed = true;
return action;
}
changeBuffStats(buff, action, isAdd){
for(let attrib of attributes){
if(buff[attrib] == null) continue;
let origStat = this[attrib];
if(isAdd){
this[attrib] += buff[attrib];
}else{
this[attrib] -= buff[attrib];
}
//cap at min of 0 to prevent negative attack/movement
this[attrib] = Math.max(0, this[attrib]);
//but for health, only lower to min of 1 so you can't kill off a piece by adding or removing a buff
if(attrib === 'health'){
this[attrib] = Math.max(1, this[attrib]);
}
//update action with new values
let newAttrib = 'new' + attrib.charAt(0).toUpperCase() + attrib.slice(1);
action[newAttrib] = this[attrib];
action[attrib] = action[newAttrib] - origStat;
// let baseAttr = 'base' + capAttr;
// action[baseAttr] = this[baseAttr];
}
return action;
}
addStatuses(add, cardEvaluator){
let action = {};
if(this.range){
if((add & Statuses.Piercing) || (add & Statuses.Cleave)){
this.log.info('Ignoring pierce or cleave add to ranged piece %s', action.pieceId);
action.addStatus = action.addStatus & ~Statuses.Piercing;
action.addStatus = action.addStatus & ~Statuses.Cleave;
}
}
this.statuses = this.statuses | add;
//remove all statuses other than silence if it was silenced
if(add & Statuses.Silence){
action.removeStatus = this.statuses & ~Statuses.Silence;
//back out any buffs on the this
if(this.buffs.length > 0){
for(let b = this.buffs.length - 1; b >= 0; b--){
let buff = this.buffs[b];
let buffChange = this.removeBuff(buff);
if(!buffChange){
this.log.error('Cannot unbuff piece %j with buff %j', this, buff);
continue;
}
for(let attrib of attributes){
let newAttrib = 'new' + attrib.charAt(0).toUpperCase() + attrib.slice(1);
action[attrib] = buffChange[attrib];
action[newAttrib] = buffChange[newAttrib];
this.log.info('un buffing piece %s to %s %s', this.id, this[attrib], attrib);
}
}
}
//aura and events go bye bye
this.aura = null;
this.events = null;
this.statuses = Statuses.Silence;
//remove timers for this piece
cardEvaluator.cleanupTimers(this);
}
action.addStatus = add || 0;
action.removeStatus = action.removeStatus || 0;
action.statuses = this.statuses;
return action;
}
removeStatuses(remove, cardEvaluator){
this.statuses = this.statuses & ~remove;
return {
removeStatus: remove,
statuses: this.statuses
};
}
}