Files
aliendefender/aliens.cpp
2010-08-31 19:25:29 +04:00

119 lines
3.3 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.angle = 180.0f*(- std::atan2( al.pos.x() - al.path.at(al.pathIndex).x(),al.pos.y() - al.path.at(al.pathIndex).y()))/M_PI;
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 angl,arctg = 0;
int curMiss = -1;
while (distance2((*i).pos, (*i).path.at((*i).pathIndex)) < (*i).speed*(*i).speed)
{
(*i).pathIndex++;
if ((*i).pathIndex >= (*i).path.size())
{
missIndex.push_back((*i).id);
curMiss = (*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";
}
if (curMiss < 0)
{
// TODO: smooth rotate (how???)
arctg = std::atan2((*i).pos.x() - (*i).path.at((*i).pathIndex).x(),(*i).pos.y() - (*i).path.at((*i).pathIndex).y());
//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++;
qDebug() << tr("Missing aliens = %1!").arg(gameData->missingAliens);
delAlien(missIndex.at(j));
}
}