#include "aliens.h" //using std::vector; Aliens::Aliens(int TmpDestx, int TmpDesty, int afw, int afh, int alcellsize) { cellsize = alcellsize; DestPoint.setX(TmpDestx - 1); DestPoint.setY(TmpDesty); fw = afw; fh = afh; //AliensCnt = -1; CurWave = -1; Cells = new int*[fw]; TmpCells = new int*[fw]; for (int i = 0; i < fw; i++) { Cells[i] = new int[fh]; TmpCells[i] = new int[fh]; } clearCells(); Alien al; al.PicType = 0; al.PathIndex = 1; al.PicIndex = 0; al.PicFrame = 0; al.Speed = 0.1; loadPixmaps(&al); srcAliens.push_back(al); CurWave = 0; } bool Aliens::loadPixmaps(Alien * al) { bool load = true; int i = 0; while(load) { QVector pixmaps; if (loadPixmap(i,&pixmaps) > 0) { AliensPixmaps.push_back(pixmaps); i++; } else load = false; } qDebug() << "Pictures loaded for" << AliensPixmaps.size() << "alien types!"; if (AliensPixmaps.size() > 0) return true; else return false; } int Aliens::loadPixmap(int PicType, QVector * pixmaps) { int i = 1; QString str1,str2; QPixmap tmpAlPixmap; str1.setNum(i); str2.setNum(PicType); qDebug() << PicType; while (i != 0) { str1.setNum(i-1); tmpAlPixmap.load(":/images/Aliens/Al_" + str2 + "_" + str1 + ".png"); if (!tmpAlPixmap.isNull()) { pixmaps->push_back(tmpAlPixmap); tmpAlPixmap = 0; i++; } else i = 0; } qDebug() << "pictures:" << pixmaps->size(); return pixmaps->size(); } void Aliens::clearCells() { for (int i = 0; i < fw; i++) for (int j = 0; j < fh; j++) Cells[i][j] = 0; } bool Aliens::AddAlien() { //if (CurWave < 0) CurWave = 0; curAliens.push_back(srcAliens.at(CurWave)); //curAliens[AliensCnt].Level = CurWave + 1; curAliens[curAliens.size()-1].Position.pnt.setX(0*cellsize); curAliens[curAliens.size()-1].Position.pnt.setY(fh/2*cellsize); //(int)(fh/2+(1-0.58)*6); //DestPoint.setX(fw - 1); //DestPoint.setY(fh / 2); //curAliens[AliensCnt].Position.pnt.y(); curAliens[curAliens.size()-1].DestPnt = DestPoint; for (int i = 0; i < fw; i++) { for (int j = 0; j < fh; j++) TmpCells[i][j] = Cells[i][j]; } if (CreatePath(&curAliens[curAliens.size()-1])) { curAliens[curAliens.size()-1].Position.pnt.setX( curAliens[curAliens.size()-1].path[0].x()*cellsize); curAliens[curAliens.size()-1].Position.pnt.setY( curAliens[curAliens.size()-1].path[0].y()*cellsize); return true; } return false; } bool Aliens::CreatePath(Alien * al) { QPointF tp; QVector tmpPnt; if (WaveTrace(al)) { for (int j=0; j<4; j++) { tmpPnt.clear(); tp = al->path[0]; //qDebug() << al->path.at(0); tmpPnt.push_back(tp); for (int i = 0; i < al->path.size() - 1; i++) { tp.setX((al->path[i].x() + al->path[i + 1].x()) / 2.0); tp.setY((al->path[i].y() + al->path[i + 1].y()) / 2.0); tmpPnt.push_back(tp); } tp = al->path[al->path.size() - 1]; tmpPnt.push_back(tp); //qDebug() << tmpPnt.size(); al->path = tmpPnt; } tmpPnt.clear(); return true; } return false; } void Aliens::updateAliens() { qDebug("*********************"); //qDebug() << curAliens[0].path; for (int i = 0; i < curAliens.size(); i++) updateAlienPos(&curAliens[i]); qDebug("*********************"); } void Aliens::updateAlienPos(Alien * al) { float arctg = 0; float px,py,dk; float dx,dy,fdx,fdy; //qDebug() << cellsize; px = al->path[al->PathIndex].x() - al->path[al->PathIndex - 1].x(); py = al->path[al->PathIndex].y() - al->path[al->PathIndex - 1].y(); dx = al->Position.pnt.x() - al->path[al->PathIndex].x() * cellsize; dy = al->Position.pnt.y() - al->path[al->PathIndex].y() * cellsize; dk = cellsize / sqrtf(px * px + py * py); qDebug() << dx << ";" << dy; qDebug() << px << ";" << py; //qDebug() << al->path;//[al->PathIndex]; //qDebug() << al->path.size(); qDebug() << "PathIndex" << al->PathIndex; //qDebug() << al->path[al->PathIndex-1]; if (al->PathIndex + 1 < al->path.size()) { fdx = al->Position.pnt.x() - al->path[al->PathIndex+1].x() * cellsize; fdy = al->Position.pnt.y() - al->path[al->PathIndex+1].y() * cellsize; if (fdy != 0) arctg = atanf(fdx/fdy); else if (fdx < 0) arctg = -3.1415/2; else arctg = 3.1415/2; } //qDebug() << fdx << ";" << fdy; al->Position.angle = 180*(-arctg)/3.1415; if (fdy < 0) al->Position.angle = 180 + al->Position.angle; //qDebug() << al->Position.angle; qDebug() << al->Position.pnt; if (qAbs(dx) <= al->Speed * dk && qAbs(dy) <= al->Speed * dk) { qDebug("next"); al->Position.pnt.setX(al->path[al->PathIndex].x() * cellsize); al->Position.pnt.setY(al->path[al->PathIndex].y() * cellsize); al->PathIndex++; } else { qDebug("go"); al->Position.pnt.setX(al->Position.pnt.x()+px * al->Speed * dk); al->Position.pnt.setY(al->Position.pnt.y()+py * al->Speed * dk); } qDebug() << al->Position.pnt; al->PicIndex++; //qDebug() << "PicIndex" << al->PicIndex; //qDebug() << "PicType" << al->PicType; //qDebug() << AliensPixmaps[al->PicType].size(); if (al->PicIndex >= AliensPixmaps[al->PicType].size()) al->PicIndex = 0; if (al->PathIndex >= al->path.size()) { //qDebug() << "run agan"; al->PathIndex = 1; al->Position.pnt.setX(al->path.value(0).x()*cellsize); al->Position.pnt.setY(al->path.value(0).y()*cellsize); } } /*bool Aliens::PathIntersect(Alien* Al, Rectangle rect) { //PathIntersect = False for (int i = Al->PathIndex; i<=Al->path.size(); i++) // To UBound(Al.path(), 1) if (Al->path[i].x + 0.5 >= rect.x0 - 1 && Al->path[i].x + 0.5 <= rect.x1 + 1) if (Al->path[i].y + 0.5 >= rect.y0 - 1 && Al->path[i].y + 0.5 <= rect.y1 + 1) return true; //PathIntersect = True //Exit Function return false; }*/ bool Aliens::recreatePath() { QVector tmpPath; QPointF tmppnt; //bool trace; for (int i = 0; i < fw; i++) for (int j = 0; j < fh; j++) TmpCells[i][j] = Cells[i][j]; for (int i = 0; i < curAliens.size(); i++) { qDebug() << "##############"; qDebug() << "Alinen N" << i; qDebug() << curAliens[i].Position.pnt; qDebug() << curAliens[i].PathIndex; if (curAliens[i].Position.pnt.x() - curAliens[i].path[curAliens[i].PathIndex].x()*cellsize < cellsize/2 && curAliens[i].Position.pnt.y() - curAliens[i].path[curAliens[i].PathIndex].y()*cellsize < cellsize/2) qDebug() << "Yes!!"; else qDebug() << "NO!!"; tmppnt = curAliens[i].Position.pnt; curAliens[i].Position.pnt.setX(curAliens[i].path[curAliens[i].PathIndex - 1].x() * cellsize); curAliens[i].Position.pnt.setY(curAliens[i].path[curAliens[i].PathIndex - 1].y() * cellsize); curAliens[i].PathIndex = 1; qDebug() << curAliens[i].Position.pnt; tmpPath = curAliens[i].path; curAliens[i].path.clear(); //trace = CreatePath(&curAliens[i]); if (!CreatePath(&curAliens[i])) { curAliens[i].path = tmpPath; curAliens[i].Position.pnt = tmppnt; tmpPath.clear(); //trace = false; qDebug() << "##############"; return false; } curAliens[i].Position.pnt = tmppnt; qDebug() << curAliens[i].Position.pnt; qDebug() << "==============="; } tmpPath.clear(); qDebug() << "Re-create path sucsess!"; return true; } bool Aliens::WaveTrace(Alien * al) { bool stop = false; int step = 2; QPoint cp, tp; QRect fr(0, 0, fw, fh); QVector tmpp, curp; cp.setX(al->Position.pnt.x()/cellsize); cp.setY(al->Position.pnt.y()/cellsize); curp.push_back(cp); TmpCells[cp.x()][cp.y()] = 1; while (!stop) { //qDebug() << "trace"; tmpp = curp; curp.clear(); //qDebug() << tmpp.size(); stop = true; for (int i = 0; i < tmpp.size(); i++) { cp = tmpp[i]; if (cp == al->DestPnt) { TmpCells[cp.x()][cp.y()] = step; qDebug() << "Wawe trace done"; InvWaveTrace(cp, step, al); qDebug() << al->path.size(); return true; } tp.setX(cp.x() - 1); tp.setY(cp.y()); if (fr.contains(tp) && TmpCells[tp.x()][tp.y()] == 0) { TmpCells[tp.x()][tp.y()] = step; curp.push_back(tp); stop = false; } tp.setX(cp.x() + 1); if (fr.contains(tp) && TmpCells[tp.x()][tp.y()] == 0) { TmpCells[tp.x()][tp.y()] = step; curp.push_back(tp); stop = false; } tp.setX(cp.x()); tp.setY(cp.y() - 1); if (fr.contains(tp) && TmpCells[tp.x()][tp.y()] == 0) { TmpCells[tp.x()][tp.y()] = step; curp.push_back(tp); stop = false; } tp.setY(cp.y() + 1); if (fr.contains(tp) && TmpCells[tp.x()][tp.y()] == 0) { TmpCells[tp.x()][tp.y()] = step; curp.push_back(tp); stop = false; } } step++; } qDebug() << "false wawetrace"; return false; } void Aliens::InvWaveTrace(QPoint cp, int cnt, Alien * al) { QPoint wp, Ppnt; int Ind, c, AliensTmpDestX, AliensTmpDestY, xpp, ypp, xnn, ynn; unsigned char chk; Ppnt = wp = cp; xnn=0; xpp=0; ynn=0; ypp=0; cnt--; al->path.push_front(Ppnt); AliensTmpDestX = al->Position.pnt.x(); AliensTmpDestY = al->Position.pnt.y(); while (cnt > 1) { cnt--; chk = 0; Ind = 0; c = 0; if (wp.x() - 1 >= 0 && TmpCells[wp.x()-1][wp.y()] == cnt) { chk = chk | 0x01; c++; } if (wp.x() + 1 < fw && TmpCells[wp.x()+1][wp.y()] == cnt) { chk = chk | 0x02; c++; } if (wp.y() - 1 >= 0 && TmpCells[wp.x()][wp.y()-1] == cnt) { chk = chk | 0x04; c++; } if (wp.y() + 1 < fh && TmpCells[wp.x()][wp.y()+1] == cnt) { chk = chk | 0x08; c++; } if (c == 0 || chk == 0) qDebug() << "ERROR!!!"; if (c > 1) { if ((chk & 0x01)==0x01 && (chk & 0x04)==0x04) { if (xnn <= ynn && Ind == 0){ wp.rx()--; xnn++; if (xnn == ynn) xnn++; Ind = 1; } else if (Ind == 0) { wp.ry()--; ynn++; ynn++; Ind = 1; } } if ((chk & 0x02)==0x02 && (chk & 0x04)==0x04) { if (xpp <= ynn && Ind == 0){ wp.rx()++; xpp++; if (xpp == ynn) xpp++; Ind = 1; } else if (Ind == 0) { wp.ry()--; ynn++; ynn++; Ind = 1; } } if ((chk & 0x01)==0x01 && (chk & 0x08)==0x08) { if (xnn <= ypp && Ind == 0){ wp.rx()--; xnn++; if (xnn == ypp) xnn++; Ind = 1; } else if (Ind == 0) { wp.ry()++; ypp++; ypp++; Ind = 1; } } if ((chk & 0x02)==0x02 && (chk & 0x08)==0x08) { if (xpp <= ypp && Ind == 0){ wp.rx()++; xpp++; if (xpp == ypp) xpp++; Ind = 1; } else if (Ind == 0) { wp.ry()++; ypp++; ypp++; Ind = 1; } } } if (c == 1 || Ind == 0) { xnn=0; xpp=0; ynn=0; ypp=0; if ((chk & 0x01)==0x01) { wp.rx()--; xnn++; } else if ((chk & 0x02)==0x02) { wp.rx()++; xpp++; } else if ((chk & 0x04)==0x04) { wp.ry()--; ynn++; } else if ((chk & 0x08)==0x08) { wp.ry()++; ypp++; } } Ppnt = wp; al->path.push_front(Ppnt); } } void Aliens::clearAliens() { curAliens.clear(); }