diff --git a/adpainter.cpp b/adpainter.cpp index 1e0deac..73ad61a 100644 --- a/adpainter.cpp +++ b/adpainter.cpp @@ -80,7 +80,7 @@ void adpainter::drawAlien(Alien * al) painter.setBrush(*brush); painter.drawPixmap(0,0,*background); 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); + painter.drawEllipse(al->path.at(i).x() * cellsize +4, al->path.at(i).y() * cellsize +4, cellsize - 8, cellsize - 8); } /*tmpdx = position->pnt.x() - path[StepAlien].x()*cellsize; tmpdy = position->pnt.y() - path[StepAlien].y()*cellsize; @@ -117,13 +117,13 @@ void adpainter::drawAlien(Alien * al) } }*/ //painter.save(); - painter.translate(al->pos().pnt.x()+cellsize/2, al->pos().pnt.y()+cellsize/2); + painter.translate(al->Pos().x()+cellsize/2, al->Pos().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(al->pos().angle); + painter.rotate(al->Angl()); //position->pnt.setX(path.at(0).x()); //position->pnt.setY(path.at(0).y()); painter.drawPixmap(-cellsize, -cellsize, cellsize*2, cellsize*2, AlienPix[al->indexPix()]); diff --git a/adpainter.h b/adpainter.h index 7927683..7429642 100644 --- a/adpainter.h +++ b/adpainter.h @@ -1,18 +1,13 @@ #ifndef ADPAINTER_H #define ADPAINTER_H -#include "basestruct.h" #include "alien.h" #include #include -#include -#include #include #include -#include -#include using std::atan; @@ -28,7 +23,7 @@ public: void drawAlien(Alien * al); int AnimAlien, StepAlien; private: - GameData * data; + //GameData * data; QPixmap * buff; QPixmap * background; QPen * pen; diff --git a/alien.cpp b/alien.cpp index 8ec3fce..331faa4 100644 --- a/alien.cpp +++ b/alien.cpp @@ -2,25 +2,27 @@ #include -Alien::Alien(GameData * AlienData, float alienspeed) +Alien::Alien(games *parent, float alienspeed) { - data = AlienData; + game = parent; 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; + TmpCells = new int*[game->size.width()]; + for (int i = 0; i < game->size.width(); i++) + TmpCells[i] = new int[game->size.height()]; + position = game->start*game->cellsize; + path.clear(); + PicIndex = 0; RecreatePath(); } bool Alien::RecreatePath() { - for (int i = 0; i < data->size.width(); i++) + for (int i = 0; i < game->size.width(); i++) { - for (int j = 0; j < data->size.height(); j++) + for (int j = 0; j < game->size.height(); j++) { - if (data->Cells[i][j] < 0 ) TmpCells[i][j] = -1; + if (game->Cells[i][j] < 0 ) TmpCells[i][j] = -1; else TmpCells[i][j] = 0; } } @@ -29,13 +31,13 @@ bool Alien::RecreatePath() QVector srcPath; QVector tmpPath; PathIndex = 1; - 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)); + start.setX(qRound(position.x()/(float)game->cellsize)); + start.setY(qRound(position.y()/(float)game->cellsize)); + srcPath = InvWaveTrace(game->finish,WaveTrace(start,game->finish)); for (int i=0; iCells[srcPath[i].x()][srcPath[i].y()] = 1; - tmpPath.push_back(QPointF(srcPath[i])); + game->Cells[srcPath.at(i).x()][srcPath.at(i).y()] = 1; + tmpPath.push_back(QPointF(srcPath.at(i))); } srcPath.clear(); //qDebug() << tmpPath.size(); @@ -44,15 +46,16 @@ bool Alien::RecreatePath() for (int j=0; j<4; j++) { path.clear(); - tp = tmpPath[0]; + tp = tmpPath.at(0); path.push_back(tp); 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); + //if (j > 1) path.push_back(tmpPath[i]); + tp.setX((tmpPath.at(i).x() + tmpPath.at(i + 1).x()) / 2.0); + tp.setY((tmpPath.at(i).y() + tmpPath.at(i + 1).y()) / 2.0); path.push_back(tp); } - tp = tmpPath[tmpPath.size() - 1]; + tp = tmpPath.at(tmpPath.size() - 1); path.push_back(tp); tmpPath = path; } @@ -73,48 +76,48 @@ bool Alien::RecreatePath() void Alien::update() { 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) + tmpdx = position.x() - path.at(PathIndex).x()*game->cellsize; + tmpdy = position.y() - path.at(PathIndex).y()*game->cellsize; + while (std::sqrt(tmpdx*tmpdx +tmpdy*tmpdy) < 2*Speed*game->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; + if (PathIndex >= path.size()) + { + PathIndex = 0; + position = game->start*game->cellsize; + } + tmpdx = position.x() - path.at(PathIndex).x()*game->cellsize; + tmpdy = position.y() - path.at(PathIndex).y()*game->cellsize; //qDebug() << "next"; } arctg = std::atan(tmpdx/tmpdy); 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; + /*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*/ angle = angl; //qDebug() << "[" << PathIndex << ";" << PicIndex << "]" << "angle:" << Position.angle << "arctg:" << arctg << "Pos:" << Position.pnt; - Position.pnt.setX(Position.pnt.x() - -Speed*(float)data->cellsize*std::sin(arctg)); - Position.pnt.setY(Position.pnt.y() - -Speed*(float)data->cellsize*std::cos(arctg)); + position.setX(position.x() + -Speed*(float)game->cellsize*std::sin(arctg)); + position.setY(position.y() + -Speed*(float)game->cellsize*std::cos(arctg)); PicIndex++; } -/*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; -}*/ - - int Alien::WaveTrace(QPoint start, QPoint finish) { bool stop = false; int step = 2; QPoint cp, tp; - QRect fr(0, 0, data->size.width(), data->size.height()); + QRect fr(0, 0, game->size.width(), game->size.height()); QVector tmpp, curp; cp = start; curp.push_back(cp); @@ -125,7 +128,7 @@ int Alien::WaveTrace(QPoint start, QPoint finish) curp.clear(); stop = true; for (int i = 0; i < tmpp.size(); i++) { - cp = tmpp[i]; + cp = tmpp.at(i); if (cp == finish) { TmpCells[cp.x()][cp.y()] = step; //qDebug() << "trace done!"; @@ -160,7 +163,7 @@ int Alien::WaveTrace(QPoint start, QPoint finish) } step++; } - qDebug() << "trace false"; + //qDebug() << "trace false"; return -1; } @@ -190,7 +193,7 @@ QVector Alien::InvWaveTrace(QPoint finish, int cnt) chk = chk | 0x01; c++; } - if (wp.x() + 1 < data->size.width() && TmpCells[wp.x()+1][wp.y()] == cnt) + if (wp.x() + 1 < game->size.width() && TmpCells[wp.x()+1][wp.y()] == cnt) { chk = chk | 0x02; c++; @@ -200,7 +203,7 @@ QVector Alien::InvWaveTrace(QPoint finish, int cnt) chk = chk | 0x04; c++; } - if (wp.y() + 1 < data->size.height() && TmpCells[wp.x()][wp.y()+1] == cnt) + if (wp.y() + 1 < game->size.height() && TmpCells[wp.x()][wp.y()+1] == cnt) { chk = chk | 0x08; c++; diff --git a/alien.h b/alien.h index da70929..91c947b 100644 --- a/alien.h +++ b/alien.h @@ -1,24 +1,26 @@ #ifndef ALIEN_H #define ALIEN_H -#include "basestruct.h" +#include "games.h" class Alien { public: - Alien(GameData * AlienData, float alienspeed = 0.1); + Alien(games *parent, float alienspeed = 0.1); bool RecreatePath(); //bool testTrace() {return (WaveTrace(data->start,data->finish) > 0);} - void setPos(QPoint pos) {Position.pnt = pos;} + void setPos(QPointF pos) {position = pos;} void update(); void resetIndexPic() {PicIndex = 0;} - FPVector pos() const {return Position;} + QPointF Pos() const {return position;} + float Angl() const {return angle;} int indexPix() const {return PicIndex;} QVector path; private: int ** TmpCells; - GameData * data; - FPVector Position; + games *game; + QPointF position; + float angle; //int PicType; //int FlX; //int FlY; diff --git a/aliendefender.pro b/aliendefender.pro index 2cf08be..2072ee5 100644 --- a/aliendefender.pro +++ b/aliendefender.pro @@ -2,26 +2,26 @@ # Project created by QtCreator 2009-08-17T19:38:55 # ------------------------------------------------- QT += network \ - opengl + opengl TARGET = aliendefender TEMPLATE = app SOURCES += main.cpp \ - mainwindow.cpp \ - adpainter.cpp \ - aliens.cpp \ - udpsender.cpp \ - towers.cpp \ - map.cpp \ - game.cpp \ + mainwindow.cpp \ + adpainter.cpp \ + aliens.cpp \ + udpsender.cpp \ + towers.cpp \ + map.cpp \ + games.cpp \ alien.cpp HEADERS += mainwindow.h \ - adpainter.h \ - aliens.h \ - basestruct.h \ - udpsender.h \ - towers.h \ - map.h \ - game.h \ - alien.h + adpainter.h \ + aliens.h \ + basestruct.h \ + udpsender.h \ + towers.h \ + map.h \ + games.h \ + alien.h FORMS += mainwindow.ui RESOURCES += images.qrc diff --git a/aliens.cpp b/aliens.cpp index d84ee69..557af56 100644 --- a/aliens.cpp +++ b/aliens.cpp @@ -1,16 +1,14 @@ #include "aliens.h" -//using std::vector; - -Aliens::Aliens(GameData *dataAliens) +Aliens::Aliens(games *parent) { - data = dataAliens; + game = parent; } bool Aliens::AddAlien() { - curAliens.push_back(new Alien(data,0.1)); + curAliens.push_back(new Alien(game,0.1)); return !curAliens.isEmpty(); } diff --git a/aliens.h b/aliens.h index b0478d3..b2e34df 100644 --- a/aliens.h +++ b/aliens.h @@ -1,18 +1,17 @@ #ifndef ALIENS_H #define ALIENS_H -#include "basestruct.h" #include "alien.h" class Aliens { public: - Aliens(GameData *dataAliens); + Aliens(games *parent); bool AddAlien(); void clearAliens(); Alien * alienAt(int index) const {return curAliens.at(index);} private: - GameData * data; + games * game; QVector srcAliens; QVector curAliens; }; diff --git a/basestruct.h b/basestruct.h index 9049e8a..3e9a84a 100644 --- a/basestruct.h +++ b/basestruct.h @@ -10,23 +10,25 @@ #include - +/* struct FPVector { QPointF pnt; float angle; }; +*/ - +/* struct GameData { QSize size; QPoint start; QPoint finish; + QPixmap * buff; int cellsize; int ** Cells; }; - +*/ /*struct Effect { diff --git a/games.cpp b/games.cpp new file mode 100644 index 0000000..86a4c0a --- /dev/null +++ b/games.cpp @@ -0,0 +1,16 @@ +#include "games.h" + +games::games(QSize formsize, QPoint startpnt, QPoint finishpnt, int sizeofcell) +{ + buff = new QPixmap(formsize); + background = new QPixmap(formsize); + cellsize = sizeofcell; + size = formsize/cellsize; + start = startpnt/cellsize; + finish = finishpnt/cellsize; + Cells = new int*[size.width()]; + for (int i = 0; i < size.width(); i++) { + Cells[i] = new int[size.height()]; + for (int j = 0; j < size.height(); j++) Cells[i][j] = 0; + } +} diff --git a/games.h b/games.h new file mode 100644 index 0000000..ed84a5e --- /dev/null +++ b/games.h @@ -0,0 +1,19 @@ +#ifndef GAMES_H +#define GAMES_H + +#include "basestruct.h" + +class games +{ +public: + games(QSize formsize, QPoint startpnt, QPoint finishpnt, int sizeofcell = 16); + QSize size; + QPoint start; + QPoint finish; + QPixmap * buff; + QPixmap * background; + int cellsize; + int ** Cells; +}; + +#endif // GAMES_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 2198a5b..04c13fb 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -7,8 +7,8 @@ MainWindow::MainWindow(QWidget *parent) { qDebug() << "Load START!"; ui->setupUi(this); - sndr = new UdpSender("127.0.0.1",10101); - sndr->connectSend("hellow"); + //sndr = new UdpSender("127.0.0.1",10101); + //sndr->connectSend("hellow"); cellsize = 16; destx = this->width() / cellsize; desty = this->height() / cellsize / 2; @@ -17,8 +17,8 @@ MainWindow::MainWindow(QWidget *parent) adp->clear(); adp->drawgrid(); connect(timer, SIGNAL(timeout()), this, SLOT(ADrender())); - GameMain = new game(this->size(),QPoint(0,this->height()/2),QPoint(this->width()-cellsize,this->height()/2), cellsize); - aliens = new Aliens(GameMain->data); + GameMain = new games(this->size(),QPoint(0,this->height()/2),QPoint(this->width()-cellsize,this->height()/2), cellsize); + aliens = new Aliens(GameMain); aliens->AddAlien(); this->setGeometry(100,100,this->width(),this->height()); //qDebug() << "QT version" << qVersion(); @@ -45,14 +45,14 @@ void MainWindow::paintEvent(QPaintEvent*) } -void MainWindow::mouseMoveEvent ( QMouseEvent * event ) +void MainWindow::mouseMoveEvent (QMouseEvent *event) { if (mousebt == 1) ClearCell(event->pos()); if (mousebt == 2) SetCell(event->pos()); } -void MainWindow::mousePressEvent ( QMouseEvent * event ) +void MainWindow::mousePressEvent (QMouseEvent *event) { if (event->button() == Qt::RightButton) { ClearCell(event->pos()); @@ -76,7 +76,7 @@ void MainWindow::SetCell(QPoint pos) cp.setX(pos.x()/cellsize); cp.setY(pos.y()/cellsize); //qDebug() << "click: " << cp.x() << ";" << cp.y(); - GameMain->data->Cells[cp.x()][cp.y()] = - 1; + GameMain->Cells[cp.x()][cp.y()] = - 1; adp->drawcell(cp); //aliens->clearAliens(); //adp->StepAlien=0; @@ -95,7 +95,7 @@ void MainWindow::ClearCell(QPoint pos) cp.setX(pos.x()/cellsize); cp.setY(pos.y()/cellsize); //qDebug() << "click: " << cp.x() << ";" << cp.y(); - GameMain->data->Cells[cp.x()][cp.y()] = 0; + GameMain->Cells[cp.x()][cp.y()] = 0; adp->clearcell(cp); //aliens->clearAliens(); //aliens->AddAlien(); @@ -105,6 +105,17 @@ void MainWindow::ClearCell(QPoint pos) } +void MainWindow::keyPressEvent(QKeyEvent * e) +{ + qDebug() << e->key(); + switch(e->key()) { + case 69: + aliens->AddAlien(); + break; + } +} + + MainWindow::~MainWindow() { delete ui; diff --git a/mainwindow.h b/mainwindow.h index 043479a..29f0412 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -3,10 +3,11 @@ #include #include +#include +#include #include "adpainter.h" #include "aliens.h" #include "udpsender.h" -#include "game.h" namespace Ui { @@ -27,13 +28,14 @@ private: void paintEvent(QPaintEvent*); void SetCell(QPoint pos); void ClearCell(QPoint pos); - void mouseMoveEvent ( QMouseEvent * event ); - void mousePressEvent ( QMouseEvent * event ); + void mouseMoveEvent(QMouseEvent *e); + void mousePressEvent(QMouseEvent *e); + void keyPressEvent(QKeyEvent *e); Ui::MainWindow *ui; Aliens * aliens; - UdpSender * sndr; + //UdpSender * sndr; adpainter * adp; - game * GameMain; + games * GameMain; QTimer *timer; int mousebt; int cellsize, destx, desty; diff --git a/map.cpp b/map.cpp index b7b2aeb..c5ec992 100644 --- a/map.cpp +++ b/map.cpp @@ -1,6 +1,6 @@ #include "map.h" -map::map(GameData *dataMap) +map::map(games *parent) { } diff --git a/map.h b/map.h index bc2d4cf..c7f91da 100644 --- a/map.h +++ b/map.h @@ -1,12 +1,12 @@ #ifndef MAP_H #define MAP_H -#include "basestruct.h" +#include "games.h" class map { public: - map(GameData *dataMap); + map(games *parent); QPixmap * background; }; diff --git a/towers.cpp b/towers.cpp index 6a2d043..64164a2 100644 --- a/towers.cpp +++ b/towers.cpp @@ -1,6 +1,6 @@ #include "towers.h" -towers::towers(GameData *dataTowers) +towers::towers(games *parent) { } diff --git a/towers.h b/towers.h index 14d717a..739885c 100644 --- a/towers.h +++ b/towers.h @@ -1,14 +1,14 @@ #ifndef TOWERS_H #define TOWERS_H -#include "basestruct.h" +#include "games.h" class towers { public: - towers(GameData *dataTowers); - QVector srcTowers; - QVector curTowers; + towers(games *parent); + //QVector srcTowers; + //QVector curTowers; void AddTower(QPoint point, int type); };