-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.mjs
193 lines (174 loc) · 5 KB
/
render.mjs
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
import {
COLOR_BLUE, COLOR_YELLOW, COLOR_RED,
KIND_PILL, KIND_VIRUS,
S,
COLORS,
} from './constants.mjs';
const rad180 = Math.PI;
const rad90 = rad180 / 2;
const rad360 = rad180 * 2;
function createCanvas([w, h], toAppend) {
const el = document.createElement('canvas');
el.setAttribute('width', S * w);
el.setAttribute('height', S * h);
if (toAppend) {
document.body.appendChild(el);
}
return el;
}
function renderBg(el, m) {
const ctx = el.getContext('2d');
const W = S * m.w;
const H = S * m.h;
ctx.clearRect(0, 0, W, H);
ctx.strokeStyle = 'white';
ctx.lineWidth = 0.5;
m.entries().forEach(([[x, y], { i }]) => {
ctx.strokeRect(S * x + 0.5, S * y + 0.5, S -1, S - 1);
});
return el;
}
function renderVirusCell(el, colorIdx) {
const R = 0.25;
const d = S * R;
const D = S * (1 - R);
const ctx = el.getContext('2d');
ctx.strokeStyle = COLORS[colorIdx];
ctx.lineCap = 'round';
ctx.lineWidth = S * 0.3;
ctx.beginPath();
ctx.moveTo(d, d);
ctx.lineTo(D, D);
ctx.moveTo(D, d);
ctx.lineTo(d, D);
ctx.stroke();
return el;
}
function renderPillCell(el, colorIdx) {
const R = 0.38;
const s2 = S/2;
const r = S * R;
const xc = s2;
const xf = S;
const yc = s2;
const yi = yc - r;
const yf = yc + r;
const ctx = el.getContext('2d');
ctx.fillStyle = COLORS[colorIdx];
ctx.beginPath();
ctx.moveTo(xc, yi);
ctx.lineTo(xf, yi);
ctx.lineTo(xf, yf);
ctx.lineTo(xc, yf);
ctx.arc(xc, yc, r, 0, rad360, false);
ctx.fill();
return el;
}
function drawRotated(ctx, sprite, [x, y], ninetyDegTimes) {
if (ninetyDegTimes === 0) {
ctx.drawImage(sprite, x, y);
return;
}
ctx.save();
ctx.translate(x + S/2, y + S/2)
ctx.rotate(ninetyDegTimes * rad90);
ctx.drawImage(sprite, -S/2, -S/2);
ctx.restore();
}
function render(el, m, p, { bg, viruses, pills }, r) {
let rr = 0;
if (r > 0.5 && m.canMoveDown) {
rr = 2 * (r - 0.5);
}
const ctx = el.getContext('2d');
ctx.clearRect(0, 0, S * m.w, S * m.h);
ctx.drawImage(bg, 0, 0);
// draw board
m.entries().forEach(([[x, y], { color, kind, rotation, leaving, falling }]) => {
x *= S;
y *= S;
ctx.globalAlpha = leaving ? 0.5 : 1;
ctx.save();
ctx.translate(0, falling ? S * rr : 0);
if (kind === KIND_VIRUS) {
ctx.drawImage(viruses[color], x, y);
} else if (kind === KIND_PILL) {
drawRotated(ctx, pills[color], [x, y], rotation);
}
ctx.restore();
});
ctx.globalAlpha = 1;
// draw current piece
ctx.save();
ctx.translate(0, S * rr);
p.m.entries().forEach(([[x_, y_], { color, kind, rotation }]) => {
const x = S * (x_ + p.pos[0]);
const y = S * (y_ + p.pos[1]);
if (kind === KIND_PILL) {
drawRotated(ctx, pills[color], [x, y], rotation);
}
});
ctx.restore();
// draw next piece
ctx.globalAlpha = 0.66;
m.nextP.m.entries().forEach(([[x_, y_], { color, kind, rotation }]) => {
const x = S * x_;
const y = S * y_;
if (kind === KIND_PILL) {
drawRotated(ctx, pills[color], [x, y], rotation);
}
});
ctx.globalAlpha = 1;
// print positions
/*ctx.font = '14px bold sans-serif';
ctx.fillStyle = '#f7f';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
m.positions().forEach(([x, y]) => {
const xx = S * (x + 0.5);
const yy = S * (y + 0.5);
ctx.fillText(`${x},${y}`, xx, yy);
});*/
// hacky hud
ctx.font = '20px sans-serif';
ctx.fillStyle = '#f7f';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
{
const label = `next: level: ${m.level + 1} viruses left: ${m.virusesLeft}`;
const xt = S * m.w / 2;
const yt = S * 0.5;
ctx.fillStyle = '#000';
ctx.fillText(label, xt+2, yt+2);
ctx.fillStyle = '#7f7';
ctx.fillText(label, xt, yt);
if (m.alertText) {
ctx.font = '15px sans-serif';
ctx.fillText(m.alertText, xt, S * m.h / 2);
}
}
}
export function setupRender(m, p) {
// prepare static textures
const sprites = {
bg: renderBg(createCanvas([m.w, m.h]), m),
viruses: [
undefined,
renderVirusCell(createCanvas([1, 1]), COLOR_BLUE),
renderVirusCell(createCanvas([1, 1]), COLOR_YELLOW),
renderVirusCell(createCanvas([1, 1]), COLOR_RED),
],
pills: [
undefined,
renderPillCell(createCanvas([1, 1]), COLOR_BLUE),
renderPillCell(createCanvas([1, 1]), COLOR_YELLOW),
renderPillCell(createCanvas([1, 1]), COLOR_RED),
],
};
const mainEl = createCanvas([m.w, m.h], true);
// setup refresh function
const refresh = (r) => {
render(mainEl, m, p, sprites, r);
}
return [mainEl, refresh];
}