114 lines
3.1 KiB
C++
114 lines
3.1 KiB
C++
#include "aliens.h"
|
|
#include <cmath>
|
|
|
|
Aliens::Aliens(Game_Data *gd, QObject *parent) :
|
|
QObject(parent)
|
|
{
|
|
gameData = gd;
|
|
nextId = 1;
|
|
}
|
|
|
|
|
|
void Aliens::addAlien(int srcId)
|
|
{
|
|
if (srcId < 0 || srcId >= gameData->srcAliens.size())
|
|
{
|
|
qCritical("ERROR out of aliens range");
|
|
return;
|
|
}
|
|
AlienType al;// = gameData->srcAliens.at(srcId);
|
|
al.id = nextId;
|
|
al.src = srcId;
|
|
al.finish = gameData->map->finishs().at(qrand()%gameData->map->finishs().size());
|
|
al.pos = QPointF(gameData->map->starts().at(qrand()%gameData->map->starts().size()));
|
|
al.path = gameData->map->createPath(al.pos.toPoint(),al.finish);
|
|
al.pathIndex = 1;
|
|
al.animIndex = 0.f;
|
|
if (al.path.isEmpty()) qFatal("ERROR create path");
|
|
//qDebug() << al.path;
|
|
al.health = gameData->srcAliens.at(srcId).health;
|
|
al.speed = gameData->srcAliens.at(srcId).speed;
|
|
al.imgType = gameData->srcAliens.at(srcId).imgType;
|
|
//gameData->map->printMap();
|
|
gameData->curAliens.insert(al.id,al);
|
|
nextId++;
|
|
}
|
|
|
|
|
|
void Aliens::retrace(bool * OK)
|
|
{
|
|
qDebug("re-trace!");
|
|
gameData->map->removeAliensPath();
|
|
for (QHash<int, AlienType>::iterator i = gameData->curAliens.begin(); i != gameData->curAliens.end(); ++i)
|
|
{
|
|
(*i).path = gameData->map->createPath((*i).pos.toPoint(),(*i).finish);
|
|
(*i).pathIndex = 1;
|
|
if ((*i).path.isEmpty()) *OK = false;
|
|
}
|
|
}
|
|
|
|
|
|
void Aliens::delAlien(int Id)
|
|
{
|
|
gameData->curAliens.remove(Id);
|
|
}
|
|
|
|
|
|
void Aliens::update()
|
|
{
|
|
QList <int> missIndex;
|
|
for (QHash<int, AlienType>::iterator i = gameData->curAliens.begin(); i != gameData->curAliens.end(); ++i)
|
|
{
|
|
AlienType al = (*i);
|
|
//qDebug() << i;
|
|
//if (al.health < 0) emit AlienKill();
|
|
float tmpdx,tmpdy,angl,arctg = 0;
|
|
tmpdx = (*i).pos.x() - (*i).path.at((*i).pathIndex).x();
|
|
tmpdy = (*i).pos.y() - (*i).path.at((*i).pathIndex).y();
|
|
while (std::sqrt(tmpdx*tmpdx +tmpdy*tmpdy) < 2*(*i).speed)
|
|
{
|
|
(*i).pathIndex++;
|
|
if ((*i).pathIndex >= (*i).path.size())
|
|
{
|
|
qDebug() << tr("Missing aliens = %1!").arg(gameData->missingAliens);
|
|
missIndex.push_back((*i).id);
|
|
break;
|
|
}
|
|
/*{
|
|
PathIndex = 0;
|
|
position = game->start*game->cellsize;
|
|
}*/
|
|
tmpdx = (*i).pos.x() - (*i).path.at((*i).pathIndex).x();
|
|
tmpdy = (*i).pos.y() - (*i).path.at((*i).pathIndex).y();
|
|
//qDebug() << "next";
|
|
}
|
|
arctg = std::atan2(tmpdx,tmpdy);
|
|
//if (tmpdy < 0) arctg=arctg+M_PI;
|
|
angl = 180.0f*(-arctg)/M_PI;
|
|
/*if (PathIndex > 1)
|
|
{
|
|
if ((Position.angle-angl < -5 || Position.angle-angl > 5) && angl < 175 && angl > -175)
|
|
{
|
|
if (angl > Position.angle) Position.angle += 5;
|
|
else Position.angle -= 5;
|
|
}
|
|
else Position.angle = angl;
|
|
}
|
|
else*/
|
|
(*i).angle = angl;
|
|
//qDebug() << "[" << PathIndex << ";" << PicIndex << "]" << "angle:" << Position.angle << "arctg:" << arctg << "Pos:" << Position.pnt;
|
|
(*i).pos.setX((*i).pos.x()
|
|
-(*i).speed*std::sin(arctg));
|
|
(*i).pos.setY((*i).pos.y()
|
|
-(*i).speed*std::cos(arctg));
|
|
//return true;
|
|
//gameData->curAliens.insert(al.id,al);
|
|
//qDebug() <<"alien"<< i << " path index=" << al.pathIndex << ", pos=" << al.pos;
|
|
}
|
|
for (int j=0; j<missIndex.size(); j++)
|
|
{
|
|
gameData->missingAliens++;
|
|
delAlien(missIndex.at(j));
|
|
}
|
|
}
|