//CGame.cpp


#include "CGame.h"
#include "windows.h"
#include <dinput.h>

#include "CGraphicSystem.h"
#include "CDInputManager.h"
#include "CObjectManager.h"
#include "CConsole.h"
#include "CImageManager.h"
#include "CBombObject.h"
#include "CTextReader.h"
#include "CParticleManager.h"
#include "CAIPlayerObject.h"
#include "CAIMap.h"

#define PADDLE_SPEED 10

bool CGame::Init()
{
	m_tileSize	=	CTextReader::getInt("TileSize");

	g_GraphicSystem.setGlobalScale(float((float)m_tileSize/64),float((float)m_tileSize/64));

	m_mapAnchor.x = 0;
	m_mapAnchor.y = m_tileSize;

	m_Map = (CMap*) g_ObjectManager.CreateObject(typMap);	//This is how you create an object!
	m_Map->setX(m_mapAnchor.x);
	m_Map->setY(m_mapAnchor.y);
	m_Map->Init("./maps/map1");

	m_Player1 = (CPlayerObject*) g_ObjectManager.CreateObject(typPlayer);
	m_Player1->Init("./objects/bomberman/bomberman.txt");

	 
	//Hopefully just temporary... sets the player at the right position	
	m_Player1->setX(m_tileSize);
	m_Player1->setY(m_tileSize*2);

	
	m_Player2 = (CAIPlayerObject*) g_ObjectManager.CreateObject(typAIPlayer);
	m_Player2->Init("./objects/bomberman/bomberman.txt");

	m_Player2->setX(15*m_tileSize);
	m_Player2->setY(10*m_tileSize);

	m_Player3 = (CAIPlayerObject*) g_ObjectManager.CreateObject(typAIPlayer);
	m_Player3->Init("./objects/bomberman/bomberman.txt");

	m_Player3->setX(15*m_tileSize);
	m_Player3->setY(2*m_tileSize);
	 

	m_Player4 = (CAIPlayerObject*) g_ObjectManager.CreateObject(typAIPlayer);
	m_Player4->Init("./objects/bomberman/bomberman.txt");

	m_Player4->setX(m_tileSize);
	m_Player4->setY(10*m_tileSize);


	m_moveUpP1.Init(DIK_UP);
	m_moveUpP1.mustRelease=false;

	m_moveDownP1.Init(DIK_DOWN);
	m_moveDownP1.mustRelease=false;

	m_moveRightP1.Init(DIK_RIGHT);
	m_moveRightP1.mustRelease=false;

	m_moveLeftP1.Init(DIK_LEFT);
	m_moveLeftP1.mustRelease=false;

	m_dropBombP1.Init(DIK_SPACE);
	m_dropBombP1.mustRelease=true;
	
	m_testSound = g_SoundSystem.LoadCSound("./sound/music.wav");
	g_SoundSystem.PlayCSound(m_testSound, true);

	

	return 0;
}

void CGame::Clear()
{

}

void CGame::ShutDown()
{

}



int CGame::MainLoop()
{
	//Input

	if(m_moveUpP1.isActive())
	{
		m_Player1->Move(N);
	}
	else if(m_moveDownP1.isActive())
	{
		m_Player1->Move(S);
	}
	else if(m_moveLeftP1.isActive())
	{		
		m_Player1->Move(W);
	}
	else if(m_moveRightP1.isActive())
	{
		m_Player1->Move(E);
	}

	if(m_dropBombP1.isActive())
	{		
		m_Player1->dropBomb();
	}

	g_ObjectManager.updateObjects();			
	g_ObjectManager.collisionDetect();


	g_ObjectManager.drawObjects();		

	
	//CAIMap* aiMap = g_ObjectManager.getAIMap();
	//aiMap->draw();


	
	//Temporary. This is a cheap way to make sure the player is always drawn on top. 
	//What we need to do is keep the list of objects drawn in the correct order, keeping the player 
	//on top and putting bombs and stuff underneath him.
	//m_Player1->Draw();	
	
	g_ObjectManager.RemoveObjectsInQueue();	

	return 0;
}

POINT CGame::getMapFromScreen(POINT scr)
{
	POINT mapCords;
	mapCords.x=0;
	mapCords.y=0;		
	
	mapCords.x = ((scr.x-m_mapAnchor.x)/m_tileSize);
	mapCords.y = ((scr.y-m_mapAnchor.y)/m_tileSize);


	return mapCords;

}