This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
115 lines (90 loc) · 2.8 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
<!doctype html>
<html>
<head>
<title>socket.io</title>
<style>
* { margin: 100; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
canvas { padding: 20px; position: fixed; }
</style>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
</head>
<body>
<audio id="audiotag1" src="jump.mp3" preload="auto"></audio>
<script>
function playSound(tag) {
document.getElementById(tag).play();
}
</script>
<div onmousedown="playSound('audiotag1')">
hello
</div>
<form id="player-form" action="">
<input type="button" onclick="submitPlayer()" value="Start with me as Player 1">
</form>
<script>
var seed = Math.random();
function submitPlayer() {
var player = document.getElementById("player-form").elements[0].value;
socket.emit('setPlayer', seed);
}
</script>
<canvas id="gameCanvas" width="800" height="600" style="border:1px solid #c3c3c3;" >
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
var me = "";
// Socket
var socket = io();
socket.on('world', function(world){
render(world);
});
socket.on('setPlayer', function(remotePlayer){
console.log("remotePlayer: " + remotePlayer);
console.log("seed: " + seed);
me = seed === remotePlayer ? 'p1' : 'p2';
console.log("me: " + me);
socket.emit('action', { actor: me, action: '23' } );
});
// Canvas
var c = document.getElementById("gameCanvas");
var ctx = c.getContext("2d");
document.addEventListener('keydown', function(e) {
var action;
switch(e.keyCode) {
case 87: action = 'up'; break;
case 83: action = 'down'; break;
case 65: action = 'left'; break;
case 68: action = 'right'; break;
}
if(action) {
socket.emit('action', { actor: me, action: action } );
}
}, false);
function render(world) {
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, 800, 600);
render_player(world.p1, "#00ff00");
render_player(world.p2, "#ff00ff");
ctx.fillStyle = "#000000";
};
function render_player(player, color) {
ctx.fillStyle = color;
ctx.fillRect(player.x, player.y, 50, 30);
ctx.fillStyle = "#000000";
};
// Speed test
socket.on('speedTest', function(msg){
console.log('on server: ' + (msg.on_server - msg.sent) + ' ms');
console.log('on client: ' + (new Date().getTime() - msg.sent) + ' ms');
console.log("---");
});
document.addEventListener('keydown', function(e) {
if (e.keyCode == 88) { // x key
socket.emit('speedTest', { sent: new Date().getTime() });
}
}, false);
</script>
</body>
</html>