now this work stable
global restruct fuinish part 1 of 3
This commit is contained in:
@@ -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()]);
|
||||
|
||||
@@ -1,18 +1,13 @@
|
||||
#ifndef ADPAINTER_H
|
||||
#define ADPAINTER_H
|
||||
|
||||
#include "basestruct.h"
|
||||
#include "alien.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <QPainter>
|
||||
#include <QPaintEvent>
|
||||
#include <QPaintDevice>
|
||||
#include <QBrush>
|
||||
#include <QPen>
|
||||
#include <QPixmap>
|
||||
#include <QImage>
|
||||
|
||||
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;
|
||||
|
||||
97
alien.cpp
97
alien.cpp
@@ -2,25 +2,27 @@
|
||||
|
||||
#include <cmath>
|
||||
|
||||
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<QPoint> srcPath;
|
||||
QVector<QPointF> 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; i<srcPath.size();i++)
|
||||
{
|
||||
data->Cells[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<QPoint> 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<QPoint> 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<QPoint> 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++;
|
||||
|
||||
14
alien.h
14
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<QPointF> path;
|
||||
private:
|
||||
int ** TmpCells;
|
||||
GameData * data;
|
||||
FPVector Position;
|
||||
games *game;
|
||||
QPointF position;
|
||||
float angle;
|
||||
//int PicType;
|
||||
//int FlX;
|
||||
//int FlY;
|
||||
|
||||
@@ -12,7 +12,7 @@ SOURCES += main.cpp \
|
||||
udpsender.cpp \
|
||||
towers.cpp \
|
||||
map.cpp \
|
||||
game.cpp \
|
||||
games.cpp \
|
||||
alien.cpp
|
||||
HEADERS += mainwindow.h \
|
||||
adpainter.h \
|
||||
@@ -21,7 +21,7 @@ HEADERS += mainwindow.h \
|
||||
udpsender.h \
|
||||
towers.h \
|
||||
map.h \
|
||||
game.h \
|
||||
games.h \
|
||||
alien.h
|
||||
FORMS += mainwindow.ui
|
||||
RESOURCES += images.qrc
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
5
aliens.h
5
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<Alien*> srcAliens;
|
||||
QVector<Alien*> curAliens;
|
||||
};
|
||||
|
||||
@@ -10,23 +10,25 @@
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
|
||||
/*
|
||||
struct FPVector
|
||||
{
|
||||
QPointF pnt;
|
||||
float angle;
|
||||
};
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
struct GameData
|
||||
{
|
||||
QSize size;
|
||||
QPoint start;
|
||||
QPoint finish;
|
||||
QPixmap * buff;
|
||||
int cellsize;
|
||||
int ** Cells;
|
||||
};
|
||||
|
||||
*/
|
||||
|
||||
/*struct Effect
|
||||
{
|
||||
|
||||
16
games.cpp
Normal file
16
games.cpp
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
19
games.h
Normal file
19
games.h
Normal file
@@ -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
|
||||
@@ -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();
|
||||
@@ -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;
|
||||
|
||||
12
mainwindow.h
12
mainwindow.h
@@ -3,10 +3,11 @@
|
||||
|
||||
#include <QtGui/QMainWindow>
|
||||
#include <QTimer>
|
||||
#include <QMouseEvent>
|
||||
#include <QKeyEvent>
|
||||
#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;
|
||||
|
||||
2
map.cpp
2
map.cpp
@@ -1,6 +1,6 @@
|
||||
#include "map.h"
|
||||
|
||||
|
||||
map::map(GameData *dataMap)
|
||||
map::map(games *parent)
|
||||
{
|
||||
}
|
||||
|
||||
4
map.h
4
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;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "towers.h"
|
||||
|
||||
towers::towers(GameData *dataTowers)
|
||||
towers::towers(games *parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
8
towers.h
8
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<Tower> srcTowers;
|
||||
QVector<Tower> curTowers;
|
||||
towers(games *parent);
|
||||
//QVector<Tower> srcTowers;
|
||||
//QVector<Tower> curTowers;
|
||||
void AddTower(QPoint point, int type);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user