//-----------------------------------------------------------------
// http://www.digitalartsandentertainment.be/
//-----------------------------------------------------------------
#include "StdAfx.h"
#include "Resource.h"
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "BarbarianOnslaughtApp.h"	

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

BarbarianOnslaughtApp::BarbarianOnslaughtApp()
	:m_HeroPtr(0)
	,m_SpawnPoint(40,450)
	,m_LevelPtr(0)
	,m_CamAngle(0)
	,m_CameraScale(1)
	,m_HealthBar(0)
	,m_RageBar(0)
	,m_FoodList(0)
	,m_BarrelList(0)
	,m_EnemyList(0)
{
	// nothing to create
}

BarbarianOnslaughtApp::~BarbarianOnslaughtApp()																						
{
	// nothing to destroy
}

void BarbarianOnslaughtApp::GameInitialize(GameConfigurator *gameConfiguratorPtr)			
{
	gameConfiguratorPtr->SetWindowTitle("Barbarian Onslaught: The Secret Of Steel");
	gameConfiguratorPtr->SetWidth(640);
	gameConfiguratorPtr->SetHeight(500);
	gameConfiguratorPtr->EnableWindowResize(false);
}

void BarbarianOnslaughtApp::GameStart(ResourceLoader* resourceLoaderPtr)
{
	m_HeroPtr = new Hero(resourceLoaderPtr,"./resources/Hero_Sprites.tif",34,5000);
	m_HeroPtr->SetPosition(m_SpawnPoint);

	m_LevelPtr = new Level(resourceLoaderPtr,"./resources/Level.tif","./resources/LvlCollision_Mesh.svg","./resources/LvlCollision_Mesh_Raycast.svg");
	m_HealthBar = new Bar(resourceLoaderPtr,"./resources/Health_Bar.png",Point2D(10,5),Point2D(6,6),m_HeroPtr->GetHitPoints(),90,25,ColorRGBA(153,0,0,255));
	m_RageBar = new Bar(resourceLoaderPtr,"./resources/Rage_Bar.png",Point2D(49,33),Point2D(5,4),m_HeroPtr->getRage(),63,14,ColorRGBA(255,255,128,255));
	
	m_FoodList = new FoodList();
	Food *cake = new Food(resourceLoaderPtr,"./resources/Cake.png",0.25);
	cake->SetPosition(Point2D(3200,625));
	m_FoodList->Add(cake);

	m_BarrelList = new BarrelList();
	Barrel *barrel = new Barrel(resourceLoaderPtr,"./resources/Barrel.png");
	barrel->SetPosition(Point2D(2400,500));
	m_BarrelList->Add(barrel);

	m_EnemyList = new EnemyList();
	Goblin *goblin1 = new Goblin(resourceLoaderPtr,"./resources/Enemy_Shield_Sprites.tif",25);
	goblin1->SetPosition(Point2D(800,300));
	Goblin *goblin2 = new Goblin(resourceLoaderPtr,"./resources/Enemy_Shield_Sprites.tif",25);
	goblin2->SetPosition(Point2D(1400,300));
	Goblin *goblin3 = new Goblin(resourceLoaderPtr,"./resources/Enemy_Shield_Sprites.tif",25);
	goblin3->SetPosition(Point2D(1450,300));
	Goblin *goblin4 = new Goblin(resourceLoaderPtr,"./resources/Enemy_Shield_Sprites.tif",25);
	goblin4->SetPosition(Point2D(2500,500));
	Goblin *goblin5 = new Goblin(resourceLoaderPtr,"./resources/Enemy_Shield_Sprites.tif",25);
	goblin5->SetPosition(Point2D(3400,500));
	Goblin *goblin6 = new Goblin(resourceLoaderPtr,"./resources/Enemy_Shield_Sprites.tif",25);
	goblin6->SetPosition(Point2D(3450,500));
	Goblin *goblin7 = new Goblin(resourceLoaderPtr,"./resources/Enemy_Shield_Sprites.tif",25);
	goblin7->SetPosition(Point2D(3500,500));

	Archer *archer1 = new Archer(resourceLoaderPtr,"./resources/Archer_Sprites.tif","./resources/Arrow.png",25);
	archer1->SetPosition(Point2D(1550,300));
	m_EnemyList->Add(goblin1);
	m_EnemyList->Add(goblin2);
	m_EnemyList->Add(goblin3);
	m_EnemyList->Add(goblin4);
	m_EnemyList->Add(goblin5);
	m_EnemyList->Add(goblin6);
	m_EnemyList->Add(goblin7);
	m_EnemyList->Add(archer1);

}
void BarbarianOnslaughtApp::GameEnd()
{
	delete m_HeroPtr;
	delete m_LevelPtr;
	delete m_HealthBar;
	delete m_RageBar;
	delete m_FoodList;
	delete m_BarrelList;
	delete m_EnemyList;
}
void BarbarianOnslaughtApp::KeyUp(TCHAR cKey)
{
	m_HeroPtr->KeyUp(cKey);
	m_BarrelList->KeyUp(cKey);
}

void BarbarianOnslaughtApp::GameTick(InputState *inputStatePtr)
{
	//temporary spawn button
	if(inputStatePtr->IsKeyDown('S'))
	{
		m_HeroPtr->SetPosition(m_SpawnPoint);
		m_HeroPtr->ResetPitPos();
	}

	m_LevelPtr->SetPosition(Point2D(m_LevelPtr->GetWidth()/2/*-80*/,m_LevelPtr->GetHeight()/2));
	m_HeroPtr->Tick(inputStatePtr);

	m_EnemyList->Tick(inputStatePtr,m_HeroPtr);

	m_LevelPtr->Tick(inputStatePtr);
	m_HealthBar->setPoints(m_HeroPtr->GetCurrentHitPoints());
	m_HealthBar->Tick(inputStatePtr);
	m_RageBar->setPoints(m_HeroPtr->getCurrentRage());
	m_RageBar->Tick(inputStatePtr);
	m_FoodList->Tick(inputStatePtr);
	m_FoodList->HitTest(m_HeroPtr);
	m_BarrelList->Tick(inputStatePtr);
	m_BarrelList->HitTest(m_HeroPtr);

	//Handle Collisions
	CollisionHandling(m_HeroPtr,m_LevelPtr);

	m_EnemyList->CheckCollision(m_LevelPtr);
	m_BarrelList->CheckCollision(m_LevelPtr, inputStatePtr);

	//m_Campos is the position of the camera in world coordinates
	//Center the position of the camera around the position of the hero
	if(m_HeroPtr->getPitPos() != Point2D(0,0))
	{
		m_CamPos = m_HeroPtr->getPitPos();
	} else {
		m_CamPos = m_HeroPtr->GetPosition();
	}
	m_CamPos.x -= inputStatePtr->GetWindowWidth()/2;
	m_CamPos.y -= inputStatePtr->GetWindowHeight()/2;

	if(m_CamPos.x < 0) m_CamPos.x = 0;
	if(m_CamPos.y < 0) m_CamPos.y = 0;

	// make sure the camera doesn't go outside of the level
	if(m_CamPos.x + inputStatePtr->GetWindowWidth()  > m_LevelPtr->GetWidth())
	{
		m_CamPos.x = m_LevelPtr->GetWidth() - inputStatePtr->GetWindowWidth();
	}

	if(m_CamPos.y + inputStatePtr->GetWindowHeight()  > m_LevelPtr->GetHeight())
	{
		m_CamPos.y = m_LevelPtr->GetHeight() - inputStatePtr->GetWindowHeight();
	}

	Matrix2D matCamPosition;
	matCamPosition.SetAsTranslate(m_CamPos);
	m_MatScreen = matCamPosition.Inverse();
}

void BarbarianOnslaughtApp::GamePaint(Painter *painterPtr)
{
	painterPtr->EnableAntiAlias(false);
	painterPtr->Clear(193,193,193);
	m_LevelPtr->Paint(painterPtr,m_MatScreen);
	m_EnemyList->Paint(painterPtr,m_MatScreen);
	m_HeroPtr->Paint(painterPtr,m_MatScreen);
	m_FoodList->Paint(painterPtr,m_MatScreen);
	m_BarrelList->Paint(painterPtr,m_MatScreen);
	m_HealthBar->Paint(painterPtr);
	m_RageBar->Paint(painterPtr);
}

void BarbarianOnslaughtApp::CallAction(Caller* callerPtr)
{

}

void BarbarianOnslaughtApp::WindowResized(double width, double height)
{

}

void BarbarianOnslaughtApp::CollisionHandling(Character *characterPtr, LevelProp *propPtr)
{
	propPtr->CheckCollision(characterPtr);
}





