Game Development Reference
In-Depth Information
Listing 14-41. Game.js - The Game's Check Methods Check the Status of the Level
p.checkEnemyAttack = function (time) {
if (time >= this.lastEnemyAttack + this.levelData.enemyAttackWait &&
!this.attackSelected && !this.enemiesAreAttacking) {
this.lastEnemyAttack = time + (this.ENEMY_ATTACK_DURATION *
this.levelData.enemyStreak);
this.beginEnemyAttack();
}
}
p.checkBattlePanel = function () {
if (!this.enemiesAreAttacking) {
this.battlePanel.update();
}
}
p.checkHeroHealth = function () {
if (this.hero.HP <= 0) {
this.levelComplete = true;
this.loseLevel();
}
}
p.checkLevel = function () {
if (this.enemies.length <= 0) {
this.levelComplete = true;
this.winLevel();
}
}
The checkEnemyAttack determines if it's time to start another enemy attack. The time property from the
ticker event is passed into it so it can be compared to the sum of the class property lastEnemyAttack and the
enemyAttackWait property in the level data. If time is currently greater than this number, the enemies should attack.
But only if both the hero and the enemies are not already in the middle of an attack can this happen. If the enemies
should start an attack, the beginEnemyAttack method is called. At this point, the lastEnemyAttack value should be set
again. Only a few additions need to be added to the current time. This value should be set to the time it will be when
the enemy streak is over. You can get this value by multiplying the level's attack streak number by the ENEMY_ATTACK_
DURATION constant.
this.lastEnemyAttack = time + (this.ENEMY_ATTACK_DURATION *
this.levelData.enemyStreak);
This might seem a little strange, but the ticker's current time will not be in scope within the enemy attack
evaluation functions.
The next check method, checkBattlePanel , is a lot simpler. Its only task is to update the battle panel if the
enemies are not currently attacking. The checkHeroHealth method is equally as simple. If the hero's hit points have all
been depleted, the levelComplete property is set to true and the loseLevel method is called.
The final check function is called after an enemy has been attacked or destroyed. It checks the number of
enemies left. If all enemies have been defeated, levelComplete is set to true and the winLevel method is called.
Search WWH ::




Custom Search