-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartGame.ts
More file actions
127 lines (107 loc) · 3.91 KB
/
Copy pathStartGame.ts
File metadata and controls
127 lines (107 loc) · 3.91 KB
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
import { SETTINGS } from "./Settings"
import { Position } from "./index";
import { timeout } from "./functions";
export class StartGame {
gameLostDiv: HTMLElement = document.getElementById("gameLost") as HTMLElement;
boardDiv: HTMLElement = document.getElementById("board") as HTMLElement;
constructor() {
this.pickRandom()
}
createBoard = async (array: Position[]) => {
const BOARD = SETTINGS.board
const boardDiv = document.getElementById("board")
boardDiv ? boardDiv.innerHTML = "" : false
let counter = 0
for (let i = 0; i < BOARD.length; i++) {
const tr = document.createElement("div")
tr.classList.add("row")
boardDiv?.appendChild(tr)
for (let j = 0; j < BOARD[i].length; j++) {
const td = document.createElement("div")
td.id = i + "_" + j
td.classList.add("square")
tr.appendChild(td)
counter++;
}
}
let x!: Position;
let xDiv!: HTMLElement;
for (let i = 0; i < SETTINGS.board.length; i++) {
for (let j = 0; j < SETTINGS.board[i].length; j++) {
if (SETTINGS.board[i][j] == "X") {
x = { i: i, j: j }
SETTINGS.board[i][j] = ""
xDiv = document.getElementById(i + "_" + j) as HTMLElement
}
}
}
this.startAnimation()
var audio = new Audio('sounds/open-game.mp3');
audio.play();
array.forEach(async (e, i) => {
(function (index) {
setTimeout(function () {
const td = document.getElementById(e.i + "_" + e.j) as HTMLElement
td.className += " " + SETTINGS.board[e.i][e.j];
}, i * 3);
})(i);
});
await this.walkingGuy(xDiv, x);
}
startAnimation = async () => {
this.gameLostDiv.style.display = "block"
this.gameLostDiv.style.color = "white"
this.gameLostDiv.innerHTML = "Player1, " + SETTINGS.lives + " men ,cave A/1"
this.boardDiv.style.animationName = "startGame"
}
pickRandom = async () => {
let arr = []
for (let i = 0; i < SETTINGS.board.length; i++) {
for (let j = 0; j < SETTINGS.board[i].length; j++) {
arr.push({ i: i, j: j })
}
}
this.shuffle(arr)
await this.createBoard(arr)
}
walkingGuy = async (xDiv: HTMLElement, x: Position) => {
for (let i = 0; i < 9; i++) {
(function (index) {
setTimeout(function () {
if (i % 2 == 0)
xDiv.className = "square w";
else
xDiv.className = "square";
}, i * 500);
})(i);
}
await timeout(4400)
xDiv.className = "square boom1"
console.log("boom1");
await timeout(200)
xDiv.className = "square boom2"
console.log("boom2");
await timeout(200)
xDiv.className = "square boom3"
console.log("boom3");
await timeout(200)
xDiv.className = "square X";
// const backupBoard = SETTINGS.boardBackup
// SETTINGS.board = [...backupBoard];
SETTINGS.board[x.i][x.j] = "X"
console.table(SETTINGS.board)
}
shuffle = (array: Position[]) => {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
}