hextris/js/entities.js

291 lines
8 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 = settings.blockHeight;
2014-05-19 20:22:30 +00:00
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;
this.tint = 0;
this.opacity = 1;
this.initializing = 1;
this.ct = 0;
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-21 22:39:02 +00:00
2014-05-19 20:22:30 +00:00
if (distFromHex) {
this.distFromHex = distFromHex;
} else {
this.distFromHex = 320;
2014-05-19 20:22:30 +00:00
}
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;
2014-05-24 17:11:17 +00:00
lane = (lane+MainClock.sides) % MainClock.sides;
2014-05-22 18:19:58 +00:00
2014-05-23 00:44:14 +00:00
this.opacity = this.opacity - 0.1;
if (this.opacity <= 0) {
this.opacity = 0;
2014-05-24 22:28:32 +00:00
this.deleted = 2;
}
}
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 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) {
this.incrementOpacity();
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;
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;
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
// 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"),
"#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 04:39:23 +00:00
this.position = 1;
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) {
2014-05-24 19:59:13 +00:00
if (gameState != 1) return;
2014-05-19 20:22:30 +00:00
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;
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);
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;
2014-05-24 17:11:17 +00:00
lane = (lane+this.sides) % this.sides;
2014-05-19 20:22:30 +00:00
var arr = this.blocks[lane];
if (position !== undefined) {
arr = tArr;
if (position <= 0) {
2014-05-24 22:28:32 +00:00
debugger;
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-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) {
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
}
2014-05-24 22:28:32 +00:00
else {
block.settled = 0;
}
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) {
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() {
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;
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
}