forked from Overv/WebCraft
-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiplayer.html
247 lines (196 loc) · 7.54 KB
/
multiplayer.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
<!DOCTYPE HTML>
<html>
<head>
<title>WebCraft</title>
<!-- Character encoding -->
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<!-- Stylesheet -->
<link href="style/main.css" rel="stylesheet" type="text/css">
<!-- Modules -->
<script src="js/glMatrix-1.2.min.js" type="text/javascript"></script>
<script src="js/blocks.js" type="text/javascript"></script>
<script src="js/helpers.js" type="text/javascript"></script>
<script src="js/world.js" type="text/javascript"></script>
<script src="js/render.js" type="text/javascript"></script>
<script src="js/physics.js" type="text/javascript"></script>
<script src="js/player.js" type="text/javascript"></script>
<script src="js/network.js" type="text/javascript"></script>
<script type="text/javascript">
requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame
|| function(func) {
setTimeout(func, 16);
}
</script>
<!-- Load socket.io client module from Node.js server at port 3000 -->
<script type="text/javascript">
var head = document.getElementsByTagName( "head" )[0];
var script = document.createElement( "script" );
script.type = "text/javascript";
script.src = "http://" + location.host + ":3000/socket.io/socket.io.js";
head.appendChild( script );
</script>
</head>
<body oncontextmenu="return false">
<!-- Render surface -->
<canvas id="renderSurface" style="visiblity: hidden"></canvas>
<!-- Material selection -->
<table id="materialSelector" style="display:none">
<tr></tr>
</table>
<!-- Chatbox -->
<div id="chatbox" style="visibility: hidden">
<span id="chatbox_text"></span>
</div>
<input id="chatbox_entry" type="text" maxlength="100" spellcheck="false" onkeypress="onChatEnter( this, event )" style="visibility: hidden" />
<!-- Nickname box -->
<div id="nickname">
<span>Nickname:</span><br />
<input id="nickname_input" type="text" maxlength="15" spellcheck="false" onkeypress="onNicknameEnter( this, event )" />
</div>
<!-- Joining information -->
<div id="joininfo" style="visibility: hidden">
<span id="joininfo_text"></span>
</div>
<!-- Main code -->
<script type="text/javascript">
// HTML elements
var page = {};
page.renderSurface = document.getElementById( "renderSurface" );
page.materialSelector = document.getElementById( "materialSelector" );
page.chatbox = document.getElementById( "chatbox" );
page.chatboxText = document.getElementById( "chatbox_text" );
page.chatboxEntry= document.getElementById( "chatbox_entry" );
page.nickname = document.getElementById( "nickname" );
page.nicknameInput = document.getElementById( "nickname_input" );
page.joinInfo = document.getElementById( "joininfo" );
page.joinInfoText = document.getElementById( "joininfo_text" );
// Game elements
var client, render, world, player
// Focus on username field
page.nicknameInput.focus();
// Respond to username entry
function onNicknameEnter( nicknameInput, keyEvent )
{
var nickname = nicknameInput.value.trim();
if ( keyEvent.keyCode != 13 ) return;
if ( nickname.length == 0 ) return;
nicknameInput.blur();
joinGame( nickname );
}
// Respond to chat message entry
function onChatEnter( chatInput, keyEvent )
{
var msg = chatInput.value.trim();
if ( keyEvent.keyCode != 13 ) return;
chatInput.blur();
page.chatbox.style.height = null;
if ( msg.length == 0 ) return;
client.sendMessage( msg );
chatInput.value = "";
}
// Join game
function joinGame( nickname )
{
// Show join info
page.nickname.style.visibility = "hidden";
page.joinInfo.style.visibility = null;
page.joinInfoText.innerHTML = "Connecting to server...";
// Create client and attempt connection
client = new Client( io );
client.connect( "http://" + location.host + ":3000", nickname );
// Update connection status
client.on( "connect", function()
{
page.joinInfoText.innerHTML = "Receiving world...";
} );
// Initialise world
client.on( "world", function( w )
{
page.joinInfoText.innerHTML = "Building chunks...";
// Set up world
world = w;
// Set up renderer and build level chunks
render = new Renderer( "renderSurface" );
render.setWorld( world, 8 );
render.setPerspective( 60, 0.01, 200 );
// Build all world chunks
render.buildChunks( 999 );
page.joinInfoText.innerHTML = "Spawning...";
} );
// Spawn local player
client.on( "spawn", function()
{
// Set up local player
player = new Player();
player.setWorld( world );
player.setClient( client );
player.setInputCanvas( "renderSurface" );
player.setMaterialSelector( "materialSelector" );
// Handle open chat on 't' event
player.on( "openChat", function()
{
page.chatboxEntry.focus();
page.chatbox.style.height = ( render.gl.viewportHeight - 145 ) + "px";
} );
// Open game view
page.joinInfo.style.visibility = "hidden";
page.renderSurface.style.visibility = null;
page.materialSelector.style.display = null;
page.chatbox.style.visibility = null;
page.chatboxEntry.style.visibility = null;
// Render loop
var lastUpdate = +new Date() / 1000.0;
requestAnimationFrame(function loop() {
var time = +new Date() / 1000.0;
// Update local player
player.update();
// Update networked player
if ( time - lastUpdate > 0.033 ) {
client.updatePlayer();
lastUpdate = time;
}
// Build chunks
render.buildChunks( 5 );
// Draw world
render.setCamera( player.getEyePos().toArray(), player.angles );
render.draw();
requestAnimationFrame(loop, page.renderSurface);
//while ( new Date().getTime() / 1000 - time < 0.016 );
}, page.renderSurface );
} );
// Display chat messages
client.on( "chat", function( username, message )
{
page.chatboxText.innerHTML += "<<span style=\"color: #0a0\">" + username + "</span>> " + message + "<br />";
} );
client.on( "message", function( message )
{
page.chatboxText.innerHTML += "<span style=\"color: #ff8\">" + message + "</span><br />";
} );
// Handle kicking
client.on( "kick", function( message )
{
page.joinInfo.style.visibility = null;
page.renderSurface.style.visibility = "hidden";
page.materialSelector.style.display = "none";
page.chatbox.style.visibility = "hidden";
page.chatboxEntry.style.visibility = "hidden";
page.joinInfoText.innerHTML = "<span style=\"color: #f00; text-shadow: #a00 2px 2px 0px\">Kicked:</span> " + message;
} );
// Handle being disconnected
client.on( "disconnect", function( kicked )
{
if ( !kicked ) {
page.joinInfo.style.visibility = null;
page.renderSurface.style.visibility = "hidden";
page.materialSelector.style.display = "none";
page.chatbox.style.visibility = "hidden";
page.chatboxEntry.style.visibility = "hidden";
page.joinInfoText.innerHTML = "<span style=\"color: #f00; text-shadow: #a00 2px 2px 0px\">Connection Problem:</span> Lost connection to server!";
}
} );
}
</script>
</body>
</html>