changes in arhitecture, some improvments, much optimising and simplify,
many bagfixes and new graphics engine
This commit is contained in:
109
aliens.cpp
109
aliens.cpp
@@ -1,54 +1,55 @@
|
||||
#include "aliens.h"
|
||||
#include <cmath>
|
||||
|
||||
Aliens::Aliens(Game_Data *gd, QObject *parent) :
|
||||
QObject(parent)
|
||||
Aliens::Aliens(Map * map_, QObject *parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
gameData = gd;
|
||||
map = map_;
|
||||
nextId = 1;
|
||||
m_missingAliens = 0;
|
||||
}
|
||||
|
||||
|
||||
void Aliens::addAlien(int srcId)
|
||||
{
|
||||
if (srcId < 0 || srcId >= gameData->srcAliens.size())
|
||||
if (srcId < 0 || srcId >= srcAliens.size())
|
||||
{
|
||||
qCritical("ERROR out of aliens range");
|
||||
return;
|
||||
}
|
||||
AlienType al;// = gameData->srcAliens.at(srcId);
|
||||
AlienType al;
|
||||
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.src = &(srcAliens[srcId]);
|
||||
al.finish = map->finishs().at(qrand()%map->finishs().size());
|
||||
al.pos = QPointF(map->starts().at(qrand()%map->starts().size()));
|
||||
al.path = 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;
|
||||
if (al.path.isEmpty())
|
||||
{
|
||||
qCritical("ERROR create path");
|
||||
return; // FIXME: this
|
||||
}
|
||||
//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);
|
||||
al.health = al.src->health;
|
||||
al.speed = al.src->speed;
|
||||
al.item = new ADItem(al.id,ADItem::Alien,al.src->images,QRectF(-cellSize,-cellSize,cellSize*2,cellSize*2));
|
||||
al.item->setPos(al.pos*cellSize+QPointF(cellSize/2,cellSize/2));
|
||||
al.item->setRotation(al.angle);
|
||||
curAliens.insert(al.id,al);
|
||||
scene->addItem(al.item);
|
||||
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)
|
||||
map->removeAliensPath();
|
||||
for (QHash<int, AlienType>::iterator i = curAliens.begin(); i != curAliens.end(); ++i)
|
||||
{
|
||||
(*i).path = gameData->map->createPath((*i).pos.toPoint(),(*i).finish);
|
||||
(*i).pathIndex = 1;
|
||||
if ((*i).path.isEmpty())
|
||||
i->path = map->createPath(i->pos.toPoint(),i->finish);
|
||||
i->pathIndex = 1;
|
||||
if (i->path.isEmpty())
|
||||
{
|
||||
*OK = false;
|
||||
qDebug("Aliens retrace = false");
|
||||
@@ -57,70 +58,52 @@ void Aliens::retrace(bool * OK)
|
||||
}
|
||||
|
||||
|
||||
void Aliens::delAlien(int Id)
|
||||
void Aliens::delAlien(int id, bool missed)
|
||||
{
|
||||
gameData->curAliens.remove(Id);
|
||||
if (missed)
|
||||
{
|
||||
m_missingAliens++;
|
||||
qDebug() << tr("Missing aliens = %1!").arg(m_missingAliens);
|
||||
}
|
||||
scene->removeItem(curAliens[id].item);
|
||||
delete curAliens[id].item;
|
||||
curAliens.remove(id);
|
||||
}
|
||||
|
||||
|
||||
void Aliens::update()
|
||||
{
|
||||
QList <int> missIndex;
|
||||
for (QHash<int, AlienType>::iterator i = gameData->curAliens.begin(); i != gameData->curAliens.end(); ++i)
|
||||
for (QHash<int, AlienType>::iterator i = curAliens.begin(); i != 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)
|
||||
while (distance2(i->pos, i->path.at(i->pathIndex)) < i->speed * i->speed)
|
||||
{
|
||||
(*i).pathIndex++;
|
||||
if ((*i).pathIndex >= (*i).path.size())
|
||||
i->pathIndex++;
|
||||
if (i->pathIndex >= i->path.size())
|
||||
{
|
||||
missIndex.push_back((*i).id);
|
||||
curMiss = (*i).id;
|
||||
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;
|
||||
arctg = std::atan2(i->pos.x() - i->path.at(i->pathIndex).x(),i->pos.y() - i->path.at(i->pathIndex).y());
|
||||
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;
|
||||
i->angle = angl;
|
||||
i->pos.rx() -= i->speed*std::sin(arctg);
|
||||
i->pos.ry() -= i->speed*std::cos(arctg);
|
||||
}
|
||||
i->item->setPos(i->pos*cellSize+QPointF(cellSize/2,cellSize/2));
|
||||
i->item->setRotation(i->angle);
|
||||
i->item->setBarValue(i->health/i->src->health);
|
||||
i->item->next(i->speed*cellSize);
|
||||
}
|
||||
for (int j=0; j<missIndex.size(); j++)
|
||||
{
|
||||
gameData->missingAliens++;
|
||||
qDebug() << tr("Missing aliens = %1!").arg(gameData->missingAliens);
|
||||
delAlien(missIndex.at(j));
|
||||
delAlien(missIndex.at(j),true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user