papercats/player.js

486 lines
12 KiB
JavaScript
Raw Normal View History

var Stack = require("./stack.js");
var Color = require("./color.js");
var Grid = require("./grid.js");
2017-02-22 01:34:59 -05:00
function defineGetter(getter) {
return {
get: getter,
enumerable: true
};
}
function defineInstanceMethods(thisobj, data /*, methods...*/)
{
for (var i = 2; i < arguments.length; i++)
thisobj[arguments[i].name] = arguments[i].bind(this, data);
}
function defineAccessorProperties(thisobj, data /*, names...*/)
{
var descript = {};
function getAt(name) { return function() {return data[name] } }
for (var i = 2; i < arguments.length; i++)
descript[arguments[i]] = defineGetter(getAt(arguments[i]));
Object.defineProperties(thisobj, descript);
}
var CELL_WIDTH = 40;
var GRID_SIZE = 80;
function TailMove(orientation)
{
this.move = 1;
Object.defineProperty(this, "orientation", {
value: orientation,
enumerable: true
});
}
2017-02-22 19:51:41 -05:00
function Tail(player, sdata)
{
var data = {
tail: [],
tailGrid: [],
prev: null,
startRow: 0,
startCol: 0,
prevRow: 0,
prevCol: 0,
player: player
};
2017-02-22 01:34:59 -05:00
if (sdata)
2017-02-22 01:34:59 -05:00
{
data.startRow = data.prevRow = sdata.startRow || 0;
data.startCol = data.prevCol = sdata.startCol || 0;
sdata.forEach(function(val) {
addTail(data, val.orientation, val.move);
2017-02-22 01:34:59 -05:00
});
}
data.grid = player.grid;
2017-02-22 01:34:59 -05:00
defineInstanceMethods(this, data, addTail, hitsTail, fillTail, renderTail, reposition, serialData);
Object.defineProperty(this, "moves", {
get: function() {return data.tail.slice(0);},
enumerable: true
});
}
//Instance methods.
function serialData(data) {
return JSON.serialize({
tail: data.tail,
startRow: data.startRow,
startCol: data.startCol
});
}
function setTailGrid(data, tailGrid, r, c)
{
if (!tailGrid[r])
tailGrid[r] = [];
tailGrid[r][c] = true;
}
function addTail(data, orientation, count)
{
if (count === undefined)
count = 1;
if (!count || count < 0)
return;
2017-02-22 01:34:59 -05:00
var prev = data.prev;
var r = data.prevRow, c = data.prevCol;
if (data.tail.length === 0)
setTailGrid(data, data.tailGrid, r, c);
2017-02-22 01:34:59 -05:00
if (!prev || prev.orientation !== orientation)
2017-02-22 01:34:59 -05:00
{
prev = data.prev = new TailMove(orientation);
data.tail.push(prev);
}
else
prev.move += count;
2017-02-22 01:34:59 -05:00
for (var i = 0; i < count; i++)
{
2017-02-22 01:34:59 -05:00
var pos = walk([data.prevRow, data.prevCol], null, orientation, 1);
data.prevRow = pos[0];
data.prevCol = pos[1];
setTailGrid(data, data.tailGrid, pos[0], pos[1]);
}
}
function reposition(data, row, col)
{
data.prevRow = data.startRow = row;
data.prevCol = data.startCol = col;
data.prev = null;
if (data.tail.length === 0)
return;
else
2017-02-22 01:34:59 -05:00
{
var ret = data.tail;
data.tail = [];
data.tailGrid = [];
return ret;
2017-02-22 01:34:59 -05:00
}
}
/*
function render2(data, ctx)
{
ctx.fillStyle = data.player.tailColor.rgbString();
for (var r = 0; r < data.tailGrid.length; r++)
2017-02-22 01:34:59 -05:00
{
if (!data.tailGrid[r])
continue;
for (var c = 0; c < data.tailGrid[r].length; c++)
if (data.tailGrid[r][c])
ctx.fillRect(c * CELL_WIDTH, r * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH);
2017-02-22 01:34:59 -05:00
}
}
*/
//Helper methods.
function renderTail(data, ctx)
{
if (data.tail.length === 0)
return;
2017-02-22 01:34:59 -05:00
ctx.fillStyle = data.player.tailColor.rgbString();
var prevOrient = -1;
var start = [data.startRow, data.startCol];
//fillTailRect(ctx, start, start);
data.tail.forEach(function(tail) {
var negDir = tail.orientation === 0 || tail.orientation === 3;
2017-02-22 01:34:59 -05:00
var back = start;
if (!negDir)
start = walk(start, null, tail.orientation, 1);
var finish = walk(start, null, tail.orientation, tail.move - 1);
2017-02-22 01:34:59 -05:00
if (tail.move > 1)
fillTailRect(ctx, start, finish);
if (prevOrient !== -1)
//Draw folding triangle.
renderCorner(ctx, back, prevOrient, tail.orientation);
start = finish;
if (negDir)
walk(start, start, tail.orientation, 1);
prevOrient = tail.orientation;
});
2017-02-22 01:34:59 -05:00
var curOrient = data.player.currentHeading;
if (prevOrient === curOrient)
2017-02-22 01:34:59 -05:00
{
fillTailRect(ctx, start, start);
2017-02-22 01:34:59 -05:00
}
else
renderCorner(ctx, start, prevOrient, curOrient);
}
function renderCorner(ctx, cornerStart, dir1, dir2)
{
if (dir1 === 0 || dir2 === 0)
walk(cornerStart, cornerStart, 2, 1);
if (dir1 === 3 || dir2 === 3)
walk(cornerStart, cornerStart, 1, 1);
var a = walk(cornerStart, null, dir2, 1);
var b = walk(a, null, dir1, 1);
2017-02-22 01:34:59 -05:00
var triangle = new Path2D();
triangle.moveTo(cornerStart[1] * CELL_WIDTH, cornerStart[0] * CELL_WIDTH);
triangle.lineTo(a[1] * CELL_WIDTH, a[0] * CELL_WIDTH);
triangle.lineTo(b[1] * CELL_WIDTH, b[0] * CELL_WIDTH);
triangle.closePath();
for (var i = 0; i < 2; i++)
ctx.fill(triangle);
}
function walk(from, ret, orient, dist)
{
ret = ret || [];
ret[0] = from[0];
ret[1] = from[1];
switch (orient)
2017-02-22 01:34:59 -05:00
{
case 0: ret[0] -= dist; break; //UP
case 1: ret[1] += dist; break; //RIGHT
case 2: ret[0] += dist; break; //DOWN
case 3: ret[1] -= dist; break; //LEFT
2017-02-22 01:34:59 -05:00
}
return ret;
}
function fillTailRect(ctx, start, end)
{
var x = start[1] * CELL_WIDTH;
var y = start[0] * CELL_WIDTH;
var width = (end[1] - start[1]) * CELL_WIDTH;
var height = (end[0] - start[0]) * CELL_WIDTH;
if (width === 0)
width += CELL_WIDTH;
if (height === 0)
height += CELL_WIDTH;
2017-02-22 01:34:59 -05:00
if (width < 0)
2017-02-22 01:34:59 -05:00
{
x += width;
width = -width;
2017-02-22 01:34:59 -05:00
}
if (height < 0)
2017-02-22 01:34:59 -05:00
{
y += height;
height = -height;
2017-02-22 01:34:59 -05:00
}
ctx.fillRect(x, y, width, height);
}
//TODO: fade in colors using grid property-getters/setters
2017-02-25 00:33:34 -05:00
function fillTail(data)
{
if (data.tail.length === 0)
return;
function onTail(c) { return data.tailGrid[c[0]] && data.tailGrid[c[0]][c[1]]; }
2017-02-25 00:33:34 -05:00
var grid = data.grid;
var start = [data.startRow, data.startCol];
var been = new Grid(grid.size);
var coords = [];
2017-02-22 01:34:59 -05:00
coords.push(start);
while (coords.length > 0) //BFS for all tail spaces.
2017-02-22 01:34:59 -05:00
{
var coord = coords.shift();
var r = coord[0];
var c = coord[1];
2017-02-22 01:34:59 -05:00
if (grid.isOutOfBounds(r, c))
continue;
2017-02-22 01:34:59 -05:00
if (been.get(r, c))
continue;
if (onTail(coord)) //on the tail.
2017-02-22 01:34:59 -05:00
{
2017-02-22 19:51:41 -05:00
been.set(r, c, true);
grid.set(r, c, data.player);
2017-02-22 01:34:59 -05:00
//Find all spots that this tail encloses.
floodFill(data, grid, r + 1, c, been);
floodFill(data, grid, r - 1, c, been);
floodFill(data, grid, r, c + 1, been);
floodFill(data, grid, r, c - 1, been);
2017-02-22 01:34:59 -05:00
coords.push([r + 1, c]);
coords.push([r - 1, c]);
coords.push([r, c + 1]);
coords.push([r, c - 1]);
}
}
}
function floodFill(data, grid, row, col, been)
{
var coords = [];
var filled = new Stack(40000);
var surrounded = true;
function onTail(c) { return data.tailGrid[c[0]] && data.tailGrid[c[0]][c[1]]; }
coords.push([row, col]);
while (coords.length > 0)
{
var coord = coords.shift();
var r = coord[0];
var c = coord[1];
if (grid.isOutOfBounds(r, c))
2017-02-22 01:34:59 -05:00
{
surrounded = false;
continue;
2017-02-22 01:34:59 -05:00
}
//End this traverse on boundaries (where we been, on the tail, and when we enter our territory)
if (been.get(r, c) || onTail(coord) || grid.get(r, c) === data.player)
continue;
been.set(r, c, true);
if (surrounded)
filled.push(coord);
coords.push([r + 1, c]);
coords.push([r - 1, c]);
coords.push([r, c + 1]);
coords.push([r, c - 1]);
2017-02-22 01:34:59 -05:00
}
if (surrounded)
2017-02-22 01:34:59 -05:00
{
while (!filled.isEmpty())
{
coord = filled.pop();
grid.set(coord[0], coord[1], data.player);
}
2017-02-22 01:34:59 -05:00
}
return surrounded;
}
function hitsTail(data, other)
{
return (data.prevRow !== other.row || data.prevCol !== other.col) &&
(data.startRow !== other.row || data.startCol !== other.col) &&
!!(data.tailGrid[other.row] && data.tailGrid[other.row][other.col]);
}
var CELL_WIDTH = 40;
var SPEED = 5;
var SHADOW_OFFSET = 10;
function Player(isClient, grid, sdata) {
var data = {};
2017-02-22 01:34:59 -05:00
//Parameters
data.num = sdata.num;
data.name = sdata.name || "Player " + (data.num + 1);
data.isCient = isClient;
data.grid = grid;
data.posX = sdata.posX;
data.posY = sdata.posY;
this.heading = data.currentHeading = sdata.currentHeading; //0 is up, 1 is right, 2 is down, 3 is left.
data.dead = false;
//Only need colors for client side.
if (isClient)
{
2017-02-22 01:34:59 -05:00
var hue = Math.random();
2017-02-22 19:51:41 -05:00
var base = new Color(hue, .8, .5);
this.baseColor = base;
this.shadowColor = base.deriveLumination(-.3);
this.tailColor = base.deriveLumination(.2).deriveAlpha(.5);
2017-02-22 01:34:59 -05:00
}
//Tail requires special handling.
2017-02-25 00:33:34 -05:00
this.grid = grid; //Temporary
if (sdata.tail)
data.tail = new Tail(this, sdata.tail);
else
2017-02-22 01:34:59 -05:00
{
data.tail = new Tail(this);
2017-02-25 00:33:34 -05:00
data.tail.reposition(calcRow(data), calcCol(data));
2017-02-22 01:34:59 -05:00
}
//Instance methods.
this.move = move.bind(this, data);
this.die = function() {data.dead = true;};
this.serialData = function() {
return {
num: data.num,
name: data.name,
posX: data.posX,
posY: data.posY,
currentHeading: data.currentHeading,
tail: data.tail.serialData()
};
}
2017-02-25 00:33:34 -05:00
//Read-only Properties.
2017-02-25 00:33:34 -05:00
defineAccessorProperties(this, data, "currentHeading", "dead", "name", "num", "posX", "posY", "grid", "tail");
Object.defineProperties(this, {
2017-02-25 00:33:34 -05:00
row: defineGetter(function() { return calcRow(data); }),
col: defineGetter(function() { return calcCol(data); })
});
}
2017-02-25 00:33:34 -05:00
//Gets the next integer in positive or negative direction.
function nearestInteger(positive, val)
{
return positive ? Math.ceil(val) : Math.floor(val);
}
function calcRow(data)
{
return nearestInteger(data.currentHeading === 2 /*DOWN*/, data.posY / CELL_WIDTH);
}
function calcCol(data)
{
return nearestInteger(data.currentHeading === 1 /*RIGHT*/, data.posX / CELL_WIDTH);
}
//Instance methods
Player.prototype.render = function(ctx, fade)
{
//Render tail.
this.tail.renderTail(ctx);
//Render player.
fade = fade || 1;
ctx.fillStyle = this.shadowColor.deriveAlpha(fade).rgbString();
ctx.fillRect(this.posX, this.posY, CELL_WIDTH, CELL_WIDTH);
var mid = CELL_WIDTH / 2;
var grd = ctx.createRadialGradient(this.posX + mid, this.posY + mid - SHADOW_OFFSET, 1,
this.posX + mid, this.posY + mid - SHADOW_OFFSET, CELL_WIDTH);
grd.addColorStop(0, this.baseColor.deriveAlpha(fade).rgbString());
grd.addColorStop(1, "white");
ctx.fillStyle = grd;
ctx.fillRect(this.posX, this.posY - SHADOW_OFFSET, CELL_WIDTH, CELL_WIDTH);
2017-02-22 01:34:59 -05:00
//Render name
ctx.fillStyle = this.shadowColor.deriveAlpha(fade).rgbString();
ctx.textAlign = "center";
2017-02-24 02:17:49 -05:00
var yoff = -SHADOW_OFFSET * 2
if (this.row === 0)
yoff = SHADOW_OFFSET * 2 + CELL_WIDTH;
ctx.font = "18px Changa";
ctx.fillText(this.name, this.posX + CELL_WIDTH / 2, this.posY + yoff);
};
function move(data)
{
//Move to new position.
var heading = this.heading;
if (this.posX % CELL_WIDTH !== 0 || this.posY % CELL_WIDTH !== 0)
heading = data.currentHeading;
else
data.currentHeading = heading;
switch (heading)
{
case 0: data.posY -= SPEED; break; //UP
case 1: data.posX += SPEED; break; //RIGHT
case 2: data.posY += SPEED; break; //DOWN
case 3: data.posX -= SPEED; break; //LEFT
}
2017-02-22 01:34:59 -05:00
//Check for out of bounds.
var row = this.row, col = this.col;
if (data.grid.isOutOfBounds(row, col))
{
data.dead = true;
return;
}
2017-02-22 01:34:59 -05:00
//Update tail position.
if (data.grid.get(row, col) === this)
{
//Safe zone!
2017-02-25 00:33:34 -05:00
this.tail.fillTail();
this.tail.reposition(row, col);
}
//If we are completely in a new cell (not in our safe zone), we add to the tail.
else if (this.posX % CELL_WIDTH === 0 && this.posY % CELL_WIDTH === 0)
this.tail.addTail(heading);
}
module.exports = Player;