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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
<!doctype html>
<title>LDJam 57</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width">
<style>
html, body {
background: #222222;
font-family: sans-serif;
height: 100%;
margin: 0;
overflow: hidden;
text-align: center;
width: 100%;
color: white;
}
h1 {
margin: 0;
}
button {
font-size: 150%;
height: 8%;
margin: 0.3em;
width: 25%;
}
canvas {
margin: auto;
}
</style>
<div id="tutorial" style="overflow-y: scroll; height: 90vh">
<h1>Depths of the Mind</h1>
<p>
The mind is an intricate thing, with many tiny components working together
in perfect harmony... Until something came in and messed everything up!
</p>
<p>
If you want to set all the pieces of the mind right, you'll need to know how
it works!
</p>
<img src=
#include "../build/tutorial_black_white.png.b64"
>
<p>
The structure of the brain consists of black blocks filling a white void.
</p>
<img src=
#include "../build/tutorial_red_yellow.png.b64"
>
<p>
Black blocks will turn red when clicked (if you have red in your inventory on the left).
While the brain is running, black blocks next to red blocks will turn red!
Red blocks will eventually tire to yellow, and then back to black. They don't stay red very long!
This is what allows the brain to send signals along a series of black blocks.
Yellow blocks can be placed with a right click on a black block (if you have one in your inventory)
</p>
<img src=
#include "../build/tutorial_direction_red.png.b64"
>
<p>
Sometimes, only one side of a block turns red.
When this happens, it will try to move in that direction (and leave a yellow behind it)
but if it cannot it will turn clockwise and try again.
</p>
<span>
<img src=
#include "../build/tutorial_blue_1.png.b64"
>
<img src=
#include "../build/tutorial_blue_2.png.b64"
>
<p>
Blue blocks need to be moved around to keep things running smoothly!
When a blue block is touched by a single red block, it will start rolling
away from it.
Each level has a goal in faint red which needs at least one blue block to be delivered to it.
Some levels will also have faint blue areas that create new blue blocks for you!
</p>
<img src=
#include "../build/tutorial_blue_clone.png.b64"
>
<p>
Finally, if a blue block touches a red block <em>while in motion</em> it will be cloned!
One will be left behind and the other will continue.
</p>
<button id="play-button">Play!</button>
</div>
<canvas style="display: none;"></canvas>
<script>
// Mirror these in src/all.c
const INPUT_NONE = 0;
const INPUT_CLICK = 1;
const INPUT_RCLICK = 2;
const INPUT_PAUSE_PLAY = 3;
const INPUT_MOVE = 4;
const INPUT_RESTART = 5;
const WASM =
#include "../build/main.wasm.b64"
const MUSIC =
#include "../build/music.mp3.b64"
const IMAGES = [
#include "../build/continue.png.b64"
,
#include "../build/exit.png.b64"
,
#include "../build/pause.png.b64"
,
#include "../build/play.png.b64"
,
#include "../build/restart.png.b64"
,
#include "../build/nelson.png.b64"
,
#include "../build/nelson_left.png.b64"
,
#include "../build/nelson_up.png.b64"
,
#include "../build/nelson_right.png.b64"
,
#include "../build/nelson_down.png.b64"
,
];
async function main() {
let bytes = Uint8Array.from(atob(WASM), function(c) {
return c.charCodeAt(0);
});
let module = await WebAssembly.compile(bytes);
let wasm = await WebAssembly.instantiate(module);
let exports = wasm.exports;
let html = document.querySelector("html");
let canvas = document.querySelector("canvas");
const tutorial = document.querySelector("#tutorial");
let ctx = canvas.getContext("2d");
let memory = exports.memory;
let mousex = 0;
let mousey = 0;
const audio = new Audio();
audio.src = MUSIC;
audio.volume = 0.2;
audio.loop = true;
let musicPlaying = false;
let images = [null];
for (let i = 0; i < IMAGES.length; i++) {
const image = new Image();
image.src = IMAGES[i];
images.push(image);
}
const start = Date.now();
function now() {
return Date.now() - start;
}
function min(a, b) {
return b<a ? b : a;
}
function render() {
if (64 + html.clientHeight * (20 / 16) > html.clientWidth) {
canvas.width = html.clientWidth;
canvas.height = (html.clientWidth - 64) * (16 / 20);
} else {
canvas.width = 64 + html.clientHeight * (20 / 16);
canvas.height = html.clientHeight;
}
let width = canvas.width;
let height = canvas.height;
let ptr = exports.game_render(width, height, mousex, mousey);
let dl = new Int32Array(memory.buffer, ptr);
let len = dl[0];
let ops = dl.subarray(1);
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, width, height);
for (let i = 0; i < len; i++) {
let op = ops.subarray(7*i, 7*i+7);
const color = new Uint8Array(new Uint32Array(op.subarray(4, 6)).buffer);
ctx.fillStyle = `#${color[0].toString(16).padStart(2, "0")}${color[1].toString(16).padStart(2, "0")}${color[2].toString(16).padStart(2, "0")}`;
ctx.globalAlpha = color[3] / 255;
ctx.fillRect(op[0], op[1], op[2], op[3]);
ctx.strokeStyle = `#${color[4].toString(16).padStart(2, "0")}${color[5].toString(16).padStart(2, "0")}${color[6].toString(16).padStart(2, "0")}`;
ctx.globalAlpha = color[7] / 255;
ctx.strokeRect(op[0], op[1], op[2], op[3]);
const image = new Uint8Array(new Uint32Array(op.subarray(6, 7)).buffer);
if (image[0] !== 0) {
console.log(image);
ctx.globalAlpha = image[1] / 255;
ctx.drawImage(images[image[0]], op[0], op[1], op[2], op[3]);
}
}
}
function onresize() { /*html.style.maxWidth = `${max_width()}px`; */}
window.addEventListener("resize", onresize);
onresize();
function doUpdate(input) {
if (exports.game_update(input, mousex, mousey, now()) !== 0) {
canvas.style.display = "none";
tutorial.style.display = "block";
}
}
canvas.addEventListener("mousemove", function(e) {
const rect = e.target.getBoundingClientRect();
mousex = e.clientX - rect.left;
mousey = e.clientY - rect.top;
doUpdate(INPUT_MOVE);
});
canvas.addEventListener("mousedown", function(e) {
const rect = e.target.getBoundingClientRect();
mousex = e.clientX - rect.left;
mousey = e.clientY - rect.top;
if (e.button == 0) {
doUpdate(INPUT_CLICK);
} else if (e.button == 2) {
e.preventDefault();
doUpdate(INPUT_RCLICK);
}
});
document.getElementById("play-button").addEventListener("click", function (e) {
if (!musicPlaying) {
musicPlaying = true;
audio.play();
}
canvas.style.display = "block";
tutorial.style.display = "none";
});
canvas.addEventListener("contextmenu", function (e) {
e.preventDefault();
});
document.addEventListener("keydown", function (e) {
if (e.key === " ") {
doUpdate(INPUT_PAUSE_PLAY);
} else if (e.key == "r") {
doUpdate(INPUT_RESTART);
}
});
function animate() {
// TODO: stop requesting frames when state is static
requestAnimationFrame(animate);
doUpdate(INPUT_NONE);
render();
}
requestAnimationFrame(animate);
exports.game_init();
}
main()
</script>
|