-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrender.ts
254 lines (234 loc) · 9.86 KB
/
render.ts
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
249
250
251
252
253
254
import type * as cg from '../../types';
import type { PieceName, SquareClasses } from '../../render';
import { p1Pov } from '../../board';
import { State } from '../../state';
import { createEl } from '../../util';
import { AnimCurrent, AnimFadings, AnimVector, AnimVectors } from '../../anim';
import { DragCurrent } from '../../drag';
import { appendValue, isPieceNode, isSquareNode, posZIndex, removeNodes } from '../../render';
import { translateAbs, translateRel } from './util';
import { computeMoveVectorPostMove } from './engine';
// @TODO: remove parts unrelated to Abalone
export const render = (s: State): void => {
const orientation = s.orientation,
asP1: boolean = p1Pov(s),
posToTranslate = s.dom.relative
? s.posToTranslateRelative
: s.posToTranslateAbsolute(s.dom.bounds(), s.dimensions, s.variant),
translate = s.dom.relative ? translateRel : translateAbs,
boardEl: HTMLElement = s.dom.elements.board,
pieces: cg.Pieces = s.pieces,
curAnim: AnimCurrent | undefined = s.animation.current,
anims: AnimVectors = curAnim ? curAnim.plan.anims : new Map(),
fadings: AnimFadings = curAnim ? curAnim.plan.fadings : new Map(),
curDrag: DragCurrent | undefined = s.draggable.current,
squares: SquareClasses = computeSquareClasses(s),
samePieces: Set<cg.Key> = new Set(),
sameSquares: Set<cg.Key> = new Set(),
movedPieces: Map<PieceName, cg.PieceNode[]> = new Map(),
movedSquares: Map<string, cg.SquareNode[]> = new Map(); // by class name
let k: cg.Key,
el: cg.PieceNode | cg.SquareNode | undefined,
pieceAtKey: cg.Piece | undefined,
elPieceName: PieceName,
anim: AnimVector | undefined,
fading: cg.Piece | undefined,
pMvdset: cg.PieceNode[] | undefined,
pMvd: cg.PieceNode | undefined,
sMvdset: cg.SquareNode[] | undefined,
sMvd: cg.SquareNode | undefined;
// walk over all board dom elements, apply animations and flag moved pieces
el = boardEl.firstChild as cg.PieceNode | cg.SquareNode | undefined;
while (el) {
k = el.cgKey;
if (isPieceNode(el)) {
pieceAtKey = pieces.get(k);
anim = anims.get(k);
fading = fadings.get(k);
elPieceName = el.cgPiece;
// if piece not being dragged anymore, remove dragging style
if (el.cgDragging && (!curDrag || curDrag.orig !== k)) {
el.classList.remove('dragging');
translate(el, posToTranslate(s.key2pos(k), orientation, s.dimensions, s.variant));
el.cgDragging = false;
}
// remove fading class if it still remains
if (!fading && el.cgFading) {
el.cgFading = false;
el.classList.remove('fading');
}
// there is now a piece at this dom key
if (pieceAtKey) {
// continue animation if already animating and same piece
// (otherwise it could animate a captured piece)
if (anim && el.cgAnimating && elPieceName === pieceNameOf(pieceAtKey, s.myPlayerIndex, s.orientation)) {
const pos = s.key2pos(k);
pos[0] += anim[2];
pos[1] += anim[3];
el.classList.add('anim');
translate(el, posToTranslate(s.key2pos(k), orientation, s.dimensions, s.variant));
} else if (el.cgAnimating) {
el.cgAnimating = false;
el.classList.remove('anim');
translate(el, posToTranslate(s.key2pos(k), orientation, s.dimensions, s.variant));
if (s.addPieceZIndex) el.style.zIndex = posZIndex(s.key2pos(k), orientation, asP1, s.dimensions);
}
// same piece: flag as same
if (elPieceName === pieceNameOf(pieceAtKey, s.myPlayerIndex, s.orientation) && (!fading || !el.cgFading)) {
samePieces.add(k);
}
// different piece: flag as moved unless it is a fading piece
else {
if (fading && elPieceName === pieceNameOf(fading, s.myPlayerIndex, s.orientation)) {
el.classList.add('fading');
el.cgFading = true;
} else {
appendValue(movedPieces, elPieceName, el);
}
}
}
// no piece: flag as moved
else {
appendValue(movedPieces, elPieceName, el);
}
} else if (isSquareNode(el)) {
const cn = el.className;
if (squares.get(k) === cn) sameSquares.add(k);
else if (movedSquares.has(cn)) appendValue(movedSquares, cn, el);
else movedSquares.set(cn, [el]);
}
el = el.nextSibling as cg.PieceNode | cg.SquareNode | undefined;
}
// walk over all squares in current set, apply dom changes to moved squares
// or append new squares
for (const [sk, className] of squares) {
if (!sameSquares.has(sk)) {
sMvdset = movedSquares.get(className);
sMvd = sMvdset && sMvdset.pop();
const translation = posToTranslate(s.key2pos(sk), orientation, s.dimensions, s.variant);
if (sMvd) {
sMvd.cgKey = sk;
translate(sMvd, translation);
} else {
const squareNode = createEl('square', className) as cg.SquareNode;
squareNode.cgKey = sk;
translate(squareNode, translation);
boardEl.insertBefore(squareNode, boardEl.firstChild);
}
}
}
// walk over all pieces in current set, apply dom changes to moved pieces
// or append new pieces
for (const [k, p] of pieces) {
anim = anims.get(k);
if (!samePieces.has(k)) {
pMvdset = movedPieces.get(pieceNameOf(p, s.myPlayerIndex, s.orientation));
pMvd = pMvdset && pMvdset.pop();
// a same piece was moved
if (pMvd) {
// apply dom changes
pMvd.cgKey = k;
if (pMvd.cgFading) {
pMvd.classList.remove('fading');
pMvd.cgFading = false;
}
const pos = s.key2pos(k);
if (s.addPieceZIndex) pMvd.style.zIndex = posZIndex(pos, orientation, asP1, s.dimensions);
if (anim) {
pMvd.cgAnimating = true;
pMvd.classList.add('anim');
pos[0] += anim[2];
pos[1] += anim[3];
}
translate(pMvd, posToTranslate(pos, orientation, s.dimensions, s.variant));
}
// no piece in moved obj: insert the new piece
// assumes the new piece is not being dragged
else {
const pieceName = pieceNameOf(p, s.myPlayerIndex, s.orientation),
pieceNode = createEl('piece', pieceName) as cg.PieceNode,
pos = s.key2pos(k); // used here to compute position
pieceNode.cgPiece = pieceName;
pieceNode.cgKey = k;
if (anim) {
pieceNode.cgAnimating = true;
pos[0] += anim[2];
pos[1] += anim[3];
}
translate(pieceNode, posToTranslate(pos, orientation, s.dimensions, s.variant));
if (s.addPieceZIndex) pieceNode.style.zIndex = posZIndex(pos, orientation, asP1, s.dimensions);
boardEl.appendChild(pieceNode);
}
}
}
// remove any element that remains in the moved sets
for (const nodes of movedPieces.values()) removeNodes(s, nodes);
for (const nodes of movedSquares.values()) removeNodes(s, nodes);
};
function pieceNameOf(piece: cg.Piece, myPlayerIndex: cg.PlayerIndex, orientation: cg.Orientation): string {
const side =
(piece.playerIndex === myPlayerIndex && orientation === myPlayerIndex) ||
(piece.playerIndex !== myPlayerIndex && orientation !== myPlayerIndex)
? 'ally'
: 'enemy';
return `${piece.playerIndex} ${piece.role} ${side}`;
}
function addSquare(squares: SquareClasses, key: cg.Key, klass: string): void {
const classes = squares.get(key);
if (classes) squares.set(key, `${classes} ${klass}`);
else squares.set(key, klass);
}
// @TODO: clean this up to remove any notion non related to Abalone
export function computeSquareClasses(s: State): SquareClasses {
const squares: SquareClasses = new Map();
if (s.lastMove && s.lastMove.length === 2 && s.highlight.lastMove) {
const moveImpact = computeMoveVectorPostMove(s.pieces, s.lastMove[0], s.lastMove[1]);
const player = s.turnPlayerIndex;
moveImpact?.landingSquares.forEach(coordinates => {
addSquare(squares, coordinates, `last-move to ${player}${moveImpact.directionString}`);
});
}
if (s.selected) {
addSquare(squares, s.selected, 'selected');
if (s.movable.showDests) {
const dests = s.movable.dests?.get(s.selected);
if (dests)
for (const k of dests) {
addSquare(squares, k, 'move-dest' + (s.pieces.has(k) ? ' oc' : ''));
}
const pDests = s.premovable.dests;
if (pDests)
for (const k of pDests) {
addSquare(squares, k, 'premove-dest' + (s.pieces.has(k) ? ' oc' : ''));
}
}
} else if (s.dropmode.active || s.draggable.current?.orig === 'a0') {
const piece = s.dropmode.active ? s.dropmode.piece : s.draggable.current?.piece;
if (piece) {
// TODO: there was a function called isPredroppable that was used in drag.ts or drop.ts or both.
// Maybe use the same here to decide what to render instead of potentially making it possible both
// kinds of highlighting to happen if something was not cleared up in the state.
// In other place (pocket.ts) this condition is used ot decide similar question: ctrl.myplayerIndex === ctrl.turnPlayerIndex
if (s.dropmode.showDropDests) {
const dests = s.dropmode.dropDests?.get(piece.role);
if (dests)
for (const k of dests) {
addSquare(squares, k, 'move-dest');
}
}
if (s.predroppable.showDropDests) {
const pDests = s.predroppable.dropDests;
if (pDests)
for (const k of pDests) {
addSquare(squares, k, 'premove-dest' + (s.pieces.has(k) ? ' oc' : ''));
}
}
}
}
const premove = s.premovable.current;
if (premove) for (const k of premove) addSquare(squares, k, 'current-premove');
else if (s.predroppable.current) addSquare(squares, s.predroppable.current.key, 'current-premove');
const o = s.exploding;
if (o) for (const k of o.keys) addSquare(squares, k, 'exploding' + o.stage);
return squares;
}