#include "stdafx.h"

#include "Missile.h"


const float Missile::CALIBRATION_ANGLE_X = 90.0f;
const float Missile::CALIBRATION_ANGLE_Y = 0.0f;
const float Missile::CALIBRATION_ANGLE_Z = 0.0f;
const float Missile::SCALE_FACTOR        = 5.0f;
const float Missile::SPEED_DEFAULT = 0.2f;
const float Missile::ROT_SPEED_DEFAULT = 0.2f;
const unsigned int Missile::TIME_TO_LIVE = 5000;

Missile::Missile(void)
{
}


Missile::Missile(LPCTSTR file, LPCTSTR path, float calibrationAngleX, float calibrationAngleY, float calibrationAngleZ):Projectile(file, path, Missile::CALIBRATION_ANGLE_X, Missile::CALIBRATION_ANGLE_Y, Missile::CALIBRATION_ANGLE_Z)
{    
    Initialize();
}


Missile::~Missile(void)
{
}

void Missile::Initialize()
{
    m_Speed = Missile::SPEED_DEFAULT;
    m_ScaleFactorX = Missile::SCALE_FACTOR;
    m_ScaleFactorY = Missile::SCALE_FACTOR;
    m_ScaleFactorZ = Missile::SCALE_FACTOR;
    InitializeBoundingBoxByModel();
}

void Missile::Cycle( float deltaMs )
{
    if (m_Active)
        m_LineOfSightIntercepting.Cycle(deltaMs);

    m_Velocity.x = m_Speed * sin(D3DXToRadian(m_RotZ));
    m_Velocity.y = m_Speed * cos(D3DXToRadian(m_RotZ)) ;

    UpdatePosition(deltaMs);

    CheckTimeToLive(deltaMs);
}


void Missile::Fire(GameObject3D& shootingCraft, const GameObject3D& preyObject)
{

    m_Speed         = Missile::SPEED_DEFAULT;
    m_RotationSpeed = Missile::ROT_SPEED_DEFAULT;
    timeToLive		= Missile::TIME_TO_LIVE;

    m_LineOfSightIntercepting.SetPreyObject(preyObject);
    m_LineOfSightIntercepting.SetHostObject(this);   
    m_RotZ = shootingCraft.m_RotZ;
    timeTraveledMs = 0.0;

    m_Position = shootingCraft.m_Position;
    m_Velocity = shootingCraft.m_Velocity;
    m_Active = true;
}

void Missile::Destroy()
{
    m_Active = false;
}