-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttackPieceProcessor.js
176 lines (150 loc) · 6.59 KB
/
AttackPieceProcessor.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
import Statuses from '../models/Statuses.js';
import AttackPiece from '../actions/AttackPiece.js';
import PieceHealthChange from '../actions/PieceHealthChange.js';
import PieceStatusChange from '../actions/PieceStatusChange.js';
import TilesCleared from '../actions/TilesCleared.js';
import loglevel from 'loglevel-decorator';
import Message from '../actions/Message.js';
import Constants from '../util/Constants.js';
import {faceDirection, cleavePositions, piercePositions} from '../models/Direction.js';
/**
* Handle the units attacking each other
*/
@loglevel
export default class AttackPieceProcessor
{
constructor(pieceState, cardEvaluator, mapState, turnState)
{
this.pieceState = pieceState;
this.cardEvaluator = cardEvaluator;
this.mapState = mapState;
this.turnState = turnState;
}
/**
* Proc
*/
async handleAction(action, queue)
{
if (!(action instanceof AttackPiece)) {
return;
}
var attacker = this.pieceState.piece(action.attackingPieceId);
var target = this.pieceState.piece(action.targetPieceId);
if(!attacker || !target ){
this.log.warn('Attacker or target not found in attack %j', this.pieceState);
return queue.cancel(action);
}
if(attacker.id === target.id){
this.log.warn('Stop hitting yourself!');
return queue.cancel(action);
}
if(attacker.attack === 0){
this.log.warn('Attacking piece has no attack!');
return queue.cancel(action);
}
let targetDistance = Number.MAX_VALUE;
if(action.isTauntAttack || attacker.range != null){
targetDistance = this.mapState.kingDistance(attacker.position, target.position);
}else{
targetDistance = this.mapState.tileDistance(attacker.position, target.position);
}
let rangedAttack = attacker.range != null && targetDistance > 1;
if(targetDistance > 1 && (attacker.range == null || attacker.range < targetDistance)){
this.log.warn('Attacker too far away from target %s %s', targetDistance, attacker.range);
//queue.push(new Message('Too far away!', action.playerId));
return queue.cancel(action);
}
//check height differential
let attackerTile = this.mapState.getTile(attacker.position);
let targetTile = this.mapState.getTile(target.position)
if(attacker.range == null && !this.mapState.isHeightPassable(attackerTile, targetTile)){
this.log.info('Cannot attack due to tile heigh diff');
queue.push(new Message('Can\'t attack up that slope!', action.playerId));
return queue.cancel(action);
}
if(!action.isTauntAttack && (attacker.statuses & Statuses.CantAttack)){
this.log.warn('Cannot attack with piece %s with Cant Attack', attacker.id, attacker.statuses);
queue.push(new Message('Unable to attack with this status!', action.playerId));
return queue.cancel(action);
}
if(attacker.statuses & Statuses.Petrify){
this.log.info('Cannot attack with piece %s when petrified', attacker.id);
return queue.cancel(action);
}
if(!action.isTauntAttack && (target.statuses & Statuses.Cloak)){
this.log.warn('Cannot attack piece %s with Cloak', target.id);
queue.push(new Message('Cannot attack a cloaked minion!', action.playerId));
return queue.cancel(action);
}
//check if piece is 'old' enough to attack
if(!action.isTauntAttack &&
(
attacker.bornOn === null ||
((this.turnState.currentTurn - attacker.bornOn) < 1 && !(attacker.statuses & Statuses.Charge))
)
){
this.log.warn('Piece %s must wait a turn to attack', attacker.id);
queue.push(new Message('Minions need time to prepare!', action.playerId));
return queue.cancel(action);
}
if(!action.isTauntAttack && !attacker.canAttack){
this.log.warn('Piece %s has already attacked', attacker.id);
queue.push(new Message('Piece has already attacked!', action.playerId));
return queue.cancel(action);
}
//determine direction piece should be facing to see if rotation is necessary
let attackerNewDirection = faceDirection(target.position, attacker.position);
attacker.direction = action.direction = attackerNewDirection;
let targetNewDirection = faceDirection(attacker.position, target.position);
target.direction = action.targetDirection = targetNewDirection;
let bonus = 0;
let bonusMsg = null;
if(rangedAttack && (attackerTile.position.y - targetTile.position.y) >= Constants.heightDeltaThreshold ){
bonus = -1;
bonusMsg = 'High Ground';
}
if(rangedAttack && (targetTile.position.y - attackerTile.position.y) >= Constants.heightDeltaThreshold ){
bonus = 1;
bonusMsg = 'Low Ground';
}
queue.push(new PieceHealthChange({pieceId: action.targetPieceId, change: -attacker.attack, bonus, bonusMsg}));
//counter attack if in range
if(!rangedAttack || (target.range != null && target.range >= targetDistance)){
if(!(target.statuses & Statuses.Petrify)){
queue.push(new PieceHealthChange({pieceId: action.attackingPieceId, change: -target.attack}));
}
}
//cleave and piercing checks
let extraHitPositions = null;
if(attacker.statuses & Statuses.Cleave){
extraHitPositions = cleavePositions(attacker.position, attacker.direction);
}
if(attacker.statuses & Statuses.Piercing){
extraHitPositions = piercePositions(attacker.position, attacker.direction);
}
if(extraHitPositions && extraHitPositions.length){
var extraHitPieces = extraHitPositions.map(p => this.pieceState.pieceAt(p)).filter(p => p);
for (const extraHitPiece of extraHitPieces) {
if(extraHitPiece.playerId === attacker.playerId) continue;
queue.push(new PieceHealthChange({pieceId: extraHitPiece.id, change: -attacker.attack, bonus, bonusMsg}));
}
let clearableTiles = this.mapState.map.tiles.filter(t => t.clearable && extraHitPositions.some(p => p.tileEquals(t.position)));
if(clearableTiles.length){
queue.push(new TilesCleared(clearableTiles.map(t => t.position)));
}
}
attacker.attackCount++;
if(attacker.range != null){
attacker.moveCount = attacker.movement;
}
//Remove cloak once they've attacked
if(attacker.statuses & Statuses.Cloak){
queue.push(new PieceStatusChange({pieceId: attacker.id, remove: Statuses.Cloak }));
}
this.cardEvaluator.evaluatePieceEvent('attacks', attacker, {targetPieceId: target.id});
this.log.info('piece %s (%s/%s) attacked %s (%s/%s) direction %s',
attacker.id, attacker.attack, attacker.health,
target.id, target.attack, target.health, attackerNewDirection);
queue.complete(action);
}
}