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;
|
2014-05-25 05:11:24 +00:00
|
|
|
this.height = settings.blockHeight;
|
2014-05-25 14:20:32 +00:00
|
|
|
this.fallingLane = lane;
|
2014-05-19 20:22:30 +00:00
|
|
|
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-25 05:11:24 +00:00
|
|
|
this.tint = 0;
|
2014-05-24 19:35:38 +00:00
|
|
|
this.opacity = 1;
|
|
|
|
this.initializing = 1;
|
|
|
|
this.ct = 0;
|
2014-05-22 17:01:39 +00:00
|
|
|
this.parentArr;
|
2014-05-24 00:22:53 +00:00
|
|
|
this.iter = iter;
|
2014-05-24 19:41:44 +00:00
|
|
|
this.initLen = 9;
|
2014-05-25 14:03:28 +00:00
|
|
|
this.attachedLane;
|
2014-05-21 22:39:02 +00:00
|
|
|
|
2014-05-19 20:22:30 +00:00
|
|
|
if (distFromHex) {
|
|
|
|
this.distFromHex = distFromHex;
|
|
|
|
} else {
|
2014-05-25 05:11:24 +00:00
|
|
|
this.distFromHex = 320;
|
2014-05-19 20:22:30 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 17:01:39 +00:00
|
|
|
this.incrementOpacity = function() {
|
|
|
|
if (this.deleted) {
|
2014-05-23 00:44:14 +00:00
|
|
|
this.opacity = this.opacity - 0.1;
|
2014-05-22 17:01:39 +00:00
|
|
|
if (this.opacity <= 0) {
|
|
|
|
this.opacity = 0;
|
2014-05-24 22:28:32 +00:00
|
|
|
this.deleted = 2;
|
2014-05-22 17:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-24 22:28:32 +00:00
|
|
|
|
2014-05-24 19:48:48 +00:00
|
|
|
if(!this.deleted && this.opacity < 1){
|
2014-05-24 18:12:33 +00:00
|
|
|
this.opacity = this.opacity + 0.05;
|
|
|
|
}
|
2014-05-22 17:10:21 +00:00
|
|
|
};
|
2014-05-22 23:01:39 +00:00
|
|
|
|
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-22 17:01:39 +00:00
|
|
|
|
2014-05-19 20:22:30 +00:00
|
|
|
this.draw = function(attached, index) {
|
2014-05-22 17:01:39 +00:00
|
|
|
this.incrementOpacity();
|
2014-05-24 19:35:38 +00:00
|
|
|
if(attached === undefined)
|
2014-05-19 20:22:30 +00:00
|
|
|
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-24 19:35:38 +00:00
|
|
|
var p1;
|
|
|
|
var p2;
|
|
|
|
var p3;
|
|
|
|
var p4;
|
|
|
|
if (this.initializing) {
|
|
|
|
this.ct++;
|
|
|
|
var rat = (this.ct/this.initLen);
|
|
|
|
p1 = rotatePoint((-this.width / 2) * rat, this.height / 2, this.angle);
|
|
|
|
p2 = rotatePoint((this.width / 2) * rat, this.height / 2, this.angle);
|
|
|
|
p3 = rotatePoint((this.widthWide / 2) * rat, -this.height / 2, this.angle);
|
|
|
|
p4 = rotatePoint((-this.widthWide / 2) * rat, -this.height / 2, this.angle);
|
|
|
|
if (this.ct == this.initLen) {
|
|
|
|
this.initializing = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p1 = rotatePoint(-this.width / 2, this.height / 2, this.angle);
|
|
|
|
p2 = rotatePoint(this.width / 2, this.height / 2, this.angle);
|
|
|
|
p3 = rotatePoint(this.widthWide / 2, -this.height / 2, this.angle);
|
|
|
|
p4 = rotatePoint(-this.widthWide / 2, -this.height / 2, this.angle);
|
|
|
|
}
|
2014-05-19 20:22:30 +00:00
|
|
|
|
|
|
|
ctx.fillStyle = this.color;
|
2014-05-22 17:01:39 +00:00
|
|
|
ctx.globalAlpha = this.opacity;
|
2014-05-22 23:30:40 +00:00
|
|
|
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();
|
2014-05-22 17:01:39 +00:00
|
|
|
|
2014-05-22 17:10:21 +00:00
|
|
|
if (this.tint) {
|
2014-05-22 17:26:44 +00:00
|
|
|
if (this.opacity < 1) {
|
|
|
|
this.tint = 0;
|
|
|
|
}
|
2014-05-22 17:10:21 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 17:01:39 +00:00
|
|
|
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-24 19:35:38 +00:00
|
|
|
// t: current time, b: begInnIng value, c: change In value, d: duration
|
|
|
|
function easeOutCubic(t, b, c, d) {
|
|
|
|
return c*((t=t/d-1)*t*t + 1) + b;
|
|
|
|
}
|
|
|
|
|
2014-05-19 17:25:20 +00:00
|
|
|
var colorSounds = {"#e74c3c": new Audio("../sounds/lowest.ogg"),
|
2014-05-22 23:01:39 +00:00
|
|
|
"#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;
|
2014-05-25 14:03:28 +00:00
|
|
|
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;
|
2014-05-22 23:01:39 +00:00
|
|
|
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
|
|
|
|
2014-05-22 23:01:39 +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) {
|
2014-05-24 19:59:13 +00:00
|
|
|
if (gameState != 1) return;
|
2014-05-19 20:22:30 +00:00
|
|
|
block.settled = 1;
|
2014-05-22 23:01:39 +00:00
|
|
|
block.tint = 0.6;
|
2014-05-25 14:20:32 +00:00
|
|
|
var lane = this.sides - block.fallingLane;// -this.position;
|
|
|
|
this.shakes.push({lane:block.fallingLane, magnitude:2});
|
2014-05-19 20:22:30 +00:00
|
|
|
lane += this.position;
|
2014-05-24 17:11:17 +00:00
|
|
|
lane = (lane+this.sides) % this.sides;
|
2014-05-19 20:22:30 +00:00
|
|
|
block.distFromHex = MainClock.sideLength / 2 * Math.sqrt(3) + block.height * this.blocks[lane].length;
|
|
|
|
this.blocks[lane].push(block);
|
2014-05-25 14:03:28 +00:00
|
|
|
block.attachedLane = lane;
|
2014-05-22 17:01:39 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-25 14:03:28 +00:00
|
|
|
|
2014-05-19 20:22:30 +00:00
|
|
|
if (position !== undefined) {
|
|
|
|
arr = tArr;
|
|
|
|
if (position <= 0) {
|
2014-05-24 22:28:32 +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-25 14:03:28 +00:00
|
|
|
consolidateBlocks(this, block.attachedLane, block.getIndex());
|
2014-05-24 22:28:32 +00:00
|
|
|
} else {
|
|
|
|
block.settled = 0;
|
2014-05-19 20:22:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-24 22:28:32 +00:00
|
|
|
if (arr[position - 1].settled && block.distFromHex - block.iter - arr[position - 1].distFromHex - arr[position - 1].height <= 0) {
|
2014-05-19 20:36:28 +00:00
|
|
|
block.distFromHex = arr[position - 1].distFromHex + arr[position - 1].height;
|
2014-05-19 20:22:30 +00:00
|
|
|
block.settled = 1;
|
2014-05-25 14:03:28 +00:00
|
|
|
consolidateBlocks(this, block.attachedLane, block.getIndex());
|
2014-05-19 20:22:30 +00:00
|
|
|
}
|
2014-05-24 22:28:32 +00:00
|
|
|
else {
|
|
|
|
block.settled = 0;
|
|
|
|
}
|
2014-05-19 20:22:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-25 14:20:32 +00:00
|
|
|
var lane = this.sides - block.fallingLane;// -this.position;
|
2014-05-25 14:03:28 +00:00
|
|
|
lane += this.position;
|
|
|
|
|
|
|
|
lane = (lane+this.sides) % this.sides;
|
|
|
|
var arr = this.blocks[lane];
|
|
|
|
|
2014-05-19 20:22:30 +00:00
|
|
|
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) {
|
2014-05-19 20:36:28 +00:00
|
|
|
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) {
|
2014-05-24 20:35:32 +00:00
|
|
|
if (gameState != 1 && gameState != -2) return;
|
2014-05-19 20:22:30 +00:00
|
|
|
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-25 04:39:23 +00:00
|
|
|
this.position = (this.position + this.sides) % this.sides;
|
2014-05-19 20:22:30 +00:00
|
|
|
this.blocks.forEach(function(blocks) {
|
|
|
|
blocks.forEach(function(block) {
|
|
|
|
block.targetAngle = block.targetAngle - steps * 60;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.targetAngle = this.targetAngle - steps * 60;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.draw = function() {
|
2014-05-22 23:01:39 +00:00
|
|
|
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-24 19:06:57 +00:00
|
|
|
drawPolygon(this.x + gdx, this.y + gdy + this.dy, this.sides, this.sideLength, this.angle, this.fillColor, 0, 'rgba(0,0,0,0)');
|
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
|
|
|
}
|