//---------------------------
// Includes
//---------------------------
#include "StdAfx.h"
#include "resource.h"
#include "Character.h"

#include "Painter.h"
#include "ResourceLoader.h"
#include "InputState.h"
#include "CollisionMesh.h"
#include "Image.h"
#include "TextFormat.h"
#include "Audio.h"

//---------------------------
// Constructor & Destructor
//---------------------------
Character::Character(ResourceLoader* resourceLoaderPtr, const String& imagename, int hitPoints)
	:m_ImagePtr(0)
	,m_VCollisionMeshPtr(0)
	,m_HCollisionMeshPtr(0)
	,m_WeaponCollisionMeshPtr(0)
	,m_Position()
	,m_Velocity()
	,m_Speed(100)
	,m_Gravity(0,500)
	,m_CurrentAnimationStep(0)
	,m_CurrentState(0)
	,m_HitGround(false)
	,m_HitWall(false)
	,m_HeadingRight(true)
	,m_HitPoints(hitPoints)
	,m_CurrentHitPoints(hitPoints)
	,m_PitPos(0,0)
	,m_HitItem(false)
	,m_DamageAmount(0)
	,m_dead(false)
{
	m_ImagePtr = resourceLoaderPtr->LoadImage(imagename);
	if(m_ImagePtr == 0)
	{
		cout << "The image was not loaded properly";
	}
	m_VCollisionMeshPtr = resourceLoaderPtr->GetCollisionMesh();
	m_HCollisionMeshPtr = resourceLoaderPtr->GetCollisionMesh();

}

Character::~Character()
{
}

double Character::GetWidth() const
{
	return m_ImagePtr->GetWidth();
}

double Character::GetHeight() const
{
	return m_ImagePtr->GetHeight();
}

Vector2D Character::GetVelocity() const
{
	return m_Velocity;
}

void Character::SetVelocity(Vector2D vel)
{ 
	m_Velocity = vel;
}

Point2D Character::GetPosition() const
{
	return m_Position;
}

void Character::SetPosition(Point2D pt)
{
	//Als Character op een plaats wordt gezet, dan moeten ook beide CollisonMeshes die positie aannemen. 
	m_Position = pt;
	m_VCollisionMeshPtr->SetPos(pt);
	m_HCollisionMeshPtr->SetPos(pt);
}


CollisionMesh *Character::GetVCollisionMesh() const
{
	return m_VCollisionMeshPtr;
}

CollisionMesh *Character::GetHCollisionMesh() const
{
	return m_HCollisionMeshPtr;
}

CollisionMesh *Character::GetWeaponCollisionMesh() const
{
	return m_WeaponCollisionMeshPtr;
}

void Character::ChangeCurrentHitPoints(int hitPointsModifier)
{
	int hitPoints = GetCurrentHitPoints() + hitPointsModifier;
	if(hitPoints > m_HitPoints)
	{
		m_CurrentHitPoints = m_HitPoints;
	} else if(hitPoints < 0) {
		m_CurrentHitPoints = 0;
	} else {
		m_CurrentHitPoints = hitPoints;
	}
}

int Character::GetCurrentHitPoints() const
{
	return m_CurrentHitPoints;
}
int Character::GetHitPoints() const
{
	return m_HitPoints;
}

void Character::SetHitGround(bool hitGround)
{
	m_HitGround = hitGround;
}

void Character::SetHitWall(bool hitWall)
{
	m_HitWall = hitWall;
}

Point2D Character::getPitPos() const
{
	return m_PitPos;
}


void Character::HitPit()
{
	if(m_PitPos == Point2D(0,0))
	{
		m_PitPos = m_Position;
	}
	ChangeCurrentHitPoints(-GetHitPoints());
	m_dead = true;
}

void Character::ResetPitPos()
{
	m_PitPos = Point2D(0,0);
}

void Character::HitItem(bool hit)
{
	m_HitItem = hit;
}

// Set the current state to the given state and makes sure currentAnimationStep equals 0.
// Always use this method if you want to change the state.
void Character::SetCurrentState(int currentState)
{
	m_CurrentState = currentState;
	m_CurrentAnimationStep = 0;
}

int Character::GetDamageAmount() const
{
	return m_DamageAmount;
}

bool Character::GetDead() const
{
	return m_dead;
}

void Character::CheckDead()
{
	if(GetCurrentHitPoints() == 0)
	{
		m_dead = true;
		SetCurrentState(STATE_DYING);
	}
}

void Character::CleanUp()
{
	SafeDelete(&m_ImagePtr);
}


