2014-05-26 02:50:13 +00:00
|
|
|
function Block(fallingLane, color, iter, distFromHex, settled) {
|
2014-07-04 16:47:37 +00:00
|
|
|
// whether or not a block is rested on the center hex or another block
|
2014-05-26 02:50:13 +00:00
|
|
|
this.settled = (settled === undefined) ? 0 : 1;
|
|
|
|
this.height = settings.blockHeight;
|
|
|
|
//the lane which the block was shot from
|
|
|
|
this.fallingLane = fallingLane;
|
2014-05-30 23:51:53 +00:00
|
|
|
|
2014-08-18 23:12:12 +00:00
|
|
|
this.checked=0;
|
2014-05-26 02:50:13 +00:00
|
|
|
//the angle at which the block falls
|
|
|
|
this.angle = 90 - (30 + 60 * fallingLane);
|
2014-07-04 16:47:37 +00:00
|
|
|
//for calculating the rotation of blocks attached to the center hex
|
2014-05-26 02:50:13 +00:00
|
|
|
this.angularVelocity = 0;
|
|
|
|
this.targetAngle = this.angle;
|
|
|
|
this.color = color;
|
|
|
|
//blocks that are slated to be deleted after a valid score has happened
|
|
|
|
this.deleted = 0;
|
2014-07-04 16:47:37 +00:00
|
|
|
//blocks slated to be removed from falling and added to the hex
|
2014-05-26 02:50:13 +00:00
|
|
|
this.removed = 0;
|
2014-07-04 16:47:37 +00:00
|
|
|
//value for the opacity of the white blcok drawn over falling block to give it the glow as it attaches to the hex
|
2014-05-26 02:50:13 +00:00
|
|
|
this.tint = 0;
|
|
|
|
//value used for deletion animation
|
|
|
|
this.opacity = 1;
|
|
|
|
//boolean for when the block is expanding
|
|
|
|
this.initializing = 1;
|
2014-07-04 16:42:44 +00:00
|
|
|
this.ict = MainHex.ct;
|
2014-05-26 02:50:13 +00:00
|
|
|
//speed of block
|
2014-06-29 18:35:10 +00:00
|
|
|
this.iter = iter;
|
2014-05-26 02:50:13 +00:00
|
|
|
//number of iterations before starting to drop
|
|
|
|
this.initLen = settings.creationDt;
|
|
|
|
//side which block is attached too
|
2014-06-28 16:01:41 +00:00
|
|
|
this.attachedLane = 0;
|
2014-07-04 16:47:37 +00:00
|
|
|
//distance from center hex
|
2014-05-26 02:50:13 +00:00
|
|
|
this.distFromHex = distFromHex || settings.startDist * settings.scale ;
|
|
|
|
|
|
|
|
this.incrementOpacity = function() {
|
|
|
|
if (this.deleted) {
|
|
|
|
//add shakes
|
2014-06-28 16:01:41 +00:00
|
|
|
if (this.opacity >= 0.925) {
|
2014-07-04 16:42:44 +00:00
|
|
|
var tLane = this.attachedLane - MainHex.position;
|
|
|
|
tLane = MainHex.sides - tLane;
|
2014-05-26 02:50:13 +00:00
|
|
|
while (tLane < 0) {
|
2014-07-04 16:42:44 +00:00
|
|
|
tLane += MainHex.sides;
|
2014-05-26 02:50:13 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 16:42:44 +00:00
|
|
|
tLane %= MainHex.sides;
|
|
|
|
MainHex.shakes.push({lane:tLane, magnitude:3 * (window.devicePixelRatio ? window.devicePixelRatio : 1) * (settings.scale)});
|
2014-05-26 02:50:13 +00:00
|
|
|
}
|
|
|
|
//fade out the opacity
|
|
|
|
this.opacity = this.opacity - 0.075;
|
|
|
|
if (this.opacity <= 0) {
|
|
|
|
//slate for final deletion
|
|
|
|
this.opacity = 0;
|
|
|
|
this.deleted = 2;
|
2014-07-19 23:12:28 +00:00
|
|
|
if (gameState == 1 || gameState==0) {
|
2014-06-28 17:44:43 +00:00
|
|
|
localStorage.setItem("saveState", exportSaveState());
|
|
|
|
}
|
2014-05-26 02:50:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getIndex = function (){
|
|
|
|
//get the index of the block in its stack
|
2014-07-04 16:42:44 +00:00
|
|
|
var parentArr = MainHex.blocks[this.attachedLane];
|
2014-05-26 02:50:13 +00:00
|
|
|
for (var i = 0; i < parentArr.length; i++) {
|
|
|
|
if (parentArr[i] == this) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.draw = function(attached, index) {
|
|
|
|
this.height = settings.blockHeight;
|
2014-06-24 00:55:04 +00:00
|
|
|
if (Math.abs(settings.scale - settings.prevScale) > 0.000000001) {
|
2014-05-26 02:50:13 +00:00
|
|
|
this.distFromHex *= (settings.scale/settings.prevScale);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.incrementOpacity();
|
|
|
|
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;
|
|
|
|
}
|
2014-07-03 22:25:09 +00:00
|
|
|
|
2014-05-26 02:50:13 +00:00
|
|
|
this.width = 2 * this.distFromHex / Math.sqrt(3);
|
2014-07-27 12:27:15 +00:00
|
|
|
this.widthWide = 2 * (this.distFromHex + this.height) / Math.sqrt(3);
|
|
|
|
//this.widthWide = this.width + this.height + 3;
|
2014-05-26 02:50:13 +00:00
|
|
|
var p1;
|
|
|
|
var p2;
|
|
|
|
var p3;
|
|
|
|
var p4;
|
|
|
|
if (this.initializing) {
|
2014-07-04 16:42:44 +00:00
|
|
|
var rat = ((MainHex.ct - this.ict)/this.initLen);
|
2014-05-26 02:50:13 +00:00
|
|
|
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);
|
2014-07-04 16:42:44 +00:00
|
|
|
if ((MainHex.ct - this.ict) == this.initLen) {
|
2014-05-26 02:50:13 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.deleted) {
|
|
|
|
ctx.fillStyle = "#FFF";
|
2014-06-24 00:55:04 +00:00
|
|
|
} else if (gameState === 0) {
|
2014-06-24 02:26:00 +00:00
|
|
|
if (this.color.charAt(0) == 'r') {
|
|
|
|
ctx.fillStyle = rgbColorsToTintedColors[this.color];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ctx.fillStyle = hexColorsToTintedColors[this.color];
|
|
|
|
}
|
2014-06-24 00:55:04 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-05-26 02:50:13 +00:00
|
|
|
ctx.fillStyle = this.color;
|
|
|
|
}
|
2014-07-02 02:29:53 +00:00
|
|
|
|
2014-05-26 02:50:13 +00:00
|
|
|
ctx.globalAlpha = this.opacity;
|
|
|
|
var baseX = trueCanvas.width / 2 + Math.sin((this.angle) * (Math.PI / 180)) * (this.distFromHex + this.height / 2) + gdx;
|
|
|
|
var baseY = trueCanvas.height / 2 - Math.cos((this.angle) * (Math.PI / 180)) * (this.distFromHex + this.height / 2) + gdy;
|
|
|
|
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);
|
2014-07-27 12:27:15 +00:00
|
|
|
//ctx.lineTo(baseX + p1.x, baseY + p1.y);
|
2014-05-26 02:50:13 +00:00
|
|
|
ctx.closePath();
|
|
|
|
ctx.fill();
|
|
|
|
|
|
|
|
if (this.tint) {
|
|
|
|
if (this.opacity < 1) {
|
2014-07-19 23:12:28 +00:00
|
|
|
if (gameState == 1 || gameState==0) {
|
2014-06-28 17:44:43 +00:00
|
|
|
localStorage.setItem("saveState", exportSaveState());
|
|
|
|
}
|
2014-06-29 18:35:10 +00:00
|
|
|
|
|
|
|
this.iter = 2.25;
|
2014-05-26 02:50:13 +00:00
|
|
|
this.tint = 0;
|
|
|
|
}
|
2014-07-02 02:29:53 +00:00
|
|
|
|
2014-05-26 02:50:13 +00:00
|
|
|
ctx.fillStyle = "#FFF";
|
|
|
|
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-26 16:49:20 +00:00
|
|
|
|
|
|
|
function findCenterOfBlocks(arr) {
|
|
|
|
var avgDFH = 0;
|
|
|
|
var avgAngle = 0;
|
|
|
|
for (var i = 0; i < arr.length; i++) {
|
|
|
|
avgDFH += arr[i].distFromHex;
|
2014-05-26 17:09:59 +00:00
|
|
|
var ang = arr[i].angle;
|
|
|
|
while (ang < 0) {
|
|
|
|
ang += 360;
|
2014-05-26 16:49:20 +00:00
|
|
|
}
|
|
|
|
|
2014-05-26 17:09:59 +00:00
|
|
|
avgAngle += ang % 360;
|
2014-05-26 16:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
avgDFH /= arr.length;
|
|
|
|
avgAngle /= arr.length;
|
|
|
|
|
|
|
|
return {
|
|
|
|
x:trueCanvas.width/2 + Math.cos(avgAngle * (Math.PI / 180)) * avgDFH,
|
2014-05-26 17:18:10 +00:00
|
|
|
y:trueCanvas.height/2 + Math.sin(avgAngle * (Math.PI / 180)) * avgDFH
|
2014-05-26 17:30:39 +00:00
|
|
|
};
|
2014-05-26 17:04:23 +00:00
|
|
|
}
|