

#include "groupennemy.h"
#include "Config.h"

#include "MemoryManager/debugNew.h"


GroupEnnemy::GroupEnnemy(void)
{
	this->radius = 0;
	this->groupPos.setCoord(0,0);
	this->units.clear();
}

GroupEnnemy::~GroupEnnemy(void)
{
	this->units.clear();
}

std::vector<EnnemyUnit*>& GroupEnnemy::getUnits()
{
	return this->units;
}

Point2D& GroupEnnemy::getPosition()
{
	groupPos = Point2D(0,0);
	for (std::vector<EnnemyUnit*>::iterator it = units.begin(); it != units.end(); ++it){
		groupPos+=(*it)->getPosition();
	}
	if (units.size()==0)
		return groupPos;
	groupPos.setX( groupPos.getX()/units.size());
	groupPos.setY( groupPos.getY()/units.size());
	return this->groupPos;
}

float GroupEnnemy::getRadius()
{
	return this->radius;
}

std::string GroupEnnemy::toString(void) { return ""; }

int GroupEnnemy::getForce(){
	int ret = 0;
	for(std::vector<EnnemyUnit*>::iterator it = units.begin(); it != units.end(); ++it){
		ret += (*it)->getUnit()->getWeapon()->damage;
	}
	return ret; 
}

int GroupEnnemy::getHP() {
	int ret = 0;
	for(std::vector<EnnemyUnit*>::iterator it = units.begin(); it != units.end(); ++it){
		ret += (*it)->getHealthPoint();
	}
	return ret;
}

int GroupEnnemy::getHPMax() {
	static float archerMaxHP = Config::getInstance().getVariable("Archer","healthPoint");
	static float infantryMaxHP = Config::getInstance().getVariable("Infantry","healthPoint");
	static float lancerMaxHP = Config::getInstance().getVariable("Lancer","healthPoint");
	int ret = 0;
	for(std::vector<EnnemyUnit*>::iterator it = units.begin(); it != units.end(); ++it){
		switch ((*it)->getType()){
			case unitEnumeration::ARCHER:
				ret += archerMaxHP;
				break;
			case unitEnumeration::LANCER:
				ret += lancerMaxHP;
				break;
			case unitEnumeration::INFANTRY:
				ret += infantryMaxHP;
				break;
		}
	}
	return ret;
}

int GroupEnnemy::getNumber() { return units.size (); }

void GroupEnnemy::addEnnemy(EnnemyUnit* ennemy){
	if (!containEnnemy(ennemy)){
		units.push_back(ennemy);		
	}
}

void GroupEnnemy::removeEnnemy(EnnemyUnit* ennemy){
	for (std::vector<EnnemyUnit*>::iterator it = units.begin(); it != units.end(); ++it){
		if (*it == ennemy){
			units.erase(it);
			// iterateur invalide
			return;
		}
	}
}

void GroupEnnemy::removeAllEnnemy(){
	units.clear();
}

bool GroupEnnemy::containEnnemy(EnnemyUnit* ennemy){
	for (std::vector<EnnemyUnit*>::iterator it = units.begin(); it != units.end(); ++it){
		if (*it == ennemy){
			return true;
		}
	}
	return false;
}

