-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathgame-client.js
128 lines (113 loc) · 3.48 KB
/
game-client.js
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
/* global $ */
var client = require("./client");
var core = require("./game-core");
var io = require('socket.io-client');
var GRID_SIZE = core.GRID_SIZE;
var CELL_WIDTH = core.CELL_WIDTH;
client.allowAnimation = true;
client.renderer = require("./client-modes/user-mode");
/**
* Provides requestAnimationFrame in a cross browser way. (edited so that this is also compatible with node.)
* @author paulirish / http://paulirish.com/
*/
// window.requestAnimationFrame = function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
// window.setTimeout( callback, 1000 / 60 );
// };
var hasWindow;
try {
window.document;
hasWindow = true;
} catch (e) {
hasWindow = false;
}
var requestAnimationFrame;
if ( !requestAnimationFrame ) {
requestAnimationFrame = ( function() {
if (hasWindow) {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
setTimeout( callback, 1000 / 60 );
};
} else {
return function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
setTimeout( callback, 1000 / 60 );
};
}
})();
}
function run() {
client.connectGame('//' + window.location.hostname + ':8081', $('#name').val(), function(success, msg) {
if (success)
{
$("#begin").addClass("hidden");
$("#begin").animate({
opacity: 0
}, 1000);
}
else
{
var error = $("#error");
error.text(msg);
}
});
}
$(function() {
var error = $("#error");
if (!window.WebSocket)
{
error.text("Your browser does not support WebSockets!");
return;
}
error.text("Loading..."); //TODO: show loading screen.
var success = false;
var socket = io('http://' + window.location.hostname + ':8081', {
'forceNew': true,
upgrade: false,
transports: ['websocket']
});
socket.on('connect_error', function() {
if (!success)
error.text("Cannot connect with server. This probably is due to misconfigured proxy server. (Try using a different browser)");
});
socket.emit("checkConn", function() {
success = true;
socket.disconnect();
});
setTimeout(function() {
if (!success)
error.text("Cannot connect with server. This probably is due to misconfigured proxy server. (Try using a different browser)");
else
{
error.text("");
$("input").keypress(function(evt) {
if (evt.which === 13)
requestAnimationFrame(run);
});
$("button").click(function(evt) {
requestAnimationFrame(run);
});
}
}, 2000);
});
//Event listeners
$(document).keydown(function(e) {
var newHeading = -1;
switch (e.which)
{
case 37: newHeading = 3; break; //LEFT
case 65: newHeading = 3; break; //LEFT (A)
case 38: newHeading = 0; break; //UP
case 87: newHeading = 0; break; //UP (W)
case 39: newHeading = 1; break; //RIGHT
case 68: newHeading = 1; break; //RIGHT (D)
case 40: newHeading = 2; break; //DOWN
case 83: newHeading = 2; break; //DOWN (S)
default: return; //exit handler for other keys.
}
client.changeHeading(newHeading);
e.preventDefault();
});