As part of a little game I am making, I have an enemy object which fires projectiles at the character object, controlled by the player. The enemy has an hp attribute with a value of 10000, and as this value depletes, I want the projectile-firing patterns to change. This is my current situation:
this.fireOnce = function(){ ... }
this.fireRandomly = function(){ ... }
this.fireAtTarget = function(){ ... }
this.fireWave = function(){ ... }
this.beginFire = function(){
if(hp<3000){
this.fireWave();
}
else if(hp<5000){
this.fireAtTarget();
}
else if(hp<9000){
this.fireRandomly();
}
else{
this.fireOnce();
}
setTimeout(beginFire, 500);
}
The main loop already has enough complexity already, and things get laggy when many projectiles are on the screen. My concern about if-else statements derives from something my professor said about them being fairly expensive (I can't remember the context though so I could be wrong).
During the creation of this little game, I've used the above structure several times for different matters, and considering the functions get called several times each second, I assume that it takes its toll on the game's performance.
One possibility in an other situation would be to use an object containing the functions, but since we are talking about integer ranges, I can't think of something to use as a key.
Aucun commentaire:
Enregistrer un commentaire