2017-02-23 23:10:36 -05:00
|
|
|
var Player = require("./player.js");
|
|
|
|
var Grid = require("./grid.js");
|
2017-02-24 19:57:13 -05:00
|
|
|
var Color = require("./color.js");
|
2017-02-23 23:10:36 -05:00
|
|
|
|
2017-02-21 00:54:44 -05:00
|
|
|
/**
|
|
|
|
* Provides requestAnimationFrame in a cross browser way.
|
|
|
|
* @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-02-21 00:54:44 -05:00
|
|
|
if ( !window.requestAnimationFrame ) {
|
|
|
|
window.requestAnimationFrame = ( function() {
|
|
|
|
return window.webkitRequestAnimationFrame ||
|
|
|
|
window.mozRequestAnimationFrame ||
|
|
|
|
window.oRequestAnimationFrame ||
|
|
|
|
window.msRequestAnimationFrame ||
|
|
|
|
function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
|
|
|
|
window.setTimeout( callback, 1000 / 60 );
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
}
|
2017-02-21 17:17:06 -05:00
|
|
|
|
2017-02-21 00:54:44 -05:00
|
|
|
|
|
|
|
$(function() {
|
2017-02-22 22:36:29 -05:00
|
|
|
var BORDER_WIDTH = 20;
|
2017-02-23 17:02:47 -05:00
|
|
|
var GRID_SIZE = 80;
|
|
|
|
var CELL_WIDTH = 40;
|
2017-02-21 00:54:44 -05:00
|
|
|
var SPEED = 5;
|
|
|
|
var SHADOW_OFFSET = 5;
|
2017-02-23 17:02:47 -05:00
|
|
|
var ANIMATE_FRAMES = 24;
|
|
|
|
var BOUNCE_FRAMES = [8, 4];
|
|
|
|
var DROP_HEIGHT = 24;
|
2017-02-22 19:51:41 -05:00
|
|
|
var DROP_SPEED = 2;
|
2017-02-23 17:02:47 -05:00
|
|
|
var MIN_BAR_WIDTH = 65;
|
|
|
|
var BAR_HEIGHT = SHADOW_OFFSET + CELL_WIDTH;
|
|
|
|
var BAR_WIDTH = 400;
|
2017-02-21 00:54:44 -05:00
|
|
|
var canvas = $("#main-ui")[0];
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
|
|
|
2017-02-23 17:02:47 -05:00
|
|
|
var canvasWidth = canvas.width = window.innerWidth;
|
|
|
|
var canvasHeight = canvas.height = window.innerHeight - 20;
|
|
|
|
canvas.style.marginTop = 20 / 2;
|
|
|
|
|
|
|
|
var gameWidth = canvasWidth;
|
|
|
|
var gameHeight = canvasHeight - BAR_HEIGHT;
|
2017-02-22 19:51:41 -05:00
|
|
|
|
2017-02-23 17:02:47 -05:00
|
|
|
var newPlayerFrames = [];
|
|
|
|
var playerPortion = [];
|
|
|
|
var allPlayers = [];
|
2017-02-21 00:54:44 -05:00
|
|
|
var players = [];
|
2017-02-23 17:02:47 -05:00
|
|
|
var animateOff = false;
|
2017-02-22 19:51:41 -05:00
|
|
|
var animateGrid = new Grid(GRID_SIZE);
|
|
|
|
var grid = new Grid(GRID_SIZE, function(row, col, before, after) {
|
2017-02-23 17:02:47 -05:00
|
|
|
//Keep track of areas.
|
|
|
|
if (before)
|
|
|
|
playerPortion[before.num]--;
|
|
|
|
if (after)
|
|
|
|
playerPortion[after.num]++;
|
|
|
|
|
|
|
|
//Queue animation
|
2017-02-22 19:51:41 -05:00
|
|
|
if (before === after || animateOff)
|
|
|
|
return;
|
|
|
|
animateGrid.set(row, col, {
|
|
|
|
before: before,
|
|
|
|
after: after,
|
|
|
|
frame: 0
|
|
|
|
});
|
|
|
|
});
|
2017-02-21 00:54:44 -05:00
|
|
|
|
|
|
|
//Load players.
|
2017-02-22 19:51:41 -05:00
|
|
|
for (var p = 0; p < 9; p++)
|
2017-02-21 00:54:44 -05:00
|
|
|
{
|
|
|
|
//TODO: socket loading.
|
2017-02-23 17:02:47 -05:00
|
|
|
var pRow = getRandomInt(0, GRID_SIZE);
|
|
|
|
var pCol = getRandomInt(0, GRID_SIZE);
|
2017-02-23 23:10:36 -05:00
|
|
|
var sdata = {
|
|
|
|
posX: pCol * CELL_WIDTH,
|
|
|
|
posY: pRow * CELL_WIDTH,
|
|
|
|
currentHeading: getRandomInt(0, 4),
|
|
|
|
//name: ...,
|
|
|
|
num: p
|
|
|
|
}
|
2017-02-23 17:02:47 -05:00
|
|
|
|
|
|
|
playerPortion[p] = 0;
|
2017-02-23 23:10:36 -05:00
|
|
|
allPlayers[p] = players[p] = new Player(true, grid, sdata);
|
2017-02-23 17:02:47 -05:00
|
|
|
|
|
|
|
for (var dr = -1; dr <= 1; dr++)
|
|
|
|
for (var dc = -1; dc <= 1; dc++)
|
|
|
|
if (!grid.isOutOfBounds(dr + pRow, dc + pCol))
|
|
|
|
grid.set(dr + pRow, dc + pCol, players[p]);
|
2017-02-21 00:54:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//Load grid.
|
2017-02-22 19:51:41 -05:00
|
|
|
for (var r = 0; r < grid.size; r++)
|
2017-02-21 00:54:44 -05:00
|
|
|
{
|
2017-02-22 19:51:41 -05:00
|
|
|
for (var c = 0; c < grid.size; c++)
|
2017-02-21 00:54:44 -05:00
|
|
|
{
|
|
|
|
//TODO: load data.
|
2017-02-23 17:02:47 -05:00
|
|
|
//if (Math.random() > .9)
|
|
|
|
// grid.set(r, c, players[getRandomInt(0, players.length)]);
|
2017-02-21 00:54:44 -05:00
|
|
|
}
|
|
|
|
}
|
2017-02-22 19:51:41 -05:00
|
|
|
animateOff = false;
|
2017-02-21 00:54:44 -05:00
|
|
|
|
|
|
|
var frameCount = 0;
|
|
|
|
var animateTo = [0, 0];
|
|
|
|
var offset = [0, 0];
|
|
|
|
|
2017-02-23 17:02:47 -05:00
|
|
|
var userPortion = 0;
|
|
|
|
var lagPortion = 0;
|
|
|
|
var portionSpeed = 0;
|
2017-02-24 19:57:13 -05:00
|
|
|
var zoom = 1;
|
2017-02-23 17:02:47 -05:00
|
|
|
|
2017-02-22 01:34:59 -05:00
|
|
|
//TODO: current player index
|
|
|
|
var user = players[0];
|
2017-02-23 17:02:47 -05:00
|
|
|
|
2017-02-22 19:51:41 -05:00
|
|
|
centerOnPlayer(user, offset);
|
2017-02-21 00:54:44 -05:00
|
|
|
|
|
|
|
function update()
|
|
|
|
{
|
|
|
|
//Change grid offsets.
|
2017-02-22 01:34:59 -05:00
|
|
|
for (var i = 0; i <= 1; i++)
|
2017-02-21 00:54:44 -05:00
|
|
|
{
|
|
|
|
if (animateTo[i] !== offset[i])
|
|
|
|
{
|
|
|
|
var delta = animateTo[i] - offset[i];
|
|
|
|
var dir = Math.sign(delta);
|
|
|
|
var mag = Math.min(SPEED, Math.abs(delta));
|
|
|
|
offset[i] += dir * mag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 17:02:47 -05:00
|
|
|
//Change area percentage
|
|
|
|
if (lagPortion !== userPortion)
|
|
|
|
{
|
|
|
|
delta = userPortion - lagPortion;
|
|
|
|
dir = Math.sign(delta);
|
|
|
|
mag = Math.min(Math.abs(portionSpeed), Math.abs(delta));
|
|
|
|
lagPortion += dir * mag;
|
|
|
|
}
|
2017-02-22 01:34:59 -05:00
|
|
|
|
2017-02-21 00:54:44 -05:00
|
|
|
//Move players.
|
|
|
|
var dead = [];
|
|
|
|
players = players.filter(function(val) {
|
2017-02-23 17:02:47 -05:00
|
|
|
if (!newPlayerFrames[val.num])
|
|
|
|
newPlayerFrames[val.num] = 0;
|
|
|
|
|
|
|
|
if (newPlayerFrames[val.num] < ANIMATE_FRAMES)
|
|
|
|
newPlayerFrames[val.num]++;
|
|
|
|
else
|
|
|
|
val.move();
|
|
|
|
|
2017-02-21 00:54:44 -05:00
|
|
|
if (val.dead)
|
|
|
|
dead.push(val);
|
|
|
|
return !val.dead;
|
|
|
|
});
|
|
|
|
|
|
|
|
//Remove players with collisions.
|
|
|
|
var removing = [];
|
|
|
|
for (var i = 0; i < players.length; i++)
|
|
|
|
{
|
|
|
|
for (var j = i; j < players.length; j++)
|
|
|
|
{
|
|
|
|
|
|
|
|
//Remove those players when other players have hit their tail.
|
|
|
|
if (!removing[j] && players[j].tail.hitsTail(players[i]))
|
|
|
|
removing[j] = true;
|
|
|
|
if (!removing[i] && players[i].tail.hitsTail(players[j]))
|
|
|
|
removing[i] = true;
|
|
|
|
|
|
|
|
//Remove players with collisons...
|
2017-02-25 00:33:34 -05:00
|
|
|
if (i !== j && squaresIntersect(players[i].posX, players[j].posX) &&
|
|
|
|
squaresIntersect(players[i].posY, players[j].posY))
|
2017-02-21 00:54:44 -05:00
|
|
|
{
|
|
|
|
//...if one player is own his own territory, the other is out.
|
2017-02-22 19:51:41 -05:00
|
|
|
if (grid.get(players[i].row, players[i].col) === players[i])
|
2017-02-21 00:54:44 -05:00
|
|
|
removing[j] = true;
|
2017-02-22 19:51:41 -05:00
|
|
|
else if (grid.get(players[j].row, players[j].col) === players[j])
|
2017-02-21 00:54:44 -05:00
|
|
|
removing[i] = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//...otherwise, the one that sustains most of the collision will be removed.
|
2017-02-21 17:17:06 -05:00
|
|
|
var areaI = area(players[i]);
|
|
|
|
var areaJ = area(players[j]);
|
2017-02-21 00:54:44 -05:00
|
|
|
|
|
|
|
if (areaI === areaJ)
|
|
|
|
removing[i] = removing[j] = true;
|
|
|
|
else if (areaI > areaJ)
|
|
|
|
removing[i] = true;
|
|
|
|
else
|
|
|
|
removing[j] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
players = players.filter(function(val, i) {
|
|
|
|
if (removing[i])
|
2017-02-21 17:17:06 -05:00
|
|
|
{
|
2017-02-21 00:54:44 -05:00
|
|
|
dead.push(val);
|
2017-02-21 17:17:06 -05:00
|
|
|
val.die();
|
|
|
|
}
|
2017-02-21 00:54:44 -05:00
|
|
|
return !removing[i];
|
|
|
|
});
|
|
|
|
|
|
|
|
dead.forEach(function(val) {
|
|
|
|
console.log(val.name + " is dead");
|
2017-02-23 17:02:47 -05:00
|
|
|
allPlayers[val.num] = undefined;
|
2017-02-21 00:54:44 -05:00
|
|
|
});
|
2017-02-22 19:51:41 -05:00
|
|
|
for (var r = 0; r < grid.size; r++)
|
|
|
|
{
|
|
|
|
for (var c = 0; c < grid.size; c++)
|
|
|
|
{
|
|
|
|
if (dead.indexOf(grid.get(r, c)) !== -1)
|
|
|
|
grid.set(r, c, null);
|
|
|
|
}
|
|
|
|
}
|
2017-02-21 00:54:44 -05:00
|
|
|
|
2017-02-23 17:02:47 -05:00
|
|
|
//Update user's portions and top ranks.
|
|
|
|
userPortion = playerPortion[user.num] / (GRID_SIZE * GRID_SIZE);
|
|
|
|
portionSpeed = Math.abs(userPortion - lagPortion) / ANIMATE_FRAMES;
|
|
|
|
|
|
|
|
//TODO: animate player is dead. (maybe explosion?)
|
|
|
|
//TODO: show when this player is dead
|
2017-02-22 19:51:41 -05:00
|
|
|
centerOnPlayer(user, animateTo);
|
|
|
|
}
|
|
|
|
|
|
|
|
function centerOnPlayer(player, pos)
|
|
|
|
{
|
2017-02-24 19:57:13 -05:00
|
|
|
var xOff = Math.floor(player.posX - (gameWidth / zoom - CELL_WIDTH) / 2);
|
|
|
|
var yOff = Math.floor(player.posY - (gameHeight / zoom - CELL_WIDTH) / 2);
|
|
|
|
pos[0] = Math.max(Math.min(xOff, grid.size * CELL_WIDTH + BORDER_WIDTH * 2 - gameWidth / zoom), 0);
|
|
|
|
pos[1] = Math.max(Math.min(yOff, grid.size * CELL_WIDTH + BORDER_WIDTH * 2 - gameHeight / zoom), 0);
|
2017-02-21 00:54:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function area(player)
|
|
|
|
{
|
|
|
|
var xDest = player.col * CELL_WIDTH;
|
|
|
|
var yDest = player.row * CELL_WIDTH;
|
|
|
|
|
2017-02-25 00:33:34 -05:00
|
|
|
if (player.posX === xDest)
|
|
|
|
return Math.abs(player.posY - yDest);
|
2017-02-21 00:54:44 -05:00
|
|
|
else
|
2017-02-25 00:33:34 -05:00
|
|
|
return Math.abs(player.posX - xDest);
|
2017-02-21 00:54:44 -05:00
|
|
|
}
|
|
|
|
|
2017-02-22 19:51:41 -05:00
|
|
|
//Thanks to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
|
|
|
|
function getRandomInt(min, max) {
|
|
|
|
min = Math.ceil(min);
|
|
|
|
max = Math.floor(max);
|
|
|
|
return Math.floor(Math.random() * (max - min)) + min;
|
|
|
|
}
|
|
|
|
|
2017-02-21 00:54:44 -05:00
|
|
|
function squaresIntersect(a, b)
|
|
|
|
{
|
|
|
|
if (a < b)
|
|
|
|
return b < a + CELL_WIDTH;
|
|
|
|
else
|
|
|
|
return a < b + CELL_WIDTH;
|
|
|
|
}
|
|
|
|
|
2017-02-22 22:36:29 -05:00
|
|
|
function paintGridBorder()
|
|
|
|
{
|
|
|
|
ctx.fillStyle = 'lightgray';
|
|
|
|
var gridWidth = CELL_WIDTH * GRID_SIZE;
|
|
|
|
|
|
|
|
ctx.fillRect(-BORDER_WIDTH, 0, BORDER_WIDTH, gridWidth);
|
|
|
|
ctx.fillRect(-BORDER_WIDTH, -BORDER_WIDTH, gridWidth + BORDER_WIDTH * 2, BORDER_WIDTH);
|
|
|
|
ctx.fillRect(gridWidth, 0, BORDER_WIDTH, gridWidth);
|
|
|
|
ctx.fillRect(-BORDER_WIDTH, gridWidth, gridWidth + BORDER_WIDTH * 2, BORDER_WIDTH);
|
|
|
|
}
|
|
|
|
|
2017-02-22 19:51:41 -05:00
|
|
|
function paintGridLines()
|
|
|
|
{
|
|
|
|
ctx.fillStyle = 'lightgray';
|
|
|
|
ctx.beginPath();
|
2017-02-23 17:02:47 -05:00
|
|
|
for (var x = modRotate(-offset[0], CELL_WIDTH); x < gameWidth; x += CELL_WIDTH)
|
2017-02-22 19:51:41 -05:00
|
|
|
{
|
|
|
|
ctx.moveTo(x, 0);
|
2017-02-23 17:02:47 -05:00
|
|
|
ctx.lineTo(x, gameHeight);
|
2017-02-22 19:51:41 -05:00
|
|
|
}
|
2017-02-23 17:02:47 -05:00
|
|
|
for (var y = modRotate(-offset[1], CELL_WIDTH); y < gameHeight; y+= CELL_WIDTH)
|
2017-02-22 19:51:41 -05:00
|
|
|
{
|
|
|
|
ctx.moveTo(0, y);
|
2017-02-23 17:02:47 -05:00
|
|
|
ctx.lineTo(gameWidth, y);
|
2017-02-22 19:51:41 -05:00
|
|
|
}
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
|
|
|
|
function modRotate(val, mod)
|
|
|
|
{
|
|
|
|
var res = val % mod;
|
|
|
|
if (res >= 0)
|
|
|
|
return res;
|
|
|
|
else
|
|
|
|
return mod + res;
|
|
|
|
}
|
|
|
|
|
2017-02-21 00:54:44 -05:00
|
|
|
function paintGrid()
|
|
|
|
{
|
2017-02-23 17:02:47 -05:00
|
|
|
//Paint background.
|
|
|
|
ctx.fillStyle = "#e2ebf3";
|
|
|
|
ctx.fillRect(0, 0, CELL_WIDTH * GRID_SIZE, CELL_WIDTH * GRID_SIZE);
|
2017-02-22 22:36:29 -05:00
|
|
|
|
|
|
|
paintGridBorder();
|
2017-02-22 19:51:41 -05:00
|
|
|
//paintGridLines();
|
2017-02-21 00:54:44 -05:00
|
|
|
|
2017-02-22 22:36:29 -05:00
|
|
|
//Get viewing limits
|
2017-02-24 19:57:13 -05:00
|
|
|
var offsetX = (offset[0] - BORDER_WIDTH);
|
|
|
|
var offsetY = (offset[1] - BORDER_WIDTH);
|
|
|
|
|
|
|
|
var minRow = Math.max(Math.floor(offsetY / CELL_WIDTH), 0);
|
|
|
|
var minCol = Math.max(Math.floor(offsetX / CELL_WIDTH), 0);
|
|
|
|
var maxRow = Math.min(Math.ceil((offsetY + gameHeight / zoom) / CELL_WIDTH), grid.size);
|
|
|
|
var maxCol = Math.min(Math.ceil((offsetX + gameWidth / zoom) / CELL_WIDTH), grid.size);
|
2017-02-22 22:36:29 -05:00
|
|
|
|
2017-02-22 19:51:41 -05:00
|
|
|
//Paint occupied areas. (and fading ones).
|
2017-02-22 22:36:29 -05:00
|
|
|
for (var r = minRow; r < maxRow; r++)
|
2017-02-21 00:54:44 -05:00
|
|
|
{
|
2017-02-22 22:36:29 -05:00
|
|
|
for (var c = minCol; c < maxCol; c++)
|
2017-02-21 00:54:44 -05:00
|
|
|
{
|
2017-02-22 19:51:41 -05:00
|
|
|
var p = grid.get(r, c);
|
|
|
|
var x = c * CELL_WIDTH, y = r * CELL_WIDTH, baseColor, shadowColor;
|
|
|
|
|
|
|
|
var animateSpec = animateGrid.get(r, c);
|
2017-02-24 19:57:13 -05:00
|
|
|
var adjust = 1;
|
2017-02-22 19:51:41 -05:00
|
|
|
if (!animateOff && animateSpec)
|
|
|
|
{
|
|
|
|
if (animateSpec.before) //fading animation
|
|
|
|
{
|
2017-02-24 19:57:13 -05:00
|
|
|
var frac = (animateSpec.frame / ANIMATE_FRAMES);
|
|
|
|
var back = new Color(.58, .41, .92, 1);
|
|
|
|
baseColor = animateSpec.before.baseColor.interpolateToString(back, frac);
|
|
|
|
shadowColor = animateSpec.before.shadowColor.interpolateToString(back, frac);
|
|
|
|
adjust = 0;
|
2017-02-22 19:51:41 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (p)
|
2017-02-21 00:54:44 -05:00
|
|
|
{
|
2017-02-22 19:51:41 -05:00
|
|
|
baseColor = p.baseColor;
|
|
|
|
shadowColor = p.shadowColor;
|
|
|
|
}
|
|
|
|
else //No animation nor is this player owned.
|
|
|
|
continue;
|
|
|
|
|
2017-02-23 17:02:47 -05:00
|
|
|
var hasBottom = !grid.isOutOfBounds(r + 1, c);
|
|
|
|
var bottomAnimate = hasBottom && animateGrid.get(r + 1, c);
|
|
|
|
var bottomEmpty = !bottomAnimate || (bottomAnimate.after && bottomAnimate.before);
|
|
|
|
if (hasBottom && ((!!bottomAnimate ^ !!animateSpec) || bottomEmpty))
|
2017-02-22 22:36:29 -05:00
|
|
|
{
|
2017-02-24 19:57:13 -05:00
|
|
|
|
2017-02-22 22:36:29 -05:00
|
|
|
ctx.fillStyle = shadowColor.rgbString();
|
2017-02-24 19:57:13 -05:00
|
|
|
ctx.fillRect(x, y + CELL_WIDTH + 1, CELL_WIDTH + 1, SHADOW_OFFSET);
|
2017-02-22 22:36:29 -05:00
|
|
|
}
|
2017-02-22 19:51:41 -05:00
|
|
|
ctx.fillStyle = baseColor.rgbString();
|
2017-02-24 19:57:13 -05:00
|
|
|
ctx.fillRect(x, y, CELL_WIDTH + 1, CELL_WIDTH + 1);
|
2017-02-22 19:51:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (animateOff)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//Paint squares with drop in animation.
|
|
|
|
for (var r = 0; r < grid.size; r++)
|
|
|
|
{
|
|
|
|
for (var c = 0; c < grid.size; c++)
|
|
|
|
{
|
|
|
|
animateSpec = animateGrid.get(r, c);
|
|
|
|
x = c * CELL_WIDTH, y = r * CELL_WIDTH;
|
|
|
|
|
|
|
|
if (animateSpec && !animateOff)
|
|
|
|
{
|
2017-02-22 22:36:29 -05:00
|
|
|
var viewable = r >= minRow && r < maxRow && c >= minCol && c < maxCol;
|
|
|
|
if (animateSpec.after && viewable)
|
2017-02-22 19:51:41 -05:00
|
|
|
{
|
2017-02-22 22:36:29 -05:00
|
|
|
//Bouncing the squares.
|
2017-02-23 17:02:47 -05:00
|
|
|
var offsetBounce = getBounceOffset(animateSpec.frame);
|
2017-02-22 19:51:41 -05:00
|
|
|
y -= offsetBounce;
|
|
|
|
|
|
|
|
shadowColor = animateSpec.after.shadowColor;
|
|
|
|
baseColor = animateSpec.after.baseColor.deriveLumination(-(offsetBounce / DROP_HEIGHT) * .1);
|
|
|
|
|
|
|
|
ctx.fillStyle = shadowColor.rgbString();
|
2017-02-24 19:57:13 -05:00
|
|
|
ctx.fillRect(x, y + CELL_WIDTH, CELL_WIDTH, SHADOW_OFFSET);
|
2017-02-22 19:51:41 -05:00
|
|
|
ctx.fillStyle = baseColor.rgbString();
|
2017-02-24 19:57:13 -05:00
|
|
|
ctx.fillRect(x, y, CELL_WIDTH + 1, CELL_WIDTH + 1);
|
2017-02-22 19:51:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
animateSpec.frame++;
|
2017-02-23 17:02:47 -05:00
|
|
|
if (animateSpec.frame >= ANIMATE_FRAMES)
|
2017-02-22 19:51:41 -05:00
|
|
|
animateGrid.set(r, c, null);
|
2017-02-21 00:54:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 17:02:47 -05:00
|
|
|
function getBounceOffset(frame)
|
|
|
|
{
|
|
|
|
var offsetBounce = ANIMATE_FRAMES;
|
|
|
|
var bounceNum = BOUNCE_FRAMES.length - 1;
|
|
|
|
while (bounceNum >= 0 && frame < offsetBounce - BOUNCE_FRAMES[bounceNum])
|
|
|
|
{
|
|
|
|
offsetBounce -= BOUNCE_FRAMES[bounceNum];
|
|
|
|
bounceNum--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bounceNum === -1)
|
|
|
|
{
|
|
|
|
return (offsetBounce - frame) * DROP_SPEED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
offsetBounce -= BOUNCE_FRAMES[bounceNum];
|
|
|
|
frame = frame - offsetBounce;
|
|
|
|
var midFrame = BOUNCE_FRAMES[bounceNum] / 2;
|
|
|
|
if (frame >= midFrame)
|
|
|
|
return (BOUNCE_FRAMES[bounceNum] - frame) * DROP_SPEED;
|
|
|
|
else
|
|
|
|
return frame * DROP_SPEED;
|
|
|
|
}
|
|
|
|
}
|
2017-02-22 01:34:59 -05:00
|
|
|
|
2017-02-22 19:51:41 -05:00
|
|
|
var showedDead = false;
|
2017-02-21 00:54:44 -05:00
|
|
|
function paintLoop()
|
|
|
|
{
|
|
|
|
ctx.fillStyle = 'whitesmoke';
|
2017-02-23 17:02:47 -05:00
|
|
|
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
|
2017-02-21 00:54:44 -05:00
|
|
|
|
2017-02-24 19:57:13 -05:00
|
|
|
//Move grid to viewport as said with the offsets, below the stats
|
2017-02-23 17:02:47 -05:00
|
|
|
ctx.save();
|
2017-02-24 19:57:13 -05:00
|
|
|
ctx.translate(0, BAR_HEIGHT);
|
2017-02-23 17:02:47 -05:00
|
|
|
ctx.beginPath();
|
2017-02-24 19:57:13 -05:00
|
|
|
ctx.rect(0, 0, gameWidth, gameHeight);
|
2017-02-23 17:02:47 -05:00
|
|
|
ctx.clip();
|
2017-02-24 19:57:13 -05:00
|
|
|
|
|
|
|
//Zoom in/out based on player stats.
|
|
|
|
ctx.scale(zoom, zoom);
|
|
|
|
ctx.translate(-offset[0] + BORDER_WIDTH, -offset[1] + BORDER_WIDTH);
|
|
|
|
|
2017-02-21 00:54:44 -05:00
|
|
|
paintGrid();
|
|
|
|
players.forEach(function (p) {
|
2017-02-23 17:02:47 -05:00
|
|
|
var fr = newPlayerFrames[p.num] || 0;
|
|
|
|
if (fr < ANIMATE_FRAMES)
|
|
|
|
p.render(ctx, fr / ANIMATE_FRAMES);
|
|
|
|
else
|
|
|
|
p.render(ctx);
|
2017-02-21 00:54:44 -05:00
|
|
|
});
|
2017-02-23 17:02:47 -05:00
|
|
|
|
|
|
|
//Reset transform to paint fixed UI elements
|
|
|
|
ctx.restore();
|
|
|
|
|
|
|
|
//UI Bar background
|
|
|
|
ctx.fillStyle = "#24422c";
|
|
|
|
ctx.fillRect(0, 0, canvasWidth, BAR_HEIGHT);
|
|
|
|
|
|
|
|
var barOffset;
|
|
|
|
ctx.fillStyle = "white";
|
2017-02-24 02:17:49 -05:00
|
|
|
ctx.font = "24px Changa";
|
2017-02-23 17:02:47 -05:00
|
|
|
barOffset = ctx.measureText(user.name).width + 10;
|
|
|
|
ctx.fillText(user.name, 5, CELL_WIDTH - 5);
|
|
|
|
|
2017-02-24 23:59:05 -05:00
|
|
|
zoom = 1 / (lagPortion + 1); //Zoom goes from 1 to .5, decreasing as portion goes up. TODO: maybe can modify this?
|
2017-02-24 19:57:13 -05:00
|
|
|
|
2017-02-23 17:02:47 -05:00
|
|
|
//Draw filled bar.
|
|
|
|
ctx.fillStyle = "rgba(180, 180, 180, .3)";
|
|
|
|
ctx.fillRect(barOffset, 0, BAR_WIDTH, BAR_HEIGHT);
|
|
|
|
|
|
|
|
var barSize = Math.ceil((BAR_WIDTH - MIN_BAR_WIDTH) * lagPortion + MIN_BAR_WIDTH);
|
|
|
|
ctx.fillStyle = user.baseColor.rgbString();
|
|
|
|
ctx.fillRect(barOffset, 0, barSize, CELL_WIDTH);
|
|
|
|
ctx.fillStyle = user.shadowColor.rgbString();
|
|
|
|
ctx.fillRect(barOffset, CELL_WIDTH, barSize, SHADOW_OFFSET);
|
|
|
|
|
|
|
|
//Percentage
|
|
|
|
ctx.fillStyle = "white";
|
2017-02-24 02:17:49 -05:00
|
|
|
ctx.font = "18px Changa";
|
2017-02-23 17:02:47 -05:00
|
|
|
ctx.fillText((lagPortion * 100).toFixed(3) + "%", 5 + barOffset, CELL_WIDTH - 5);
|
2017-02-21 00:54:44 -05:00
|
|
|
|
2017-02-22 19:51:41 -05:00
|
|
|
if (user.dead && !showedDead)
|
2017-02-21 17:17:06 -05:00
|
|
|
{
|
2017-02-22 19:51:41 -05:00
|
|
|
showedDead = true;
|
2017-02-21 17:17:06 -05:00
|
|
|
console.log("You died!");
|
2017-02-22 19:51:41 -05:00
|
|
|
//return;
|
2017-02-21 17:17:06 -05:00
|
|
|
}
|
|
|
|
|
2017-02-21 00:54:44 -05:00
|
|
|
//TODO: sync each loop with server. (server will give frame count.)
|
|
|
|
frameCount++;
|
|
|
|
update();
|
|
|
|
requestAnimationFrame(paintLoop);
|
|
|
|
}
|
|
|
|
paintLoop();
|
|
|
|
|
2017-02-21 17:17:06 -05:00
|
|
|
|
2017-02-21 00:54:44 -05:00
|
|
|
//Event listeners
|
|
|
|
$(document).keydown(function(e) {
|
2017-02-22 19:51:41 -05:00
|
|
|
if (user.dead)
|
|
|
|
return;
|
2017-02-21 00:54:44 -05:00
|
|
|
var newHeading = -1;
|
|
|
|
switch (e.which)
|
|
|
|
{
|
|
|
|
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 17:17:06 -05:00
|
|
|
if (newHeading === user.currentHeading || ((newHeading % 2 === 0) ^
|
|
|
|
(user.currentHeading % 2 === 0)))
|
|
|
|
{
|
|
|
|
//TODO: notify server.
|
|
|
|
user.heading = newHeading;
|
|
|
|
}
|
2017-02-21 00:54:44 -05:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
2017-02-21 17:17:06 -05:00
|
|
|
});
|