papercats/game-client.js

122 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-02-25 18:52:52 -05:00
/* global $ */
2017-10-30 22:24:39 -04:00
var client = require("./client");
var core = require("./game-core");
2017-02-28 02:20:32 -05:00
var io = require('socket.io-client');
2017-02-25 18:52:52 -05:00
2017-10-30 22:24:39 -04:00
var GRID_SIZE = core.GRID_SIZE;
var CELL_WIDTH = core.CELL_WIDTH;
client.allowAnimation = true;
2017-10-30 22:24:39 -04:00
client.renderer = require("./client-modes/user-mode");
2017-02-21 00:54:44 -05: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/
*/
// window.requestAnimationFrame = function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
// window.setTimeout( callback, 1000 / 60 );
// };
2017-09-04 18:59:35 -04:00
var hasWindow;
try {
window.document;
hasWindow = true;
} catch (e) {
hasWindow = false;
}
var requestAnimationFrame;
2017-09-04 13:23:12 -04:00
if ( !requestAnimationFrame ) {
requestAnimationFrame = ( function() {
2017-09-04 18:59:35 -04:00
if (hasWindow) {
2017-09-04 13:23:12 -04:00
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() {
client.connectGame('//' + window.location.hostname + ':8081', $('#name').val(), function(success, msg) {
2017-03-03 16:52:59 -05:00
if (success)
{
$("#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-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
//Event listeners
$(document).keydown(function(e) {
var newHeading = -1;
switch (e.which)
2017-02-21 00:54: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
}
client.changeHeading(newHeading);
e.preventDefault();
2017-10-30 22:24:39 -04:00
});