ApplyRules
This commit is contained in:
@@ -49,6 +49,8 @@ var CandyFucker = function(idScreen){
|
|||||||
this.Grid = null;
|
this.Grid = null;
|
||||||
this.GridOffset = {X: 0, Y: 0};
|
this.GridOffset = {X: 0, Y: 0};
|
||||||
this.CandyTypes = ["Red", "Blue", "Cyan", "Green", "Yellow"];
|
this.CandyTypes = ["Red", "Blue", "Cyan", "Green", "Yellow"];
|
||||||
|
this.Locked = false;
|
||||||
|
this.Falling = false;
|
||||||
this.Changed = false;
|
this.Changed = false;
|
||||||
|
|
||||||
window.Images.LoadImages(
|
window.Images.LoadImages(
|
||||||
@@ -70,7 +72,31 @@ CandyFucker.prototype = {
|
|||||||
this.BuildGrid(15, 15);
|
this.BuildGrid(15, 15);
|
||||||
},
|
},
|
||||||
Proc: function(gameScreen){
|
Proc: function(gameScreen){
|
||||||
|
if(this.Locked){
|
||||||
|
if(this.Falling){
|
||||||
|
if(!this.CandyFall()){
|
||||||
|
this.Falling = false;
|
||||||
|
console.log("Stoped");
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
if(this.ApplyRules()){
|
||||||
|
this.Falling = true;
|
||||||
|
console.log("Falling");
|
||||||
|
}else{
|
||||||
|
this.Locked = false;
|
||||||
|
console.log("Stoped");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
if(this.Changed){
|
||||||
|
if(this.ApplyRules()){
|
||||||
|
this.Locked = true;
|
||||||
|
this.Falling = true;
|
||||||
|
console.log("Falling");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.Changed = false;
|
||||||
},
|
},
|
||||||
End: function(gameScreen){ },
|
End: function(gameScreen){ },
|
||||||
GetCandy: function(x, y){
|
GetCandy: function(x, y){
|
||||||
@@ -106,6 +132,138 @@ CandyFucker.prototype = {
|
|||||||
}
|
}
|
||||||
this.Changed = true;
|
this.Changed = true;
|
||||||
},
|
},
|
||||||
|
ScanHorizontalRuns: function(){
|
||||||
|
var x,y;
|
||||||
|
var xPrev,yPrev;
|
||||||
|
var currentColor;
|
||||||
|
var prevColor;
|
||||||
|
var horizontalRuns = [];
|
||||||
|
for(y=0;y<this.GridSize.Y;y++){
|
||||||
|
yPrev = y;
|
||||||
|
xPrev = 0;
|
||||||
|
prevColor = "";
|
||||||
|
for(x=0;x<this.GridSize.X;x++){
|
||||||
|
var candy = this.GetCandy(x, y);
|
||||||
|
if(candy){
|
||||||
|
currentColor = candy.Color;
|
||||||
|
}else{
|
||||||
|
currentColor = "";
|
||||||
|
}
|
||||||
|
if(currentColor != prevColor){
|
||||||
|
if((x-xPrev)>2){
|
||||||
|
if(prevColor!=""){
|
||||||
|
// Run found
|
||||||
|
horizontalRuns.push({
|
||||||
|
Start: {X: xPrev, Y: yPrev},
|
||||||
|
End: {X: x-1, Y: y},
|
||||||
|
Color: prevColor
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xPrev = x;
|
||||||
|
yPrev = y;
|
||||||
|
prevColor = currentColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if((x-xPrev)>2){
|
||||||
|
if(prevColor!=""){
|
||||||
|
// Run found
|
||||||
|
horizontalRuns.push({
|
||||||
|
Start: {X: xPrev, Y: yPrev},
|
||||||
|
End: {X: x-1, Y: y},
|
||||||
|
Color: prevColor
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return horizontalRuns;
|
||||||
|
},
|
||||||
|
ScanVerticalRuns: function(){
|
||||||
|
var x,y;
|
||||||
|
var xPrev,yPrev;
|
||||||
|
var currentColor;
|
||||||
|
var prevColor;
|
||||||
|
var verticalRuns = [];
|
||||||
|
for(x=0;x<this.GridSize.X;x++){
|
||||||
|
yPrev = 0;
|
||||||
|
xPrev = x;
|
||||||
|
prevColor = "";
|
||||||
|
for(y=0;y<this.GridSize.Y;y++){
|
||||||
|
var candy = this.GetCandy(x, y);
|
||||||
|
if(candy){
|
||||||
|
currentColor = candy.Color;
|
||||||
|
}else{
|
||||||
|
currentColor = "";
|
||||||
|
}
|
||||||
|
if(currentColor != prevColor){
|
||||||
|
if((y-yPrev)>2){
|
||||||
|
if(prevColor!=""){
|
||||||
|
// Run found
|
||||||
|
verticalRuns.push({
|
||||||
|
Start: {X: xPrev, Y: yPrev},
|
||||||
|
End: {X: x, Y: y-1},
|
||||||
|
Color: prevColor
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xPrev = x;
|
||||||
|
yPrev = y;
|
||||||
|
prevColor = currentColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if((y-yPrev)>2){
|
||||||
|
if(prevColor!=""){
|
||||||
|
// Run found
|
||||||
|
verticalRuns.push({
|
||||||
|
Start: {X: xPrev, Y: yPrev},
|
||||||
|
End: {X: x, Y: y-1},
|
||||||
|
Color: prevColor
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return verticalRuns;
|
||||||
|
},
|
||||||
|
RemoveRuns: function(runs){
|
||||||
|
var pointsMultiplier = 10;
|
||||||
|
var points = 0;
|
||||||
|
for(var i=0,n=runs.length;i<n;i++){
|
||||||
|
var run = runs[i];
|
||||||
|
if(run.Start.X == run.End.X){
|
||||||
|
// Vertical run
|
||||||
|
for(var y=run.Start.Y;y<=run.End.Y;y++){
|
||||||
|
var candy = this.RemoveCandy(run.Start.X, y);
|
||||||
|
if(candy){
|
||||||
|
candy.Delete();
|
||||||
|
points += pointsMultiplier;
|
||||||
|
pointsMultiplier = pointsMultiplier*2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
// Horizontal run
|
||||||
|
for(var x=run.Start.X;x<=run.End.X;x++){
|
||||||
|
var candy = this.RemoveCandy(x, run.Start.Y);
|
||||||
|
if(candy){
|
||||||
|
candy.Delete();
|
||||||
|
points += pointsMultiplier;
|
||||||
|
pointsMultiplier = pointsMultiplier*2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return points;
|
||||||
|
},
|
||||||
|
ApplyRules: function(){
|
||||||
|
var horizontalRuns = this.ScanHorizontalRuns();
|
||||||
|
var verticalRuns = this.ScanVerticalRuns();
|
||||||
|
var runs = horizontalRuns.concat(verticalRuns);
|
||||||
|
var points = this.RemoveRuns(runs);
|
||||||
|
console.log(points)
|
||||||
|
return (runs.length>0);
|
||||||
|
},
|
||||||
|
CandyFall: function(){
|
||||||
|
// FIXME: Implement this
|
||||||
|
},
|
||||||
Debug: false
|
Debug: false
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user