/*
*file freeMountManager.cpp
*class FreeMountManager
*
*created on 25/01/2006
*Copyright Piquier Production
*
*/

#include "FreeMountManager.h"
#include "MemoryManager/debugNew.h"
#include "GroundMap.h"
#include "DebugAI.h"
#include "Renderer.h"

using namespace std;

FreeMountManager * FreeMountManager::instance = NULL;

//constructor

FreeMountManager:: FreeMountManager(){
}

FreeMountManager::~FreeMountManager(){
}

void FreeMountManager::createFreeMounts(int number, Point2D& pos, float radius){
	Point2D p;
	float alpha;
	float rad;
	ground::Case* c;
	for(int i=0;i<number;i++){
		do {
			alpha = rand();
			rad = radius*cos((float)rand());
			p.setCoord(pos.getX()+ rad*cos(alpha) ,pos.getY()+ rad*sin(alpha));
			c = ground::GroundMap::getInstance().getCase((int)(p.getX()+0.5f),(int)(p.getY()+0.5f));
		}while(	c==NULL ||				//dans le terrain
				!c->isPassable() ||		//passable
				c->getGroundType()==ground::FOREST || // pas de la forêt ni de l'orée
				c->getGroundType()==ground::OREE );

		this->createMount(p);
	}
}

void FreeMountManager::createMount(Point2D position) {
	Mount* mount = new Mount(position);
	mount->setProperties("Horse","Rouge");
	mount->setArmy("Neutre");
	RenderSystem::Renderer::getInstance().addRenderableEntity(*mount);
    addMount(mount);
}


void FreeMountManager:: addMount(Mount* mountToAdd) {
     mount[mountToAdd] = mountToAdd;
}


void FreeMountManager::removeMount(Mount* m){
	map<void*,Mount*>::iterator it = mount.find(m);
	if (it != mount.end()){
		// le mount existe dans la map
		mount.erase(it);
	}
}    

bool FreeMountManager::contains(Mount* m){
	return mount.find(m) != mount.end();
}

FreeMountManager& FreeMountManager::getInstance(){
	if (instance == NULL){
		instance = new FreeMountManager();
	}
	return *instance;
}

void FreeMountManager::destroy(){
	delete instance;
	instance = NULL;
}

std::map<void*,Mount*>* FreeMountManager::getAllFreeMount()
{
	return &mount;
}
