This repository has been archived by the owner on Oct 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
game.html
181 lines (166 loc) · 6.07 KB
/
game.html
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
<!DOCTYPE html>
<html>
<html lang="en">
<meta charset="utf-8"/>
<title> Brainfuck Game Engine - Running </title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<style type="text/css">
@import url(https://fonts.googleapis.com/css?family=Roboto);
* {
margin: 0;
border: 0;
padding: 0;
outline: 0;
font-size: 0;
box-sizing: border-box;
vertical-align: middle;
list-style-type: none;
}
html,body {
font-family: "Roboto", sans-serif;
width: 100%;
min-width: 100%;
height: 100%;
min-height: 100%;
background-color: #0e0e0e;
overflow: hidden;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
canvas {
width: 800px;
height: 608px;
}
</style>
</head>
<body onload="init();">
<canvas id="game"></canvas>
<script type="text/javascript">
const DEBUG = false;
let canvas = document.getElementById("game");
canvas.width = 800;
canvas.height = 600;
let ctx = canvas.getContext("2d");
let tape = new Uint8Array(1e4);
let timeout;
let paused = false;
let update_code = "no code";
if("require" in window) {
require("electron").ipcRenderer.on("tape-and-code", (event, arg) => {
paused = true;
if("update" in arg && arg.update != "") {
update_code = new Function("", arg.update);
}
let i = arg.tape.length;
while(i--) tape[i] = arg.tape[i];
paused = false;
console.log("received");
});
}
let key = { u: 0, d: 0, l: 0, r: 0, a: 0, s: 0 }
let mse = { x: 0, y: 0, c: 0 }
let init = function() {
update();
}
let color = [
"#333",
"#ccc",
"#fff",
"#c33",
"#3c3",
"#33c",
""
];
let update = function() {
ctx.fillStyle = color[2];
ctx.fillRect(0, 0, canvas.width, canvas.height);
if(paused) {
ctx.fillStyle = color[0];
ctx.font = "20px Roboto";
ctx.fillText("Receiving new code", canvas.width / 2 - 45, canvas.height / 2);
} else if(update_code !== "no code") {
update_code();
ctx.fillStyle = color[0];
for (let i = 0, l = 25; i < l; i++) {
for (let j = 0, k = 19; j < k; j++) {
let current_cell = tape[i*25 + j];
if (current_cell % color.length >= 1) {
ctx.fillStyle = color[current_cell % color.length - 1];
ctx.fillRect(i * 32, j * 32, 32, 32);
}
}
}
} else {
ctx.fillStyle = color[0];
ctx.font = "20px Roboto";
ctx.fillText("Loading...", canvas.width / 2 - 45, canvas.height / 2);
}
timeout = setTimeout(update, 30);
}
let add_event = window.addEventListener
? function(obj, evtname, func) {
if(obj instanceof Array)
for(var i = 0; i < obj.length; i++)
obj[i].addEventListener(evtname, func, false);
else
obj.addEventListener(evtname, func, false);
}
: function(obj, evtname, func) {
if(obj instanceof Array)
for(var i = 0; i < obj.length; i++)
obj[i].attachEvent("on" + evtname, func);
else
obj.attachEvent("on" + evtname, func);
};
add_event(document, "keydown", function(e) {
if("isTrusted" in e && !e.isTrusted) return;
if(e.keyCode == 37 || e.which == 37 || e.key == "ArrowLeft" || e.code == "ArrowLeft")
key.l = 1;
if(e.keyCode == 38 || e.which == 38 || e.key == "ArrowUp" || e.code == "ArrowUp")
key.u = 1;
if(e.keyCode == 39 || e.which == 39 || e.key == "ArrowRight" || e.code == "ArrowRight")
key.r = 1;
if(e.keyCode == 40 || e.which == 40 || e.key == "ArrowDown" || e.code == "ArrowDown")
key.d = 1;
if(e.keyCode == 65 || e.which == 65 || e.key == "a" || e.code == "KeyA")
key.a = 1;
if(e.keyCode == 83 || e.which == 83 || e.key == "s" || e.code == "KeyS")
key.s = 1;
});
add_event(document, "keyup", function(e) {
if("isTrusted" in e && !e.isTrusted) return;
if(e.keyCode == 37 || e.which == 37 || e.key == "ArrowLeft" || e.code == "ArrowLeft")
key.l = 0;
if(e.keyCode == 38 || e.which == 38 || e.key == "ArrowUp" || e.code == "ArrowUp")
key.u = 0;
if(e.keyCode == 39 || e.which == 39 || e.key == "ArrowRight" || e.code == "ArrowRight")
key.r = 0;
if(e.keyCode == 40 || e.which == 40 || e.key == "ArrowDown" || e.code == "ArrowDown")
key.d = 0;
if(e.keyCode == 65 || e.which == 65 || e.key == "a" || e.code == "KeyA")
key.a = 0;
if(e.keyCode == 83 || e.which == 83 || e.key == "s" || e.code == "KeyS")
key.s = 0;
});
add_event(canvas, "mousemove", function(e) {
if("isTrusted" in e && !e.isTrusted) return;
mse.x = ~~(e.offsetX/32);
mse.y = ~~(e.offsetY/32);
if(DEBUG) console.log(mse.x, mse.y, mse.x*25+mse.y);
});
add_event(canvas, "mousedown", function(e) {
if("isTrusted" in e && !e.isTrusted) return;
mse.c = 1;
if(DEBUG) console.log("PRESSED");
});
add_event(canvas, "mouseup", function(e) {
if("isTrusted" in e && !e.isTrusted) return;
mse.c = 0;
if(DEBUG) console.log("UNPRESSED");
});
</script>
</body>
</html>