Skip to navigation

Gameplay: UpdateEnemyTimers

Name: UpdateEnemyTimers [Show more] Type: Subroutine Category: Gameplay Summary: Update the timers that control the enemy tactics
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * IRQHandler calls UpdateEnemyTimers
.UpdateEnemyTimers LDA updateTimer \ If updateTimer is non-zero, jump to time3 to skip BNE time3 \ updating the enemy timers and run down the update \ timer \ If we get here then updateTimer is zero, so we \ update the enemy timers \ \ The update timer loops through 2, 1, 0, so this means \ we decrement the timers on one out of every three \ calls to the UpdateEnemyTimers \ \ UpdateEnemyTimers is called from the interrupt handler \ at IRQHandler, but only once the game has started and \ the Sentinel is active \ \ IRQHandler performs its actions 50 times a second, so \ this means the counters tick down at a rate of 16.7 \ times a second, or once every 0.06 seconds \ \ So setting a timer to n means it will count down in \ 3 * n / 50 = 3 * 0.06 seconds LDX #23 \ There are 24 bytes of enemy timers, split into three \ timers keeping track of up to eight enemies each: \ \ * enemyDrainTimer \ \ * enemyTacticTimer \ \ * enemyTimer3 \ \ So set a byte counter in X to update all the timers \ in one loop .time1 LDA enemyDrainTimer,X \ If the X-th timer is less than 2 then it has already CMP #2 \ counted down to 1, so jump to time2 to leave the timer BCC time2 \ alone as it is inactive \ So the timers count down until they reach 1, at which \ point they stay put, inactive until they are reset DEC enemyDrainTimer,X \ The X-th timer is actively counting down, so decrement \ the timer .time2 DEX \ Decrement the byte counter BPL time1 \ Loop back until we have decremented all the active \ enemy timers LDA #2 \ Set updateTimer = 2 so it loops back to the start to STA updateTimer \ count through 2, 1, 0 again RTS \ Return from the subroutine .time3 DEC updateTimer \ Decrement the update timer RTS \ Return from the subroutine