hextris/js/entities.js

283 lines
7.4 KiB
JavaScript
Raw Normal View History

2014-05-22 20:36:32 +00:00
var angularVelocityConst = 4;
2014-05-19 19:49:05 +00:00
2014-05-24 00:22:53 +00:00
function Block(lane, color, iter, distFromHex, settled) {
2014-05-22 20:36:32 +00:00
this.settled = (settled === undefined) ? 0 : 1;
this.height = 15;
2014-05-19 20:22:30 +00:00
this.width = 65;
this.lane = lane;
this.angle = 90 - (30 + 60 * lane);
this.angularVelocity = 0;
this.targetAngle = this.angle;
this.color = color;
2014-05-21 22:39:02 +00:00
this.deleted = 0;
2014-05-22 23:08:08 +00:00
this.removed = 0;
2014-05-21 22:39:02 +00:00
this.tint = 0; //todo
this.opacity = 1;
2014-05-24 16:57:22 +00:00
this.initializing = 1;
this.parentArr;
2014-05-24 00:22:53 +00:00
this.iter = iter;
2014-05-21 22:39:02 +00:00
2014-05-19 20:22:30 +00:00
if (distFromHex) {
this.distFromHex = distFromHex;
} else {
this.distFromHex = 300;
}
this.incrementOpacity = function() {
if (this.deleted) {
2014-05-22 18:19:58 +00:00
var lane = MainClock.sides - this.lane;// -this.position;
lane += MainClock.position;
while (lane < 0) {
lane += MainClock.sides;
}
lane = lane % MainClock.sides;
2014-05-23 00:44:14 +00:00
this.opacity = this.opacity - 0.1;
if (this.opacity <= 0) {
this.opacity = 0;
var i = 0;
2014-05-22 19:42:05 +00:00
var j = this.getIndex();
this.parentArr.splice(j,1);
if (j < this.parentArr.length) {
for (i = j; i < this.parentArr.length; i++) {
this.parentArr[i].settled = 0;
}
}
}
}
};
2014-05-22 19:42:05 +00:00
this.getIndex = function (){
for (var i = 0; i < this.parentArr.length; i++) {
if (this.parentArr[i] == this) {
return i;
}
}
}
2014-05-19 20:22:30 +00:00
this.draw = function(attached, index) {
// var xd = (this.settled) ? gdx : 0;
// var yd = (this.settled) ? gdy : 0;
this.incrementOpacity();
2014-05-19 20:22:30 +00:00
if(attached == undefined)
attached = false;
if(this.angle > this.targetAngle) {
this.angularVelocity -= angularVelocityConst;
}
else if(this.angle < this.targetAngle) {
this.angularVelocity += angularVelocityConst;
}
if (Math.abs(this.angle - this.targetAngle + this.angularVelocity) <= Math.abs(this.angularVelocity)) { //do better soon
this.angle = this.targetAngle;
this.angularVelocity = 0;
}
else {
this.angle += this.angularVelocity;
}
this.width = 2 * this.distFromHex / Math.sqrt(3);
2014-05-20 02:16:43 +00:00
this.widthWide = this.width + this.height + 3;
2014-05-19 20:22:30 +00:00
var p1 = rotatePoint(-this.width / 2, this.height / 2, this.angle);
var p2 = rotatePoint(this.width / 2, this.height / 2, this.angle);
2014-05-20 02:16:43 +00:00
var p3 = rotatePoint(this.widthWide / 2, -this.height / 2, this.angle);
var p4 = rotatePoint(-this.widthWide / 2, -this.height / 2, this.angle);
2014-05-19 20:22:30 +00:00
ctx.fillStyle = this.color;
ctx.globalAlpha = this.opacity;
var baseX = canvas.originalWidth / 2 + Math.sin((this.angle) * (Math.PI / 180)) * (this.distFromHex + this.height / 2) + gdx;
var baseY = canvas.originalHeight / 2 - Math.cos((this.angle) * (Math.PI / 180)) * (this.distFromHex + this.height / 2) + gdy;
2014-05-19 20:22:30 +00:00
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);
ctx.closePath();
ctx.fill();
if (this.tint) {
if (this.opacity < 1) {
this.tint = 0;
}
ctx.fillStyle = "#FFFFFF";
ctx.globalAlpha = this.tint;
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);
ctx.closePath();
ctx.fill();
this.tint -= 0.02;
if (this.tint < 0) {
this.tint = 0;
}
}
ctx.globalAlpha = 1;
2014-05-19 20:22:30 +00:00
};
2014-05-17 18:25:22 +00:00
}
2014-05-21 22:39:02 +00:00
2014-05-19 17:25:20 +00:00
var colorSounds = {"#e74c3c": new Audio("../sounds/lowest.ogg"),
"#f1c40f":new Audio("../sounds/highest.ogg"),
"#3498db":new Audio("../sounds/middle.ogg"),
"#2ecc71":new Audio("../sounds/highest.ogg") //fix this later
2014-05-19 17:25:20 +00:00
};
2014-05-17 18:25:22 +00:00
2014-05-17 22:52:15 +00:00
function Clock(sideLength) {
2014-05-19 20:22:30 +00:00
this.fillColor = '#2c3e50';
this.angularVelocity = 0;
this.position = 0;
2014-05-23 20:13:23 +00:00
this.dy = 0;
2014-05-19 20:22:30 +00:00
this.sides = 6;
this.blocks = [];
this.angle = 180 / this.sides;
this.targetAngle = this.angle;
this.shakes = [];
2014-05-19 20:22:30 +00:00
this.sideLength = sideLength;
this.strokeColor = 'blue';
this.x = canvas.originalWidth / 2;
this.y = canvas.originalHeight / 2;
for (var i = 0; i < this.sides; i++) {
this.blocks.push([]);
2014-05-19 17:25:20 +00:00
}
2014-05-19 20:22:30 +00:00
this.shake = function(obj) { //lane as in particle lane
var angle = 30 + obj.lane * 60;
angle *= Math.PI / 180;
var dx = Math.cos(angle) * obj.magnitude;
var dy = Math.sin(angle) * obj.magnitude;
gdx -= dx;
gdy += dy;
obj.magnitude /= 2;
if (obj.magnitude < 1) {
for (var i = 0; i < this.shakes.length; i++) {
if (this.shakes[i] == obj) {
this.shakes.splice(i, 1);
}
}
}
};
2014-05-19 20:22:30 +00:00
this.addBlock = function(block) {
block.settled = 1;
block.tint = 0.6;
2014-05-19 20:22:30 +00:00
var lane = this.sides - block.lane;// -this.position;
this.shakes.push({lane:block.lane, magnitude:2});
2014-05-19 20:22:30 +00:00
lane += this.position;
while (lane < 0) {
lane += this.sides;
}
lane = lane % this.sides;
block.distFromHex = MainClock.sideLength / 2 * Math.sqrt(3) + block.height * this.blocks[lane].length;
this.blocks[lane].push(block);
block.parentArr = this.blocks[lane];
2014-05-19 20:22:30 +00:00
consolidateBlocks(this, lane, this.blocks[lane].length - 1);
2014-05-22 19:42:05 +00:00
};
2014-05-19 20:22:30 +00:00
2014-05-24 00:22:53 +00:00
this.doesBlockCollide = function(block, position, tArr) {
2014-05-19 20:22:30 +00:00
if (block.settled) {
return;
}
var lane = this.sides - block.lane;// -this.position;
lane += this.position;
while (lane < 0) {
lane += this.sides;
}
lane = lane % this.sides;
var arr = this.blocks[lane];
if (position !== undefined) {
arr = tArr;
if (position <= 0) {
2014-05-24 00:22:53 +00:00
if (block.distFromHex + block.iter - (this.sideLength / 2) * Math.sqrt(3) <= 0) {
2014-05-19 20:22:30 +00:00
block.distFromHex = (this.sideLength / 2) * Math.sqrt(3);
block.settled = 1;
2014-05-22 19:42:05 +00:00
consolidateBlocks(this, lane, block.getIndex());
2014-05-19 20:22:30 +00:00
}
} else {
2014-05-24 00:22:53 +00:00
if (block.distFromHex + block.iter - arr[position - 1].distFromHex - arr[position - 1].height <= 0) {
block.distFromHex = arr[position - 1].distFromHex + arr[position - 1].height;
2014-05-19 20:22:30 +00:00
block.settled = 1;
2014-05-22 19:42:05 +00:00
consolidateBlocks(this, lane, block.getIndex());
2014-05-19 20:22:30 +00:00
}
}
} else {
if (arr.length > 0) {
2014-05-24 00:22:53 +00:00
if (block.distFromHex + block.iter - arr[arr.length - 1].distFromHex - arr[arr.length - 1].height <= 0) {
block.distFromHex = arr[arr.length - 1].distFromHex + arr[arr.length - 1].height;
2014-05-19 20:22:30 +00:00
this.addBlock(block);
}
} else {
2014-05-24 00:22:53 +00:00
if (block.distFromHex + block.iter - (this.sideLength / 2) * Math.sqrt(3) <= 0) {
2014-05-19 20:22:30 +00:00
block.distFromHex = (this.sideLength / 2) * Math.sqrt(3);
this.addBlock(block);
}
}
}
};
this.rotate = function(steps) {
this.position += steps;
2014-05-22 20:36:32 +00:00
if (!history[count]) {
history[count] = {};
}
if (!history[count].rotate) {
history[count].rotate = steps;
}
else {
history[count].rotate += steps;
}
2014-05-19 20:22:30 +00:00
while (this.position < 0) {
this.position += 6;
}
this.position = this.position % this.sides;
this.blocks.forEach(function(blocks) {
blocks.forEach(function(block) {
block.targetAngle = block.targetAngle - steps * 60;
});
});
this.targetAngle = this.targetAngle - steps * 60;
};
this.draw = function() {
gdx = 0;
gdy = 0;
for (var i = 0; i < this.shakes.length; i++) {
this.shake(this.shakes[i]);
}
2014-05-19 20:22:30 +00:00
if (this.angle > this.targetAngle) {
this.angularVelocity -= angularVelocityConst;
}
else if(this.angle < this.targetAngle) {
this.angularVelocity += angularVelocityConst;
}
if (Math.abs(this.angle - this.targetAngle + this.angularVelocity) <= Math.abs(this.angularVelocity)) { //do better soon
this.angle = this.targetAngle;
this.angularVelocity = 0;
}
else {
this.angle += this.angularVelocity;
}
2014-05-20 02:16:43 +00:00
ctx.shadowColor = '#2980b9';
ctx.shadowBlur = 15;
2014-05-23 20:13:23 +00:00
drawPolygon(this.x + gdx, this.y + gdy + this.dy, this.sides, this.sideLength, this.angle, this.fillColor);
2014-05-20 02:16:43 +00:00
clearShadows();
2014-05-19 20:22:30 +00:00
};
2014-05-17 14:24:39 +00:00
}