134 lines
3.6 KiB
C++
134 lines
3.6 KiB
C++
#include "splashes.h"
|
|
#include <cmath>
|
|
|
|
Splashes::Splashes(Game_Data *gd, QObject *parent) :
|
|
QObject(parent)
|
|
{
|
|
gameData = gd;
|
|
nextId = 1;
|
|
}
|
|
|
|
|
|
bool Splashes::addSplash(int srcId, QPointF pos)
|
|
{
|
|
qFatal("don't use this function addSplash(int srcId, QPointF pos)");
|
|
if (srcId < 0 || srcId >= gameData->srcSplashes.size())
|
|
{
|
|
qCritical("ERROR out of splashes range");
|
|
return false;
|
|
}
|
|
srcSplashType src = gameData->srcSplashes.at(srcId);
|
|
SplashType spl;// = gameData->srcAliens.at(srcId);
|
|
spl.id = nextId;
|
|
spl.src = srcId;
|
|
spl.TowerId = QPoint(-1,-1);
|
|
spl.life = 0;
|
|
spl.imgType = src.imgType;
|
|
if (!gameData->map->rect().contains(pos.toPoint()))
|
|
{
|
|
qCritical("ERROR splash out of map size");
|
|
return false;
|
|
}
|
|
spl.pos = pos;
|
|
spl.AlienId = -1;
|
|
spl.angle = 0;
|
|
spl.destination = pos;
|
|
TriggerType trig;
|
|
for(int i=0; i<src.triggerIndexes.size(); i++)
|
|
{
|
|
trig.timer = gameData->srTriggers.at(src.triggerIndexes.at(i)).timer;
|
|
trig.src = src.triggerIndexes.at(i);
|
|
spl.triggers.push_back(trig);
|
|
}
|
|
gameData->curSplashes.insert(spl.id,spl);
|
|
nextId++;
|
|
return true;
|
|
}
|
|
|
|
|
|
void Splashes::addSplash(srcSplashType src, int index, TowerType tw)
|
|
{
|
|
SplashType spl;
|
|
spl.id = nextId;
|
|
spl.TowerId = tw.pos;
|
|
if (!gameData->curAliens.contains(tw.aim))
|
|
qFatal("Error finding splash aim! Tower has nvalid aim!");
|
|
spl.destination = gameData->curAliens.value(tw.aim).pos;
|
|
if (src.autoControl)
|
|
spl.AlienId = tw.aim;
|
|
else
|
|
spl.AlienId = -1;
|
|
spl.angle = tw.angle; // FIXME: tower not rotate
|
|
spl.pos = tw.pos; // FIXME: pos not in center or left-angle of tower
|
|
spl.imgType = src.imgType;
|
|
spl.life = 0;
|
|
spl.src = index;
|
|
TriggerType trig;
|
|
for(int i=0; i<src.triggerIndexes.size(); i++)
|
|
{
|
|
trig.timer = gameData->players.at(tw.PlayerId)->srcTowers.at(tw.src).triggers.at(src.triggerIndexes.at(i)).timer;
|
|
trig.src = src.triggerIndexes.at(i);
|
|
spl.triggers.push_back(trig);
|
|
}
|
|
gameData->curSplashes.insert(spl.id,spl);
|
|
nextId++;
|
|
}
|
|
|
|
|
|
void Splashes::update()
|
|
{
|
|
QList <int> deadIndexes;
|
|
for (int i=0; i<gameData->curSplashes.size(); i++)
|
|
{
|
|
//int curDead = -1;
|
|
float arctg,angl;
|
|
SplashType spl = gameData->curSplashes.values().at(i);
|
|
//bool isTowerSplash = false;
|
|
srcSplashType src;
|
|
TowerType tw;
|
|
tw = gameData->curTowers.value(spl.TowerId);
|
|
src = gameData->players.at(tw.PlayerId)->srcTowers.at(tw.src).splashes.at(spl.src);
|
|
spl.life++;
|
|
if (spl.life > src.lifetime)
|
|
deadIndexes.append(spl.id);
|
|
//isTowerSplash = true;
|
|
//if (!isTowerSplash)
|
|
// src = gameData->srcSplashes.at(spl.src);
|
|
if (src.autoControl)
|
|
{
|
|
if (distance2(spl.pos, spl.destination) < src.speed*src.speed)
|
|
deadIndexes.push_back(spl.id);
|
|
if (gameData->curAliens.contains(spl.AlienId))
|
|
spl.destination = gameData->curAliens.value(spl.AlienId).pos;
|
|
arctg = std::atan2(spl.pos.x() - spl.destination.x(),spl.pos.y() - spl.destination.y());
|
|
//if (tmpdy < 0) arctg=arctg+M_PI;
|
|
angl = 180.0f*(-arctg)/M_PI;
|
|
|
|
spl.angle = angl;
|
|
|
|
spl.pos.setX(spl.pos.x()
|
|
-src.speed*std::sin(arctg));
|
|
spl.pos.setY(spl.pos.y()
|
|
-src.speed*std::cos(arctg));
|
|
} else {
|
|
if (!gameData->map->rect().contains(spl.pos.toPoint()))
|
|
deadIndexes.push_back(spl.id);
|
|
spl.pos.setX(spl.pos.x()
|
|
-src.speed*std::sin(-spl.angle*M_PI/180.f));
|
|
spl.pos.setY(spl.pos.y()
|
|
-src.speed*std::cos(-spl.angle*M_PI/180.f));
|
|
}
|
|
// TODO: smooth splash rotate
|
|
// TODO: activate trigger and more...
|
|
gameData->curSplashes.insert(spl.id,spl);
|
|
}
|
|
for (int j=0; j<deadIndexes.size(); j++)
|
|
delSplash(deadIndexes.at(j));
|
|
}
|
|
|
|
|
|
void Splashes::delSplash(int Id)
|
|
{
|
|
gameData->curSplashes.remove(Id);
|
|
}
|