From fc0042a784e88cc9c1f687f819d9ad17f8b60bee Mon Sep 17 00:00:00 2001 From: Fabrice Ecaille Date: Thu, 17 Apr 2014 13:49:21 +0200 Subject: [PATCH] Optim: managing alien health --- js/models/aliens.js | 4 ++-- js/spaceinvaders-core.js | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/js/models/aliens.js b/js/models/aliens.js index 76f3831..a731f33 100644 --- a/js/models/aliens.js +++ b/js/models/aliens.js @@ -17,14 +17,14 @@ var ALIENS = { animation : ALIENS_TYPE[0] }, alien2 : { - health : 1, + health : 2, weapon : WEAPONS.ALIEN, score : 10, aggression : 0.001, animation : ALIENS_TYPE[1] }, alien3 : { - health : 1, + health : 3, weapon : WEAPONS.ALIEN, score : 20, aggression : 0.0015, diff --git a/js/spaceinvaders-core.js b/js/spaceinvaders-core.js index f29032c..b6c0083 100644 --- a/js/spaceinvaders-core.js +++ b/js/spaceinvaders-core.js @@ -216,6 +216,7 @@ Game = { posx = $(this).x(), weapon = $(this)[0].weapon; + // Lost shots if( posy < -$(this).height() || posy > PLAYGROUND_HEIGHT || posx < -$(this).width() || posx > PLAYGROUND_WIDTH ) { Game.shots.lost = Game.shots.lost + 1; this.remove(); @@ -227,23 +228,24 @@ Game = { if( weapon.callback ) { weapon.callback($(this)); } else { + // Shots collisions var collisions = $(this).collision(".alien,."+$.gQ.groupCssClass); collisions.each( function() { var alien = $(this)[0], - aliensNotInArray = $.grep( Game.aliens, function( elementOfArray, index) { - return elementOfArray.id == alien.id; - }, true); alienX = alien.alien.x, alienY = alien.alien.y, alienWidth = alien.alien.width, alienHeight = alien.alien.height; - Game.aliens = aliensNotInArray; + alien.alien.hit(); + Game.aliens = $.grep( Game.aliens, function( elementOfArray, index) { + return elementOfArray.health == 0 && elementOfArray.id == alien.id; + }, true); var remainingAliens = $(".alien").length; if( remainingAliens == Game.wave.bonus[0] || remainingAliens == Game.wave.bonus[1] ) { Game.bonus(alienX, alienY, alienWidth, alienHeight); } - }) + }); if( collisions.length > 0 ) { this.remove() }