1942 - Code Sample

//Updates the game state constantly. Called via an interval with a time of 1 ms to force to run as often as possible.
Game.prototype._update = function()
{
    //As this is called though a timer, it does not have the this varible set.
    with (myGame)
    {
        //Calculate the delta time from the last update call.
        var thisUpdate = new Date();
        var dt = (thisUpdate - _lastUpdate) / 1000;
        _lastUpdate = new Date();
        
        /*
        Update the length of time the level has been running.
        This may be used by the level scripts to modify difficulty level
        as the level progresses, to begin preloading the following level,
        to spawn boss fights, to end the level, or various other purposes.
        */
        levelObj.levelDuration += dt;
        
        /*
        Update the level. This function should spawn enemies and bonuses.
        */
        levelObj.update(dt);
        
        //Let the player attack.
        if (_mouseDown)
            playerAttack(0);
        
        //Force update the display



        playerHealthElement.innerHTML = _players[0].health + "%
"
+ playerLives + " Lives"; accuracyElement.innerHTML = hits + " hits (" + Math.floor(hits / shots * 100) + "%)
"
+ kills + " kills (" + Math.floor(kills / levelObj.totalEnemies * 100) + "%)"; playerScoreElement.innerHTML = _playerScore; var fpsDT = (thisUpdate - _lastFPSUpdate); _lastFPSFrames++; if (fpsDT >= 250) { fpsDT /= 1000; fpsElement.innerHTML = "fps: " + (Math.floor((_lastFPSFrames / fpsDT) * 10) / 10) + "
dt: "
+ (Math.floor(dt * 1000) / 1000); _lastFPSFrames = 0; _lastFPSUpdate = thisUpdate; } //Give the player extra lives if (_playerScore > _nextExtraLife) { _nextExtraLife += 50000; playerLives++; } //Update projectiles for (var y in _playerProjectiles) { for (var x in _enemies) { if (_enemies[x].isCollide(_playerProjectiles[y])) { if (_enemies[x].health > 0) { _enemies[x].health -= _playerProjectiles[y].health; hits++; if (_enemies[x].health <= 0) { playerScore(_enemies[x].worth); kills++; } _playerProjectiles[y].die(); break; } } } } //Update players (really only 1, but this code was wrtten with the intent of allowing additional players in co-op play) for (var y in _players) { //Check for the player colliding with enemies. for (var x in _enemies) { if (_players[y].isCollide(_enemies[x])) { _enemies[x].health -= 1000; if (_players[y].invincible <= 0) _players[y].health -= 1000; } } //Check for the player colliding with enemy projectiles for (var x in _enemyProjectiles) { if (_players[y].isCollide(_enemyProjectiles[x])) { if (_players[y].invincible <= 0) { if (_players[y].health > 0) { _players[y].health -= _enemyProjectiles[x].health; _enemyProjectiles[x].die(); } } } } //Check for the player colliding with objects (bonuses) for (var x in _objects) if (_players[y].isCollide(_objects[x])) if (_players[y].invincible <= 0) _objects[x]._aiName(_objects[x], "hit", _players[y]); //Update the player's special attack duration _players[y].specialDuration -= 10; if (_players[y].specialDuration <= 0) { _players[y].attack = playerAttackNormal; _players[y].specialDuration = 1000; } //Update the players. This causes the player to move towards the mouse. _players[y].update(dt); } //Move player projectiles for (var x in _playerProjectiles) { _playerProjectiles[x].update(dt); } //Move enemy projectiles for (var x in _enemyProjectiles) { _enemyProjectiles[x].update(dt); } //Move enemies for (var x in _enemies) { _enemies[x].update(dt); } //Move objects for (var x in _objects) { _objects[x].update(dt); } } }