Game Development Reference
In-Depth Information
Listing 14-39.
Game.js - Evaluating the Enemy Attacks
p.evaluateEnemyStreak = function () {
this.currentEnemyAttackCount++;
this.currentEnemyAttackIndex++;
if (this.currentEnemyAttackIndex === this.enemies.length) {
this.currentEnemyAttackIndex = 0;
}
if (!this.levelComplete && this.currentEnemyAttackCount < this.levelData.enemyStreak &&
this.enemies.length > 0) {
this.beginEnemyAttack();
}
else {
this.enemyAttacksComplete();
}
}
p.enemyAttacksComplete = function () {
this.currentEnemyAttackCount = 0;
this.enemiesAreAttacking = false;
if (this.battlePanel.waitingToAttack) {
this.battlePanel.enableButtons();
}
}
The purpose of this function is to increase
currentEnemyAttackCount
, which was initially declared as 0, and
determine if the streak should continue. The length of the streak is unique to the level and is retrieved from the level
data. If the current count is less than total streak, the
beginEnemyAttack
is called again. If the streak should be over,
enemyAttacksComplete
is called, which destroys the streak and continues the game.
Creating the Check Level Functions
The main application is built to fire a
run
function on the current scene on every tick of the game loop. This function
was introduced when starting the
Game
class in the “Starting the Game Class and Its Properties” section. As a recap,
Listing 14-40 shows this
run
method.
Listing 14-40.
Game.js - The run Method is Called From the Main Application's Game Loop
p.run = function (tickerEvent) {
if (!this.levelComplete) {
this.checkBattlePanel();
this.checkEnemyAttack(tickerEvent.time);
this.checkHeroHealth();
}
}
This function will run three checks when the level is still in progress. Listing 14-41 shows these three functions,
plus the
checkLevel
method that is called after an enemy is attacked.