#include "Interrupt.h"

u16 collisionTimerCount = 0;
u16 oxygenTimerCount = 0;
u8 collisionActive = FALSE;
u8 oxygenDepleting = FALSE;
u8 runWaterLogic = FALSE;

// The current count of how long since the most recent projectile was destroyed.
u8 projectileDestroyedTimerCount = 0;

// The current count of the water timer for the chasing\gesture water.
u16 waterTimerCount = 0;

// Used to ensure the sound and music plays at the right speed.
u8 soundCounter_ = 0;

void interruptHandler()
{
    // Scan for any keyboard changes.
    cpct_scanKeyboard_if();

    if (collisionActive)
    {
        if (collisionTimerCount++ > 320)
        {
            collisionActive = FALSE;
        }
    }

    if (oxygenDepleting)
    {
        if (oxygenTimerCount++ > oxygenDepletionTimerInterval)
        {
            oxygenDepleting = 2;
            oxygenTimerCount = 0;
        }
    }

    // Every 1000 iterations of the interrupt handler the water logic should be run.
    if (waterTimerCount++ > 1000)
    {
        runWaterLogic = TRUE;
        waterTimerCount = 0;
    }

    // There is a delay between shooting projectiles to prevent the player shooting too quickly.
    if (projectileDestroyedTimerCount && projectileDestroyedTimerCount++ > PROJECTILE_DESTROY_DELAY)
    {
        projectileDestroyedTimerCount = 0;
    }

    // Sound is played every 6 interrupt to get the speed right.
    // Make sure the sound has not previously been stopped.
    if (soundMode == SOUND_MODE_ON && soundCounter_++ > 5)
    {
        playSound();
        soundCounter_ = 0;
    }
}

void activateCollisionTimer()
{
    if (!collisionActive)
    {
        collisionActive = TRUE;
        collisionTimerCount = 0;
    }
}