2017-02-25 18:52:52 -05:00
|
|
|
/* global $ */
|
2017-09-04 12:27:08 -04:00
|
|
|
var client = require("./player-client.js");
|
2017-02-25 18:52:52 -05:00
|
|
|
var consts = require("./game-consts.js");
|
2017-02-28 02:20:32 -05:00
|
|
|
var io = require('socket.io-client');
|
2017-02-25 18:52:52 -05:00
|
|
|
|
2017-02-25 21:36:44 -05:00
|
|
|
var GRID_SIZE = consts.GRID_SIZE;
|
|
|
|
var CELL_WIDTH = consts.CELL_WIDTH;
|
|
|
|
|
2017-09-04 12:27:08 -04:00
|
|
|
client.allowAnimation = true;
|
|
|
|
client.renderer = require("./game-renderer.js");
|
2017-02-25 21:36:44 -05:00
|
|
|
|
2017-02-21 00:54:44 -05:00
|
|
|
/**
|
2017-09-04 12:27:08 -04:00
|
|
|
* Provides requestAnimationFrame in a cross browser way. (edited so that this is also compatible with node.)
|
2017-02-21 00:54:44 -05:00
|
|
|
* @author paulirish / http://paulirish.com/
|
|
|
|
*/
|
2017-02-23 17:02:47 -05:00
|
|
|
// window.requestAnimationFrame = function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
|
|
|
|
// window.setTimeout( callback, 1000 / 60 );
|
|
|
|
// };
|
2017-09-04 13:23:12 -04:00
|
|
|
var window, requestAnimationFrame;
|
|
|
|
if ( !requestAnimationFrame ) {
|
|
|
|
requestAnimationFrame = ( function() {
|
|
|
|
if (window) {
|
|
|
|
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 );
|
|
|
|
};
|
|
|
|
}
|
2017-02-21 00:54:44 -05:00
|
|
|
})();
|
|
|
|
}
|
2017-02-21 17:17:06 -05:00
|
|
|
|
2017-03-02 14:02:03 -05:00
|
|
|
function run() {
|
2017-09-04 12:27:08 -04:00
|
|
|
client.connectGame('//' + window.location.hostname + ':8081', $('#name').val(), function(success, msg) {
|
2017-03-03 16:52:59 -05:00
|
|
|
if (success)
|
|
|
|
{
|
2017-03-04 07:44:27 -05:00
|
|
|
$("#begin").addClass("hidden");
|
2017-03-03 16:52:59 -05:00
|
|
|
$("#begin").animate({
|
|
|
|
opacity: 0
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var error = $("#error");
|
|
|
|
error.text(msg);
|
|
|
|
}
|
2017-03-02 14:02:03 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$(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;
|
2017-03-02 19:00:09 -05:00
|
|
|
var socket = io('http://' + window.location.hostname + ':8081', {
|
2017-03-02 14:02:03 -05:00
|
|
|
'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)");
|
2017-09-04 12:27:08 -04:00
|
|
|
});
|
2017-03-02 14:02:03 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2017-02-21 00:54:44 -05:00
|
|
|
|
2017-02-25 21:36:44 -05:00
|
|
|
//Event listeners
|
|
|
|
$(document).keydown(function(e) {
|
|
|
|
var newHeading = -1;
|
|
|
|
switch (e.which)
|
2017-02-21 00:54:44 -05:00
|
|
|
{
|
2017-02-25 21:36:44 -05:00
|
|
|
case 37: newHeading = 3; break; //LEFT
|
|
|
|
case 38: newHeading = 0; break; //UP
|
|
|
|
case 39: newHeading = 1; break; //RIGHT
|
|
|
|
case 40: newHeading = 2; break; //DOWN
|
|
|
|
default: return; //exit handler for other keys.
|
2017-02-21 00:54:44 -05:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:27:08 -04:00
|
|
|
client.changeHeading(newHeading);
|
2017-02-25 21:36:44 -05:00
|
|
|
e.preventDefault();
|
2017-09-04 12:27:08 -04:00
|
|
|
});
|