diff --git a/code/CandyFucker.js b/code/CandyFucker.js index 5de9852..0da9648 100644 --- a/code/CandyFucker.js +++ b/code/CandyFucker.js @@ -1,4 +1,3 @@ - window.Images = new ImageLoader(); window.Sounds = new SoundLoader(); @@ -7,24 +6,28 @@ window.Sounds = new SoundLoader(); // // Particle // -var Particle = function(game, position, image) { +var Particle = function (game, position, image) { this.Game = game; this.GameEntity = new GameEntity( game.GameScreen, - position, - {X:image.naturalWidth, Y:image.naturalHeight}, + position, { + X: image.naturalWidth, + Y: image.naturalHeight + }, image, - "Particle" - ); - this.Speed = Vec2D.Scale(Vec2D.Normalize({ - X: Math.floor(Math.random() * 33)-16, - Y: Math.floor(Math.random() * 33)-16}),48); + "Particle"); + this.Speed = Vec2D.Scale( + Vec2D.Normalize({ + X: Math.floor(Math.random() * 33) - 16, + Y: Math.floor(Math.random() * 33) - 16 + }), + 48); }; Particle.prototype = { - Update: function() { + Update: function () { this.Speed = Vec2D.Scale(this.Speed, 1.5); this.GameEntity.AddPosition(this.Speed); - if(this.GameEntity.InsideScreen()==false){ + if (this.GameEntity.InsideScreen() === false) { this.GameEntity.Delete(); } } @@ -35,498 +38,626 @@ Particle.prototype = { // // CandyEntity // -var CandyEntity = function(game, color, gridPosition){ +var CandyEntity = function (game, color, gridPosition) { this.Game = game; - this.GridPosition = gridPosition || {X: 0, Y: 0}; + this.GridPosition = gridPosition || { + X: 0, + Y: 0 + }; this.Color = color; this.GameEntity = new GameEntity( - game.GameScreen, - null, - {X: 32, Y: 32}, - Images.GetImage("Balls" + color), - "Candy" - ); + game.GameScreen, + null, { + X: 32, + Y: 32 + }, + Images.GetImage("Balls" + color), + "Candy"); this.SetGridPosition(gridPosition.X, gridPosition.Y); }; CandyEntity.prototype = { - Update: function(){ }, - SetGridPosition: function(x, y){ + Update: function () {}, + SetGridPosition: function (x, y) { this.GridPosition.X = x; this.GridPosition.Y = y; this.GameEntity.UpdatePosition({ - X: this.Game.GridOffset.X + (x * 32), + X: this.Game.GridOffset.X + (x * 32), Y: this.Game.GridOffset.Y + (y * 32) }); }, - Delete: function(){ + Delete: function () { var frag; - for(var i=0; i<4; i++){ + var i; + for (i = 0; i < 4; i++) { frag = new Particle( this.Game, this.GameEntity.PositionDest, - Images.GetImage("Frags" + this.Color) - ); + Images.GetImage("Frags" + this.Color)); + frag.GameEntity.Update(); this.Game.GameScreen.AddEntity(frag); } this.GameEntity.Delete(); window.Sounds.PlaySound("Explosion"); }, - SetOffset: function(x, y){ + SetOffset: function (x, y) { this.GameEntity.UpdatePosition({ - X: (this.Game.GridOffset.X + (this.GridPosition.X * 32)) + x, + X: (this.Game.GridOffset.X + (this.GridPosition.X * 32)) + x, Y: (this.Game.GridOffset.Y + (this.GridPosition.Y * 32)) + y }); }, - ResetPosition: function(){ + ResetPosition: function () { this.SetGridPosition(this.GridPosition.X, this.GridPosition.Y); }, Debug: false }; +CandyEntity.RandomCandy = function () { + //var candyTypes = ["Red", "Blue", "Cyan", "Green", "Yellow"]; + var candyTypes = ["Red", "Blue", "Cyan", "Yellow"]; + return candyTypes[Math.floor(Math.random() * candyTypes.length)]; +}; + + +///////////////////////////////////////// +// +// CandyBoard +// +var CandyBoard = function (game) { + this.Game = game; + this.GridSize = { + X: 0, + Y: 0 + }; + this.Grid = null; + + this.Changed = false; +}; +CandyBoard.prototype = { + IsChanged: function () { + return this.Changed; + }, + GetCandy: function (x, y) { + if (x < 0 || x > this.GridSize.X || y < 0 || y > this.GridSize.Y) { + return null; + } + return this.Grid[y][x]; + }, + SetCandy: function (x, y, candy) { + if (x < 0 || x > this.GridSize.X || y < 0 || y > this.GridSize.Y) { + return; + } + this.Grid[y][x] = candy; + this.Changed = true; + candy.SetGridPosition(x, y); + }, + RemoveCandy: function (x, y) { + if (x < 0 || x > this.GridSize.X || y < 0 || y > this.GridSize.Y) { + return; + } + var candy = this.Grid[y][x]; + this.Grid[y][x] = null; + this.Changed = true; + return candy; + }, + BuildGrid: function (width, height) { + var x, + y; + this.GridSize = { + X: width, + Y: height + }; + // Allocate Grid + this.Grid = []; + for (y = 0; y < height; y++) { + this.Grid.push([]); + for (x = 0; x < width; x++) { + this.Grid[y].push(null); + } + } + + // Fill Grid + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + var entCandy = new CandyEntity( + this.Game, + CandyEntity.RandomCandy(), { + X: x, + Y: y + }); + this.Game.GameScreen.AddEntity(entCandy); + this.SetCandy(x, y, entCandy); + } + } + 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; + }, + ScanRuns: function () { + var horizontalRuns = this.ScanHorizontalRuns(); + var verticalRuns = this.ScanVerticalRuns(); + return horizontalRuns.concat(verticalRuns); + }, + ExplodeCandy: function (x, y) { + var entCandy = this.RemoveCandy(x, y); + if (entCandy) { + entCandy.Delete(); + return true; + } + return false; + }, + RemoveRuns: function (runs) { + var pointsMultiplier = 10; + var points = 0; + var i; + var n; + for (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++) { + if (this.ExplodeCandy(run.Start.X, y)) { + points += pointsMultiplier; + pointsMultiplier = pointsMultiplier + 10; + } + } + } else { + // Horizontal run + for (var x = run.Start.X; x <= run.End.X; x++) { + if (this.ExplodeCandy(x, run.Start.Y)) { + points += pointsMultiplier; + pointsMultiplier = pointsMultiplier + 10; + } + } + } + } + return points; + }, + CandyFall: function () { + var falling = false; + var x; + var y; + for (y = (this.GridSize.Y - 1); y >= 0; y--) { + for (x = 0; x < this.GridSize.X; x++) { + var candy = this.GetCandy(x, y); + if (candy === null) { + if (y === 0) { + var entCandy = new CandyEntity( + this.Game, + CandyEntity.RandomCandy(), { + X: x, + Y: y - 1 + }); + this.Game.GameScreen.AddEntity(entCandy); + this.SetCandy(x, y, entCandy); + falling = true; + } else { + var candyUp = this.RemoveCandy(x, y - 1); + if (candyUp) { + this.SetCandy(x, y, candyUp); + falling = true; + } + } + + } + } + } + return falling; + }, + Debug: false +}; ///////////////////////////////////////// // // CandyFucker // -var CandyFucker = function(idScreen, idInfoDisplay){ - var self = this; - this.GameScreen = new GameScreen(idScreen, +var CandyFucker = function (idScreen, idInfoDisplay) { + this.GameScreen = new GameScreen(idScreen, this.Init.bind(this), this.Proc.bind(this), this.End.bind(this), - 10 - ); - this.InfoDisplay = document.getElementById(idInfoDisplay); - this.Grid = null; - this.GridOffset = {X: 0, Y: 0}; - //this.CandyTypes = ["Red", "Blue", "Cyan", "Green", "Yellow"]; - this.CandyTypes = ["Red", "Blue", "Cyan", "Yellow"]; - this.Locked = false; + 10); + + this.Board = new CandyBoard(this); this.Falling = false; - this.Changed = false; + this.GridOffset = { + X: 0, + Y: 0 + }; + + this.InfoDisplay = document.getElementById(idInfoDisplay); + this.Locked = false; this.Score = 0; - + this.SwapDirection = null; this.SwapDistance = 0; this.SwapCandy1 = null; this.SwapCandy2 = null; - + this.MaxSwapDistance = 32; - + this.LoadImages(); }; CandyFucker.prototype = { - LoadImages: function(){ + LoadImages: function () { var self = this; window.Images.LoadImages( [ - {Name: "BallsRed", Url: "gfx/BallsRed.png"}, - {Name: "BallsBlue", Url: "gfx/BallsBlue.png"}, - {Name: "BallsCyan", Url: "gfx/BallsCyan.png"}, - {Name: "BallsGreen", Url: "gfx/BallsGreen.png"}, - {Name: "BallsYellow", Url: "gfx/BallsYellow.png"}, - {Name: "FragsRed", Url: "gfx/FragsRed.png"}, - {Name: "FragsBlue", Url: "gfx/FragsBlue.png"}, - {Name: "FragsCyan", Url: "gfx/FragsCyan.png"}, - {Name: "FragsGreen", Url: "gfx/FragsGreen.png"}, - {Name: "FragsYellow", Url: "gfx/FragsYellow.png"}, + { + Name: "BallsRed", + Url: "gfx/BallsRed.png" + }, + { + Name: "BallsBlue", + Url: "gfx/BallsBlue.png" + }, + { + Name: "BallsCyan", + Url: "gfx/BallsCyan.png" + }, + { + Name: "BallsGreen", + Url: "gfx/BallsGreen.png" + }, + { + Name: "BallsYellow", + Url: "gfx/BallsYellow.png" + }, + { + Name: "FragsRed", + Url: "gfx/FragsRed.png" + }, + { + Name: "FragsBlue", + Url: "gfx/FragsBlue.png" + }, + { + Name: "FragsCyan", + Url: "gfx/FragsCyan.png" + }, + { + Name: "FragsGreen", + Url: "gfx/FragsGreen.png" + }, + { + Name: "FragsYellow", + Url: "gfx/FragsYellow.png" + } ], - function(){ + function () { self.LoadSounds(); - } - ); + }); }, - LoadSounds: function(){ + LoadSounds: function () { var self = this; window.Sounds.LoadSounds( [ - {Name: "Explosion", Url: "sfx/explosion1.wav"}, - {Name: "PickCandy", Url: "sfx/pickcandy.wav"}, + { + Name: "Explosion", + Url: "sfx/explosion1.wav" + }, + { + Name: "PickCandy", + Url: "sfx/pickcandy.wav" + } ], - function(){ + function () { self.GameScreen.Start(); - } - ); + }); }, - Init: function(gameScreen){ - this.BuildGrid(12, 12); + Init: function () { + var width = 12; + var height = 12; + 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.Board.BuildGrid(12, 12); + this.UpdateInfoDisplay(); }, - Proc: function(gameScreen){ - if(this.Locked){ - if(this.Falling){ - if(!this.CandyFall()){ + Proc: function () { + if (this.Locked) { + if (this.Falling) { + if (!this.Board.CandyFall()) { this.Falling = false; } - }else{ - if(this.ApplyRules()){ - this.Falling = this.CandyFall(); - }else{ + } else { + if (this.ApplyRules()) { + this.Falling = this.Board.CandyFall(); + } else { this.Locked = false; } } - }else{ - if(this.GameScreen.Mouse.Down){ + } else { + if (this.GameScreen.Mouse.Down) { this.ProcessSwap(); - }else{ + } else { this.CancelSwap(); } - if(this.Changed){ - if(this.ApplyRules()){ + if (this.Board.IsChanged()) { + if (this.ApplyRules()) { this.Locked = true; - this.Falling = this.CandyFall(); + this.Falling = this.Board.CandyFall(); } } } this.Changed = false; }, - End: function(gameScreen){ }, - UpdateInfoDisplay: function(){ + End: function () {}, + UpdateInfoDisplay: function () { this.InfoDisplay.innerHTML = "Score: " + this.Score; }, - GetCandy: function(x, y){ - if(x<0 || x>this.GridSize.X || y<0 || y>this.GridSize.Y){ return null; } - return this.Grid[y][x]; - }, - SetCandy: function(x, y, candy){ - if(x<0 || x>this.GridSize.X || y<0 || y>this.GridSize.Y){ return; } - this.Grid[y][x] = candy; - this.Changed = true; - candy.SetGridPosition(x, y); - }, - RemoveCandy: function(x, y){ - if(x<0 || x>this.GridSize.X || y<0 || y>this.GridSize.Y){ return; } - 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; - // Allocate Grid - 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; - }, - ExplodeCandy: function(x, y){ - var entCandy = this.RemoveCandy(x, y); - if(entCandy){ - entCandy.Delete(); - return true; - } - return false; - }, - RemoveRuns: function(runs){ - var pointsMultiplier = 10; - var points = 0; - for(var i=0,n=runs.length;i0){ + ApplyRules: function () { + var runs = this.Board.ScanRuns(); + var points = this.Board.RemoveRuns(runs); + if (points > 0) { this.Score += points; this.UpdateInfoDisplay(); console.log("Score: +" + points); } - return (runs.length>0); + 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 Math.abs(y)){ - if(x>0){ - this.ProcSwapRight(x, y); + if (this.SwapDirection !== null) { + var x = this.GameScreen.Mouse.EndPosition.X - + this.GameScreen.Mouse.StartPosition.X; + var y = this.GameScreen.Mouse.EndPosition.Y - + this.GameScreen.Mouse.StartPosition.Y; + if (Math.abs(x) > Math.abs(y)) { + if (x > 0) { + this.ProcSwapRight(x); } - if(x<0){ - this.ProcSwapLeft(x, y); + if (x < 0) { + this.ProcSwapLeft(-x); } - }else{ - if(y>0){ - this.ProcSwapDown(x, y); + } else { + if (y > 0) { + this.ProcSwapDown(y); } - if(y<0){ - this.ProcSwapUp(x, y); + if (y < 0) { + this.ProcSwapUp(-y); } } } }, - ProcSwapLeft: function(x, y){ - if(this.SwapDirection!="Left"){ - this.SwapDirection = "Left" - if(this.SwapCandy2){ + ProcSwapLeft: function (dist) { + if (this.SwapDirection != "Left") { + this.SwapDirection = "Left"; + if (this.SwapCandy2) { this.SwapCandy2.ResetPosition(); } - this.SwapCandy2 = this.GetCandy(this.SwapCandy1.GridPosition.X-1, this.SwapCandy1.GridPosition.Y); + this.SwapCandy2 = this.Board.GetCandy( + this.SwapCandy1.GridPosition.X - 1, + this.SwapCandy1.GridPosition.Y); } - this.SwapDistance = -x; - if(this.SwapDistance > this.MaxSwapDistance){ + this.SwapDistance = dist; + if (this.SwapDistance > this.MaxSwapDistance) { this.DoSwap(); - }else{ - if(this.SwapCandy2){ + } else { + if (this.SwapCandy2) { this.SwapCandy1.SetOffset(-this.SwapDistance, 0); this.SwapCandy2.SetOffset(this.SwapDistance, 0); - }else{ + } else { this.SwapCandy1.ResetPosition(); } } }, - ProcSwapRight: function(x, y){ - if(this.SwapDirection!="Right"){ - this.SwapDirection = "Right" - if(this.SwapCandy2){ + ProcSwapRight: function (dist) { + if (this.SwapDirection != "Right") { + this.SwapDirection = "Right"; + if (this.SwapCandy2) { this.SwapCandy2.ResetPosition(); } - this.SwapCandy2 = this.GetCandy(this.SwapCandy1.GridPosition.X+1, this.SwapCandy1.GridPosition.Y); + this.SwapCandy2 = this.Board.GetCandy( + this.SwapCandy1.GridPosition.X + 1, + this.SwapCandy1.GridPosition.Y); } - this.SwapDistance = x; - if(this.SwapDistance > this.MaxSwapDistance){ + this.SwapDistance = dist; + if (this.SwapDistance > this.MaxSwapDistance) { this.DoSwap(); - }else{ - if(this.SwapCandy2){ + } else { + if (this.SwapCandy2) { this.SwapCandy1.SetOffset(this.SwapDistance, 0); this.SwapCandy2.SetOffset(-this.SwapDistance, 0); - }else{ + } else { this.SwapCandy1.ResetPosition(); } } }, - ProcSwapUp: function(x, y){ - if(this.SwapDirection!="Up"){ - this.SwapDirection = "Up" - if(this.SwapCandy2){ + ProcSwapUp: function (dist) { + if (this.SwapDirection != "Up") { + this.SwapDirection = "Up"; + if (this.SwapCandy2) { this.SwapCandy2.ResetPosition(); } - this.SwapCandy2 = this.GetCandy(this.SwapCandy1.GridPosition.X, this.SwapCandy1.GridPosition.Y-1); + this.SwapCandy2 = this.Board.GetCandy( + this.SwapCandy1.GridPosition.X, this.SwapCandy1.GridPosition.Y - 1); } - this.SwapDistance = -y; - if(this.SwapDistance > this.MaxSwapDistance){ + this.SwapDistance = dist; + if (this.SwapDistance > this.MaxSwapDistance) { this.DoSwap(); - }else{ - if(this.SwapCandy2){ + } else { + if (this.SwapCandy2) { this.SwapCandy1.SetOffset(0, -this.SwapDistance); this.SwapCandy2.SetOffset(0, this.SwapDistance); - }else{ + } else { this.SwapCandy1.ResetPosition(); } } }, - ProcSwapDown: function(x, y){ - if(this.SwapDirection!="Down"){ - this.SwapDirection = "Down" - if(this.SwapCandy2){ + ProcSwapDown: function (dist) { + if (this.SwapDirection != "Down") { + this.SwapDirection = "Down"; + if (this.SwapCandy2) { this.SwapCandy2.ResetPosition(); } - this.SwapCandy2 = this.GetCandy(this.SwapCandy1.GridPosition.X, this.SwapCandy1.GridPosition.Y+1); + this.SwapCandy2 = this.Board.GetCandy( + this.SwapCandy1.GridPosition.X, + this.SwapCandy1.GridPosition.Y + 1); } - this.SwapDistance = y; - if(this.SwapDistance > this.MaxSwapDistance){ + this.SwapDistance = dist; + if (this.SwapDistance > this.MaxSwapDistance) { this.DoSwap(); - }else{ - if(this.SwapCandy2){ + } else { + if (this.SwapCandy2) { this.SwapCandy1.SetOffset(0, this.SwapDistance); this.SwapCandy2.SetOffset(0, -this.SwapDistance); - }else{ + } else { this.SwapCandy1.ResetPosition(); } } }, - StartSwap: function(candy){ + StartSwap: function (candy) { this.SwapDirection = ""; this.SwapCandy1 = candy; this.SwapCandy2 = null; this.SwapDistance = 0; }, - CancelSwap: function(){ + CancelSwap: function () { this.GameScreen.Mouse.Cancel(); this.SwapDirection = null; - if(this.SwapCandy1){ + if (this.SwapCandy1) { this.SwapCandy1.ResetPosition(); } - if(this.SwapCandy2){ + if (this.SwapCandy2) { this.SwapCandy2.ResetPosition(); } this.SwapCandy1 = null; this.SwapCandy2 = null; this.SwapDistance = 0; }, - DoSwap: function(){ - if(this.SwapCandy1 == null || this.SwapCandy2 == null){ + DoSwap: function () { + if (this.SwapCandy1 === null || this.SwapCandy2 === null) { this.CancelSwap(); return; } @@ -534,15 +665,16 @@ CandyFucker.prototype = { var y1 = this.SwapCandy1.GridPosition.Y; var x2 = this.SwapCandy2.GridPosition.X; var y2 = this.SwapCandy2.GridPosition.Y; - var candy1 = this.RemoveCandy(x1, y1); - var candy2 = this.RemoveCandy(x2, y2); - this.SetCandy(x2, y2, candy1); - this.SetCandy(x1, y1, candy2); + var candy1 = this.Board.RemoveCandy(x1, y1); + var candy2 = this.Board.RemoveCandy(x2, y2); + this.Board.SetCandy(x2, y2, candy1); + this.Board.SetCandy(x1, y1, candy2); this.GameScreen.Mouse.Cancel(); - this.SwapDirection = null + this.SwapDirection = null; this.SwapCandy1 = null; this.SwapCandy2 = null; this.SwapDistance = 0; }, Debug: false }; + diff --git a/code/GameLib.js b/code/GameLib.js index e1892a1..e1e2181 100644 --- a/code/GameLib.js +++ b/code/GameLib.js @@ -1,22 +1,150 @@ +///////////////////////////////////////// +// +// Mouse +// +var Mouse = function (screen, size) { + this.Screen = screen; + this.Size = size || { + X: screen.width, + Y: screen.height + }; + + this.Down = false; + this.StartPosition = { + X: 0, + Y: 0 + }; + this.EndPosition = { + X: 0, + Y: 0 + }; + + this.Screen.addEventListener("mousedown", this.OnMouseDown.bind(this), false); + this.Screen.addEventListener("mousemove", this.OnMouseMove.bind(this), false); + this.Screen.addEventListener("mouseup", this.OnMouseUp.bind(this), false); + this.Screen.addEventListener("mouseleave", this.OnMouseLeave.bind(this), + false); + this.Screen.addEventListener("touchstart", this.OnTouchStart.bind(this), + false); + this.Screen.addEventListener("touchmove", this.OnTouchMove.bind(this), false); + this.Screen.addEventListener("touchend", this.OnTouchEnd.bind(this), false); + this.Screen.addEventListener("touchcancel", this.OnTouchEnd.bind(this), false); +}; +Mouse.prototype = { + GetEventPoistion: function (positionEvent) { + var position = { + X: positionEvent.X, + Y: positionEvent.Y + }; + var element = this.Screen; + while (element) { + position.X -= element.offsetLeft; + position.Y -= element.offsetTop; + element = element.offsetParent; + } + position.X = (position.X / this.Screen.offsetWidth) * this.Size.X; + position.Y = (position.Y / this.Screen.offsetHeight) * this.Size.Y; + return position; + }, + OnMouseDown: function (event) { + var position = this.GetEventPoistion({ + X: event.clientX, + Y: event.clientY + }); + this.RealDown = true; + this.Down = true; + this.StartPosition.X = position.X; + this.StartPosition.Y = position.Y; + this.EndPosition.X = position.X; + this.EndPosition.Y = position.Y; + }, + OnMouseMove: function (event) { + if (this.RealDown === false) { + return; + } + var position = this.GetEventPoistion({ + X: event.clientX, + Y: event.clientY + }); + this.RealDown = true; + this.Down = true; + this.EndPosition.X = position.X; + this.EndPosition.Y = position.Y; + }, + OnMouseUp: function (event) { + var position = this.GetEventPoistion({ + X: event.clientX, + Y: event.clientY + }); + this.RealDown = false; + this.EndPosition.X = position.X; + this.EndPosition.Y = position.Y; + }, + OnMouseLeave: function () { + this.RealDown = false; + this.Down = false; + }, + OnTouchStart: function (event) { + var position = this.GetEventPoistion({ + X: event.touches[0].clientX, + Y: event.touches[0].clientY + }); + this.RealDown = true; + this.Down = true; + this.StartPosition.X = position.X; + this.StartPosition.Y = position.Y; + this.EndPosition.X = position.X; + this.EndPosition.Y = position.Y; + }, + OnTouchMove: function (event) { + if (this.RealDown === false) { + return; + } + var position = this.GetEventPoistion({ + X: event.touches[0].clientX, + Y: event.touches[0].clientY + }); + this.RealDown = true; + this.Down = true; + this.EndPosition.X = position.X; + this.EndPosition.Y = position.Y; + }, + OnTouchEnd: function () { + this.RealDown = false; + }, + Update: function () { + if (this.RealDown === false) { + this.Down = false; + } + }, + Cancel: function () { + this.RealDown = false; + this.Down = false; + }, + Debug: false +}; + + ///////////////////////////////////////// // // Vec2D // var Vec2D = { - Scale: function(vecIn, scale) { + Scale: function (vecIn, scale) { return { X: vecIn.X * scale, Y: vecIn.Y * scale }; }, - Normalize: function(vecIn){ - var len = Math.sqrt( - (vecIn.X * vecIn.X) + - (vecIn.Y * vecIn.Y)); + Normalize: function (vecIn) { + var len = + Math.sqrt( + (vecIn.X * vecIn.X) + + (vecIn.Y * vecIn.Y)); return { - X: vecIn.X / len, + X: vecIn.X / len, Y: vecIn.Y / len }; } @@ -27,10 +155,13 @@ var Vec2D = { // // GameScreen // -var GameScreen = function(idScreen, funcInit, funcProc, funcEnd, tps){ +var GameScreen = function (idScreen, funcInit, funcProc, funcEnd, tps) { this.Screen = document.getElementById(idScreen); this.Ctx = this.Screen.getContext('2d'); - this.Size = {X: this.Screen.width, Y: this.Screen.height}; + this.Size = { + X: this.Screen.width, + Y: this.Screen.height + }; this.Entities = []; this.NewEntities = []; this.Running = false; @@ -38,18 +169,18 @@ var GameScreen = function(idScreen, funcInit, funcProc, funcEnd, tps){ this.FuncProc = funcProc; this.FuncEnd = funcEnd; this.TPS = tps || 10; - - this.TickTime = 1000/this.TPS; + + this.TickTime = 1000 / this.TPS; this.AccTickTime = this.TickTime; this.PreviousTime = 0; - + this.Mouse = new Mouse(this.Screen, this.Size); - + var self = this; - this.Tick = function(){ - while(self.AccTickTime>=self.TickTime){ + this.Tick = function () { + while (self.AccTickTime >= self.TickTime) { self.Update(); - if(self.FuncProc){ + if (self.FuncProc) { self.FuncProc(self); } self.Mouse.Update(); @@ -57,82 +188,91 @@ var GameScreen = function(idScreen, funcInit, funcProc, funcEnd, tps){ self.InsertAdded(); self.AccTickTime -= self.TickTime; } - self.Draw(self.AccTickTime/self.TickTime); - + self.Draw(self.AccTickTime / self.TickTime); + var timeNow = performance.now(); self.AccTickTime += timeNow - self.PreviousTime; self.PreviousTime = timeNow; - - if(self.Running){ + + if (self.Running) { window.requestAnimationFrame(self.Tick); - }else{ - if(self.FuncEnd){ + } else { + if (self.FuncEnd) { self.FuncEnd(self); } } - } + }; }; GameScreen.prototype = { // For internal use - CleanDead: function(){ - var i = this.Entities.length-1; - while(i>0){ - if(this.Entities[i].GameEntity.Deleted){ - this.Entities.splice(i,1); + CleanDead: function () { + var i = this.Entities.length - 1; + while (i > 0) { + if (this.Entities[i].GameEntity.Deleted) { + this.Entities.splice(i, 1); } i--; } }, - InsertAdded: function(){ - for(var i=0,n=this.NewEntities.length;i (this.PositionDest.X - (this.Size.X/2)) && - point.Y < (this.PositionDest.Y + (this.Size.Y/2)) && - point.Y > (this.PositionDest.Y - (this.Size.Y/2)) && - true - ); + point.X < (this.PositionDest.X + (this.Size.X / 2)) && + point.X > (this.PositionDest.X - (this.Size.X / 2)) && + point.Y < (this.PositionDest.Y + (this.Size.Y / 2)) && + point.Y > (this.PositionDest.Y - (this.Size.Y / 2)) && + true); }, - InsideScreen: function(){ + InsideScreen: function () { return ( - (this.Position.X+(this.Size.X/2))>0 && - (this.Position.Y+(this.Size.Y/2))>0 && - (this.Position.X-(this.Size.X/2)) - 0 && + (this.Position.Y + (this.Size.Y / 2)) > 0 && + (this.Position.X - (this.Size.X / 2)) < this.GameScreen.Size.X && + (this.Position.Y - (this.Size.Y / 2)) < this.GameScreen.Size.Y); }, Debug: false }; @@ -316,11 +375,11 @@ Mouse.prototype = { // // ImageLoader // -var ImageLoader = function(){ +var ImageLoader = function () { this.Images = {}; }; ImageLoader.prototype = { - IsImageOk: function(img){ + IsImageOk: function (img) { if (!img.complete) { return false; } @@ -329,101 +388,110 @@ ImageLoader.prototype = { } return true; }, - LoadImages: function(imageList, funcOnLoad){ + LoadImages: function (imageList, funcOnLoad) { this.ImageCount = imageList.length; this.FuncOnLoad = funcOnLoad; - - var i,n; - for(i=0,n=imageList.length;i