hextris/js/Hex.js

171 lines
4.9 KiB
JavaScript
Raw Normal View History

2014-07-04 16:42:44 +00:00
function Hex(sideLength) {
this.playThrough = 0;
2014-06-23 21:59:51 +00:00
this.fillColor = [44,62,80];
2014-06-24 00:58:07 +00:00
this.tempColor = [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;
2014-07-04 16:42:22 +00:00
this.lastCombo = this.ct - settings.comboTime;
this.lastColorScored = "#000";
2014-07-04 16:42:22 +00:00
this.comboTime = 1;
this.texts = [];
2014-07-04 17:23:00 +00:00
this.lastRotate = Date.now();
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 || gameState === 0)) return;
2014-05-26 02:50:13 +00:00
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;
2014-07-04 16:42:44 +00:00
block.distFromHex = MainHex.sideLength / 2 * Math.sqrt(3) + block.height * this.blocks[lane].length;
2014-05-26 02:50:13 +00:00
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;
block.iter = 1.5 + (waveone.difficulty/15) * 3;
2014-05-26 02:50:13 +00:00
}
} 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;
block.iter = 1.5 + (waveone.difficulty/15) * 3;
2014-05-26 02:50:13 +00:00
}
}
} 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) {
2014-07-05 01:20:43 +00:00
if(Date.now()-this.lastRotate<75 && !(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) ) return;
2014-07-01 15:18:13 +00:00
if (!(gameState === 1 || gameState === 0)) return;
2014-05-26 02:50:13 +00:00
this.position += steps;
if (!history[this.ct]) {
history[this.ct] = {};
2014-05-26 02:50:13 +00:00
}
if (!history[this.ct].rotate) {
history[this.ct].rotate = steps;
2014-05-26 02:50:13 +00:00
}
else {
history[this.ct].rotate += steps;
2014-05-26 02:50:13 +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;
2014-07-04 17:23:00 +00:00
this.lastRotate = Date.now();
2014-05-26 02:50:13 +00:00
};
this.draw = function() {
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-24 00:58:07 +00:00
drawPolygon(this.x + gdx, this.y + gdy + this.dy, this.sides, this.sideLength, this.angle,arrayToColor(this.fillColor) , 0, 'rgba(0,0,0,0)');
2014-05-26 02:50:13 +00:00
};
}
2014-06-24 00:58:07 +00:00
2014-06-23 21:59:51 +00:00
function arrayToColor(arr){
2014-06-23 22:35:28 +00:00
return 'rgb(' + arr[0]+ ','+arr[1]+','+arr[2]+')';
2014-06-23 21:59:51 +00:00
}