window.Images = new ImageLoader(); ///////////////////////////////////////// // // CandyEntity // var CandyEntity = function(game, color, gridPosition){ this.Game = game; this.GridPosition = gridPosition || {X: 0, Y: 0}; this.Color = color; this.GameEntity = new GameEntity( game.GameScreen, null, {X: 32, Y: 32}, Images.GetImage(color), color ); this.SetGridPosition(gridPosition.X, gridPosition.Y); }; CandyEntity.prototype = { Update: function(){ }, SetGridPosition: function(x, y){ this.GridPosition.X = x; this.GridPosition.Y = y; this.GameEntity.SetPosition({ X: this.Game.GridOffset.X + (x * 32), Y: this.Game.GridOffset.Y + (y * 32) }); }, Delete: function(){ this.GameEntity.Delete(); }, Debug: false }; ///////////////////////////////////////// // // CandyFucker // var CandyFucker = function(idScreen, idInfoDisplay){ var self = this; this.GameScreen = new GameScreen(idScreen, this.Init.bind(this), this.Proc.bind(this), this.End.bind(this) ); this.InfoDisplay = document.getElementById(idInfoDisplay); this.Grid = null; this.GridOffset = {X: 0, Y: 0}; this.CandyTypes = ["Red", "Blue", "Cyan", "Green", "Yellow"]; this.Locked = false; this.Falling = false; this.Changed = false; this.Score = 0; window.Images.LoadImages( [ {Name: "Red", Url: "gfx/Red.png"}, {Name: "Blue", Url: "gfx/Blue.png"}, {Name: "Cyan", Url: "gfx/Cyan.png"}, {Name: "Green", Url: "gfx/Green.png"}, {Name: "Yellow", Url: "gfx/Yellow.png"}, ], function(){ self.GameScreen.Start(); } ); }; CandyFucker.prototype = { Init: function(gameScreen){ this.BuildGrid(15, 15); this.UpdateInfoDisplay(); }, Proc: function(gameScreen){ if(this.Locked){ if(this.Falling){ if(!this.CandyFall()){ this.Falling = false; } }else{ if(this.ApplyRules()){ this.Falling = true; }else{ this.Locked = false; } } }else{ if(this.Changed){ if(this.ApplyRules()){ this.Locked = true; this.Falling = true; } } } this.Changed = false; }, End: function(gameScreen){ }, UpdateInfoDisplay: function(){ this.InfoDisplay.innerHTML = "Score: " + this.Score; }, GetCandy: function(x, y){ return this.Grid[y][x]; }, SetCandy: function(x, y, candy){ this.Grid[y][x] = candy; this.Changed = true; candy.SetGridPosition(x, y); }, RemoveCandy: function(x, y){ var candy = this.Grid[y][x]; this.Grid[y][x] = null; this.Changed = true; return candy; }, RandomCandy: function(){ return this.CandyTypes[Math.floor(Math.random() * this.CandyTypes.length)]; }, BuildGrid: function(width, height){ var x,y; this.GridSize = {X: width, Y: height}; this.GridOffset.X = (this.GameScreen.Size.X - ((width - 1) * 32)) / 2.0; this.GridOffset.Y = (this.GameScreen.Size.Y - ((height - 1) * 32)) / 2.0; this.Grid = []; for(y=0;y2){ 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;x2){ 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;i0){ this.Score += points; this.UpdateInfoDisplay(); console.log("Score: +" + points); } return (runs.length>0); }, CandyFall: function(){ var falling = false; var x,y; for(y=(this.GridSize.Y-1);y>=0;y--){ for(x=0;x