hextris/entities.js

145 lines
3.9 KiB
JavaScript
Raw Normal View History

2014-05-17 14:43:16 +00:00
//you can change these to sexier stuff
2014-05-17 18:25:22 +00:00
function Block(lane, color, distFromHex, settled) {
this.settled = (settled == undefined) ? 0 : 1;
this.height = 20;
this.width = 65;
this.lane = lane;
this.angle = 90 - (30 + 60 * lane);
if (this.angle < 0) {
this.angle += 360;
}
this.color = color;
if (distFromHex) {
this.distFromHex = distFromHex;
}
else {
2014-05-17 21:19:23 +00:00
this.distFromHex = 300;
2014-05-17 18:25:22 +00:00
}
2014-05-17 22:52:15 +00:00
2014-05-17 18:25:22 +00:00
this.draw = function() {
this.width = 2 * this.distFromHex / Math.sqrt(3);
this.widthswag = this.width + this.height + 3;
2014-05-17 18:25:22 +00:00
var p1 = rotatePoint(-this.width/2, this.height/2, this.angle);
var p2 = rotatePoint(this.width/2, this.height/2, this.angle);
var p3 = rotatePoint(this.widthswag/2, -this.height/2, this.angle);
var p4 = rotatePoint(-this.widthswag/2, -this.height/2, this.angle);
2014-05-17 18:25:22 +00:00
ctx.fillStyle=this.color;
2014-05-17 18:25:22 +00:00
var baseX = canvas.width/2 + Math.sin((this.angle) * (Math.PI/180)) * (this.distFromHex + this.height/2);
var baseY = canvas.height/2 - Math.cos((this.angle) * (Math.PI/180)) * (this.distFromHex + this.height/2);
ctx.beginPath();
ctx.moveTo(baseX + p1.x, baseY + p1.y);
ctx.lineTo(baseX + p2.x, baseY + p2.y);
ctx.lineTo(baseX + p3.x, baseY + p3.y);
ctx.lineTo(baseX + p4.x, baseY + p4.y);
ctx.lineTo(baseX + p1.x, baseY + p1.y);
2014-05-17 18:25:22 +00:00
ctx.closePath();
ctx.fill();
};
2014-05-17 18:25:22 +00:00
}
2014-05-17 22:52:15 +00:00
function Clock(sideLength) {
this.fillColor = '#2c3e50';
2014-05-17 15:03:11 +00:00
this.position = 0;
2014-05-17 16:50:12 +00:00
this.sides = 6;
2014-05-17 14:24:39 +00:00
this.blocks = [];
2014-05-17 16:50:12 +00:00
this.angle = 30;
this.sideLength = sideLength;
2014-05-17 17:55:02 +00:00
this.strokeColor = 'blue';
2014-05-17 16:50:12 +00:00
this.x = canvas.width / 2;
this.y = canvas.height / 2;
for(var i=0; i< this.sides; i++) {
2014-05-17 14:24:39 +00:00
this.blocks.push([]);
}
2014-05-17 15:03:11 +00:00
this.addBlock = function(block) {
2014-05-17 17:55:02 +00:00
block.settled = 1;
2014-05-17 21:02:24 +00:00
var lane = this.sides - block.lane;// -this.position;
lane += this.position;
while (lane < 0) {
lane += this.sides;
2014-05-17 16:41:54 +00:00
}
2014-05-17 20:48:55 +00:00
lane = lane % this.sides;
2014-05-17 18:02:09 +00:00
block.distFromHex = MainClock.sideLength / 2 * Math.sqrt(3) + block.height * this.blocks[lane].length;
2014-05-17 16:41:54 +00:00
this.blocks[lane].push(block);
2014-05-17 21:28:15 +00:00
consolidateBlocks(this,lane,this.blocks[lane].length-1);
2014-05-17 17:55:02 +00:00
};
2014-05-17 22:52:15 +00:00
this.doesBlockCollide = function(block, iter, position, tArr) {
2014-05-17 17:55:02 +00:00
if (block.settled) return;
2014-05-17 20:48:55 +00:00
2014-05-17 21:02:24 +00:00
var lane = this.sides - block.lane;// -this.position;
lane += this.position;
2014-05-17 20:48:55 +00:00
while (lane < 0) {
2014-05-17 21:02:24 +00:00
lane += this.sides;
2014-05-17 20:48:55 +00:00
}
lane = lane % this.sides;
var arr = this.blocks[lane];
2014-05-17 20:53:14 +00:00
2014-05-17 22:52:15 +00:00
if (position !== undefined) {
arr = tArr;
if (position <= 0) {
if (block.distFromHex - (this.sideLength/2) * Math.sqrt(3) <= 0) {
block.settled = 1;
2014-05-17 23:21:19 +00:00
if (iter == 2 && block.distFromHex + 1 - (this.sideLength/2) * Math.sqrt(3) <= 0) {
block.distFromHex--;
}
2014-05-17 22:52:15 +00:00
}
}
else {
if (block.distFromHex + iter - arr[position - 1].distFromHex - arr[position - 1].height <= 0) {
block.settled = 1;
2014-05-17 23:21:19 +00:00
if (iter == 2 && block.distFromHex + 1 + iter - arr[position - 1].distFromHex - arr[position - 1].height <= 0) {
block.distFromHex--;
}
2014-05-17 22:52:15 +00:00
}
2014-05-17 17:55:02 +00:00
}
}
else {
2014-05-17 22:52:15 +00:00
if (arr.length > 0) {
if (block.distFromHex + iter - arr[arr.length - 1].distFromHex - arr[arr.length - 1].height <= 0) {
2014-05-17 23:21:19 +00:00
if (iter == 2 && block.distFromHex + iter + 1 - arr[arr.length - 1].distFromHex - arr[arr.length - 1].height <= 0) {
block.distFromHex--;
}
2014-05-17 22:52:15 +00:00
this.addBlock(block);
}
}
else {
if (block.distFromHex + iter - (this.sideLength/2) * Math.sqrt(3) <= 0) {
2014-05-17 23:21:19 +00:00
if (iter == 2 && block.distFromHex + iter + 1 - (this.sideLength/2) * Math.sqrt(3) <= 0) {
block.distFromHex--;
}
2014-05-17 22:52:15 +00:00
this.addBlock(block);
}
2014-05-17 17:55:02 +00:00
}
}
};
2014-05-17 15:03:11 +00:00
this.rotate = function(steps) {
this.position += steps;
2014-05-17 20:48:55 +00:00
while (this.position < 0) {
this.position += 6;
2014-05-17 17:43:56 +00:00
}
2014-05-17 20:48:55 +00:00
this.position = this.position % this.sides;
this.blocks.forEach(function(blocks){
blocks.forEach(function(block){
block.angle = block.angle - steps * 60;
});
});
2014-05-17 21:19:23 +00:00
2014-05-17 20:48:55 +00:00
this.angle = this.angle + steps * 60;
2014-05-17 17:55:02 +00:00
};
2014-05-17 16:50:12 +00:00
this.draw = function() {
drawPolygon(this.x, this.y, this.sides, this.sideLength, this.angle, this.fillColor);
2014-05-17 17:55:02 +00:00
};
2014-05-17 14:24:39 +00:00
}