diff --git a/adpainter.cpp b/adpainter.cpp index 19743cd..1e0deac 100644 --- a/adpainter.cpp +++ b/adpainter.cpp @@ -8,6 +8,14 @@ adpainter::adpainter(int width, int height, int adpcellsize) background = new QPixmap(width,height); pen = new QPen(); brush = new QBrush(); + loadPixmaps(); + //AnimAlien = 0; + //StepAlien = 0; +} + + +void adpainter::loadPixmaps() +{ QString str; QPixmap tmpAlPixmap; int i=1; @@ -15,7 +23,6 @@ adpainter::adpainter(int width, int height, int adpcellsize) while (i != 0) { str.setNum(i); tmpAlPixmap.load(":/aliens/images/Aliens/Al_00_" + str + ".png"); - //qDebug() << tmpAlPixmap.isNull(); if (!tmpAlPixmap.isNull()) { AlienPix.push_back(tmpAlPixmap); tmpAlPixmap = 0; @@ -24,14 +31,12 @@ adpainter::adpainter(int width, int height, int adpcellsize) else i = 0; } qDebug() << "pictures:" << AlienPix.size(); - AnimAlien = 0; - StepAlien = 0; } void adpainter::clear() { - painter.begin(background); + QPainter painter(background); pen->setColor(QColor::fromRgb(qRgb(150,150,150))); painter.setPen(*pen); painter.fillRect(buff->rect(),QColor::fromRgb(qRgb(150,150,150))); @@ -42,7 +47,7 @@ void adpainter::clear() void adpainter::drawgrid() { int i; - painter.begin(background); + QPainter painter(background); pen->setColor(QColor::fromRgb(qRgb(100,100,100))); painter.setPen(*pen); for (i = 0;iwidth()/cellsize;i++) @@ -57,7 +62,7 @@ void adpainter::drawgrid() } -void adpainter::drawAliens(QVector path, FPVector position) +void adpainter::drawAlien(Alien * al) { //float tmpdx,tmpdy,arctg,tmpdx1,tmpdy1; //unsigned char dd[4]; @@ -65,15 +70,17 @@ void adpainter::drawAliens(QVector path, FPVector position) //arctg = 0; //qDebug() << path.size(); //QTransform rotated; - painter.begin(buff); + al->update(); + if (al->indexPix() >= AlienPix.size()) al->resetIndexPic(); + QPainter painter(buff); pen->setColor(QColor::fromRgb(qRgb(80,200,100))); brush->setColor(QColor::fromRgb(qRgb(230,10,10))); brush->setStyle(Qt::SolidPattern); painter.setPen(*pen); painter.setBrush(*brush); painter.drawPixmap(0,0,*background); - for (int i = 0; i < path.size(); i++){ - painter.drawEllipse(path[i].x() * cellsize +1, path[i].y() * cellsize +1, cellsize - 2, cellsize - 2); + for (int i = 0; i < al->path.size(); i++){ + painter.drawEllipse(al->path[i].x() * cellsize +4, al->path[i].y() * cellsize +4, cellsize - 8, cellsize - 8); } /*tmpdx = position->pnt.x() - path[StepAlien].x()*cellsize; tmpdy = position->pnt.y() - path[StepAlien].y()*cellsize; @@ -110,16 +117,16 @@ void adpainter::drawAliens(QVector path, FPVector position) } }*/ //painter.save(); - painter.translate(position->pnt.x()+cellsize/2, position->pnt.y()+cellsize/2); + painter.translate(al->pos().pnt.x()+cellsize/2, al->pos().pnt.y()+cellsize/2); //if (arctg == 0) painter.rotate(90); //memcpy(dd,&gg,4); //dd[0]=0xFF; //qDebug() << "angle:" << position->angle; //qDebug() << dd[0] << dd[1] << dd[2] << dd[3]; - painter.rotate(position->angle); + painter.rotate(al->pos().angle); //position->pnt.setX(path.at(0).x()); //position->pnt.setY(path.at(0).y()); - painter.drawPixmap(-cellsize, -cellsize, cellsize*2, cellsize*2, AlienPix.at(AnimAlien)); + painter.drawPixmap(-cellsize, -cellsize, cellsize*2, cellsize*2, AlienPix[al->indexPix()]); //AnimAlien++; //painter.restore(); //if (AnimAlien >= AlienPix.size()) AnimAlien = 0; @@ -132,7 +139,7 @@ void adpainter::drawAliens(QVector path, FPVector position) void adpainter::drawcell(QPoint pnt) { - painter.begin(background); + QPainter painter(background); pen->setColor(QColor::fromRgb(qRgb(0,0,0))); brush->setColor(QColor::fromRgb(qRgb(0,0,0))); brush->setStyle(Qt::SolidPattern); @@ -145,7 +152,7 @@ void adpainter::drawcell(QPoint pnt) void adpainter::clearcell(QPoint pnt) { - painter.begin(background); + QPainter painter(background); pen->setColor(QColor::fromRgb(qRgb(150,150,150))); brush->setColor(QColor::fromRgb(qRgb(150,150,150))); brush->setStyle(Qt::SolidPattern); diff --git a/adpainter.h b/adpainter.h index ed14b84..7927683 100644 --- a/adpainter.h +++ b/adpainter.h @@ -2,20 +2,17 @@ #define ADPAINTER_H #include "basestruct.h" +#include "alien.h" #include #include -#include #include #include #include #include #include #include -#include -#include -#include using std::atan; @@ -28,16 +25,17 @@ public: void clearcell(QPoint pnt); void clear(); QPixmap * getPixmap(); - void drawAliens(QVector path, FPVector position); + void drawAlien(Alien * al); int AnimAlien, StepAlien; private: + GameData * data; QPixmap * buff; QPixmap * background; - QPainter painter; QPen * pen; QBrush * brush; QVector AlienPix; int cellsize; + void loadPixmaps(); }; diff --git a/alien.cpp b/alien.cpp index 6401b44..8ec3fce 100644 --- a/alien.cpp +++ b/alien.cpp @@ -2,8 +2,15 @@ #include -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 srcPath; QVector 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; iCells[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; } diff --git a/alien.h b/alien.h index d52ef31..da70929 100644 --- a/alien.h +++ b/alien.h @@ -6,18 +6,20 @@ class Alien { public: - Alien(); + Alien(GameData * AlienData, float alienspeed = 0.1); bool RecreatePath(); - bool testTrace() {return (WaveTrace(data->start,data->finish) > 0);} - void setPos(FPVector pos); + //bool testTrace() {return (WaveTrace(data->start,data->finish) > 0);} + void setPos(QPoint pos) {Position.pnt = pos;} void update(); + void resetIndexPic() {PicIndex = 0;} FPVector pos() const {return Position;} - QVector AlienPath() const {return path;} + int indexPix() const {return PicIndex;} + QVector path; private: int ** TmpCells; GameData * data; FPVector Position; - int PicType; + //int PicType; //int FlX; //int FlY; //int Armor; @@ -32,7 +34,6 @@ private: //float PicFrame; //float PicI; //float Regeneration; - QVector path; int WaveTrace(QPoint start, QPoint finish); QVector InvWaveTrace(QPoint finish, int cnt); diff --git a/aliens.cpp b/aliens.cpp index 439bb51..d84ee69 100644 --- a/aliens.cpp +++ b/aliens.cpp @@ -5,304 +5,17 @@ Aliens::Aliens(GameData *dataAliens) { data = dataAliens; - TmpCells = new int*[data->size.width()]; - for (int i = 0; i < data->size.width(); i++) TmpCells[i] = new int[data->size.height()]; } -/*int Aliens::loadAlienImages(int PicType) -{ - int i = 1; - QString str1,str2; - QPixmap tmpAlPixmap; - str1.setNum(i); - str2.setNum(PicType); - while (i != 0) { - str1.setNum(i); - tmpAlPixmap.load("./images/Aliens/Al_" + str2 + "_" + str1 + ".png"); - //qDebug() << tmpAlPixmap.isNull(); - if (!tmpAlPixmap.isNull()) { - //AlienPix.push_back(tmpAlPixmap); - tmpAlPixmap = 0; - i++; - } - else i = 0; - } - //qDebug() << "pictures:" << AlienPix.size(); - return i; -}*/ - - bool Aliens::AddAlien() { - //bool ok; - Alien al; - //AliensCnt++; - //if (CurWave < 0) CurWave = 0; - //if (AliensCnt > curAliens.size()) ReDim Preserve Aliens(AliensCnt) As Alien - //Aliens(AliensCnt) = SrcAliens(CurWave) - curAliens.push_back(al); //srcAliens[CurWave]); - //curAliens[AliensCnt].Level = CurWave + 1; - curAliens[0].Position.pnt = data->start*data->cellsize; - ; //(int)(fh/2+(1-0.58)*6); - //DestPoint.setX(fw - 1); - //DestPoint.setY(fh / 2); //curAliens[AliensCnt].Position.pnt.y(); - curAliens[0].DestPnt = data->finish; - // curAliens[AliensCnt].MaxFrame = 0; //srcAliens[CurWave].MaxFrame; - // curAliens[AliensCnt].PicFrame = 0; - return CreatePath(&curAliens[0]); -} - - -bool Aliens::CreatePath(Alien * al) -{ - QPointF tp; - QVector tmpPnt; - //tmpPnt.clear(); - // AliensTmpDestX = fw - 1;//al->Destpnt.x(); - // AliensTmpDestY = fh / 2;//al->Destpnt.y(); - // al->Position.pnt.x() = 0; - // al->Position.pnt.y() = fh / 2; - for (int i = 0; i < data->size.width(); i++) { - for (int j = 0; j < data->size.height(); j++) TmpCells[i][j] = data->Cells[i][j]; - } - - al->path.clear(); - al->PathIndex = 0; - if (WaveTrace(al)) - { - for (int j=0; j<4; j++) - { - tmpPnt.clear(); - tp = al->path[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); - tp.setY((al->path[i].y() + al->path[i + 1].y()) / 2); - tmpPnt.push_back(tp); - } - tp = al->path[al->path.size() - 1]; - tmpPnt.push_back(tp); - //qDebug() << tmpPnt.size(); - al->path = tmpPnt; - //al->Position.pnt.setX(al->path[0].x()*data->cellsize); - //al->Position.pnt.setY(al->path[0].y()*data->cellsize); - } - tmpPnt.clear(); - return true; - } - return false; - //return WaveTrace(al); -} - - -void Aliens::updateAlienPos(Alien * al) -{ - -} - - -/*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::WaveTrace(Alien * al) { - bool stop = false; - int step = 2; - QPoint cp, tp; - QRect fr(0, 0, data->size.width(), data->size.height()); - QVector tmpp, curp; - cp = al->Position.pnt/data->cellsize; - curp.push_back(cp); - TmpCells[cp.x()][cp.y()] = 1; - qDebug() << "trace"; - while (!stop) { - 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; - InvWaveTrace(cp, step, al); - qDebug() << "true"; - 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"; - 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 < data->size.width() && 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 < data->size.height() && 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); - } + curAliens.push_back(new Alien(data,0.1)); + return !curAliens.isEmpty(); } void Aliens::clearAliens() { - //AliensCnt = - 1; curAliens.clear(); } diff --git a/aliens.h b/aliens.h index fa1b527..b0478d3 100644 --- a/aliens.h +++ b/aliens.h @@ -8,22 +8,13 @@ class Aliens { public: Aliens(GameData *dataAliens); - - QVector srcAliens; - QVector curAliens; - //bool PathIntersect(Alien* Al, Rectangle rect); - bool AddAlien(); void clearAliens(); - bool CreatePath(Alien* al); + Alien * alienAt(int index) const {return curAliens.at(index);} private: - int ** TmpCells; GameData * data; - QVector< QVector > AliensPixmaps; - bool WaveTrace(Alien* al); - void InvWaveTrace(QPoint cp, int cnt, Alien* al); - void updateAlienPos(Alien * al); - //int loadAlienImages(int PicType); + QVector srcAliens; + QVector curAliens; }; #endif // ALIENS_H diff --git a/basestruct.h b/basestruct.h index ca9bdd7..9049e8a 100644 --- a/basestruct.h +++ b/basestruct.h @@ -13,7 +13,7 @@ struct FPVector { - QPoint pnt; + QPointF pnt; float angle; }; @@ -27,6 +27,7 @@ struct GameData int ** Cells; }; + /*struct Effect { float Health; diff --git a/mainwindow.cpp b/mainwindow.cpp index 5fec989..2198a5b 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -31,7 +31,7 @@ MainWindow::MainWindow(QWidget *parent) void MainWindow::ADrender() { - adp->drawAliens(aliens->curAliens[0].AlienPath(), aliens->curAliens[0].pos()); + adp->drawAlien(aliens->alienAt(0)); this->repaint(); } @@ -81,7 +81,7 @@ void MainWindow::SetCell(QPoint pos) //aliens->clearAliens(); //adp->StepAlien=0; //adp->AnimAlien=0; - if (!aliens->CreatePath(&(aliens->curAliens[0]))) ClearCell(pos); + if (!aliens->alienAt(0)->RecreatePath()) ClearCell(pos); else adp->StepAlien=0; } } @@ -99,7 +99,7 @@ void MainWindow::ClearCell(QPoint pos) adp->clearcell(cp); //aliens->clearAliens(); //aliens->AddAlien(); - aliens->CreatePath(&(aliens->curAliens[0])); + aliens->alienAt(0)->RecreatePath(); adp->StepAlien=0; } }