This commit is contained in:
lengstrom 2014-05-17 15:02:19 -04:00
commit b2f379994f
5 changed files with 114 additions and 21 deletions

View file

@ -1,26 +1,31 @@
function getIndex(list,index)
{
if(index>-1)
function getIndex(list,index) {
console.log(" getIndex");
if(index>-1) {
return index%list.length;
else
}
else {
return list.length+index;
}
}
function scoreCheckHorizontal(clock,side,index) {
console.log(" scoreCheckHorizontal");
clockSides = clock.blocks;
clockSides[0];
if(clockSides[getIndex(clockSides,side)][index] && clockSides[getIndex(clockSides,side-1)][index] && clockSides[getIndex(clockSides,side-2)][index]) {
if(clockSides[getIndex(clockSides,side)][index].color == clockSides[getIndex(clockSides,side-1)][index].color&& clockSides[getIndex(clockSides,side-1)][index].color === clockSides[getIndex(clockSides,side-2)][index].color) {
console.log(1);
if(clockSides[getIndex(clockSides,side)][index].color == clockSides[getIndex(clockSides,side-1)][index].color && clockSides[getIndex(clockSides,side-1)][index].color == clockSides[getIndex(clockSides,side-2)][index].color) {
return -2;
}
}
console.log(clockSides[getIndex(clockSides,side)][index],clockSides[getIndex(clockSides,side)][index-1],clockSides[getIndex(clockSides,side)][index+1])
if(clockSides[getIndex(clockSides,side)][index] && clockSides[getIndex(clockSides,side+1)][index] && clockSides[getIndex(clockSides,side-1)][index]) {
if(clockSides[getIndex(clockSides,side)][index].color == clockSides[getIndex(clockSides,side+1)][index].color&& clockSides[getIndex(clockSides,side+1)][index].color === clockSides[getIndex(clockSides,side-1)][index].color) {
console.log(2);
if(clockSides[getIndex(clockSides,side)][index].color == clockSides[getIndex(clockSides,side+1)][index].color && clockSides[getIndex(clockSides,side+1)][index].color == clockSides[getIndex(clockSides,side-1)][index].color) {
return -1;
}
}
if(clockSides[getIndex(clockSides,side)][index] && clockSides[getIndex(clockSides,side+1)][index] && clockSides[getIndex(clockSides,side+2)][index]) {
if(clockSides[getIndex(clockSides,side)][index].color == clockSides[getIndex(clockSides,side+1)][index].color&& clockSides[getIndex(clockSides,side+1)][index].color === clockSides[getIndex(clockSides,side+2)][index].color) {
console.log(3);
if(clockSides[getIndex(clockSides,side)][index].color == clockSides[getIndex(clockSides,side+1)][index].color && clockSides[getIndex(clockSides,side+1)][index].color == clockSides[getIndex(clockSides,side+2)][index].color) {
return 0;
}
}
@ -28,6 +33,7 @@ function scoreCheckHorizontal(clock,side,index) {
}
function scoreCheckVertical(clock,side,index) {
console.log(" scoreCheckVertical");
curSide = clock.blocks[side];
if(curSide[index] && curSide[index-2] && curSide[index-1]) {
if(curSide[index].color == curSide[index-2].color && curSide[index-2].color ==curSide[index-1].color) {
@ -49,6 +55,7 @@ function scoreCheckVertical(clock,side,index) {
return "false";
}
function consolidateBlocks(clock,side,index) {
console.log(" consolidateBlocks");
horizontal = scoreCheckHorizontal(clock,side,index);
vertical = scoreCheckVertical(clock,side,index);
deleted = [];
@ -67,22 +74,28 @@ function consolidateBlocks(clock,side,index) {
eraseBlocks(clock,deleted);
}
function eraseBlocks(clock,deleted) {
console.log(" eraseBlocks");
if(deleted[0].length>0){
side = deleted[0][0];
index = deleted[0][1];
horizontal = deleted[0][2];
for(var i=0;i<3;i++) {
clock.blocks[getIndex(clock.sides,side+horizontal+i)].splice(index,1);
console.log(side+horizontal+i);
clock.blocks[getIndex(clock.blocks,side+horizontal+i)].splice(index,1);
}
for(var i=0;i<3;i++) {
consolidateBlocks(clock,side+horizontal+i,index);
if(side+horizontal+i<clock.blocks.length) {
consolidateBlocks(clock,getIndex(clock.blocks,side+horizontal+i),index);
}
}
}
if(deleted[1].length>0){
side = deleted[1][0];
index = deleted[1][1];
vertical = deleted[1][2];
clock.blocks[side].splice(index+vertical,3);
consolidateBlocks(clock,side,index+vertical);
clock.blocks[side].splice(index+vertical,2+(1*(!deleted[0].length>0)));
for(var i=0; i<clock.blocks[side].length-(index+vertical); i++) {
consolidateBlocks(clock,side,index+vertical+i);
}
}
}

View file

@ -69,6 +69,7 @@ var Clock = function(sideLength) {
while(lane < 0) {
lane = lane + this.sides;
}
block.distFromHex = MainClock.sideLength / 2 * Math.sqrt(3) + block.height * this.blocks[lane].length;
this.blocks[lane].push(block);
consolidateBlocks(this,lane,this.blocks.length-1);
};
@ -76,9 +77,14 @@ var Clock = function(sideLength) {
this.doesBlockCollide = function(block, iter, index) {
if (block.settled) return;
var arr = this.blocks[(block.lane + this.position % this.sides) % this.sides];
<<<<<<< HEAD
var thisIn = index === undefined ? arr.length - 1 : index - 1;
if (arr.length > 0 && thisIn > 0) {
if (block.distFromHex + iter - arr[thisIn].distFromHex - arr[thisIn].height <= 0) {
=======
if (arr.length > 0) {
if (block.distFromHex + iter - arr[arr.length - 1].distFromHex - arr[arr.length - 1].height <= 0) {
>>>>>>> b843b427127cb9456cba537938f2cd8691013d35
this.addBlock(block);
}
}
@ -92,11 +98,21 @@ var Clock = function(sideLength) {
};
this.rotate = function(steps) {
var oldPosition = this.position;
this.position += steps;
this.position = this.position % this.sides;
while(this.position < 0) {
this.position = this.position + this.sides;
}
for(var i=0; i<this.blocks.length; i++) {
for(var j=0; j<this.blocks[i].length; j++) {
this.blocks[i][j].lane += (this.position - oldPosition);
this.blocks[i][j].lane = this.blocks[i][j].lane % this.sides;
while(this.blocks[i][j].lane < 0) {
this.blocks[i][j].lane += this.sides;
}
}
}
this.angle = 30 + this.position * 60;
};

View file

@ -1,9 +1,9 @@
keypress.register_combo({
keys: "right",
keys: "left",
on_keyup: function(){MainClock.rotate(1)},
});
keypress.register_combo({
keys: "left",
keys: "right",
on_keyup: function(){MainClock.rotate(-1)},
});

72
main.js
View file

@ -14,14 +14,23 @@ var clock = new Clock(6);
var blocks = [];
for (var i = 0; i < 1; i++) {
blocks.push(new Block(i, 'green'));
}
var MainClock = new Clock(65);
var iter = 1;
var iter = 1/100;
var lastGen = Date.now();
var nextGen = 1000;
var colors = ["green", "red"];
function Render() {
var now = Date.now();
if(now - lastGen > nextGen) {
console.log("YES coachh");
blocks.push(new Block(randInt(0, 5), colors[randInt(0, colors.length-1)]));
lastGen = Date.now();
nextGen = randInt(100, 500);
console.log(nextGen);
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
var objectsToRemove = [];
MainClock.blocks.forEach(function(hexBlocks){
@ -72,4 +81,55 @@ function drawPolygon(x, y, sides, radius, theta) {
ctx.closePath();
ctx.fill();
ctx.stroke();
}
}
function Block(lane, color, distFromHex, settled) {
this.settled = (settled == undefined) ? 0 : 1;
this.height = 20;
this.width = 65;
this.lane = lane;
this.angle = 90 - (30 + 60 * lane);
if (this.angle < 0) {
this.angle += 360;
}
this.color = color;
if (distFromHex) {
this.distFromHex = distFromHex;
}
else {
this.distFromHex = 300;
}
this.draw = function() {
this.angle = 90 - (30 + 60 * this.lane);
this.width = 2 * this.distFromHex / Math.sqrt(3);
this.widthswag = this.width + this.height + 5;
var p1 = rotatePoint(-this.width/2, this.height/2, this.angle);
var p2 = rotatePoint(this.width/2, this.height/2, this.angle);
var p3 = rotatePoint(this.widthswag/2, -this.height/2, this.angle);
var p4 = rotatePoint(-this.widthswag/2, -this.height/2, this.angle);
ctx.fillStyle=this.color;
var baseX = canvas.width/2 + Math.sin((this.angle) * (Math.PI/180)) * (this.distFromHex + this.height/2);
var baseY = canvas.height/2 - Math.cos((this.angle) * (Math.PI/180)) * (this.distFromHex + this.height/2);
ctx.beginPath();
ctx.moveTo(Math.round(baseX + p1.x), Math.round(baseY + p1.y));
ctx.lineTo(Math.round(baseX + p2.x), Math.round(baseY + p2.y));
ctx.lineTo(Math.round(baseX + p3.x), Math.round(baseY + p3.y));
ctx.lineTo(Math.round(baseX + p4.x), Math.round(baseY + p4.y));
ctx.lineTo(Math.round(baseX + p1.x), Math.round(baseY + p1.y));
ctx.closePath();
ctx.fill();
// ctx.strokeStyle = '#322'
// ctx.beginPath();
// ctx.moveTo(canvas.width/2, canvas.height/2);
// ctx.lineTo(canvas.width/2 + Math.sin((this.angle) * (Math.PI/180)) * (this.distFromHex + this.height), canvas.height/2 - Math.cos((this.angle) * (Math.PI/180)) * (this.distFromHex + this.height));
// ctx.closePath();
// ctx.stroke();
};
}

View file

@ -9,3 +9,7 @@ function rotatePoint(x, y, theta) {
};
}
function randInt(min, max) {
return Math.floor((Math.random() * max) + min);
}