171 lines
4.5 KiB
C++
171 lines
4.5 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)
|
|
{
|
|
qDebug("don't use this function addSplash(int srcId, QPointF pos)");
|
|
return false;
|
|
// FIXME: говнокод
|
|
// TODO: find and correct errors here
|
|
/* 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(QPoint curTowerId)
|
|
{
|
|
if (gameData->curTowers.contains(curTowerId))
|
|
{
|
|
TowerType tw = gameData->curTowers.value(curTowerId);
|
|
srcSplashType src = gameData->players.at(tw.PlayerId)->srcTowers.at(tw.src).splashes.at(0);
|
|
SplashType spl;
|
|
spl.id = nextId;
|
|
spl.TowerId = curTowerId;
|
|
spl.srcTower = tw.src;
|
|
spl.PlayerId = tw.PlayerId;
|
|
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;
|
|
spl.pos = tw.pos; // FIXME: splash do not create in center or left-angle of tower, it must be in tower's side
|
|
spl.imgType = src.imgType;
|
|
spl.life = 0;
|
|
spl.src = 0;
|
|
//TriggerType trig;
|
|
for(int i=0; i<src.triggerIndexes.size(); i++)
|
|
{
|
|
srcTriggerType trig;
|
|
TriggerOnDest tod;
|
|
TriggerOnAlien toa;
|
|
TriggerOnTimer tot;
|
|
trig = gameData->players.at(tw.PlayerId)->srcTowers.at(tw.src).triggers.at(src.triggerIndexes.at(i));
|
|
switch (trig.type)
|
|
{
|
|
case srcTriggerType::onDestination :
|
|
tod.src = src.triggerIndexes.at(i);
|
|
spl.trigsOnDest.append(tod);
|
|
break;
|
|
case srcTriggerType::onAlienInRadius :
|
|
toa.src = src.triggerIndexes.at(i);
|
|
spl.trigsOnAlien.append(toa);
|
|
break;
|
|
case srcTriggerType::onTimer :
|
|
tot.src = src.triggerIndexes.at(i);
|
|
tot.timer = trig.timer;
|
|
spl.trigsOnTimer.append(tot);
|
|
break;
|
|
}
|
|
}
|
|
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)
|
|
{
|
|
bool badAl = false;
|
|
if (gameData->curAliens.contains(spl.AlienId))
|
|
spl.destination = gameData->curAliens.value(spl.AlienId).pos;
|
|
else
|
|
badAl = true;
|
|
if (distance2(spl.pos, spl.destination) < src.speed*src.speed)
|
|
{
|
|
if (badAl)
|
|
deadIndexes.push_back(spl.id);
|
|
spl.pos = spl.destination;
|
|
for (int k=0; k<spl.trigsOnDest.size(); k++)
|
|
{
|
|
// TODO: activate trigger
|
|
}
|
|
}
|
|
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);
|
|
}
|