now this work

but angle isn't slowly
This commit is contained in:
2010-02-06 12:33:21 +03:00
committed by andrey
parent 4c81b32bd3
commit 6a11d7fbcd
8 changed files with 87 additions and 355 deletions

View File

@@ -2,8 +2,15 @@
#include <cmath>
Alien::Alien()
Alien::Alien(GameData * AlienData, float alienspeed)
{
data = AlienData;
Speed = alienspeed;
TmpCells = new int*[data->size.width()];
for (int i = 0; i < data->size.width(); i++)
TmpCells[i] = new int[data->size.height()];
Position.pnt = data->start*data->cellsize;
RecreatePath();
}
@@ -13,17 +20,25 @@ bool Alien::RecreatePath()
{
for (int j = 0; j < data->size.height(); j++)
{
if (TmpCells[i][j] < 0 ) data->Cells[i][j] = -1;
if (data->Cells[i][j] < 0 ) TmpCells[i][j] = -1;
else TmpCells[i][j] = 0;
}
}
QPointF tp;
QPoint start;
QVector<QPoint> srcPath;
QVector<QPointF> tmpPath;
PathIndex = 1;
start.setX(Position.pnt.x()/data->cellsize);
start.setY(Position.pnt.y()/data->cellsize);
tmpPath = InvWaveTrace(data->finish,WaveTrace(start,data->finish));
start.setX(qRound(Position.pnt.x()/(float)data->cellsize));
start.setY(qRound(Position.pnt.y()/(float)data->cellsize));
srcPath = InvWaveTrace(data->finish,WaveTrace(start,data->finish));
for (int i=0; i<srcPath.size();i++)
{
data->Cells[srcPath[i].x()][srcPath[i].y()] = 1;
tmpPath.push_back(QPointF(srcPath[i]));
}
srcPath.clear();
//qDebug() << tmpPath.size();
if (!tmpPath.isEmpty())
{
for (int j=0; j<4; j++)
@@ -31,7 +46,7 @@ bool Alien::RecreatePath()
path.clear();
tp = tmpPath[0];
path.push_back(tp);
for (int i = 0; i < al->path.size() - 1; i++)
for (int i = 0; i < tmpPath.size() - 1; i++)
{
tp.setX((tmpPath[i].x() + tmpPath[i + 1].x()) / 2.0);
tp.setY((tmpPath[i].y() + tmpPath[i + 1].y()) / 2.0);
@@ -45,9 +60,9 @@ bool Alien::RecreatePath()
if (path.size() > 10)
{
path.remove(1);
path.remove(2);
path.remove(1);
path.remove(path.size()-2);
path.remove(path.size()-2);
path.remove(path.size()-3);
}
return true;
}
@@ -57,23 +72,29 @@ bool Alien::RecreatePath()
void Alien::update()
{
float tmpdx,tmpdy,arctg,tmpdx1,tmpdy1;
arctg = 0;
tmpdx = position->pnt.x() - path[PathIndex].x()*data->cellsize;
tmpdy = position->pnt.y() - path[PathIndex].y()*data->cellsize;
if (std::sqrt(tmpdx*tmpdx +tmpdy*tmpdy) < Speed*data->cellsize) PathIndex++;
if (PathIndex >= path.size()) PathIndex = path.size()-1;
tmpdx = position->pnt.x() - path[PathIndex].x()*cellsize;
tmpdy = position->pnt.y() - path[PathIndex].y()*cellsize;
float tmpdx,tmpdy,angl,arctg = 0;
tmpdx = Position.pnt.x() - path[PathIndex].x()*data->cellsize;
tmpdy = Position.pnt.y() - path[PathIndex].y()*data->cellsize;
while (std::sqrt(tmpdx*tmpdx +tmpdy*tmpdy) < 2*Speed*data->cellsize)
{
PathIndex++;
if (PathIndex >= path.size()) PathIndex = 0;
tmpdx = Position.pnt.x() - path[PathIndex].x()*data->cellsize;
tmpdy = Position.pnt.y() - path[PathIndex].y()*data->cellsize;
//qDebug() << "next";
}
arctg = std::atan(tmpdx/tmpdy);
Position.angle = 180.0f*(-arctg)/3.1416f;
if (tmpdy < 0) Position.angle = 180.0f + Position.angle;
if (tmpdy < 0) arctg=arctg+3.1416f;
angl = 180.0f*(-arctg)/3.1416f;
//if (qAbs(Position.angle-angl) > 10) Position.angle = 5*angl/qAbs(angl);
//else
Position.angle = angl;
//qDebug() << "[" << PathIndex << ";" << PicIndex << "]" << "angle:" << Position.angle << "arctg:" << arctg << "Pos:" << Position.pnt;
Position.pnt.setX(Position.pnt.x()
+Speed*data->cellsize*std::sin(Position.angle));
-Speed*(float)data->cellsize*std::sin(arctg));
Position.pnt.setY(Position.pnt.y()
-Speed*data->cellsize*std::cos(Position.angle));
-Speed*(float)data->cellsize*std::cos(arctg));
PicIndex++;
if (PicIndex >= AlienPix.size()) PicIndex = 0;
}
@@ -98,7 +119,7 @@ int Alien::WaveTrace(QPoint start, QPoint finish)
cp = start;
curp.push_back(cp);
TmpCells[cp.x()][cp.y()] = 1;
qDebug() << "trace";
//qDebug() << "trace";
while (!stop) {
tmpp = curp;
curp.clear();
@@ -107,7 +128,7 @@ int Alien::WaveTrace(QPoint start, QPoint finish)
cp = tmpp[i];
if (cp == finish) {
TmpCells[cp.x()][cp.y()] = step;
qDebug() << "true";
//qDebug() << "trace done!";
return step;
}
tp.setX(cp.x() - 1);
@@ -139,7 +160,7 @@ int Alien::WaveTrace(QPoint start, QPoint finish)
}
step++;
}
qDebug() << "false";
qDebug() << "trace false";
return -1;
}