few optimization and find aim for towers
This commit is contained in:
48
aliens.cpp
48
aliens.cpp
@@ -17,7 +17,7 @@ void Aliens::addAlien(int srcId)
|
||||
return;
|
||||
}
|
||||
AlienType al;// = gameData->srcAliens.at(srcId);
|
||||
al.Id = nextId;
|
||||
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()));
|
||||
@@ -30,7 +30,7 @@ void Aliens::addAlien(int srcId)
|
||||
al.speed = gameData->srcAliens.at(srcId).speed;
|
||||
al.imgType = gameData->srcAliens.at(srcId).imgType;
|
||||
//gameData->map->printMap();
|
||||
gameData->curAliens.insert(al.Id,al);
|
||||
gameData->curAliens.insert(al.id,al);
|
||||
nextId++;
|
||||
}
|
||||
|
||||
@@ -39,11 +39,11 @@ void Aliens::retrace(bool * OK)
|
||||
{
|
||||
qDebug("re-trace!");
|
||||
gameData->map->removeAliensPath();
|
||||
for (int i=0; i<gameData->curAliens.size(); i++)
|
||||
for (QHash<int, AlienType>::iterator i = gameData->curAliens.begin(); i != gameData->curAliens.end(); ++i)
|
||||
{
|
||||
gameData->curAliens.values()[i].path = gameData->map->createPath(gameData->curAliens.values()[i].pos.toPoint(),gameData->curAliens.values()[i].finish);
|
||||
gameData->curAliens.values()[i].pathIndex = 1;
|
||||
if (gameData->curAliens.values()[i].path.isEmpty()) *OK = false;
|
||||
(*i).path = gameData->map->createPath((*i).pos.toPoint(),(*i).finish);
|
||||
(*i).pathIndex = 1;
|
||||
if ((*i).path.isEmpty()) *OK = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,33 +57,33 @@ void Aliens::delAlien(int Id)
|
||||
void Aliens::update()
|
||||
{
|
||||
QList <int> missIndex;
|
||||
for (int i=0; i<gameData->curAliens.size(); i++)
|
||||
for (QHash<int, AlienType>::iterator i = gameData->curAliens.begin(); i != gameData->curAliens.end(); ++i)
|
||||
{
|
||||
AlienType al = gameData->curAliens.values().at(i);
|
||||
AlienType al = (*i);
|
||||
//qDebug() << i;
|
||||
//if (al.health < 0) emit AlienKill();
|
||||
float tmpdx,tmpdy,angl,arctg = 0;
|
||||
tmpdx = al.pos.x() - al.path.at(al.pathIndex).x();
|
||||
tmpdy = al.pos.y() - al.path.at(al.pathIndex).y();
|
||||
while (std::sqrt(tmpdx*tmpdx +tmpdy*tmpdy) < 2*al.speed)
|
||||
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)
|
||||
{
|
||||
al.pathIndex++;
|
||||
if (al.pathIndex >= al.path.size())
|
||||
(*i).pathIndex++;
|
||||
if ((*i).pathIndex >= (*i).path.size())
|
||||
{
|
||||
qDebug() << tr("Missing aliens = %1!").arg(gameData->missingAliens);
|
||||
missIndex.push_back(al.Id);
|
||||
missIndex.push_back((*i).id);
|
||||
break;
|
||||
}
|
||||
/*{
|
||||
PathIndex = 0;
|
||||
position = game->start*game->cellsize;
|
||||
}*/
|
||||
tmpdx = al.pos.x() - al.path.at(al.pathIndex).x();
|
||||
tmpdy = al.pos.y() - al.path.at(al.pathIndex).y();
|
||||
tmpdx = (*i).pos.x() - (*i).path.at((*i).pathIndex).x();
|
||||
tmpdy = (*i).pos.y() - (*i).path.at((*i).pathIndex).y();
|
||||
//qDebug() << "next";
|
||||
}
|
||||
arctg = std::atan(tmpdx/tmpdy);
|
||||
if (tmpdy < 0) arctg=arctg+M_PI;
|
||||
arctg = std::atan2(tmpdx,tmpdy);
|
||||
//if (tmpdy < 0) arctg=arctg+M_PI;
|
||||
angl = 180.0f*(-arctg)/M_PI;
|
||||
/*if (PathIndex > 1)
|
||||
{
|
||||
@@ -95,14 +95,14 @@ void Aliens::update()
|
||||
else Position.angle = angl;
|
||||
}
|
||||
else*/
|
||||
al.angle = angl;
|
||||
(*i).angle = angl;
|
||||
//qDebug() << "[" << PathIndex << ";" << PicIndex << "]" << "angle:" << Position.angle << "arctg:" << arctg << "Pos:" << Position.pnt;
|
||||
al.pos.setX(al.pos.x()
|
||||
-al.speed*std::sin(arctg));
|
||||
al.pos.setY(al.pos.y()
|
||||
-al.speed*std::cos(arctg));
|
||||
(*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);
|
||||
//gameData->curAliens.insert(al.id,al);
|
||||
//qDebug() <<"alien"<< i << " path index=" << al.pathIndex << ", pos=" << al.pos;
|
||||
}
|
||||
for (int j=0; j<missIndex.size(); j++)
|
||||
|
||||
Reference in New Issue
Block a user