-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.js
180 lines (146 loc) · 5.02 KB
/
minesweeper.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
/**
* Defines all possible colors for the tile numbers
* @type {{ 1: string, 2: string, 3: string, 4: string, 6: string }}
*/
const colors = {
1: "blue",
2: "green",
3: "red",
4: "purple",
5: "yellow",
6: "pink"
};
function animateBackground(ctx, x, y, width, height, curOpacity, finalOpacity, startTime, duration, color) {
const time = (new Date()).getTime() - startTime;
if (curOpacity >= finalOpacity)
return;
const newOpacity = easeInOutCubic(time, 0, finalOpacity, duration);
if (newOpacity <= finalOpacity)
curOpacity = newOpacity;
else
curOpacity = finalOpacity;
color.a = curOpacity;
ctx.clearRect(x, y, width, height);
ctx.fillStyle = "rgba(" + color.r + "," + color.g + "," + color.b + "," + color.a + ")";
ctx.fillRect(x, y, width, height);
requestAnimFrame(function () {
animateBackground(ctx, x, y, width, height, curOpacity, finalOpacity, startTime, duration, color);
});
}
function animateTile(ctx, x, y, curWidth, curHeight, finalWidth, finalHeight, curRadius, finalRadius, startTime, duration, color) {
const time = (new Date()).getTime() - startTime;
if (curWidth === finalWidth && curHeight === finalHeight && curRadius === finalRadius)
return;
const newWidth = easeInOutCubic(time, 0, finalWidth, duration);
const newHeight = easeInOutCubic(time, 0, finalHeight, duration);
const newRadius = easeInOutCubic(time, 0, finalRadius, duration);
if (newWidth < finalWidth)
curWidth = newWidth;
else
curWidth = finalWidth;
if (newHeight < finalHeight)
curHeight = newHeight;
else
curHeight = finalHeight;
if (newRadius < finalRadius)
curRadius = newRadius;
else
curRadius = finalRadius;
drawRoundedRect(ctx, x + (finalWidth - curWidth) / 2, y + (finalHeight - curHeight) / 2, curWidth, curHeight, curRadius, color);
requestAnimFrame(() => {
animateTile(ctx, x, y, curWidth, curHeight, finalWidth, finalHeight, curRadius, finalRadius, startTime, duration, color);
});
}
function animateText(ctx, renderingConfig, text, x, y, curFontSize, finalFontSize, startTime, duration, color, font) {
const time = (new Date()).getTime() - startTime;
if (curFontSize === finalFontSize)
return;
const newFontSize = easeInOutCubic(time, 0, finalFontSize, duration);
if (newFontSize < finalFontSize) {
curFontSize = newFontSize;
} else {
curFontSize = finalFontSize;
}
const textDrawX = (x + .5 - renderingConfig.tiltX) * renderingConfig.sizeX;
const textDrawY = (y + .5 - renderingConfig.tiltY) * renderingConfig.sizeY + curFontSize * .33;
if (font === undefined)
font = "Roboto Bold";
ctx.fillStyle = color;
ctx.font = "bold " + curFontSize + "px " + font;
ctx.textAlign = "center";
ctx.fillText(text, textDrawX, textDrawY);
requestAnimFrame(function () {
animateText(ctx, renderingConfig, text, x, y, curFontSize, finalFontSize, startTime, duration, color, font);
});
}
function drawRoundedRect(context, x, y, w, h, r, color) {
context.fillStyle = color;
context.beginPath();
context.moveTo(x + r, y);
context.arcTo(x + w, y, x + w, y + h, r);
context.arcTo(x + w, y + h, x, y + h, r);
context.arcTo(x, y + h, x, y, r);
context.arcTo(x, y, x + w, y, r);
context.closePath();
context.fill();
}
function easeInOutCubic(t, b, c, d) {
t /= d;
t--;
return c * (Math.pow(t, 3) + 1) + b;
}
Object.prototype.count = function (val) {
let counter = 0;
for (let el in this) {
if (this.hasOwnProperty(el)) {
if (val === this[el].tileValue.tileValue) {
counter++;
}
}
}
return counter;
};
Object.prototype.countFlagged = function (val) {
let counter = 0;
for (let el in this) {
if (this.hasOwnProperty(el)) {
if (this[el].tileValue.flagged === val) {
counter++;
}
}
}
return counter;
};
String.prototype.toDOM=function(){
let d=document
,i
,a=d.createElement("div")
,b=d.createDocumentFragment();
a.innerHTML=this;
while(i=a.firstChild)b.appendChild(i);
return b;
};
let game;
const startContainer = document.getElementsByClassName('start-container')[0];
const startBackground = document.getElementsByClassName('start-background')[0];
const startButton = document.getElementById('startgame');
startButton.addEventListener('click', () => {
startGame();
});
function startGame() {
startContainer.classList.remove('slideUp');
startContainer.classList.add('slideDown');
startBackground.classList.add('transparent');
game = new Game();
game.initGame();
}
function restartGame() {
game.cancelGame();
setTimeout(() => {
startContainer.classList.remove('slideDown');
startContainer.classList.add('slideUp');
startBackground.classList.remove('transparent');
game.destroy();
game = null;
}, 500);
}