Files
aliendefender/splashes.cpp
2010-08-30 11:03:21 +04:00

141 lines
3.4 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;
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);
curDead = spl.id;
}
//isTowerSplash = true;
//if (!isTowerSplash)
// src = gameData->srcSplashes.at(spl.src);
if (distance2(spl.pos, spl.destination) < src.speed*src.speed)
{
deadIndexes.push_back(spl.id);
curDead = spl.id;
}
if (curDead < 0)
{
if (src.autoControl)
{
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));
}
// TODO: smooth splash rotate
// TODO: activate trigger and more...
gameData->curSplashes.insert(spl.id,spl);
}
for (int j=0; j<deadIndexes.size(); j++)
//delAlien(missIndex.at(j));
delSplash(deadIndexes.at(j));
}
void Splashes::delSplash(int Id)
{
gameData->curSplashes.remove(Id);
}