#include "EntSwimmer.h"
#include "GameLogic.h"
#include "EntPlayer.h"

EntSwimmer::EntSwimmer()
{
    m_sprite = NULL;
    GetRandomPosition(m_vPosition);
    Init();
    //find a good place to put us

    m_drownTimer = CL_System::get_time()+ GetGameLogic->m_drownTimerMax;
    SetType(C_TYPE_SWIMMER);
    SetSort(2400); //draw over beam
    m_changeSort = CL_System::get_time() + 1000;

    m_voice = Random(3);
    
    if (GetGameLogic->m_bJustStarted)
    {
        //force the first guy of the game to talk
        GetGameLogic->GetMessageDispatcher()->SendMsg(C_MESSAGE_SOUND, 0, NULL, C_SOUND_GUY1_HELP+m_voice);
        GetGameLogic->m_bJustStarted = false;
    }   else
    {
        
        if (Random(12) == 0) m_voice = 3;
        
        if (Random(5) == 0) //make talking a little rare
        {
            
            GetGameLogic->GetMessageDispatcher()->SendMsg(C_MESSAGE_SOUND, 0, NULL, C_SOUND_GUY1_HELP+m_voice);
        } else
        {
            m_voice = -1;
        }
    }
    
}

EntSwimmer::~EntSwimmer()
{
    SAFE_DELETE(m_sprite);
}

bool EntSwimmer::Init()
{
    //setup stuff
    m_sprite = new CL_Sprite("swimmer", GetApp()->GetResourceManager());
    return true; //success
}


void EntSwimmer::Render()
{
    m_sprite->set_scale(m_vScale.x, m_vScale.y);
    m_sprite->set_alpha(m_alpha);
    m_sprite->draw(m_vPosition.x, m_vPosition.y);

    //draw how long left they have

    
    if (GetType() == C_TYPE_SWIMMER)
    if (m_drownTimer > CL_System::get_time())
    {
    int sec = (m_drownTimer - CL_System::get_time())/1000; //get seconds left
    
    GetApp()->GetFont(C_FONT_GRAY)->draw(m_vPosition.x-5,m_vPosition.y-30, CL_String::from_int(sec));
    }
              
}

void EntSwimmer::OnMessage(Message &msg)
{

    switch(msg.msg)
    {
    case C_MESSAGE_GOT_RESCUED:
        if (GetType() == C_TYPE_SWIMMER)
        {
        

        SetType(C_TYPE_UNKNOWN); //so we don't get "rescued again"
        
        if (m_voice == -1)
        {
            GetGameLogic->GetMessageDispatcher()->SendMsg(C_MESSAGE_SOUND, 0, NULL, C_SOUND_RESCUE_BLIP);
        } else
        {
          GetGameLogic->GetMessageDispatcher()->SendMsg(C_MESSAGE_SOUND, 0, NULL, C_SOUND_GUY1_THANKS+m_voice);
          GetGameLogic->GetMessageDispatcher()->SendMsg(C_MESSAGE_SOUND, 40, NULL, C_SOUND_RESCUE_BLIP);
        }
        
        GetGameLogic->GetMessageDispatcher()->SendMsg(C_MESSAGE_KILL_ENTITY, 2000, this);
        
        m_bScaleUp = true;
        m_bFadeAlphaTo = true;
        m_alphaTarget = 0;
        GetGameLogic->OnRescued();
        }
        return;
   
    case C_MESSAGE_START_DAY:
        
        m_bFadeAlphaTo = true;
        SetType(C_TYPE_UNKNOWN);
        GetGameLogic->GetMessageDispatcher()->SendMsg(C_MESSAGE_KILL_ENTITY, 2000, this);
         break;
     }
 
    Entity::OnMessage(msg);
}


void EntSwimmer::Update(float step)
{
    Entity::Update(step);
 
    if (m_changeSort != 0 && m_changeSort < CL_System::get_time())
    {
        SetSort(1700); //hidden in the dark!
    }

    if (GetType() == C_TYPE_SWIMMER)
    if (m_drownTimer < CL_System::get_time())
    {
        //we just drowned!
        GetGameLogic->GetMessageDispatcher()->SendMsg(C_MESSAGE_SOUND, 0, NULL, C_SOUND_SWIMMER_DIE);
        SetType(C_TYPE_UNKNOWN); //so we don't get "rescued again"
        if (GetGameLogic->GetPlayer())
        {
            GetGameLogic->GetPlayer()->m_health--;
        }
        m_bScaleToNothing = true;
        GetGameLogic->GetMessageDispatcher()->SendMsg(C_MESSAGE_KILL_ENTITY, 2000, this);
    }

}
