-
Notifications
You must be signed in to change notification settings - Fork 0
/
undo.ts
93 lines (78 loc) · 2.44 KB
/
undo.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
// Get elements
const undoBtn = document.getElementById("undo")!!;
const redoBtn = document.getElementById("redo")!!;
const undoTenBtn = document.getElementById("undo-ten")!!;
const redoTenBtn = document.getElementById("redo-ten")!!;
const restartBtn = document.getElementById("restart")!!;
// Add an entry in the undo list
function RecordUndo() {
let posList: Array<vec2> = [];
for (const tile of currentData.tiles) {
posList.push(tile.getPos());
}
if (currentData.undoIndex < currentData.undo.length - 1)
currentData.undo = currentData.undo.slice(0, currentData.undoIndex + 1)
currentData.undo.push(posList)
currentData.undoIndex++;
UpdateUndoBtns();
}
// Undo the game
function Undo() {
if (currentData.undoIndex < 1) return;
currentData.undoIndex--;
currentData.moveCount--;
ApplyUndoList(currentData.undoIndex);
}
// Undo the game many times
async function UndoMany(count: number) {
for (let i = 0; i < count; i++) {
if (currentData.undoIndex < 1) return;
Undo();
await delay(undoDelay);
}
}
// Redo the game
function Redo() {
if (currentData.undoIndex >= currentData.undo.length - 1) return;
currentData.undoIndex++;
currentData.moveCount++;
ApplyUndoList(currentData.undoIndex);
}
// Redo the game many times
async function RedoMany(count: number) {
for (let i = 0; i < count; i++) {
if (currentData.undoIndex >= currentData.undo.length - 1) return;
Redo();
await delay(undoDelay);
}
}
// Apply an undo entry to the game
function ApplyUndoList(index: number) {
let l = currentData.undo[index];
for (let i = 0; i < l.length; i++) {
currentData.tiles[i].setPos(l[i]);
}
DisplayMoves();
UpdateUndoBtns();
}
// Update undo btns appearance
function UpdateUndoBtns() {
if (currentData.undoIndex < 1) {
undoBtn.setAttribute("disabled", "true");
restartBtn.setAttribute("disabled", "true");
undoTenBtn.setAttribute("disabled", "true");
}
else {
undoBtn.removeAttribute("disabled");
restartBtn.removeAttribute("disabled");
undoTenBtn.removeAttribute("disabled");
}
if (currentData.undoIndex >= currentData.undo.length - 1) {
redoBtn.setAttribute("disabled", "true")
redoTenBtn.setAttribute("disabled", "true")
}
else {
redoBtn.removeAttribute("disabled")
redoTenBtn.removeAttribute("disabled")
}
}