#include "stdafx.h"

#include "TimePilot.h"
#include "EnemyCraft.h"
#include "DxWrapper.h"
#include "Projectile.h"
#include "Explosion.h"
#include "MovementLogic.h"
#include "Model.h"

// statics initialisieren
const float EnemyCraft::ROTATION_SPEED = 0.2f;
const float EnemyCraft::SPEED_DEFAULT = 0.02f;
const float EnemyCraft::CALIBRATION_ANGLE_ENEMY1_X       = 90.0f;       // grund-rotation des models
const float EnemyCraft::CALIBRATION_ANGLE_ENEMY1_Y       = 0.0f;         // grund-rotation des models
const float EnemyCraft::CALIBRATION_ANGLE_ENEMY1_Z       = 0.0f;        // grund-rotation des models	
const float EnemyCraft::SCALE_FACTOR                    = 1.0f;         
Model EnemyCraft::s_Model = Model();
//--

EnemyCraft::EnemyCraft(void)
{
    Initialize();
}

EnemyCraft::EnemyCraft(LPCTSTR file, LPCTSTR path, float calibrationAngleX, float calibrationAngleY, float calibrationAngleZ): GameObject3D(file, path, calibrationAngleX, calibrationAngleY, calibrationAngleZ)
{
    Initialize();
}

EnemyCraft::EnemyCraft(const Model &model) : GameObject3D(model)
{
    Initialize();
}

EnemyCraft::~EnemyCraft(void)
{
    delete m_MovementLogic;
}


void EnemyCraft::Initialize()
{
    m_Destroyed = true;
    m_HitPoints = 1;
    m_SpeedDefault = EnemyCraft::SPEED_DEFAULT;
    m_RotationSpeed = EnemyCraft::ROTATION_SPEED;
    m_ScaleFactorX = EnemyCraft::SCALE_FACTOR;
    m_ScaleFactorY = EnemyCraft::SCALE_FACTOR;
    m_ScaleFactorZ = EnemyCraft::SCALE_FACTOR;
    InitializeBoundingBoxByModel();
}

/** wird aufgerufen, sobald das Objekt einen treffer kassiert hat
*/
void EnemyCraft::Impact(Projectile &p)
{
    m_HitPoints -= p.m_DamagePoints;
    if (m_HitPoints <= 0)
    {
        Destroy();
    }

    // projektil zerstören ?
    p.Destroy();
}

void EnemyCraft::Destroy()
{
    m_Destroyed = true;
}


/** spawnt das Objekt an den angegebenen Koordinaten
*/
void EnemyCraft::Spawn(float x, float y, float z)
{
    m_Active = true;
    m_Destroyed = false;
    m_HitPoints = 1;
    m_Speed = m_SpeedDefault;
    m_Position.set(CPos(x,y,z));
}

void EnemyCraft::Cycle(float deltaMs)
{
    if (m_Active)
    {
        m_MovementLogic->Cycle(deltaMs);
        m_Velocity.x = m_Speed * sin(D3DXToRadian(m_RotZ));
        m_Velocity.y = m_Speed * cos(D3DXToRadian(m_RotZ)) ;
        UpdatePosition(deltaMs);  
    }
}

bool EnemyCraft::IsDestroyed()
{
    return m_Destroyed;
}

void EnemyCraft::Draw()
{
    if (!m_Destroyed)
    {
        GameObject3D::Draw();
    }
	
    //only for debug purposes
    if (m_MovementLogic)
        m_MovementLogic->Draw();
		
}

void EnemyCraft::SetMovementLogic(MovementLogic* movementLogic)
{
    m_MovementLogic = movementLogic;
}

const Model& EnemyCraft::GetStandardModel()
{
	DxWrapper::Instance()->LogWrite("EnemyCraft::GetStandardModel()");

    if (!s_Model.Mesh)
    {
		DxWrapper::LoadModel(&s_Model, FILENAME_MODEL_ENEMY1, FILEPATH_MODEL_ENEMY1);
		s_Model.calibrationAngleX = EnemyCraft::CALIBRATION_ANGLE_ENEMY1_X;
		s_Model.calibrationAngleY = EnemyCraft::CALIBRATION_ANGLE_ENEMY1_Y;
		s_Model.calibrationAngleZ = EnemyCraft::CALIBRATION_ANGLE_ENEMY1_Z;
    }
    return s_Model;   
}
