#include "commun.h"

void heTouche(SONIC *s, int message)
{
        u32 i;
        if (message == ETAT_ACTION_ACTIVE)
        {
                DesactiveControle(s, CNT_TOUCHES);
                DesactiveControle(s, CNT_PHYSIQUE);
        }
        else if (message == ETAT_ACTION_DESACTIVE)
        {
                ActiveControle(s, CNT_TOUCHES);
                ActiveControle(s, CNT_PHYSIQUE);
                s->vx = 0;
                s->clignote = s->invincible;
        }
        else if (message == ETAT_ACTION_GERE)
        {
                s->invincible++;
                if (s->croche)
                        DesactiveEtat(s);
        }
        else if (message == ETAT_ACTION_AFFICHE)
        {
                s->cpt++;
                i = (s->cpt / 6) % 2;
                ExecSprScript(s->x, s->y, Obj_Spt_Pal_0_tiles, Obj_Spt_Pal_0_sprBlock + i, AjPalette(Pal_0_palette));
        }
}

void heMort(SONIC *s, int message)
{
        if (message == ETAT_ACTION_ACTIVE)
        {
                s->vy = -vMUL(5);
                CopieDMA(s, &copie_sonic, sizeof(SONIC) >> 1, DMA_16im);
                copie_sonic.x = copie_sonic.y = 0;
                s->controle = 0;
        }
        else if (message == ETAT_ACTION_DESACTIVE)
        {

                //Ne devrait pas arriver, car mort c'est mort! :p
        }
        else if (message == ETAT_ACTION_GERE)
        {
                GereSonicMort(s);
                s->clignote = 0;
                if (s->vy >= vMUL(10))
                        Quitte();
        }
        else if (message == ETAT_ACTION_AFFICHE)
        {
                ExecSprScript(s->x, s->y, Obj_Spt_Pal_0_tiles, Obj_Spt_Pal_0_sprBlock + 9, AjPalette(Pal_0_palette));
        }
}

void GereSonicNoye(SONIC *s)
{
        s->virgule_y += s->vy;
        CalcVirgule(s->y, s->virgule_y);
        s->vy += 16;
}

void heNoye(SONIC *s, int message)
{
        if (message == ETAT_ACTION_ACTIVE)
        {
                s->vy = 0;
                CopieDMA(s, &copie_sonic, sizeof(SONIC) >> 1, DMA_16im);
                copie_sonic.x = copie_sonic.y = 0;
                s->controle = 0;
                JoueSon(2, 0x52);        //Coule
        }
        else if (message == ETAT_ACTION_DESACTIVE)
        {

                //Ne devrait pas arriver, car mort c'est mort! :p
        }
        else if (message == ETAT_ACTION_GERE)
        {
                GereSonicNoye(s);
                s->clignote = 0;
                if (s->vy >= vMUL(10))
                        Quitte();
        }
        else if (message == ETAT_ACTION_AFFICHE)
        {
                ExecSprScript(s->x, s->y, Obj_Spt_Pal_0_tiles, Sonic_013_sprScript, AjPalette(Pal_0_palette));
        }
}

void heBumper(SONIC *s, int message)
{
        if (message == ETAT_ACTION_GERE)
        {
                if (s->croche || s->vy > 0)
                        DesactiveEtat(s);
        }
        else if (message == ETAT_ACTION_AFFICHE)
                ExecSprScript(s->x, s->y, Obj_Spt_Pal_0_tiles, Sonic_011_sprScript, AjPalette(Pal_0_palette));
}

void hePrendBulle(SONIC *s, int message)
{
        if (message == ETAT_ACTION_ACTIVE)
        {
                s->cpt = 0;
                s->vy = 0;
                s->saut = 0;
                Sonic_metEnBoule(s, 0);
                s->vx = 0;
                s->tempsEau = 60 * 30;
        }
        else if (message == ETAT_ACTION_GERE)
        {
                s->cpt++;
                if (s->cpt > 60 || s->croche)
                        DesactiveEtat(s);
        }
        else if (message == ETAT_ACTION_AFFICHE)
                ExecSprScript(s->x, s->y, Obj_Spt_Pal_0_tiles, Sonic_012_sprScript, AjPalette(Pal_0_palette));
}

void DesactiveEtat(SONIC *s)
{
        s->fctEtat(s, ETAT_ACTION_DESACTIVE);
        s->fctEtat = NULL;
}

void ActiveEtat(SONIC *s, FCT_ETAT nouvelEtat)
{
        s->fctEtat = nouvelEtat;
        s->fctEtat(s, ETAT_ACTION_ACTIVE);
}

void ChangeEtat(SONIC *s, FCT_ETAT nouvelEtat)
{
        if (s->fctEtat != NULL)
                DesactiveEtat(s);
        ActiveEtat(s, nouvelEtat);
}
