From d877e1407e95b4575ce53872c0f6eb20bdb00703 Mon Sep 17 00:00:00 2001 From: FRANCIS LOPEZ Date: Sat, 24 Oct 2015 08:30:44 -0500 Subject: [PATCH] enemy can drop bombs --- scripts/objects/enemy.js | 49 +++++++++++----------------------------- scripts/states/game.js | 29 ++++++++++++++++++------ 2 files changed, 35 insertions(+), 43 deletions(-) diff --git a/scripts/objects/enemy.js b/scripts/objects/enemy.js index c90f755..e342aba 100644 --- a/scripts/objects/enemy.js +++ b/scripts/objects/enemy.js @@ -1,4 +1,4 @@ -var DEFAULT_ENEMY_SPEED = 160 ; +var DEFAULT_ENEMY_SPEED = 100 ; function Enemy(game, x, y) { Player.call(this, game, x, y, 'enemy', 0); @@ -22,7 +22,7 @@ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }; Enemy.prototype.handleArificialMovement = function(rocks,rocksColliding,moveChooserVal) { - + var moving = true; this.game.physics.arcade.collide(this, this.rocks); if(!moveChooserVal || moveChooserVal==null) @@ -44,28 +44,18 @@ Enemy.prototype.handleArificialMovement = function(rocks,rocksColliding,moveChoo moving = true; } } - else - { - moveChooser= getRandomInt(2, 5) ; - this.handleArificialMovement(rocks,rocksColliding,moveChooser); - } } else if (moveChooser == 2) { if (rocksColliding.right!=false) { - if(this.alive) + if(this.alive) { this.body.velocity.x = this.speed; this.facing = "right"; moving = true; } } - else - { - moveChooser= getRandomInt(1, 5) ; - this.handleArificialMovement(rocks,rocksColliding,moveChooser); - } } else if (moveChooser == 3) { @@ -78,12 +68,7 @@ Enemy.prototype.handleArificialMovement = function(rocks,rocksColliding,moveChoo moving = true; } } - else - { - moveChooser= getRandomInt(1, 5) ; - this.handleArificialMovement(rocks,rocksColliding,moveChooser); - } - + } else if (moveChooser == 4 ) { @@ -96,24 +81,16 @@ Enemy.prototype.handleArificialMovement = function(rocks,rocksColliding,moveChoo moving = true; } } - else - { - moveChooser= getRandomInt(1, 3) ; - this.handleArificialMovement(rocks,rocksColliding,moveChooser); - } - } - else if (moveChooser == 5) - { - if(this.alive) - { - moving = false; - this.freeze(); - moveChooser= getRandomInt(1, 5) ; - this.handleArificialMovement(rocks,rocksColliding,moveChooser); - } + } + if(moving) { this.animations.play(this.facing); } - -}; \ No newline at end of file + +}; + + +Enemy.prototype.canDropBombs = function(bombsPool) { + return (bombsPool.total < this.maxBombs); +}; diff --git a/scripts/states/game.js b/scripts/states/game.js index c01333a..0b5df1a 100644 --- a/scripts/states/game.js +++ b/scripts/states/game.js @@ -84,11 +84,11 @@ Game.prototype = { //Ajustes this.ground.resizeWorld(); this.rocks.resizeWorld(); - + this.addEnemy(1); this.addEnemy(2); this.addEnemy(3); - this.game.time.events.loop(2000, this.handleEnemyMovement, this); + this.game.time.events.loop(1000, this.handleEnemyMovement, this); }, update: function() { this.physics.arcade.collide(this.player, this.rocks); @@ -105,6 +105,17 @@ Game.prototype = { if (this.player.bombButtonJustPressed && this.player.canDropBombs(this.bombsPool) && this.time.now > this.nextBomb) { this.createBomb(this.player.x + this.player.width / 2, this.player.y + this.player.height / 2, this.keyBomb, 0); } + + this.enemyDropBomb(); + + }, + enemyDropBomb: function(){ + var enemyRandom = getRandomInt(0,this.enemyPool.children.length-1); + if (this.enemyPool.children[enemyRandom].canDropBombs(this.bombsPool) + && this.time.now > this.nextBomb + && this.enemyPool.children[enemyRandom].alive) { + this.createBomb(this.enemyPool.children[enemyRandom].x + this.enemyPool.children[enemyRandom].width / 2, this.enemyPool.children[enemyRandom].y + this.enemyPool.children[enemyRandom].height / 2, this.keyBomb, 0); + } }, handleEnemyMovement: function(){ for (var i = 0, len = this.enemyPool.children.length; i < len; i++) { @@ -165,7 +176,7 @@ Game.prototype = { }, destroyEnemy: function(enemy, explosion) { enemy.kill(); - + enemy.alive = false; this.player.score += 1; this.scoreText.text = this.player.score.toString(); @@ -184,11 +195,11 @@ Game.prototype = { if (this.rocks.layer.data[row - 1] !== undefined && this.rocks.layer.data[row - 1]) { rocksColliding.up = (this.rocks.layer.data[row - 1][column].index === this.level.rockId); } - } + } else { rocksColliding.up = true; - } + } if(column>=0){ if (this.rocks.layer.data[row + 1] !== undefined && this.rocks.layer.data[row + 1]) { rocksColliding.down = (this.rocks.layer.data[row + 1][column].index === this.level.rockId); @@ -202,7 +213,7 @@ Game.prototype = { if (this.rocks.layer.data[row][column - 1] !== undefined && this.rocks.layer.data[row][column - 1]) { rocksColliding.left = (this.rocks.layer.data[row][column - 1].index === this.level.rockId); } - } + } else { rocksColliding.left = true; @@ -224,7 +235,7 @@ Game.prototype = { }, clearBombs: function() { for(var bombId in this.bombsPool) {//TODO: add detonate timer ID - //clearTimeout(this.bombsPool[bombId].detonateTimerID); + //clearTimeout(this.bombsPool[bombId].detonateTimerID); } this.bombsPool = {}; }, @@ -240,5 +251,9 @@ Game.prototype = { } }, showGameStatusAndReset: function() { + }, + getRandomInt:function(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; } + };