diff --git a/code/CandyFucker.js b/code/CandyFucker.js index a18c8b0..b9de764 100644 --- a/code/CandyFucker.js +++ b/code/CandyFucker.js @@ -1,6 +1,30 @@ window.Images = new ImageLoader(); +var Particle = function(game, position, image) { + this.Game = game; + this.GameEntity = new GameEntity( + game.GameScreen, + 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}),32); +}; +Particle.prototype = { + Update: function() { + this.Speed = Vec2D.Scale(this.Speed, 1.2); + this.GameEntity.AddPosition(this.Speed); + if(this.GameEntity.InsideScreen()==false){ + this.GameEntity.Delete(); + } + } +}; + + ///////////////////////////////////////// // // CandyEntity @@ -9,7 +33,7 @@ var CandyEntity = function(game, color, gridPosition){ this.Game = game; this.GridPosition = gridPosition || {X: 0, Y: 0}; this.Color = color; - this.GameEntity = new GameEntity( + this.GameEntity = new GameEntity( game.GameScreen, null, {X: 32, Y: 32}, @@ -29,6 +53,15 @@ CandyEntity.prototype = { }); }, Delete: function(){ + var frag; + for(var i=0; i<4; i++){ + frag = new Particle( + this.Game, + this.GameEntity.PositionDest, + Images.GetImage(this.Color) + ); + this.Game.GameScreen.AddEntity(frag); + } this.GameEntity.Delete(); }, SetOffset: function(x, y){ diff --git a/code/GameLib.js b/code/GameLib.js index 8528b8d..9fabb68 100644 --- a/code/GameLib.js +++ b/code/GameLib.js @@ -1,3 +1,21 @@ +var Vec2D = { + 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)); + return { + X: vecIn.X / len, + Y: vecIn.Y / len}; + } +}; + + ///////////////////////////////////////// // @@ -164,6 +182,10 @@ GameEntity.prototype = { this.PositionDest.X = position.X; this.PositionDest.Y = position.Y; }, + AddPosition: function(delta) { + this.PositionDest.X = this.Position.X + delta.X; + this.PositionDest.Y = this.Position.Y + delta.Y; + }, Delete: function(){ this.Deleted = true; }, @@ -176,6 +198,16 @@ GameEntity.prototype = { true ); }, + 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)) +