#include "towers.h"

Towers::Towers(Game_Data *gd, QObject *parent) :
    QObject(parent)
{
	gameData = gd;
}

bool Towers::AddTower(int playerId, int srcId, QPoint pos)
{
	//QByteArray ar;
	//ar << pos;
	if (playerId < 0 || playerId >= gameData->players.size())
	{
		qCritical("ERROR out of players range");
		return false;
	}
	if (srcId < 0 || srcId >= gameData->players.at(playerId)->srcTowers.size())
	{
		qCritical("ERROR out of towers range");
		return false;
	}
	TowerType tw;
	tw.src = srcId;
	tw.imgType = gameData->players.at(playerId)->srcTowers.at(srcId).imgType;
	tw.PlayerId = playerId;
	tw.angle = 0;
	tw.build = 0;
	tw.reload = 0;
	tw.aim = -1;
	tw .oldAim = -1;
	if (gameData->map->addTowerOnMap(playerId,pos))
	{
		tw.pos = pos;
		gameData->map->printMap();
		gameData->curTowers.insert(tw.pos,tw);
		return true;
	}
	return false;
}


void Towers::DelTower(QPoint pos)
{
	gameData->curTowers.remove(pos);
}


void Towers::update()
{
	for (int i; i<gameData->curTowers.size(); i++)
	{
		TowerType tw = gameData->curTowers.values().at(i);
		srcTowerType stw = gameData->players.at(tw.PlayerId)->srcTowers.at(tw.src);
		if (tw.build < stw.buildTime)
		{
			tw.build++;
		} else {
			// TODO: finding aim alien in this place
			if (tw.reload < stw.reload)
			{
				tw.reload++;
			} else {
				emit shot(stw.splashes.at(0), 0, tw);
			}
		}
	}
}
