From 83375598dfcf71efd6050762b23eb0f2097f5ffe Mon Sep 17 00:00:00 2001 From: clb92 Date: Tue, 5 Jul 2016 22:25:29 +0200 Subject: [PATCH] Fixed pause, drops not disappearing and more bugs --- js/game.class.js | 11 +++++++--- js/resources.class.js | 47 ++++++++++++++++++++++++++++++++++++++++--- js/snake.class.js | 1 + 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/js/game.class.js b/js/game.class.js index e1de6aa..0184d99 100644 --- a/js/game.class.js +++ b/js/game.class.js @@ -3,6 +3,7 @@ */ var Game = function() { this.gameover = false; + this.started = false; this.paused = true; this.gameLoop = null; this.gameLoopCount = 0; @@ -32,6 +33,7 @@ Game.prototype.initGrid = function() { Game.prototype.start = function() { this.paused = false; + this.started = true; /* var parent = this; this.gameLoop = setInterval(function(){ parent.gameLoopCount++; @@ -70,6 +72,7 @@ Game.prototype.start = function() { Game.prototype.update = function() { this.snake.move(); + this.resources.checkDropExpiration(); } Game.prototype.togglePause = function() { @@ -82,7 +85,9 @@ Game.prototype.togglePause = function() { } else { this.canvas.canvas.style.filter = "blur(0px)"; document.getElementById("popup-text").setAttribute("class", "hidden"); - this.start(); + if (!this.started) { + this.start(); + } } } @@ -101,7 +106,7 @@ Game.prototype.applyEffect = function(effect) { case ('faster'): console.log("faster"); this.speedModifier = 1.7; - this.effects.speedChange = 150; + this.effects.speedChange = 180; break; case ('slower'): console.log("slower"); @@ -111,7 +116,7 @@ Game.prototype.applyEffect = function(effect) { case ('bonus'): console.log("bonus"); this.score = this.score + 15; - this.snake.embiggen(5); + this.snake.embiggen(6); break; } console.log(this); diff --git a/js/resources.class.js b/js/resources.class.js index 3cd331e..31ae847 100644 --- a/js/resources.class.js +++ b/js/resources.class.js @@ -4,7 +4,7 @@ var Resources = function(game) { this.game = game; this.droprateModifier = 5; - this.maxDrops = 100; + this.maxDrops = 3; this.types = [ 'food', 'food', @@ -33,6 +33,13 @@ var Resources = function(game) { 'slower', 'slower' ]; + this.defaultLifetime = { + 'food': null, + 'death': 180, + 'bonus': 100, + 'faster': 180, + 'slower': 180 + } this.locations = []; } @@ -49,16 +56,50 @@ Resources.prototype.checkLocation = function(x, y) { return null; } +Resources.prototype.checkDropExpiration = function() { + for (var i = 0; i < this.locations.length; i++) { + if (this.locations[i][3] !== null) { + if (this.locations[i][3] > 0) { + this.locations[i][3]--; + } else { + this.despawnDrop(i); + } + } + } +} + Resources.prototype.dropRandom = function() { if (!this.game.paused) { if (this.locations.length < this.maxDrops) { - var randDrop = this.getRandomInt(0, this.types.length - 1); var randX = this.getRandomInt(0, this.game.grid.width - 1); var randY = this.getRandomInt(0, this.game.grid.height - 1); + var randDrop = this.getRandomInt(0, this.types.length - 1); this.game.canvas.setTile(this.types[randDrop], randX, randY); - this.locations.push([randX, randY, this.types[randDrop]]); + this.locations.push([ + randX, + randY, + this.types[randDrop], + this.defaultLifetime[this.types[randDrop]] + ]); + } + } +} + +Resources.prototype.despawnDrop = function(i) { + this.game.canvas.setTile('board', this.locations[i][0], this.locations[i][1]); + this.locations.splice(i, 1); +} + +Resources.prototype.removeDrop = function(x, y, despawn = false) { + for (var i = 0; i < this.locations.length; i++) { + if (this.locations[i][0] === x) { + for (var i = 0; i < this.locations.length; i++) { + if (this.locations[i][0] === x && this.locations[i][1] === y) { + this.locations.splice(i, 1); + } + } } } } diff --git a/js/snake.class.js b/js/snake.class.js index 418f296..98c7a7b 100644 --- a/js/snake.class.js +++ b/js/snake.class.js @@ -72,6 +72,7 @@ Snake.prototype.move = function() { var effect = this.game.resources.checkLocation(newX, newY); if (effect !== null) { this.game.applyEffect(effect); + this.game.resources.removeDrop(newX, newY); } for (var i = 0; i < this.locations.length; i++) {