hextris/js/checking.js

88 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-05-20 00:47:19 +00:00
function search(twoD,oneD){
2014-05-26 01:47:21 +00:00
// Searches a two dimensional array to see if it contains a one dimensional array. indexOf doesn't work in this case
2014-05-20 00:47:19 +00:00
for(var i=0;i<twoD.length;i++){
if(twoD[i][0] == oneD[0] && twoD[i][1] == oneD[1]) {
return true;
2014-05-19 14:36:13 +00:00
}
}
return false;
2014-05-17 16:39:49 +00:00
}
2014-07-04 16:47:37 +00:00
function floodFill(hex, side, index, deleting) {
if (hex.blocks[side] === undefined || hex.blocks[side][index] === undefined) {
2014-05-26 01:47:21 +00:00
//just makin sure stuff exists
return;
}
2014-05-26 01:47:21 +00:00
//store the color
2014-07-04 16:47:37 +00:00
var color = hex.blocks[side][index].color;
2014-05-26 01:47:21 +00:00
//nested for loops for navigating the blocks
2014-05-20 00:47:19 +00:00
for(var x =-1;x<2;x++){
for(var y =-1;y<2;y++){
2014-05-26 01:47:21 +00:00
//make sure the they aren't diagonals
2014-05-20 21:09:39 +00:00
if(Math.abs(x)==Math.abs(y)){continue;}
2014-05-26 01:47:21 +00:00
//calculate the side were exploring using mods
2014-07-04 16:47:37 +00:00
var curSide =(side+x+hex.sides)%hex.sides;
2014-05-26 01:47:21 +00:00
//calculate the index
2014-05-24 17:25:33 +00:00
var curIndex = index+y;
2014-05-26 01:47:21 +00:00
//making sure the block exists at this side and index
2014-07-04 16:47:37 +00:00
if(hex.blocks[curSide] === undefined){continue;}
if(hex.blocks[curSide][curIndex] !== undefined){
2014-05-26 01:47:21 +00:00
// checking equivalency of color, if its already been explored, and if it isn't already deleted
if(hex.blocks[curSide][curIndex].color == color && search(deleting,[curSide,curIndex]) === false && hex.blocks[curSide][curIndex].deleted === 0 ) {
2014-05-26 01:47:21 +00:00
//add this to the array of already explored
deleting.push([curSide,curIndex]);
2014-05-26 01:47:21 +00:00
//recall with next block explored
2014-07-04 16:47:37 +00:00
floodFill(hex,curSide,curIndex,deleting);
2014-05-19 14:36:13 +00:00
}
}
}
}
2014-05-20 00:47:19 +00:00
}
2014-05-24 19:39:27 +00:00
2014-07-04 16:47:37 +00:00
function consolidateBlocks(hex,side,index){
2014-05-26 01:47:21 +00:00
//record which sides have been changed
2014-05-20 00:47:19 +00:00
var sidesChanged =[];
2014-05-24 16:48:46 +00:00
var deleting=[];
var deletedBlocks = [];
2014-05-26 01:47:21 +00:00
//add start case
2014-05-20 00:47:19 +00:00
deleting.push([side,index]);
2014-05-26 01:47:21 +00:00
//fill deleting
2014-07-04 16:47:37 +00:00
floodFill(hex,side,index,deleting);
2014-05-26 01:47:21 +00:00
//make sure there are more than 3 blocks to be deleted
if(deleting.length<3){return;}
var i;
for(i=0; i<deleting.length;i++) {
2014-05-26 01:47:21 +00:00
var arr = deleting[i];
//just making sure the arrays are as they should be
2014-05-20 00:47:19 +00:00
if(arr !== undefined && arr.length==2) {
2014-05-26 01:47:21 +00:00
//add to sides changed if not in there
if(sidesChanged.indexOf(arr[0])==-1){
2014-05-20 00:47:19 +00:00
sidesChanged.push(arr[0]);
2014-05-19 14:36:13 +00:00
}
2014-05-26 01:47:21 +00:00
//mark as deleted
2014-07-04 16:47:37 +00:00
hex.blocks[arr[0]][arr[1]].deleted = 1;
deletedBlocks.push(hex.blocks[arr[0]][arr[1]]);
2014-05-19 14:36:13 +00:00
}
}
2014-07-06 01:05:21 +00:00
2014-05-26 01:47:21 +00:00
// add scores
2014-07-04 16:42:44 +00:00
var now = MainHex.ct;
2014-07-26 23:58:58 +00:00
if(now - hex.lastCombo < settings.comboTime ){
settings.comboTime = waveone.nextGen/16* 2;
hex.comboMultiplier += 1;
2014-07-04 16:47:37 +00:00
hex.lastCombo = now;
2014-05-26 17:04:23 +00:00
var coords = findCenterOfBlocks(deletedBlocks);
2014-07-04 16:47:37 +00:00
hex.texts.push(new Text(coords['x'],coords['y'],"x "+hex.comboMultiplier.toString(),"bold Q","#fff",fadeUpAndOut));
2014-05-26 03:12:47 +00:00
}
else{
2014-07-26 23:58:58 +00:00
settings.comboTime = 240;
2014-07-04 16:47:37 +00:00
hex.lastCombo = now;
hex.comboMultiplier = 1;
2014-05-26 03:12:47 +00:00
}
2014-07-04 16:47:37 +00:00
var adder = deleting.length * deleting.length * hex.comboMultiplier;
hex.texts.push(new Text(hex.x,hex.y,"+ "+adder.toString(),"bold Q ",deletedBlocks[0].color,fadeUpAndOut));
hex.lastColorScored = deletedBlocks[0].color;
2014-05-26 16:27:30 +00:00
score += adder;
2014-05-17 16:39:49 +00:00
}