hextris/js/Clock.js

174 lines
4.8 KiB
JavaScript
Raw Normal View History

2014-05-26 02:50:13 +00:00
function Clock(sideLength) {
2014-06-23 22:35:28 +00:00
this.fillColor = [44,62,80];
2014-05-26 02:50:13 +00:00
this.angularVelocity = 0;
this.position = 0;
this.dy = 0;
this.sides = 6;
this.blocks = [];
this.angle = 180 / this.sides;
this.targetAngle = this.angle;
this.shakes = [];
this.sideLength = sideLength;
this.strokeColor = 'blue';
this.x = trueCanvas.width / 2;
this.y = trueCanvas.height / 2;
2014-05-26 18:07:07 +00:00
this.ct = 0;
this.lastCombo = this.ct - 160;
2014-05-26 03:12:47 +00:00
this.comboMultiplier = 1;
this.texts = [];
2014-05-26 02:50:13 +00:00
for (var i = 0; i < this.sides; i++) {
this.blocks.push([]);
}
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);
}
}
}
};
this.addBlock = function(block) {
if (gameState != 1) return;
block.settled = 1;
block.tint = 0.6;
var lane = this.sides - block.fallingLane;// -this.position;
this.shakes.push({lane:block.fallingLane, magnitude:4.5 * (window.devicePixelRatio ? window.devicePixelRatio : 1) * (settings.scale)});
2014-05-26 02:50:13 +00:00
lane += this.position;
lane = (lane + this.sides) % this.sides;
block.distFromHex = MainClock.sideLength / 2 * Math.sqrt(3) + block.height * this.blocks[lane].length;
this.blocks[lane].push(block);
block.attachedLane = lane;
block.checked = 1;
2014-05-26 02:50:13 +00:00
};
this.doesBlockCollide = function(block, position, tArr) {
if (block.settled) {
return;
}
if (position !== undefined) {
arr = tArr;
if (position <= 0) {
if (block.distFromHex - block.iter * settings.scale - (this.sideLength / 2) * Math.sqrt(3) <= 0) {
block.distFromHex = (this.sideLength / 2) * Math.sqrt(3);
block.settled = 1;
block.checked = 1;
2014-05-26 02:50:13 +00:00
} else {
block.settled = 0;
}
} else {
if (arr[position - 1].settled && block.distFromHex - block.iter * settings.scale - arr[position - 1].distFromHex - arr[position - 1].height <= 0) {
block.distFromHex = arr[position - 1].distFromHex + arr[position - 1].height;
block.settled = 1;
block.checked = 1;
2014-05-26 02:50:13 +00:00
}
else {
block.settled = 0;
}
}
} else {
var lane = this.sides - block.fallingLane;// -this.position;
lane += this.position;
lane = (lane+this.sides) % this.sides;
var arr = this.blocks[lane];
if (arr.length > 0) {
if (block.distFromHex + block.iter * settings.scale - arr[arr.length - 1].distFromHex - arr[arr.length - 1].height <= 0) {
block.distFromHex = arr[arr.length - 1].distFromHex + arr[arr.length - 1].height;
this.addBlock(block);
}
} else {
if (block.distFromHex + block.iter * settings.scale - (this.sideLength / 2) * Math.sqrt(3) <= 0) {
block.distFromHex = (this.sideLength / 2) * Math.sqrt(3);
this.addBlock(block);
}
}
}
};
this.rotate = function(steps) {
if (gameState != 1 && gameState != -2) return;
this.position += steps;
if (!history[count]) {
history[count] = {};
}
if (!history[count].rotate) {
history[count].rotate = steps;
}
else {
history[count].rotate += steps;
}
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() {
this.ct++;
2014-05-26 02:50:13 +00:00
this.x = trueCanvas.width/2;
if (gameState != -2) {
2014-05-26 02:50:13 +00:00
this.y = trueCanvas.height/2;
}
this.sideLength = settings.hexWidth;
gdx = 0;
gdy = 0;
for (var i = 0; i < this.shakes.length; i++) {
this.shake(this.shakes[i]);
}
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-06-23 22:35:28 +00:00
drawPolygon(this.x + gdx, this.y + gdy + this.dy, this.sides, this.sideLength, this.angle,arrayToColor(tint(this)) , 0, 'rgba(0,0,0,0)');
2014-05-26 02:50:13 +00:00
};
}
2014-06-23 22:35:28 +00:00
function arrayToColor(arr){
return 'rgb(' + arr[0]+ ','+arr[1]+','+arr[2]+')';
}
function tint(clock){
var n = [];
2014-06-23 22:40:41 +00:00
var outerHexagon =[236,240,241];
2014-06-23 22:35:28 +00:00
if(clock.ct -clock.lastCombo<160){
for(var i=0;i<3;i++){
2014-06-23 22:40:41 +00:00
n.push( Math.ceil(clock.fillColor[i]+((outerHexagon[i]-clock.fillColor[i])/160)*(160-(clock.ct-clock.lastCombo))));
2014-06-23 22:35:28 +00:00
}
return n;
}
return clock.fillColor;
}