-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.js
172 lines (156 loc) · 5.22 KB
/
board.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
function clamp(num, min, max) {
return num <= min ? min : num >= max ? max : num;
}
class Board {
constructor(width, height) {
this.width = width;
this.height = height;
this.cells = new Array(this.width * this.height);
for (let i = 0; i < this.cells.length; ++i) {
this.cells[i] = new Cell();
}
this.player = {x: 16, y: 8};
this.playerDirection = {x: 0, y: 0};
}
warpPlayer(pos) {
this.player = pos;
}
movePlayer(playerVelocity) {
if (playerVelocity.x != 0 || playerVelocity.y != 0) {
this.playerDirection = playerVelocity
}
const newPos = {
x: clamp(this.player.x + playerVelocity.x, 0, this.width - 1),
y: clamp(this.player.y + playerVelocity.y, 0, this.height - 1)
};
const newCell = this.at(newPos.x, newPos.y);
if (!blocksPlayer(newCell.type)) {
if (newCell.type == CellType.END) {
gameState = State.WON;
}
this.set(this.player.x, this.player.y, new Cell(false, CellType.NORMAL));
this.player = newPos;
this.set(this.player.x, this.player.y, new Cell(true, CellType.PLAYER));
}
}
at(i, j) {
return this.cells[i * this.height + j];
}
setType(i, j, t) {
this.cells[i * this.height + j].type = t;
}
set(i, j, c) {
this.cells[i * this.height + j] = c;
}
update() {
for (let i = 0; i < this.width; ++i) {
for (let j = 0; j < this.height; ++j) {
this._updateNeighborCounts(i, j);
}
}
for (let i = 0; i < this.width; ++i) {
for (let j = 0; j < this.height; ++j) {
this.cells[i * this.height + j].update();
}
}
}
_updateNeighborCounts(i, j) {
this.cells[i * this.height + j].lastRoundNeighborCount = -this.at(i, j).on;
for (let ii = Math.max(0, i - 1); ii <= Math.min(this.width - 1, i + 1);
++ii) {
for (let jj = Math.max(0, j - 1); jj <= Math.min(this.height - 1, j + 1);
++jj) {
if (this.at(ii, jj).on) {
++this.cells[i * this.height + j].lastRoundNeighborCount;
}
}
}
}
draw(ctx) {
// Draw dead normal cells first.
for (let i = 0; i < this.width; ++i) {
for (let j = 0; j < this.height; ++j) {
const cell = this.at(i, j);
if (!cell.on && cell.type == CellType.NORMAL) {
cell.draw(ctx, i, j);
}
}
}
for (let i = 0; i < this.width; ++i) {
for (let j = 0; j < this.height; ++j) {
const cell = this.at(i, j);
if (!cell.on && cell.type == CellType.NORMAL) continue;
cell.draw(ctx, i, j);
if (cell.type == CellType.LOST) {
let progress = 0;
const animate = () => {
if (progress >= 1) return;
progress += 0.03;
ctx.overlay.clearRect(0, 0, htmlCanvas.width, htmlCanvas.height);
ctx.overlay.strokeStyle = 'red';
ctx.overlay.lineWidth = 3;
ctx.overlay.beginPath();
const x = i * ctx.cellSize + ctx.cellSize / 2;
const y = j * ctx.cellSize + ctx.cellSize / 2;
const radius = (1.5 * (1.1 - progress)) * ctx.cellSize;
ctx.overlay.arc(x, y, radius, 0, 2 * Math.PI, true);
ctx.overlay.stroke();
window.requestAnimationFrame(animate);
};
window.requestAnimationFrame(animate);
}
let fontSize = 30
ctx.canvas.font = fontSize.toString() + 'px Arial';
ctx.canvas.textAlign = 'center';
ctx.canvas.fillStyle = 'rgba(255, 255, 255, 0.02)';
switch (gameState) {
case State.WON:
ctx.canvas.beginPath();
ctx.canvas.rect(
htmlCanvas.width * 0.25, htmlCanvas.height * 0.4,
htmlCanvas.width * 0.5, htmlCanvas.height * 0.2);
ctx.canvas.fill();
ctx.canvas.fillStyle = 'black';
ctx.canvas.fillText(
'You kept it alive in level ' + currentLevelIndex + '.', //
htmlCanvas.width / 2, htmlCanvas.height / 2);
ctx.canvas.fillText(
'Press \'n\' to begin the next level.', //
htmlCanvas.width / 2, htmlCanvas.height / 2 + 30);
break;
case State.LOST:
ctx.canvas.beginPath();
ctx.canvas.rect(
htmlCanvas.width * 0.25, htmlCanvas.height * 0.4,
htmlCanvas.width * 0.5, htmlCanvas.height * 0.2);
ctx.canvas.fill();
ctx.canvas.fillStyle = 'black';
ctx.canvas.fillText(
'You failed to keep it alive.', //
htmlCanvas.width / 2, htmlCanvas.height / 2);
ctx.canvas.fillText(
'Press \'n\' to retry.', //
htmlCanvas.width / 2, htmlCanvas.height / 2 + 30);
break;
}
}
}
const mid = ctx.cellSize / 2
let xPos = this.player.x * ctx.cellSize + mid;
let yPos = this.player.y * ctx.cellSize + mid;
let xDir = mid * this.playerDirection.x
let yDir = mid * this.playerDirection.y
if (xDir == 0) {
xDir = 4;
xPos -= 2;
}
if (yDir == 0) {
yDir = 4;
yPos -= 2;
}
ctx.canvas.fillStyle = 'white';
ctx.canvas.beginPath();
ctx.canvas.rect(xPos, yPos, xDir, yDir);
ctx.canvas.fill();
}
}