changes in arhitecture, some improvments, much optimising and simplify,
many bagfixes and new graphics engine
This commit is contained in:
275
splashes.cpp
275
splashes.cpp
@@ -2,163 +2,206 @@
|
||||
#include <cmath>
|
||||
|
||||
Splashes::Splashes(Game_Data *gd, QObject *parent) :
|
||||
QObject(parent)
|
||||
QObject(parent)
|
||||
{
|
||||
gameData = gd;
|
||||
nextId = 1;
|
||||
}
|
||||
|
||||
|
||||
void Splashes::addSplash(int player, int srcTower, int srcId, QPointF pos, QPointF dest, int aim, QPoint curTower)
|
||||
void Splashes::addSplash(int player, srcSplashType *src, QPointF pos, QPointF dest, int aim)
|
||||
{
|
||||
srcSplashType src = gameData->players.at(player)->srcTowers.at(srcTower).splashes.at(srcId);
|
||||
SplashType spl;
|
||||
spl.id = nextId;
|
||||
spl.TowerId = curTower;
|
||||
spl.srcTower = srcTower;
|
||||
spl.towerId = QPoint(-1,-1);
|
||||
spl.srcTow = 0;
|
||||
spl.PlayerId = player;
|
||||
spl.AlienId = 0;
|
||||
spl.pos = pos;
|
||||
spl.destination = dest;
|
||||
spl.AlienId = aim;
|
||||
spl.angle = 180.0f*(- std::atan2(spl.pos.x() - spl.destination.x(),spl.pos.y() - spl.destination.y()))/M_PI;
|
||||
spl.life = 0;
|
||||
spl.src = src;
|
||||
addSplash(&spl);
|
||||
}
|
||||
|
||||
|
||||
void Splashes::addSplash(const TowerType &tower)
|
||||
{
|
||||
SplashType spl;
|
||||
spl.src = &(tower.src->splashes[0]);
|
||||
spl.id = nextId;
|
||||
spl.towerId = tower.pos;
|
||||
spl.PlayerId = tower.PlayerId;
|
||||
spl.AlienId = tower.aim;
|
||||
spl.srcTow = tower.src;
|
||||
spl.pos = QPointF(tower.pos);
|
||||
if (gameData->aliens->curAliens.contains(spl.AlienId))
|
||||
spl.destination = gameData->aliens->curAliens.value(spl.AlienId).pos+QPointF(0.5,0.5);
|
||||
else {qCritical("aim alien not exists(!)");return;}
|
||||
spl.angle = 180.0f*(- std::atan2(spl.pos.x() - spl.destination.x(),spl.pos.y() - spl.destination.y()))/M_PI;
|
||||
spl.life = 0;
|
||||
addSplash(&spl);
|
||||
}
|
||||
|
||||
|
||||
void Splashes::addSplash(srcTowerType *srctower, int id, QPointF pos, QPointF dest, int aim, QPoint towerId)
|
||||
{
|
||||
SplashType spl;
|
||||
if (srctower != 0)
|
||||
spl.src = &(srctower->splashes[id]);
|
||||
else spl.src = &(gameData->srcSplashes[id]);
|
||||
spl.id = nextId;
|
||||
spl.towerId = towerId;
|
||||
spl.PlayerId = srctower->PlayerId;
|
||||
spl.AlienId = aim;
|
||||
spl.pos = pos;
|
||||
spl.destination = dest;
|
||||
if (src.autoControl)
|
||||
{
|
||||
if (gameData->curAliens.contains(aim))
|
||||
spl.destination = gameData->curAliens.value(aim).pos+QPointF(0.5,0.5);
|
||||
}
|
||||
spl.angle = 180.0f*(- std::atan2(spl.pos.x() - spl.destination.x(),spl.pos.y() - spl.destination.y()))/M_PI;
|
||||
spl.imgType = src.imgType;
|
||||
spl.animIndex = 0;
|
||||
spl.life = 0;
|
||||
spl.src = srcId;
|
||||
srcTriggerType trig;
|
||||
for(int i=0; i<src.triggerIndexes.size(); i++)
|
||||
spl.srcTow = srctower;
|
||||
addSplash(&spl);
|
||||
}
|
||||
|
||||
|
||||
void Splashes::addSplash(SplashType * spl)
|
||||
{
|
||||
srcTriggerType * trig;
|
||||
for(int i=0; i<spl->src->triggerIndexes.size(); i++)
|
||||
{
|
||||
TriggerOnDest tod;
|
||||
TriggerOnAlien toa;
|
||||
TriggerOnTimer tot;
|
||||
trig = gameData->players.at(player)->srcTowers.at(srcTower).triggers.at(src.triggerIndexes.at(i));
|
||||
switch (trig.type)
|
||||
if (spl->srcTow != 0)
|
||||
trig = &(spl->srcTow->triggers[spl->src->triggerIndexes.at(i)]);
|
||||
else {qDebug("no parent tower"); trig = &(gameData->srcTriggers[spl->src->triggerIndexes.at(i)]);}
|
||||
switch (trig->type)
|
||||
{
|
||||
case srcTriggerType::onDestination :
|
||||
tod.src = src.triggerIndexes.at(i);
|
||||
spl.trigsOnDest.append(tod);
|
||||
tod.src = trig;
|
||||
spl->trigsOnDest.append(tod);
|
||||
break;
|
||||
case srcTriggerType::onAlienInRadius :
|
||||
toa.src = src.triggerIndexes.at(i);
|
||||
spl.trigsOnAlien.append(toa);
|
||||
toa.src = trig;
|
||||
spl->trigsOnAlien.append(toa);
|
||||
break;
|
||||
case srcTriggerType::onTimer :
|
||||
tot.src = src.triggerIndexes.at(i);
|
||||
tot.src = trig;
|
||||
tot.timer = 0;
|
||||
spl.trigsOnTimer.append(tot);
|
||||
spl->trigsOnTimer.append(tot);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
gameData->curSplashes.insert(spl.id,spl);
|
||||
spl->item = new ADItem(spl->id, ADItem::Splash, spl->src->images);
|
||||
spl->item->setPos(spl->pos*cellSize);
|
||||
spl->item->setRotation(spl->angle);
|
||||
scene->addItem(spl->item);
|
||||
gameData->curSplashes.insert(spl->id,*spl);
|
||||
nextId++;
|
||||
}
|
||||
|
||||
|
||||
void Splashes::addSplash(QPoint curTowerId)
|
||||
{
|
||||
if (gameData->curTowers.contains(curTowerId))
|
||||
{
|
||||
TowerType tw = gameData->curTowers.value(curTowerId);
|
||||
addSplash(tw.PlayerId,tw.src,0,QPointF(tw.pos+QPoint(1,1)),gameData->curAliens.value(tw.aim).pos+QPointF(0.5,0.5),tw.aim,tw.pos);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Splashes::update()
|
||||
{
|
||||
QList <int> deadIndexes;
|
||||
// FIXME : rewrite loop with iterators, and correct onTrigTimer()
|
||||
for (int i=0; i<gameData->curSplashes.size(); i++)
|
||||
for (QHash<int, SplashType>::iterator
|
||||
spl = gameData->curSplashes.begin();
|
||||
spl != gameData->curSplashes.end(); ++spl)
|
||||
{
|
||||
float arctg,angl;
|
||||
SplashType spl = gameData->curSplashes.values().at(i);
|
||||
srcSplashType src;
|
||||
// FIXME : if splash is not tower's splash, will be some trouble
|
||||
src = gameData->players.at(spl.PlayerId)->srcTowers.at(spl.srcTower).splashes.at(spl.src);
|
||||
spl.life++;
|
||||
if (spl.life > src.lifetime)
|
||||
deadIndexes.append(spl.id);
|
||||
if (!spl.trigsOnTimer.isEmpty())
|
||||
{
|
||||
doTriggerOnTimer(&spl,&deadIndexes);
|
||||
}
|
||||
if (src.autoControl)
|
||||
spl->life++;
|
||||
if (spl->life > spl->src->lifetime)
|
||||
deadIndexes.append(spl->id);
|
||||
if (!spl->trigsOnTimer.isEmpty())
|
||||
doTriggerOnTimer(spl,&deadIndexes);
|
||||
float arctg;
|
||||
float speed = spl->src->speed;
|
||||
float speed2 = speed*speed;
|
||||
if (spl->src->autoControl && spl->AlienId != -1)
|
||||
{
|
||||
bool badAl = false;
|
||||
if (gameData->curAliens.contains(spl.AlienId))
|
||||
spl.destination = gameData->curAliens.value(spl.AlienId).pos+QPointF(0.5,0.5);
|
||||
else
|
||||
if (gameData->aliens->curAliens.contains(spl->AlienId))
|
||||
spl->destination = gameData->aliens->curAliens.value(spl->AlienId).pos+QPointF(0.5,0.5);
|
||||
else {
|
||||
badAl = true;
|
||||
if (distance2(spl.pos, spl.destination) < src.speed*src.speed)
|
||||
spl->AlienId = -1;
|
||||
}
|
||||
if (distance2(spl->pos, spl->destination) < speed2)
|
||||
{
|
||||
if (badAl)
|
||||
deadIndexes.push_back(spl.id);
|
||||
spl.pos = spl.destination;
|
||||
deadIndexes.push_back(spl->id);
|
||||
spl->pos = spl->destination;
|
||||
doTriggerOnDest(spl,&deadIndexes,badAl);
|
||||
}
|
||||
arctg = std::atan2(spl.pos.x() - spl.destination.x(),spl.pos.y() - spl.destination.y());
|
||||
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));
|
||||
arctg = std::atan2(spl->pos.x() - spl->destination.x(),spl->pos.y() - spl->destination.y());
|
||||
spl->angle = 180.0f*(-arctg)/M_PI;
|
||||
} else {
|
||||
if (!gameData->map->rect().contains(spl.pos.toPoint()))
|
||||
deadIndexes.push_back(spl.id);
|
||||
if (!spl.trigsOnDest.isEmpty())
|
||||
if (!gameData->map->rect().contains(spl->pos.toPoint()))
|
||||
deadIndexes.push_back(spl->id);
|
||||
if (!spl->trigsOnDest.isEmpty())
|
||||
{
|
||||
if (distance2(spl.pos, spl.destination) < src.speed*src.speed)
|
||||
if (distance2(spl->pos, spl->destination) < speed2)
|
||||
{
|
||||
spl.pos = spl.destination;
|
||||
spl->pos = spl->destination;
|
||||
doTriggerOnDest(spl,&deadIndexes,true);
|
||||
}
|
||||
}
|
||||
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));
|
||||
arctg = -spl->angle*M_PI/180.f;
|
||||
}
|
||||
// TODO: smooth splash rotate
|
||||
gameData->curSplashes.insert(spl.id,spl);
|
||||
spl->pos.rx() -= speed*std::sin(arctg);
|
||||
spl->pos.ry() -= speed*std::cos(arctg);
|
||||
/// TODO: smooth splash rotate
|
||||
spl->item->setPos(spl->pos*cellSize);
|
||||
spl->item->setRotation(spl->angle);
|
||||
if (speed == 0) spl->item->next(1.f);
|
||||
else spl->item->next(speed*cellSize);
|
||||
}
|
||||
for (int j=0; j<deadIndexes.size(); j++)
|
||||
delSplash(deadIndexes.at(j));
|
||||
for (QHash<int,DeadAlienIndex>::iterator j = dead.begin();
|
||||
j != dead.end(); ++j)
|
||||
emit killAlien(j->player,j->id);
|
||||
dead.clear();
|
||||
}
|
||||
|
||||
|
||||
void Splashes::delSplash(int Id)
|
||||
{
|
||||
scene->removeItem(gameData->curSplashes[Id].item);
|
||||
delete gameData->curSplashes[Id].item;
|
||||
gameData->curSplashes.remove(Id);
|
||||
}
|
||||
|
||||
|
||||
void Splashes::doTriggerOnTimer(SplashType *spl, QList<int> *deadIndexes)
|
||||
/// FIXME : correct onTrigTimer()
|
||||
void Splashes::doTriggerOnTimer(QHash<int, SplashType>::iterator
|
||||
spl, QList<int> *deadIndexes)
|
||||
{
|
||||
for (int k=0; k<spl->trigsOnTimer.size(); k++)
|
||||
for(QList<TriggerOnTimer>::iterator i = spl->trigsOnTimer.begin();
|
||||
i != spl->trigsOnTimer.end(); ++i)
|
||||
{
|
||||
srcTriggerType strig = gameData->players.
|
||||
at(spl->PlayerId)->srcTowers.
|
||||
at(spl->srcTower).triggers.
|
||||
at(spl->trigsOnTimer.at(k).src);
|
||||
spl->trigsOnTimer[k].timer++;
|
||||
if (spl->trigsOnTimer.at(k).timer >= strig.timer)
|
||||
srcTriggerType * strig = i->src;
|
||||
i->timer++;
|
||||
if (i->timer >= strig->timer)
|
||||
{
|
||||
spl->trigsOnTimer[k].timer = 0;
|
||||
if (strig.delParent) deadIndexes->append(spl->id);
|
||||
if (strig.count > 0 && strig.childId > 0)
|
||||
/// TODO : add damage and other effects
|
||||
i->timer = 0;
|
||||
if (strig->delParent) deadIndexes->append(spl->id);
|
||||
if (strig->count > 0 && strig->childId > 0)
|
||||
{
|
||||
for (int j=0; j<strig.count; ++j)
|
||||
int aim;
|
||||
switch(strig->childAim)
|
||||
{
|
||||
/// TODO : othet aim types
|
||||
case srcTriggerType::parentAim : aim = spl->AlienId; break;
|
||||
case srcTriggerType::noAim : aim = -1; break;
|
||||
default : aim = -1;
|
||||
}
|
||||
for (int j=0; j<strig->count; ++j)
|
||||
{
|
||||
// TODO: randomRadiusPos
|
||||
addSplash(spl->PlayerId,spl->srcTower,strig.childId,
|
||||
spl->pos,spl->destination,-1,spl->TowerId);
|
||||
/// TODO: randomRadiusPos
|
||||
addSplash(spl->srcTow, strig->childId, spl->pos, spl->destination, aim, spl->towerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -166,50 +209,54 @@ void Splashes::doTriggerOnTimer(SplashType *spl, QList<int> *deadIndexes)
|
||||
}
|
||||
|
||||
|
||||
void Splashes::doTriggerOnDest(const SplashType &spl, QList<int> *deadIndexes,
|
||||
void Splashes::doTriggerOnDest(QHash<int, SplashType>::iterator
|
||||
spl, QList<int> *deadIndexes,
|
||||
bool badAl)
|
||||
{
|
||||
for (int k=0; k<spl.trigsOnDest.size(); k++)
|
||||
foreach(TriggerOnDest t, spl->trigsOnDest)
|
||||
{
|
||||
// TODO: aims for childs
|
||||
srcTriggerType strig = gameData->players.
|
||||
at(spl.PlayerId)->srcTowers.
|
||||
at(spl.srcTower).triggers.
|
||||
at(spl.trigsOnDest.at(k).src);
|
||||
if (strig.delParent) deadIndexes->append(spl.id);
|
||||
if (strig.count > 0 && strig.childId > 0)
|
||||
/// TODO: different aims for childs
|
||||
if (t.src->delParent) deadIndexes->append(spl->id);
|
||||
int aim;
|
||||
switch(t.src->childAim)
|
||||
{
|
||||
for (int j=0; j<strig.count; ++j)
|
||||
case srcTriggerType::parentAim : aim = spl->AlienId; break;
|
||||
/// TODO : other aim types
|
||||
case srcTriggerType::noAim : aim = -1; break;
|
||||
default : aim = -1;
|
||||
}
|
||||
if (t.src->count > 0 && t.src->childId > 0)
|
||||
{
|
||||
for (int j=0; j<t.src->count; ++j)
|
||||
{
|
||||
// TODO: randomRadiusPos
|
||||
addSplash(spl.PlayerId,spl.srcTower,strig.childId,
|
||||
spl.pos,spl.destination,-1,spl.TowerId);
|
||||
/// TODO: randomRadiusPos
|
||||
addSplash(spl->srcTow,t.src->childId,
|
||||
spl->pos,spl->destination,aim,spl->towerId);
|
||||
}
|
||||
}
|
||||
if (strig.damage > 0)
|
||||
float dmg = t.src->damage;
|
||||
float rad = t.src->radius;
|
||||
if (dmg > 0)
|
||||
{
|
||||
if (!(strig.radius > 0))
|
||||
float hh;
|
||||
if (!(rad > 0))
|
||||
{
|
||||
if (!badAl)
|
||||
{
|
||||
float hh = gameData->curAliens[spl.AlienId].health -= strig.damage;
|
||||
if (hh < 0)
|
||||
gameData->curAliens.remove(spl.AlienId);
|
||||
hh = gameData->aliens->curAliens[spl->AlienId].health -= dmg;
|
||||
if (hh <= 0) dead.insert(spl->AlienId, DeadAlienIndex(spl->AlienId, spl->PlayerId));
|
||||
}
|
||||
} else {
|
||||
QList <int> dead;
|
||||
for (QHash<int, AlienType>::iterator
|
||||
i = gameData->curAliens.begin();
|
||||
i != gameData->curAliens.end(); ++i)
|
||||
i = gameData->aliens->curAliens.begin();
|
||||
i != gameData->aliens->curAliens.end(); ++i)
|
||||
{
|
||||
if (distance2(spl.pos, (*i).pos) < strig.radius*strig.radius)
|
||||
if (distance2(spl->pos, i->pos) < rad*rad)
|
||||
{
|
||||
(*i).health -= strig.damage;
|
||||
if ((*i).health < 0) dead.append((*i).id);
|
||||
hh = i->health -= dmg;
|
||||
if (hh <= 0) dead.insert(i->id, DeadAlienIndex(i->id, spl->PlayerId));
|
||||
}
|
||||
}
|
||||
for (int i=0; i<dead.size(); ++i)
|
||||
gameData->curAliens.remove(dead.at(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user