-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
376 lines (353 loc) · 12.9 KB
/
index.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Game</title>
<style type="text/css">
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
body {
overflow: hidden;
}
</style>
</head>
<body>
<canvas id="viewport"></canvas>
<script type="text/javascript">
var Stats=function(){function s(a,g,d){var f,c,e;for(c=0;c<30;c++)for(f=0;f<73;f++)e=(f+c*74)*4,a[e]=a[e+4],a[e+1]=a[e+5],a[e+2]=a[e+6];for(c=0;c<30;c++)e=(73+c*74)*4,c<g?(a[e]=b[d].bg.r,a[e+1]=b[d].bg.g,a[e+2]=b[d].bg.b):(a[e]=b[d].fg.r,a[e+1]=b[d].fg.g,a[e+2]=b[d].fg.b)}var r=0,t=2,g,u=0,j=(new Date).getTime(),F=j,v=j,l=0,w=1E3,x=0,k,d,a,m,y,n=0,z=1E3,A=0,f,c,o,B,p=0,C=1E3,D=0,h,i,q,E,b={fps:{bg:{r:16,g:16,b:48},fg:{r:0,g:255,b:255}},ms:{bg:{r:16,g:48,b:16},fg:{r:0,g:255,b:0}},mb:{bg:{r:48,g:16,
b:26},fg:{r:255,g:0,b:128}}};g=document.createElement("div");g.style.cursor="pointer";g.style.width="80px";g.style.opacity="0.9";g.style.zIndex="10001";g.addEventListener("click",function(){r++;r==t&&(r=0);k.style.display="none";f.style.display="none";h.style.display="none";switch(r){case 0:k.style.display="block";break;case 1:f.style.display="block";break;case 2:h.style.display="block"}},!1);k=document.createElement("div");k.style.backgroundColor="rgb("+Math.floor(b.fps.bg.r/2)+","+Math.floor(b.fps.bg.g/
2)+","+Math.floor(b.fps.bg.b/2)+")";k.style.padding="2px 0px 3px 0px";g.appendChild(k);d=document.createElement("div");d.style.fontFamily="Helvetica, Arial, sans-serif";d.style.textAlign="left";d.style.fontSize="9px";d.style.color="rgb("+b.fps.fg.r+","+b.fps.fg.g+","+b.fps.fg.b+")";d.style.margin="0px 0px 1px 3px";d.innerHTML='<span style="font-weight:bold">FPS</span>';k.appendChild(d);a=document.createElement("canvas");a.width=74;a.height=30;a.style.display="block";a.style.marginLeft="3px";k.appendChild(a);
m=a.getContext("2d");m.fillStyle="rgb("+b.fps.bg.r+","+b.fps.bg.g+","+b.fps.bg.b+")";m.fillRect(0,0,a.width,a.height);y=m.getImageData(0,0,a.width,a.height);f=document.createElement("div");f.style.backgroundColor="rgb("+Math.floor(b.ms.bg.r/2)+","+Math.floor(b.ms.bg.g/2)+","+Math.floor(b.ms.bg.b/2)+")";f.style.padding="2px 0px 3px 0px";f.style.display="none";g.appendChild(f);c=document.createElement("div");c.style.fontFamily="Helvetica, Arial, sans-serif";c.style.textAlign="left";c.style.fontSize=
"9px";c.style.color="rgb("+b.ms.fg.r+","+b.ms.fg.g+","+b.ms.fg.b+")";c.style.margin="0px 0px 1px 3px";c.innerHTML='<span style="font-weight:bold">MS</span>';f.appendChild(c);a=document.createElement("canvas");a.width=74;a.height=30;a.style.display="block";a.style.marginLeft="3px";f.appendChild(a);o=a.getContext("2d");o.fillStyle="rgb("+b.ms.bg.r+","+b.ms.bg.g+","+b.ms.bg.b+")";o.fillRect(0,0,a.width,a.height);B=o.getImageData(0,0,a.width,a.height);try{performance&&performance.memory&&performance.memory.totalJSHeapSize&&
(t=3)}catch(G){}h=document.createElement("div");h.style.backgroundColor="rgb("+Math.floor(b.mb.bg.r/2)+","+Math.floor(b.mb.bg.g/2)+","+Math.floor(b.mb.bg.b/2)+")";h.style.padding="2px 0px 3px 0px";h.style.display="none";g.appendChild(h);i=document.createElement("div");i.style.fontFamily="Helvetica, Arial, sans-serif";i.style.textAlign="left";i.style.fontSize="9px";i.style.color="rgb("+b.mb.fg.r+","+b.mb.fg.g+","+b.mb.fg.b+")";i.style.margin="0px 0px 1px 3px";i.innerHTML='<span style="font-weight:bold">MB</span>';
h.appendChild(i);a=document.createElement("canvas");a.width=74;a.height=30;a.style.display="block";a.style.marginLeft="3px";h.appendChild(a);q=a.getContext("2d");q.fillStyle="#301010";q.fillRect(0,0,a.width,a.height);E=q.getImageData(0,0,a.width,a.height);return{domElement:g,update:function(){u++;j=(new Date).getTime();n=j-F;z=Math.min(z,n);A=Math.max(A,n);s(B.data,Math.min(30,30-n/200*30),"ms");c.innerHTML='<span style="font-weight:bold">'+n+" MS</span> ("+z+"-"+A+")";o.putImageData(B,0,0);F=j;if(j>
v+1E3){l=Math.round(u*1E3/(j-v));w=Math.min(w,l);x=Math.max(x,l);s(y.data,Math.min(30,30-l/100*30),"fps");d.innerHTML='<span style="font-weight:bold">'+l+" FPS</span> ("+w+"-"+x+")";m.putImageData(y,0,0);if(t==3)p=performance.memory.usedJSHeapSize*9.54E-7,C=Math.min(C,p),D=Math.max(D,p),s(E.data,Math.min(30,30-p/2),"mb"),i.innerHTML='<span style="font-weight:bold">'+Math.round(p)+" MB</span> ("+Math.round(C)+"-"+Math.round(D)+")",q.putImageData(E,0,0);v=j;u=0}}}};
</script>
<script type="text/javascript">
/* --------------- G A M E --------------- */
var Game = function() {
var stopped = false,
fps = 60,
canvas = document.getElementById('viewport'),
ctx = canvas.getContext('2d'),
entities = [],
skipTicks = 1000/fps,
nextGameTick = (new Date()).getTime(),
drawStats = new Stats(),
updateStats = new Stats();
var run = function() {
var loops = 0;
while ((new Date()).getTime() > nextGameTick) {
Game.update();
nextGameTick += skipTicks;
loops++;
}
if (loops) {
Game.draw();
}
};
var draw = function() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < entities.length; i++) {
entities[i].draw();
}
drawStats.update();
};
var update = function() {
for (var i = 0; i < entities.length; i++) {
entities[i].update();
}
updateStats.update();
};
var initialize = function() {
updateStats.domElement.style.position = 'absolute';
updateStats.domElement.style.left = '0px';
updateStats.domElement.style.top = '0px';
drawStats.domElement.style.position = 'absolute';
drawStats.domElement.style.left = '200px';
drawStats.domElement.style.top = '0px';
document.body.appendChild(updateStats.domElement);
document.body.appendChild(drawStats.domElement);
resize();
window.addEventListener("resize", resize, false);
entities.push(new Ship());
};
var resize = function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
};
return {
run: run,
draw: draw,
update: update,
entities: entities,
initialize: initialize,
resize: resize,
ctx: ctx
};
}();
/* --------------- P L A Y E R --------------- */
var Player = function() {
var input = {left: false, right: false, up: false, down: false};
var onKeydown = function(e) {
var c = e.keyCode;
switch(c) {
case 37: input.left = true; break;
case 65: input.left = true; break;
case 38: input.up = true; break;
case 87: input.up = true; break;
case 39: input.right = true; break;
case 68: input.right = true; break;
case 40: input.down = true; break;
case 83: input.down = true; break;
}
};
var onKeyup = function(e) {
var c = e.keyCode;
switch(c) {
case 37: input.left = false; break;
case 65: input.left = false; break;
case 38: input.up = false; break;
case 87: input.up = false; break;
case 39: input.right = false; break;
case 68: input.right = false; break;
case 40: input.down = false; break;
case 83: input.down = false; break;
};
};
window.addEventListener('keydown', onKeydown, false);
window.addEventListener('keyup', onKeyup, false);
return {
input: input,
onKeydown: onKeydown,
onKeyup: onKeyup
};
}();
/* --------------- S H I P --------------- */
var Ship = function() {
var x = 0,
y = 0,
vel = 5;
var draw = function() {
Game.ctx.fillRect(x, y, 30, 30);
};
var update = function() {
var v;
if ((Player.input.up || Player.input.down) && (Player.input.left || Player.input.right)) {
v = vel/1.414;
} else {
v = vel;
}
if (Player.input.up) { y -= v; }
if (Player.input.down) { y += v; }
if (Player.input.left) { x -= v; }
if (Player.input.right) { x += v; }
};
return {
draw: draw,
update: update
};
};
/* --------------- S Y S T E M ---------------*/
(function() {
var onEachFrame;
window.requestAnimationFrame = window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame;
if (window.requestAnimationFrame) {
onEachFrame = function(cb) {
var _cb = function() { cb(); window.requestAnimationFrame(_cb); };
_cb();
};
} else {
onEachFrame = function(cb) {
setInterval(cb, 1000/Game.fps);
};
}
window.onEachFrame = onEachFrame;
})();
Game.initialize();
window.onEachFrame(Game.run);
/* Game = ->
stopped = false
fps = 60
canvas = document.getElementById("viewport")
ctx = canvas.getContext("2d")
entities = []
skipTicks = 1000 / fps
nextGameTick = (new Date()).getTime()
drawStats = new Stats()
updateStats = new Stats()
run = ->
loops = 0
while (new Date()).getTime() > nextGameTick
Game.update()
nextGameTick += skipTicks
loops++
Game.draw() if loops
draw = ->
ctx.clearRect 0, 0, canvas.width, canvas.height
i = 0
while i < entities.length
entities[i].draw()
i++
drawStats.update()
update = ->
i = 0
while i < entities.length
entities[i].update()
i++
updateStats.update()
initialize = ->
updateStats.domElement.style.position = "absolute"
updateStats.domElement.style.left = "0px"
updateStats.domElement.style.top = "0px"
drawStats.domElement.style.position = "absolute"
drawStats.domElement.style.left = "200px"
drawStats.domElement.style.top = "0px"
document.body.appendChild updateStats.domElement
document.body.appendChild drawStats.domElement
resize()
window.addEventListener "resize", resize, false
entities.push new Ship()
resize = ->
canvas.width = window.innerWidth
canvas.height = window.innerHeight
run: run
draw: draw
update: update
entities: entities
initialize: initialize
resize: resize
ctx: ctx
()
Player = ->
input =
left: false
right: false
up: false
down: false
onKeydown = (e) ->
c = e.keyCode
switch c
when 37
input.left = true
when 65
input.left = true
when 38
input.up = true
when 87
input.up = true
when 39
input.right = true
when 68
input.right = true
when 40
input.down = true
when 83
input.down = true
onKeyup = (e) ->
c = e.keyCode
switch c
when 37
input.left = false
when 65
input.left = false
when 38
input.up = false
when 87
input.up = false
when 39
input.right = false
when 68
input.right = false
when 40
input.down = false
when 83
input.down = false
window.addEventListener "keydown", onKeydown, false
window.addEventListener "keyup", onKeyup, false
input: input
onKeydown: onKeydown
onKeyup: onKeyup
()
Ship = ->
x = 0
y = 0
vel = 5
draw = ->
Game.ctx.fillRect x, y, 30, 30
update = ->
v = undefined
if (Player.input.up or Player.input.down) and (Player.input.left or Player.input.right)
v = vel / 1.414
else
v = vel
y -= v if Player.input.up
y += v if Player.input.down
x -= v if Player.input.left
x += v if Player.input.right
draw: draw
update: update
(->
onEachFrame = undefined
window.requestAnimationFrame = window.webkitRequestAnimationFrame or window.mozRequestAnimationFrame or window.msRequestAnimationFrame
if window.requestAnimationFrame
onEachFrame = (cb) ->
_cb = ->
cb()
window.requestAnimationFrame _cb
_cb()
else
onEachFrame = (cb) ->
setInterval cb, 1000 / Game.fps
window.onEachFrame = onEachFrame
)()
Game.initialize()
window.onEachFrame Game.run */
</script>
</body>
</html>