Optim: managing alien health

This commit is contained in:
Fabrice Ecaille
2014-04-17 13:49:21 +02:00
parent 2d59bf7b28
commit fc0042a784
2 changed files with 9 additions and 7 deletions

View File

@@ -17,14 +17,14 @@ var ALIENS = {
animation : ALIENS_TYPE[0] animation : ALIENS_TYPE[0]
}, },
alien2 : { alien2 : {
health : 1, health : 2,
weapon : WEAPONS.ALIEN, weapon : WEAPONS.ALIEN,
score : 10, score : 10,
aggression : 0.001, aggression : 0.001,
animation : ALIENS_TYPE[1] animation : ALIENS_TYPE[1]
}, },
alien3 : { alien3 : {
health : 1, health : 3,
weapon : WEAPONS.ALIEN, weapon : WEAPONS.ALIEN,
score : 20, score : 20,
aggression : 0.0015, aggression : 0.0015,

View File

@@ -216,6 +216,7 @@ Game = {
posx = $(this).x(), posx = $(this).x(),
weapon = $(this)[0].weapon; weapon = $(this)[0].weapon;
// Lost shots
if( posy < -$(this).height() || posy > PLAYGROUND_HEIGHT || posx < -$(this).width() || posx > PLAYGROUND_WIDTH ) { if( posy < -$(this).height() || posy > PLAYGROUND_HEIGHT || posx < -$(this).width() || posx > PLAYGROUND_WIDTH ) {
Game.shots.lost = Game.shots.lost + 1; Game.shots.lost = Game.shots.lost + 1;
this.remove(); this.remove();
@@ -227,23 +228,24 @@ Game = {
if( weapon.callback ) { if( weapon.callback ) {
weapon.callback($(this)); weapon.callback($(this));
} else { } else {
// Shots collisions
var collisions = $(this).collision(".alien,."+$.gQ.groupCssClass); var collisions = $(this).collision(".alien,."+$.gQ.groupCssClass);
collisions.each( function() { collisions.each( function() {
var alien = $(this)[0], var alien = $(this)[0],
aliensNotInArray = $.grep( Game.aliens, function( elementOfArray, index) {
return elementOfArray.id == alien.id;
}, true);
alienX = alien.alien.x, alienX = alien.alien.x,
alienY = alien.alien.y, alienY = alien.alien.y,
alienWidth = alien.alien.width, alienWidth = alien.alien.width,
alienHeight = alien.alien.height; alienHeight = alien.alien.height;
Game.aliens = aliensNotInArray;
alien.alien.hit(); alien.alien.hit();
Game.aliens = $.grep( Game.aliens, function( elementOfArray, index) {
return elementOfArray.health == 0 && elementOfArray.id == alien.id;
}, true);
var remainingAliens = $(".alien").length; var remainingAliens = $(".alien").length;
if( remainingAliens == Game.wave.bonus[0] || remainingAliens == Game.wave.bonus[1] ) { if( remainingAliens == Game.wave.bonus[0] || remainingAliens == Game.wave.bonus[1] ) {
Game.bonus(alienX, alienY, alienWidth, alienHeight); Game.bonus(alienX, alienY, alienWidth, alienHeight);
} }
}) });
if( collisions.length > 0 ) { if( collisions.length > 0 ) {
this.remove() this.remove()
} }