From f3d0ca7101f18948cb9d663a50357d7c4d65097f Mon Sep 17 00:00:00 2001 From: blizer Date: Sat, 20 Aug 2011 07:34:19 +0400 Subject: [PATCH] changes in arhitecture, some improvments, much optimising and simplify, many bagfixes and new graphics engine --- ad_graphics.cpp | 198 ++++++------------- ad_graphics.h | 34 +++- adcore.cpp | 52 ++--- adcore.h | 18 +- aditem.cpp | 74 +++++++ aditem.h | 38 ++++ aliendefender.pro | 15 +- aliendefender.pro.user | 387 ++++++++++++++++--------------------- aliendefender.pro.user.1.3 | 68 ++----- aliens.cpp | 109 +++++------ aliens.h | 21 +- base_types.h | 92 ++++----- data2.xml | 72 +++---- form.cpp | 135 +++++++++++++ form.h | 41 ++++ form.ui | 107 ++++++++++ game_data.cpp | 2 +- game_data.h | 9 +- loader.cpp | 39 ++-- loader.h | 4 +- main.cpp | 13 +- map.cpp | 109 ++--------- map.h | 17 +- player.cpp | 2 +- player.h | 2 +- splashes.cpp | 275 +++++++++++++++----------- splashes.h | 27 ++- touch_butt.cpp | 108 +++++++++++ touch_butt.h | 40 ++++ touchbuttframe.cpp | 67 +++++++ touchbuttframe.h | 51 +++++ towers.cpp | 109 ++++++----- towers.h | 6 +- 33 files changed, 1444 insertions(+), 897 deletions(-) create mode 100644 aditem.cpp create mode 100644 aditem.h create mode 100644 form.cpp create mode 100644 form.h create mode 100644 form.ui create mode 100644 touch_butt.cpp create mode 100644 touch_butt.h create mode 100644 touchbuttframe.cpp create mode 100644 touchbuttframe.h diff --git a/ad_graphics.cpp b/ad_graphics.cpp index ebaf762..46c3cd6 100644 --- a/ad_graphics.cpp +++ b/ad_graphics.cpp @@ -1,21 +1,23 @@ #include "ad_graphics.h" #include +#include + AD_Graphics::AD_Graphics(AD_Core *adcore, QWidget *parent) : QGraphicsView(parent) { data = adcore->addata; core = adcore; - //scale(cellSize, cellSize); + setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); setAlignment(Qt::AlignLeft | Qt::AlignTop); scene = new QGraphicsScene(); + scene->setItemIndexMethod(QGraphicsScene::NoIndex); + core->setScene(scene); setScene(scene); - //setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); - setViewportUpdateMode(QGraphicsView::FullViewportUpdate); + //setCacheMode(); + //setViewportUpdateMode(QGraphicsView::FullViewportUpdate); mapimg = new QImage(data->map->rect().size()*cellSize,QImage::Format_ARGB32); //setDragMode(RubberBandDrag); - //gsw = new QGraphicsWidget(); - //scene->addWidget(new QPushButton()); QPainter p(mapimg); for (int i=0; imap->cells().size(); i++) { for (int j=0; jmap->cells().at(i).size(); j++) { @@ -30,14 +32,6 @@ AD_Graphics::AD_Graphics(AD_Core *adcore, QWidget *parent) : QGraphicsView(paren pen.setColor(Qt::black); brush.setColor(Qt::black); } - // if (cel <= Map::PlayerTower && cel !=Map::Wall) { - // pen.setColor(QColor(cel%255,(cel*100)%255,(cel+100)%255)); - // brush.setColor(QColor(cel%255,(cel*100)%255,(cel+100)%255)); - // } - // if (cel >= Map::PlayerAlien) { - // pen.setColor(Qt::yellow); - // brush.setColor(Qt::yellow); - // } if (cel >= Map::Free || (cel <= Map::PlayerTower && cel !=Map::Wall)) { pen.setColor(QColor(qAbs(cel+2)*345%255,(qAbs(cel+2)*721)%255,(qAbs(cel+2)*75)%255,150)); @@ -48,152 +42,82 @@ AD_Graphics::AD_Graphics(AD_Core *adcore, QWidget *parent) : QGraphicsView(paren p.drawRect(i*cellSize,j*cellSize,cellSize,cellSize); } } - - startTimer(50); - show(); + p.end(); + scene->setSceneRect(mapimg->rect()); + scene->setBackgroundBrush(*mapimg); + setCacheMode(QGraphicsView::CacheBackground); + resize(mapimg->size()); +// startTimer(50); } AD_Graphics::~AD_Graphics() { - //delete gsw; delete scene; } -void AD_Graphics::mouseDoubleClickEvent(QMouseEvent *) -{ - core->nextWave(); -} - - void AD_Graphics::mousePressEvent(QMouseEvent *event) { + scene->clearSelection(); if (event->button() == Qt::LeftButton) - core->adtowers->addTower(0,1,(QPointF(event->pos())/cellSize-QPointF(0.5,0.5)).toPoint()); + { + if (m_building) emit add_tow((QPointF(event->pos())/cellSize).toPoint()); + else + { + QList list = scene->items(event->pos(), Qt::IntersectsItemBoundingRect, Qt::AscendingOrder); + ADItem * al = 0; + foreach(QGraphicsItem* x, list) + { + ADItem * i = qgraphicsitem_cast(x); + if (i->itemType() == ADItem::Tower) + { + i->setSelected(true); + emit tower_select(i->tid()); + return; + } + if (i->itemType() == ADItem::Alien) + { + al = i; + } + } + if (al != 0) + { + al->setSelected(true); + emit alien_select(al->id()); + return; + } + } + } if (event->button() == Qt::RightButton) - core->adtowers->addTower(0,0,(QPointF(event->pos())/cellSize-QPointF(0.5,0.5)).toPoint()); - if (event->button() == Qt::MidButton) - core->adtowers->delTower((QPointF(event->pos())/cellSize-QPointF(0.5,0.5)).toPoint()); + emit cancel(); } -void AD_Graphics::timerEvent(QTimerEvent * ) -{ - //qDebug() << data->curAliens.size(); - draw(); -} +//void AD_Graphics::timerEvent(QTimerEvent * ) +//{ +// scene->update(); +//} void AD_Graphics::drawBackground(QPainter * p, const QRectF & ) { - p->drawImage(0,0,*mapimg); -// for (int i=0; imap->cells().size(); i++) { -// for (int j=0; jmap->cells().at(i).size(); j++) { -// QPen pen; -// QBrush brush; -// pen.setColor(Qt::white); -// brush.setColor(Qt::white); -// brush.setStyle(Qt::SolidPattern); -// int cel = data->map->cells().at(i).at(j); -// if (cel == Map::Wall) -// { -// pen.setColor(Qt::black); -// brush.setColor(Qt::black); -// } -// // if (cel <= Map::PlayerTower && cel !=Map::Wall) { -// // pen.setColor(QColor(cel%255,(cel*100)%255,(cel+100)%255)); -// // brush.setColor(QColor(cel%255,(cel*100)%255,(cel+100)%255)); -// // } -// // if (cel >= Map::PlayerAlien) { -// // pen.setColor(Qt::yellow); -// // brush.setColor(Qt::yellow); -// // } -// if (cel >= Map::Free || (cel <= Map::PlayerTower && cel !=Map::Wall)) -// { -// pen.setColor(QColor(qAbs(cel+2)*345%255,(qAbs(cel+2)*721)%255,(qAbs(cel+2)*75)%255,50)); -// brush.setColor(QColor(qAbs(cel+2)*345%255,(qAbs(cel+2)*721)%255,(qAbs(cel+2)*75)%255,50)); -// } -// p->setPen(pen); -// p->setBrush(brush); -// p->drawRect(i*cellSize,j*cellSize,cellSize,cellSize); -// } -// } - - //qDebug() << "t:" << data->curTowers.size(); - for (QHash::iterator i = data->curTowers.begin(); i != data->curTowers.end(); ++i) - { - //qDebug() << "draw alien"; - //p->rotate(-90); - //p->scale(cellSize, cellSize); - //p->translate(rec.width()/2, rec.height()/2); - //p->rotate(-90); - //p->translate(-rec.height()/2, -rec.width()/2); - p->translate((*i).pos * cellSize+QPointF(20,20)); - p->rotate((*i).angle); - //p->setPen(QColor(0,0,0,0)); - //p->setBrush(brush); - //p->drawRect(QRect(-10,-10,20,20)); - if ((*i).build < data->players.at((*i).PlayerId)->srcTowers.at((*i).src).buildTime) - { - QBrush brush; - brush.setColor(Qt::darkRed); - brush.setStyle(Qt::DiagCrossPattern); - p->setBrush(brush); - p->drawRect(-20,-20,40,40); - } else { - p->drawImage(QRect(-20,-20,40,40), *data->curAnimations.at((*i).imgType).images.at((*i).animIndex)); - } - p->resetTransform(); - } - //qDebug() << "a:" << data->curAliens.size(); - for (QHash::iterator i = data->curAliens.begin(); i != data->curAliens.end(); ++i) - { - //qDebug() << "draw alien"; - //p->rotate(-90); - //p->scale(cellSize, cellSize); - //p->translate(rec.width()/2, rec.height()/2); - //p->rotate(-90); - //p->translate(-rec.height()/2, -rec.width()/2); - p->translate((*i).pos * cellSize+QPointF(10,10)); - p->rotate((*i).angle); - p->drawImage(QRect(-20,-20,40,40), *data->curAnimations.at((*i).imgType).images.at((*i).animIndex)); - (*i).animIndex++; - if ((*i).animIndex >= data->curAnimations.at((*i).imgType).images.size()) - (*i).animIndex = 0; - p->resetTransform(); - } - //qDebug() << "s:" << data->curSplashes.size(); - for (QHash::iterator i = data->curSplashes.begin(); i != data->curSplashes.end(); ++i) - { - //qDebug() << "draw alien"; - //p->rotate(-90); - //p->scale(cellSize, cellSize); - //p->translate(rec.width()/2, rec.height()/2); - //p->rotate(-90); - //p->translate(-rec.height()/2, -rec.width()/2); - //QBrush brush; - //brush.setColor(Qt::red); - //brush.setStyle(Qt::SolidPattern); - p->translate((*i).pos * cellSize); - p->rotate((*i).angle); - //p->setBrush(brush); - //p->setPen(QColor(0,0,0,0)); - //p->drawEllipse(QPoint(),5,5); - QImage img; - img = *data->curAnimations.at((*i).imgType).images.at((*i).animIndex); - p->drawImage(QPoint(-(img.size().width()/2),-(img.size().height()/2)), img); - (*i).animIndex++; - if ((*i).animIndex >= data->curAnimations.at((*i).imgType).images.size()) - (*i).animIndex = 0; - p->resetTransform(); - } + p->drawImage(0,0,*mapimg); } -void AD_Graphics::draw() +void AD_Graphics::drawForeground(QPainter *p, const QRectF &rect) { - scene->update(); - //resetCachedContent(); - //repaint(); + QList l = scene->items(); + foreach(QGraphicsItem * g,l) + { + ADItem * i = qgraphicsitem_cast(g); + if (i->isBarVisible()) + { + p->setPen(Qt::black); + p->setBrush(QBrush(QColor(qRound(255*(1-i->value())),qRound(255*i->value()),0))); + p->drawRect(i->pos().x()-cellSize,i->pos().y()-cellSize*1.25,cellSize*2*i->value(),cellSize/4); + } + } } + diff --git a/ad_graphics.h b/ad_graphics.h index 0a4189d..7e30aed 100644 --- a/ad_graphics.h +++ b/ad_graphics.h @@ -7,29 +7,49 @@ #include "adcore.h" -const float cellSize = 20.f; class AD_Graphics: public QGraphicsView { Q_OBJECT + Q_PROPERTY(bool building READ building WRITE setBuilding) public: explicit AD_Graphics(AD_Core * core, QWidget * parent = 0); ~AD_Graphics(); + bool building() const + { + return m_building; + } + +public slots: + void setBuilding(bool arg) + { + m_building = arg; + } + private: - void draw(); - void drawBackground(QPainter * p, const QRectF &); - void mouseDoubleClickEvent(QMouseEvent *); + void drawBackground(QPainter * p, const QRectF &); + void drawForeground(QPainter *painter, const QRectF &rect); void mousePressEvent(QMouseEvent *event); QGraphicsScene * scene; - Game_Data * data; AD_Core * core; QImage * mapimg; - //QGraphicsWidget * gsw; + Game_Data * data; + + bool m_building; + +private slots: protected: - void timerEvent(QTimerEvent * ); + +// void timerEvent(QTimerEvent * ); +signals: + + void cancel(); + void add_tow(QPoint pnt); + void tower_select(QPoint id); + void alien_select(int id); }; #endif // AD_GRAPHICS_H diff --git a/adcore.cpp b/adcore.cpp index 9fee9f5..bf6cc74 100644 --- a/adcore.cpp +++ b/adcore.cpp @@ -4,36 +4,20 @@ AD_Core::AD_Core(QObject *parent) : QThread(parent) { step = 0; - //admap = new Map(1); - //addata = new Game_Data(admap); Loader * adloader = new Loader(); adloader->load("data2.xml"); - //adloader->save("data2.xml"); addata = adloader->loadlevel(1); delete adloader; if (addata != 0) qDebug("Sucsess"); else qFatal("Error"); - //Player * pl1 = new Player(0); -// Player * pl2 = new Player(0); - //addata->players.push_back(pl1); -// addata->players.push_back(pl2); - adaliens = new Aliens(addata); adtowers = new Towers(addata); adsplashes = new Splashes(addata); - connect(addata->map,SIGNAL(recreateAlienPath(bool*)),adaliens,SLOT(retrace(bool*))); - connect(adtowers,SIGNAL(shot(QPoint)),adsplashes,SLOT(addSplash(QPoint))); -// adaliens->addAlien(0); -// adaliens->AddAlien(0); -// adaliens->AddAlien(0); - //adtowers->addTower(0,2,QPoint(15,1)); - for (int i=0; i<20;i++) -// adtowers->addTower(0,1,QPoint(8,20-i)); -// for (int i=0; i<20; i++) -// adtowers->addTower(0,0,QPoint(12,i)); -// for (int i=0; i<20;i++) -// adtowers->addTower(0,1,QPoint(16,20-i)); + connect(addata->map,SIGNAL(recreateAlienPath(bool*)),addata->aliens,SLOT(retrace(bool*))); + connect(adtowers,SIGNAL(shot(TowerType)),adsplashes,SLOT(addSplash(TowerType))); + connect(adsplashes,SIGNAL(killAlien(int,int)),this,SLOT(killAlien(int,int))); waveInProgress = false; - startTimer(20); + waveEnd = false; + startTimer(50); } @@ -41,7 +25,7 @@ void AD_Core::next() { adtowers->update(); adsplashes->update(); - adaliens->update(); + addata->aliens->update(); waveProcessing(); step++; } @@ -58,6 +42,7 @@ void AD_Core::nextWave() if (waveInProgress) return; if (addata->curWave >= addata->waves.size()) { qDebug("level finished"); + waveEnd = false; return; } //wavetime = addata->waves.at(addata->curWave).timeout; @@ -69,13 +54,19 @@ void AD_Core::nextWave() void AD_Core::waveProcessing() { + if (addata->aliens->curAliens.isEmpty() && waveEnd) + { + addata->players.at(0)->money += addata->waves.at(addata->curWave-1).prise; + waveEnd = false; + } if (!waveInProgress) return; if (wavetime <= 0) { for (int i=0; iwaves.at(addata->curWave).counts.size(); i++) { if (addata->waves.at(addata->curWave).counts.at(i) > 0) { - adaliens->addAlien(addata->waves.at(addata->curWave).types.at(i)); + addata->aliens->addAlien(addata->waves.at(addata->curWave).types.at(i)); addata->waves[addata->curWave].counts[i]--; wavetime = addata->waves.at(addata->curWave).timeout; + waveEnd = true; return; } } @@ -85,3 +76,18 @@ void AD_Core::waveProcessing() wavetime--; } } + + +void AD_Core::killAlien(int playerId, int alienId) +{ + addata->players.at(playerId)->money += addata->aliens->curAliens[alienId].src->prise; + addata->aliens->delAlien(alienId); +} + + +void AD_Core::setScene(QGraphicsScene *scene) +{ + addata->aliens->setScene(scene); + adtowers->setScene(scene); + adsplashes->setScene(scene); +} diff --git a/adcore.h b/adcore.h index 3d51ebd..99781af 100644 --- a/adcore.h +++ b/adcore.h @@ -14,22 +14,26 @@ Q_OBJECT public: explicit AD_Core(QObject *parent = 0); Game_Data * addata; - Aliens * adaliens; Towers * adtowers; Splashes * adsplashes; - //Loader * adloader; - unsigned int step; + long int step; + + void setScene(QGraphicsScene * scene); + +public slots: + void next(); + void nextWave(); private: void timerEvent(QTimerEvent * ); void waveProcessing(); bool waveInProgress; + bool waveEnd; int wavetime; -public slots: - void next(); - void nextWave(); - //void addTower(QPoint pos) {adtowers->addTower(0, 0, pos);}; + +private slots: + void killAlien(int playerId, int alienId); signals: }; diff --git a/aditem.cpp b/aditem.cpp new file mode 100644 index 0000000..b4788fb --- /dev/null +++ b/aditem.cpp @@ -0,0 +1,74 @@ +#include "aditem.h" +#include +#include + + +ADItem::ADItem(int id, adType type, QList *images_, QRectF geometry, QGraphicsItem *parent) : + QGraphicsItem(parent) +{ + m_id = id; + m_type = type; + anim = 0; + images = images_; + animcount = images->size(); + QSize size = images->at(0)->size(); + staticImage = false; + if (animcount == 1) staticImage = true; + if (geometry.isNull()) br = QRect(QPoint(-(size.width()/2), -(size.height()/2)), size); + else br = geometry; + if (type == Alien) + { + setFlag(QGraphicsItem::ItemIsSelectable); + barValue = 1.f; + hasBar = true; + } else hasBar = false; + if (staticImage) + { + setCacheMode(QGraphicsItem::ItemCoordinateCache); + img = (*images)[0]; + } +} + + +ADItem::ADItem(QPoint id, QImage *image, QRectF geometry, QGraphicsItem *parent) : + QGraphicsItem(parent) +{ + hasBar = true; + barValue = 0.f; + m_type = Tower; + m_tid = id; + img = image; + QSize size = img->size(); + if (geometry.isNull()) br = QRect(QPoint(-(size.width()/2),-(size.height()/2)),size); + else br = geometry; + staticImage = true; + setFlag(QGraphicsItem::ItemIsSelectable); +} + + +void ADItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + if (staticImage) + painter->drawImage(br,*img); + else painter->drawImage(br, *(images->at(qRound(anim)))); + if (isSelected()) + { + painter->setPen(Qt::red); + painter->drawRect(br); + } +} + + +QRectF ADItem::boundingRect() const +{ + return br; +} + +void ADItem::next(float step) +{ + if (staticImage) return; + anim+=step; + if (qRound(anim) >= animcount) + anim = 0; +} + diff --git a/aditem.h b/aditem.h new file mode 100644 index 0000000..a20c263 --- /dev/null +++ b/aditem.h @@ -0,0 +1,38 @@ +#ifndef ADITEM_H +#define ADITEM_H + +#include + +class ADItem : public QGraphicsItem +{ +public: + enum adType {Alien, Tower, Splash}; + explicit ADItem(int id, adType type, QList *images, QRectF geometry = QRectF(), QGraphicsItem *parent = 0); + explicit ADItem(QPoint id, QImage * image, QRectF geometry = QRectF(), QGraphicsItem *parent = 0); + void setBarValue(float val) {barValue = val;} + void setBarVisible(bool visible = true) {hasBar = visible;} + void hideBar() {hasBar = false; setCacheMode(QGraphicsItem::ItemCoordinateCache);} + float value() {return barValue;} + bool isBarVisible() {return hasBar;} + void next(float step); + QPoint tid() {return m_tid;} + int id() {return m_id;} + adType itemType() {return m_type;} + +private: + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + QRectF boundingRect() const; + QList * images; + QImage * img; + float anim; + adType m_type; + int m_id; + QPoint m_tid; + int animcount; + bool staticImage; + bool hasBar; + float barValue; + QRectF br; +}; + +#endif // ADITEM_H diff --git a/aliendefender.pro b/aliendefender.pro index 0442c8b..956c8bb 100644 --- a/aliendefender.pro +++ b/aliendefender.pro @@ -7,7 +7,11 @@ HEADERS += base_types.h \ adcore.h \ player.h \ loader.h \ - ad_graphics.h + ad_graphics.h \ + form.h \ + aditem.h \ + touchbuttframe.h \ + touch_butt.h SOURCES += map.cpp \ game_data.cpp \ aliens.cpp \ @@ -17,7 +21,11 @@ SOURCES += map.cpp \ adcore.cpp \ player.cpp \ loader.cpp \ - ad_graphics.cpp + ad_graphics.cpp \ + form.cpp \ + aditem.cpp \ + touchbuttframe.cpp \ + touch_butt.cpp RESOURCES += images.qrc OTHER_FILES += schema.xml \ data2.xml @@ -27,3 +35,6 @@ QT += core \ opengl \ xml \ xmlpatterns + +FORMS += \ + form.ui diff --git a/aliendefender.pro.user b/aliendefender.pro.user index 8e4a063..de64635 100644 --- a/aliendefender.pro.user +++ b/aliendefender.pro.user @@ -1,243 +1,184 @@ - RunConfiguration0-BaseEnvironmentBase - 2 - - - RunConfiguration0-CommandLineArguments - - - - RunConfiguration0-ProFile - aliendefender.pro - - - RunConfiguration0-RunConfiguration.name - aliendefender - - - RunConfiguration0-UseDyldImageSuffix - false - - - RunConfiguration0-UseTerminal - false - - - RunConfiguration0-UserEnvironmentChanges - - - - RunConfiguration0-UserSetName - false - - - RunConfiguration0-UserSetWorkingDirectory - false - - - RunConfiguration0-UserWorkingDirectory - - - - RunConfiguration0-type - Qt4ProjectManager.Qt4RunConfiguration - - - activeRunConfiguration + ProjectExplorer.Project.ActiveTarget 0 - activebuildconfiguration - Debug - - - buildConfiguration-Debug + ProjectExplorer.Project.EditorSettings - Debug - 0 - 0 - - 2 + true + false + System + false + false + 4 + true + 1 + true + false + false + 0 + 4 + true + 1 + true + true + true + false - buildConfiguration-Release + ProjectExplorer.Project.Target.0 - Release - 0 - - 0 + + + Qt4ProjectManager.Target.DesktopTarget + 0 + 0 + 0 + + ProjectExplorer.ToolChain.Mingw:D:/Qt/2010.04/mingw/bin/g++.exe.x86-windows-msys-pe-32bit.C:/Qt/qtcreator-2.2.0/pythongdb/gdb-i686-pc-mingw32.exe + + + qmake + + QtProjectManager.QMakeBuildStep + false + + false + + + Сборка + + Qt4ProjectManager.MakeStep + false + + + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + Сборка + + Qt4ProjectManager.MakeStep + true + clean + + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + D:/Dropbox/My Dropbox/projects/aliendefender-build-desktop + 7 + ProjectExplorer.ToolChain.Mingw:D:/Qt/2010.04/mingw/bin/g++.exe.x86-windows-msys-pe-32bit.C:/Qt/qtcreator-2.2.0/pythongdb/gdb-i686-pc-mingw32.exe + false + + + ProjectExplorer.ToolChain.Mingw:C:/Qt/qtcreator-2.2.0/mingw/bin/gcc.exe.x86-windows-msys-pe-32bit.C:/Qt/qtcreator-2.2.0/pythongdb/gdb-i686-pc-mingw32.exe + + + qmake + + QtProjectManager.QMakeBuildStep + false + + false + + + Сборка + + Qt4ProjectManager.MakeStep + false + + + + 2 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + Сборка + + Qt4ProjectManager.MakeStep + true + + + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + D:/Dropbox/My Dropbox/projects/aliendefender-build-desktop + 2 + ProjectExplorer.ToolChain.Mingw:C:/Qt/qtcreator-2.2.0/mingw/bin/gcc.exe.x86-windows-msys-pe-32bit.C:/Qt/qtcreator-2.2.0/pythongdb/gdb-i686-pc-mingw32.exe + false + + 2 + + + 0 + Установка + Deploy + ProjectExplorer.BuildSteps.Deploy + + 1 + Без установки + Без установки + ProjectExplorer.DefaultDeployConfiguration + + 1 + + Конфигурация запуска Qt4 + aliendefender + Qt4ProjectManager.Qt4RunConfiguration + 2 + + aliendefender.pro + false + false + + + 3768 + true + false + + 1 - buildconfiguration-Debug-buildstep0 - - Debug - - DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-2HAd2azpnh,guid=fd81c6d6610b4c57717ec20c4c90d3de - DEFAULTS_PATH=/usr/share/gconf/gnome.default.path - DESKTOP_SESSION=gnome - DISPLAY=:0.0 - GDMSESSION=gnome - GDM_KEYBOARD_LAYOUT=us - GDM_LANG=ru_RU.UTF-8 - GNOME_DESKTOP_SESSION_ID=this-is-deprecated - GNOME_KEYRING_CONTROL=/tmp/keyring-vNW3fv - GTK_MODULES=canberra-gtk-module - HOME=/home/andrey - LANG=ru_RU.UTF-8 - LD_LIBRARY_PATH=/usr/lib/qtcreator: - LOGNAME=andrey - MANDATORY_PATH=/usr/share/gconf/gnome.mandatory.path - ORBIT_SOCKETDIR=/tmp/orbit-andrey - PATH=/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games - PWD=/home/andrey - QTDIR=/usr/share/qt4 - SESSION_MANAGER=local/buull-eeePC:@/tmp/.ICE-unix/1158,unix/buull-eeePC:/tmp/.ICE-unix/1158 - SHELL=/bin/bash - SPEECHD_PORT=6561 - SSH_AGENT_PID=1195 - SSH_AUTH_SOCK=/tmp/keyring-vNW3fv/ssh - USER=andrey - USERNAME=andrey - XAUTHORITY=/var/run/gdm/auth-for-andrey-Len9P0/database - XDG_CONFIG_DIRS=/etc/xdg/xdg-gnome:/etc/xdg - XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/ - XDG_SESSION_COOKIE=f0649decca52f2beb55c9ea74ad1d028-1284559837.801731-819619931 - - - /home/andrey/AD/aliendefender/aliendefender.pro - -spec - linux-g++ - -r - CONFIG+=debug - - /usr/bin/qmake-qt4 - false - /home/andrey/AD/aliendefender - + ProjectExplorer.Project.TargetCount + 1 - buildconfiguration-Debug-buildstep1 - - Debug - - DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-2HAd2azpnh,guid=fd81c6d6610b4c57717ec20c4c90d3de - DEFAULTS_PATH=/usr/share/gconf/gnome.default.path - DESKTOP_SESSION=gnome - DISPLAY=:0.0 - GDMSESSION=gnome - GDM_KEYBOARD_LAYOUT=us - GDM_LANG=ru_RU.UTF-8 - GNOME_DESKTOP_SESSION_ID=this-is-deprecated - GNOME_KEYRING_CONTROL=/tmp/keyring-vNW3fv - GTK_MODULES=canberra-gtk-module - HOME=/home/andrey - LANG=ru_RU.UTF-8 - LD_LIBRARY_PATH=/usr/lib/qtcreator: - LOGNAME=andrey - MANDATORY_PATH=/usr/share/gconf/gnome.mandatory.path - ORBIT_SOCKETDIR=/tmp/orbit-andrey - PATH=/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games - PWD=/home/andrey - QTDIR=/usr/share/qt4 - SESSION_MANAGER=local/buull-eeePC:@/tmp/.ICE-unix/1158,unix/buull-eeePC:/tmp/.ICE-unix/1158 - SHELL=/bin/bash - SPEECHD_PORT=6561 - SSH_AGENT_PID=1195 - SSH_AUTH_SOCK=/tmp/keyring-vNW3fv/ssh - USER=andrey - USERNAME=andrey - XAUTHORITY=/var/run/gdm/auth-for-andrey-Len9P0/database - XDG_CONFIG_DIRS=/etc/xdg/xdg-gnome:/etc/xdg - XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/ - XDG_SESSION_COOKIE=f0649decca52f2beb55c9ea74ad1d028-1284559837.801731-819619931 - - false - - -w - - /usr/bin/make - true - /home/andrey/AD/aliendefender - + ProjectExplorer.Project.Updater.EnvironmentId + {00000000-0000-0000-0000-000000000000} - buildconfiguration-Debug-cleanstep0 - - Debug - true - - clean - - - - - buildconfiguration-Release-buildstep0 - - Release - - - - buildconfiguration-Release-buildstep1 - - Release - - - - buildconfiguration-Release-cleanstep0 - - Release - - - - buildconfigurations - - Debug - Release - - - - buildstep0 - - - - - - - buildstep1 - - - - - - buildsteps - - trolltech.qt4projectmanager.qmake - trolltech.qt4projectmanager.make - - - - cleanstep0 - - - true - - - - cleansteps - - trolltech.qt4projectmanager.make - - - - defaultFileEncoding - System - - - project - + ProjectExplorer.Project.Updater.FileVersion + 9 diff --git a/aliendefender.pro.user.1.3 b/aliendefender.pro.user.1.3 index 31ed8bf..8e4a063 100644 --- a/aliendefender.pro.user.1.3 +++ b/aliendefender.pro.user.1.3 @@ -76,7 +76,7 @@ Debug - DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-wExeabp7q4,guid=18815993df3603fdc64a3f6d4c7b5822 + DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-2HAd2azpnh,guid=fd81c6d6610b4c57717ec20c4c90d3de DEFAULTS_PATH=/usr/share/gconf/gnome.default.path DESKTOP_SESSION=gnome DISPLAY=:0.0 @@ -84,7 +84,7 @@ GDM_KEYBOARD_LAYOUT=us GDM_LANG=ru_RU.UTF-8 GNOME_DESKTOP_SESSION_ID=this-is-deprecated - GNOME_KEYRING_CONTROL=/tmp/keyring-4vN5uf + GNOME_KEYRING_CONTROL=/tmp/keyring-vNW3fv GTK_MODULES=canberra-gtk-module HOME=/home/andrey LANG=ru_RU.UTF-8 @@ -95,17 +95,17 @@ PATH=/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games PWD=/home/andrey QTDIR=/usr/share/qt4 - SESSION_MANAGER=local/buull-eeePC:@/tmp/.ICE-unix/1205,unix/buull-eeePC:/tmp/.ICE-unix/1205 + SESSION_MANAGER=local/buull-eeePC:@/tmp/.ICE-unix/1158,unix/buull-eeePC:/tmp/.ICE-unix/1158 SHELL=/bin/bash SPEECHD_PORT=6561 - SSH_AGENT_PID=1239 - SSH_AUTH_SOCK=/tmp/keyring-4vN5uf/ssh + SSH_AGENT_PID=1195 + SSH_AUTH_SOCK=/tmp/keyring-vNW3fv/ssh USER=andrey USERNAME=andrey - XAUTHORITY=/var/run/gdm/auth-for-andrey-x9URui/database + XAUTHORITY=/var/run/gdm/auth-for-andrey-Len9P0/database XDG_CONFIG_DIRS=/etc/xdg/xdg-gnome:/etc/xdg XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/ - XDG_SESSION_COOKIE=f0649decca52f2beb55c9ea74ad1d028-1283151906.338331-318132279 + XDG_SESSION_COOKIE=f0649decca52f2beb55c9ea74ad1d028-1284559837.801731-819619931 /home/andrey/AD/aliendefender/aliendefender.pro @@ -124,7 +124,7 @@ Debug - DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-wExeabp7q4,guid=18815993df3603fdc64a3f6d4c7b5822 + DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-2HAd2azpnh,guid=fd81c6d6610b4c57717ec20c4c90d3de DEFAULTS_PATH=/usr/share/gconf/gnome.default.path DESKTOP_SESSION=gnome DISPLAY=:0.0 @@ -132,7 +132,7 @@ GDM_KEYBOARD_LAYOUT=us GDM_LANG=ru_RU.UTF-8 GNOME_DESKTOP_SESSION_ID=this-is-deprecated - GNOME_KEYRING_CONTROL=/tmp/keyring-4vN5uf + GNOME_KEYRING_CONTROL=/tmp/keyring-vNW3fv GTK_MODULES=canberra-gtk-module HOME=/home/andrey LANG=ru_RU.UTF-8 @@ -143,17 +143,17 @@ PATH=/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games PWD=/home/andrey QTDIR=/usr/share/qt4 - SESSION_MANAGER=local/buull-eeePC:@/tmp/.ICE-unix/1205,unix/buull-eeePC:/tmp/.ICE-unix/1205 + SESSION_MANAGER=local/buull-eeePC:@/tmp/.ICE-unix/1158,unix/buull-eeePC:/tmp/.ICE-unix/1158 SHELL=/bin/bash SPEECHD_PORT=6561 - SSH_AGENT_PID=1239 - SSH_AUTH_SOCK=/tmp/keyring-4vN5uf/ssh + SSH_AGENT_PID=1195 + SSH_AUTH_SOCK=/tmp/keyring-vNW3fv/ssh USER=andrey USERNAME=andrey - XAUTHORITY=/var/run/gdm/auth-for-andrey-x9URui/database + XAUTHORITY=/var/run/gdm/auth-for-andrey-Len9P0/database XDG_CONFIG_DIRS=/etc/xdg/xdg-gnome:/etc/xdg XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/ - XDG_SESSION_COOKIE=f0649decca52f2beb55c9ea74ad1d028-1283151906.338331-318132279 + XDG_SESSION_COOKIE=f0649decca52f2beb55c9ea74ad1d028-1284559837.801731-819619931 false @@ -168,46 +168,6 @@ buildconfiguration-Debug-cleanstep0 Debug - - DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-wExeabp7q4,guid=18815993df3603fdc64a3f6d4c7b5822 - DEFAULTS_PATH=/usr/share/gconf/gnome.default.path - DESKTOP_SESSION=gnome - DISPLAY=:0.0 - GDMSESSION=gnome - GDM_KEYBOARD_LAYOUT=us - GDM_LANG=ru_RU.UTF-8 - GNOME_DESKTOP_SESSION_ID=this-is-deprecated - GNOME_KEYRING_CONTROL=/tmp/keyring-4vN5uf - GTK_MODULES=canberra-gtk-module - HOME=/home/andrey - LANG=ru_RU.UTF-8 - LD_LIBRARY_PATH=/usr/lib/qtcreator: - LOGNAME=andrey - MANDATORY_PATH=/usr/share/gconf/gnome.mandatory.path - ORBIT_SOCKETDIR=/tmp/orbit-andrey - PATH=/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games - PWD=/home/andrey - QTDIR=/usr/share/qt4 - SESSION_MANAGER=local/buull-eeePC:@/tmp/.ICE-unix/1205,unix/buull-eeePC:/tmp/.ICE-unix/1205 - SHELL=/bin/bash - SPEECHD_PORT=6561 - SSH_AGENT_PID=1239 - SSH_AUTH_SOCK=/tmp/keyring-4vN5uf/ssh - USER=andrey - USERNAME=andrey - XAUTHORITY=/var/run/gdm/auth-for-andrey-x9URui/database - XDG_CONFIG_DIRS=/etc/xdg/xdg-gnome:/etc/xdg - XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/ - XDG_SESSION_COOKIE=f0649decca52f2beb55c9ea74ad1d028-1283151906.338331-318132279 - - true - - clean - -w - - /usr/bin/make - true - /home/andrey/AD/aliendefender true clean diff --git a/aliens.cpp b/aliens.cpp index 432263d..e014f9a 100644 --- a/aliens.cpp +++ b/aliens.cpp @@ -1,54 +1,55 @@ #include "aliens.h" #include -Aliens::Aliens(Game_Data *gd, QObject *parent) : - QObject(parent) +Aliens::Aliens(Map * map_, QObject *parent) : + QObject(parent) { - gameData = gd; + map = map_; nextId = 1; + m_missingAliens = 0; } void Aliens::addAlien(int srcId) { - if (srcId < 0 || srcId >= gameData->srcAliens.size()) + if (srcId < 0 || srcId >= srcAliens.size()) { qCritical("ERROR out of aliens range"); return; } - AlienType al;// = gameData->srcAliens.at(srcId); + AlienType al; al.id = nextId; - al.src = srcId; - al.finish = gameData->map->finishs().at(qrand()%gameData->map->finishs().size()); - al.pos = QPointF(gameData->map->starts().at(qrand()%gameData->map->starts().size())); - al.path = gameData->map->createPath(al.pos.toPoint(),al.finish); + al.src = &(srcAliens[srcId]); + al.finish = map->finishs().at(qrand()%map->finishs().size()); + al.pos = QPointF(map->starts().at(qrand()%map->starts().size())); + al.path = map->createPath(al.pos.toPoint(),al.finish); al.pathIndex = 1; al.angle = 180.0f*(- std::atan2( al.pos.x() - al.path.at(al.pathIndex).x(),al.pos.y() - al.path.at(al.pathIndex).y()))/M_PI; - al.animIndex = 0; if (al.path.isEmpty()) { qCritical("ERROR create path"); return; // FIXME: this } //qDebug() << al.path; - al.health = gameData->srcAliens.at(srcId).health; - al.speed = gameData->srcAliens.at(srcId).speed; - al.imgType = gameData->srcAliens.at(srcId).imgType; - //gameData->map->printMap(); - gameData->curAliens.insert(al.id,al); + al.health = al.src->health; + al.speed = al.src->speed; + al.item = new ADItem(al.id,ADItem::Alien,al.src->images,QRectF(-cellSize,-cellSize,cellSize*2,cellSize*2)); + al.item->setPos(al.pos*cellSize+QPointF(cellSize/2,cellSize/2)); + al.item->setRotation(al.angle); + curAliens.insert(al.id,al); + scene->addItem(al.item); nextId++; } void Aliens::retrace(bool * OK) { - //qDebug("re-trace!"); - gameData->map->removeAliensPath(); - for (QHash::iterator i = gameData->curAliens.begin(); i != gameData->curAliens.end(); ++i) + map->removeAliensPath(); + for (QHash::iterator i = curAliens.begin(); i != curAliens.end(); ++i) { - (*i).path = gameData->map->createPath((*i).pos.toPoint(),(*i).finish); - (*i).pathIndex = 1; - if ((*i).path.isEmpty()) + i->path = map->createPath(i->pos.toPoint(),i->finish); + i->pathIndex = 1; + if (i->path.isEmpty()) { *OK = false; qDebug("Aliens retrace = false"); @@ -57,70 +58,52 @@ void Aliens::retrace(bool * OK) } -void Aliens::delAlien(int Id) +void Aliens::delAlien(int id, bool missed) { - gameData->curAliens.remove(Id); + if (missed) + { + m_missingAliens++; + qDebug() << tr("Missing aliens = %1!").arg(m_missingAliens); + } + scene->removeItem(curAliens[id].item); + delete curAliens[id].item; + curAliens.remove(id); } void Aliens::update() { QList missIndex; - for (QHash::iterator i = gameData->curAliens.begin(); i != gameData->curAliens.end(); ++i) + for (QHash::iterator i = curAliens.begin(); i != curAliens.end(); ++i) { - //AlienType al = (*i); - //qDebug() << i; - //if (al.health < 0) emit AlienKill(); float angl,arctg = 0; int curMiss = -1; - while (distance2((*i).pos, (*i).path.at((*i).pathIndex)) < (*i).speed*(*i).speed) + while (distance2(i->pos, i->path.at(i->pathIndex)) < i->speed * i->speed) { - (*i).pathIndex++; - if ((*i).pathIndex >= (*i).path.size()) + i->pathIndex++; + if (i->pathIndex >= i->path.size()) { - missIndex.push_back((*i).id); - curMiss = (*i).id; + missIndex.push_back(i->id); + curMiss = i->id; break; } - /*{ - PathIndex = 0; - position = game->start*game->cellsize; - }*/ - // tmpdx = (*i).pos.x() - (*i).path.at((*i).pathIndex).x(); - // tmpdy = (*i).pos.y() - (*i).path.at((*i).pathIndex).y(); - //qDebug() << "next"; } if (curMiss < 0) { // TODO: smooth rotate (how???) - arctg = std::atan2((*i).pos.x() - (*i).path.at((*i).pathIndex).x(),(*i).pos.y() - (*i).path.at((*i).pathIndex).y()); - //if (tmpdy < 0) arctg=arctg+M_PI; + arctg = std::atan2(i->pos.x() - i->path.at(i->pathIndex).x(),i->pos.y() - i->path.at(i->pathIndex).y()); angl = 180.0f*(-arctg)/M_PI; - /*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*/ - (*i).angle = angl; - //qDebug() << "[" << PathIndex << ";" << PicIndex << "]" << "angle:" << Position.angle << "arctg:" << arctg << "Pos:" << Position.pnt; - (*i).pos.setX((*i).pos.x() - -(*i).speed*std::sin(arctg)); - (*i).pos.setY((*i).pos.y() - -(*i).speed*std::cos(arctg)); - //return true; - //gameData->curAliens.insert(al.id,al); - //qDebug() <<"alien"<< i << " path index=" << al.pathIndex << ", pos=" << al.pos; + i->angle = angl; + i->pos.rx() -= i->speed*std::sin(arctg); + i->pos.ry() -= i->speed*std::cos(arctg); } + i->item->setPos(i->pos*cellSize+QPointF(cellSize/2,cellSize/2)); + i->item->setRotation(i->angle); + i->item->setBarValue(i->health/i->src->health); + i->item->next(i->speed*cellSize); } for (int j=0; jmissingAliens++; - qDebug() << tr("Missing aliens = %1!").arg(gameData->missingAliens); - delAlien(missIndex.at(j)); + delAlien(missIndex.at(j),true); } } diff --git a/aliens.h b/aliens.h index 891fece..987863c 100644 --- a/aliens.h +++ b/aliens.h @@ -1,26 +1,39 @@ #ifndef ALIENS_H #define ALIENS_H -#include "game_data.h" +#include "base_types.h" +#include "map.h" +#include +#include class Aliens : public QObject { Q_OBJECT public: - explicit Aliens(Game_Data * gd, QObject *parent = 0); + explicit Aliens(Map * map, QObject *parent = 0); void addAlien(int srcId); void update(); + void setScene(QGraphicsScene * scene_) {scene = scene_;} + QList srcAliens; + QHash curAliens; + int missingAliens() {return m_missingAliens;} + signals: public slots: - void delAlien(int Id); + void delAlien(int id, bool missed = false); void retrace(bool * OK); + private: - Game_Data * gameData; + Map * map; + QGraphicsScene * scene; + QPointF globStart; QPointF globFinish; int nextId; + int m_missingAliens; + }; #endif // ALIENS_H diff --git a/base_types.h b/base_types.h index 8ecd101..c0213a6 100644 --- a/base_types.h +++ b/base_types.h @@ -15,6 +15,9 @@ #include #include #include +#include "aditem.h" + +const float cellSize = 20.f; struct srcAlienType @@ -27,6 +30,7 @@ struct srcAlienType bool isFlying; unsigned int score; int imgType; + QList * images; unsigned int prise; }; @@ -34,7 +38,7 @@ struct srcAlienType struct AlienType { int id; // uses for QHash - int src; + srcAlienType * src; QPointF pos; float angle; // -180 .. 180 float speed; // cells per tick (speed=1 is 60cells by 1 sec for 60fps e.g.) @@ -42,8 +46,7 @@ struct AlienType QPoint finish; QVector path; int pathIndex; - int imgType; - int animIndex; + ADItem * item; }; @@ -77,58 +80,30 @@ struct srcTriggerType struct TriggerOnDest { - int src; + srcTriggerType * src; }; struct TriggerOnTimer { - int src; + srcTriggerType * src; unsigned int timer; }; struct TriggerOnAlien { - int src; + srcTriggerType * src; QList triggerAliens; }; -struct srcSplashType -{ - QString name; - int imgType; - float speed; // in cells - int lifetime; - bool autoControl; - QList triggerIndexes; -}; - - -struct SplashType -{ - int id; - int imgType; - int animIndex; - int src; - QPoint TowerId; // parent tower (-1;-1) for null parent - int srcTower; - int PlayerId; - int AlienId; // aim alien (different from tower.AlienId) - QPointF destination; - int life; // in ticks - float angle; // -180 .. 180 - QPointF pos; - QList trigsOnAlien; - QList trigsOnDest; - QList trigsOnTimer; -}; - +struct srcSplashType; struct srcTowerType { QString name; + QImage * image; int imgType; int PlayerId; // tower's owner unsigned int cost; @@ -145,24 +120,55 @@ struct srcTowerType struct TowerType { - int src; + srcTowerType * src; int aim; // aim alien Id - int oldAim; int PlayerId; // tower's owner - int imgType; - int animIndex; - QPoint pos; // not QPointF because tower fixed on grid + QPoint pos; // same as Id; not QPointF because tower fixed on grid float angle; // -180 .. 180 unsigned int reload; // time for reload in ticks unsigned int build; float experience; + ADItem * item; + bool isBilding; }; +struct srcSplashType +{ + QString name; + int imgType; + float speed; // in cells + int lifetime; + bool autoControl; + QList triggerIndexes; + QList * images; +}; + + +struct SplashType +{ + int id; + srcSplashType * src; + srcTowerType * srcTow; + QPoint towerId; // parent tower, (0,0) or null for non parent + int PlayerId; + int AlienId; // aim alien (different from tower.AlienId) + QPointF destination; + int life; // in ticks + float angle; // -180 .. 180 + QPointF pos; + QList trigsOnAlien; + QList trigsOnDest; + QList trigsOnTimer; + ADItem * item; +}; + +/// DEPRECATED !!! struct AnimationType { - QList images; + QList * images; }; +/// DEPRECATED !!! struct WaveType @@ -176,7 +182,7 @@ struct WaveType inline uint qHash(const QPoint &pos) { - return qHash(pos.x()) + (qHash(pos.y()) << 16); + return qHash(pos.x() + (pos.y() << 16)); } diff --git a/data2.xml b/data2.xml index cd7a992..a9961af 100644 --- a/data2.xml +++ b/data2.xml @@ -1,41 +1,41 @@ - - + + - + + - - - - - - - - - + + + + + + + - + - - - - - - - - + + + + + + + - - - - - - - + + + + + - + + - - - + + + + + - + + diff --git a/form.cpp b/form.cpp new file mode 100644 index 0000000..742074b --- /dev/null +++ b/form.cpp @@ -0,0 +1,135 @@ +#include "form.h" +#include "ui_form.h" +#include "touchbuttframe.h" + + +Form::Form(QWidget *parent) : + QWidget(parent), + ui(new Ui::Form) +{ + buildTowerId = -1; + cur_tow = QPoint(); + cur_al = -1; + ui->setupUi(this); + graphics = new AD_Graphics(&core); + ui->widget->layout()->addWidget(graphics); + TouchButtFrame * tbf = new TouchButtFrame(); + tbf->addButton(tr("delete")); + foreach (srcTowerType i, core.addata->players.at(0)->srcTowers) + { + tbf->addButton(i.name); + //QPushButton * butt = new QPushButton(i.name); + //towerButtons << butt; + //butt->setAutoDefault(true); + //butt->setAutoExclusive(true); + //butt->setCheckable(true); + } + ui->gbTowers->layout()->addWidget(tbf); + connect(tbf,SIGNAL(click(int)),this,SLOT(towerBuild(int))); + connect(tbf,SIGNAL(click(int)),tbf,SLOT(set_green(int))); + connect(graphics,SIGNAL(add_tow(QPoint)),this,SLOT(add_tow(QPoint))); + connect(graphics,SIGNAL(tower_select(QPoint)),this,SLOT(towerSelected(QPoint))); + connect(graphics,SIGNAL(alien_select(int)),this,SLOT(alienSelected(int))); + connect(graphics,SIGNAL(cancel()),this,SLOT(cancel())); + this->startTimer(50); +} + + +Form::~Form() +{ + delete ui; +} + + +void Form::on_pbNextWave_clicked() +{ + core.nextWave(); +} + + +void Form::towerBuild(int index) +{ + buildTowerId = index-1; + if (index == 0) buildTowerId = -9; + graphics->setBuilding(true); + setCursor(Qt::CrossCursor); +} + + +void Form::add_tow(QPoint pnt) +{ + if (buildTowerId == -1) return; + if (buildTowerId == -9) {core.adtowers->delTower(pnt); return;} + core.adtowers->addTower(0,buildTowerId,pnt); +} + + +//void Form::del_tow(QPoint pnt) +//{ +// core.adtowers->delTower(pnt); +//} + + +void Form::cancel() +{ + buildTowerId = -1; + graphics->setBuilding(false); + setCursor(Qt::ArrowCursor); +} + + +void Form::timerEvent(QTimerEvent *) +{ + QString twstr; + QString alstr; + if (!cur_tow.isNull()) + { + if (core.addata->curTowers.contains(cur_tow)) + { + TowerType tw = core.addata->curTowers.value(cur_tow); + twstr = "\n\n " + tw.src->name; + if (tw.isBilding) twstr += tr("\n build %1/%2").arg(tw.build).arg(tw.src->buildTime); + twstr += tr("\n player %1\n exp %2").arg(tw.PlayerId).arg(tw.experience); + twstr += tr("\n reload %1/%2\n alien %3").arg(tw.reload).arg(tw.src->reload).arg(tw.aim); + twstr += tr("\n cost %1\n radius %2").arg(tw.src->cost).arg(tw.src->radius); + } else { + cur_tow = QPoint(); + } + } + if (cur_al != -1) + { + if (core.addata->aliens->curAliens.contains(cur_al)) + { + AlienType al = core.addata->aliens->curAliens.value(cur_al); + alstr = "\n\n " + al.src->name; + if (al.src->isFlying) alstr += tr("\n flying"); + else alstr += tr("\n ground"); + alstr += tr("\n health %1/%2").arg(al.health).arg(al.src->health); + alstr += tr("\n speed %1/%2").arg(al.speed).arg(al.src->speed); + alstr += tr("\n armor %1\n regeneration %2").arg(al.src->armor).arg(al.src->regeneration); + alstr += tr("\n prise %1").arg(al.src->prise); + } else { + cur_al = -1; + } + } + if (!core.addata->aliens->curAliens.contains(cur_al)) cur_al = -1; + ui->lbInfo->setText(tr(" money %1 \n missing %2").arg(core.addata->players.at(0)->money).arg(core.addata->aliens->missingAliens()) + + tr("\n aliens %1").arg(core.addata->aliens->curAliens.size()) + + tr("\n towers %1").arg(core.addata->curTowers.size()) + + tr("\n splashes %1").arg(core.addata->curSplashes.size()) + + twstr + alstr); +} + + +void Form::towerSelected(QPoint id) +{ + cur_tow = id; +} + + +void Form::alienSelected(int id) +{ + cur_al = id; +} + + diff --git a/form.h b/form.h new file mode 100644 index 0000000..9ce9039 --- /dev/null +++ b/form.h @@ -0,0 +1,41 @@ +#ifndef FORM_H +#define FORM_H + +#include +#include "adcore.h" +#include "ad_graphics.h" + +namespace Ui { + class Form; +} + +class Form : public QWidget +{ + Q_OBJECT + +public: + explicit Form(QWidget *parent = 0); + ~Form(); + +private slots: + void on_pbNextWave_clicked(); + void towerBuild(int index); + void add_tow(QPoint pnt); + //void del_tow(QPoint pnt); + void cancel(); + void towerSelected(QPoint id); + void alienSelected(int id); + +private: + void timerEvent(QTimerEvent *); + + Ui::Form *ui; + int buildTowerId; + AD_Core core; + AD_Graphics * graphics; + QPoint cur_tow; + int cur_al; + //QList towerButtons; +}; + +#endif // FORM_H diff --git a/form.ui b/form.ui new file mode 100644 index 0000000..9357572 --- /dev/null +++ b/form.ui @@ -0,0 +1,107 @@ + + + Form + + + + 0 + 0 + 700 + 448 + + + + Form + + + + 0 + + + + + + 2 + + + + + + + + + + TextLabel + + + Qt::PlainText + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + + true + + + + + 0 + 0 + 135 + 194 + + + + + 0 + + + + + Towers + + + + 0 + + + 0 + + + + + + + + + + + + Next wave + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + diff --git a/game_data.cpp b/game_data.cpp index 8b2c801..b35fad6 100644 --- a/game_data.cpp +++ b/game_data.cpp @@ -5,5 +5,5 @@ Game_Data::Game_Data(Map *mapp, QObject *parent) : { map = mapp; curWave = 0; - missingAliens = 0; + aliens = new Aliens(map); } diff --git a/game_data.h b/game_data.h index 9fcd96d..8cf2c1c 100644 --- a/game_data.h +++ b/game_data.h @@ -2,29 +2,32 @@ #define GAME_DATA_H #include "player.h" +#include "aliens.h" #include "base_types.h" #include "map.h" +#include class Game_Data : public QObject { Q_OBJECT public: explicit Game_Data(Map * map, QObject *parent = 0); - QList srcAliens; - QHash curAliens; QList srcSplashes; - QList srTriggers; + QList srcTriggers; QHash curSplashes; QHash curTowers; QList waves; QList players; QList curAnimations; Map * map; + Aliens * aliens; int curWave; int missingAliens; + signals: public slots: + }; #endif // GAME_DATA_H diff --git a/loader.cpp b/loader.cpp index 108bdb2..90d5359 100644 --- a/loader.cpp +++ b/loader.cpp @@ -1,12 +1,10 @@ #include "loader.h" Loader::Loader(QObject *parent) : - QObject(parent) + QObject(parent) { doc = new QDomDocument("data"); - //create(); qDebug() << createMapExample(); - //load(); } @@ -266,11 +264,10 @@ Game_Data * Loader::loadlevel(int id) qDebug() << tr("Loading level %1 ...").arg(levels.value(id).name); tbMap tbm = maps.value(levels.value(id).mapId); Map * map = new Map(tbm.data,tbm.size,tbm.name,tbm.maxPlayers,tbm.imgType); - //int maxPlayers = map->maxPlayers(); Game_Data * gd = new Game_Data(map); for (int i=0; imaxPlayers(); i++) { - Player * pl = new Player(0); + Player * pl = new Player(i); gd->players.append(pl); } qDebug() << gd->players.size(); @@ -312,10 +309,9 @@ Game_Data * Loader::loadlevel(int id) wt.counts.prepend(tbwpt.count); } gd->waves.prepend(wt); + qDebug() << tr("load wave #%1 prise=%2 timeout=%3").arg(i+1).arg(wt.prise).arg(wt.timeout) << wt.types << wt.counts; wplist.clear(); } - for (int i=0; iwaves.size(); ++i) - qDebug() << tr("load wave #%1 prise=%2 timeout=%3").arg(i+1).arg(gd->waves.at(i).prise).arg(gd->waves.at(i).timeout) << gd->waves.at(i).types << gd->waves.at(i).counts; wollist.clear(); for (int i=0; isrcAliens.append(salt); + gd->aliens->srcAliens.append(salt); qDebug() << tr("Alien %1 #%2 ").arg(salt.name).arg(i); } QList tollist = levTowers.values(id); @@ -374,11 +370,21 @@ Game_Data * Loader::loadlevel(int id) for (int i=0; i; for (int k=0; kappend(new QImage(animations.value(animIds.at(i)) + .pathes.at(k))); gd->curAnimations.append(anim); } + for (int i=0; ialiens->srcAliens.size(); i++) + gd->aliens->srcAliens[i].images = gd->curAnimations.at(gd->aliens->srcAliens.at(i).imgType).images; + for (int i=0; iplayers.size(); i++) + for (int j=0; jplayers.at(i)->srcTowers.size(); j++) + { + gd->players[i]->srcTowers[j].image = gd->curAnimations.at(gd->players.at(i)->srcTowers.at(j).imgType).images->at(0); + for (int k=0; kplayers.at(i)->srcTowers.at(j).splashes.size(); k++) + gd->players[i]->srcTowers[j].splashes[k].images = gd->curAnimations.at(gd->players.at(i)->srcTowers.at(j).splashes.at(k).imgType).images; + } return gd; } @@ -436,13 +442,14 @@ void Loader::fillTowerChains(QList * animIds, QList * chlist, } } + QString Loader::createMapExample() { QVector < QVector < int > > Cells; - Cells.resize(30); + Cells.resize(25); for (int i=0; i #include #include -#include "game_data.h" +#include "adcore.h" struct tbHeader { @@ -111,7 +111,6 @@ struct tbWaveOnLevel struct tbMap : tbHeader { unsigned int maxPlayers; - //QVector < QVector > cells; QByteArray data; QSize size; int imgType; @@ -142,7 +141,6 @@ class Loader : public QObject Q_OBJECT public: explicit Loader(QObject *parent = 0); - //void create(); void save(QString filename); void load(QString filename); Game_Data * loadlevel(int id); diff --git a/main.cpp b/main.cpp index 8b8bde4..6988e5f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,18 +1,15 @@ #include #include -#include "adcore.h" -#include "ad_graphics.h" +#include "form.h" +#include "ui_form.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); - AD_Core core; - AD_Graphics graphics(&core); - graphics.resize(750,450); - //QTimer * timer = new QTimer(); - //QObject::connect(timer,SIGNAL(timeout()),&core,SLOT(next())); - //timer->start(100); + a.setStyle("cleanlooks"); + Form f; + f.show(); return a.exec(); } diff --git a/map.cpp b/map.cpp index a494b15..3a8db31 100644 --- a/map.cpp +++ b/map.cpp @@ -39,28 +39,6 @@ Map::Map(QByteArray data, QSize size, QString name, int maxPlayers, int image, Q } -//void Map::ReadSettings() -//{ -// QString str; -// SettingsReader * sreader = new SettingsReader("map.conf"); -// str = sreader->getValue("cells path"); -// maxPlayers = sreader->getValue("max players","1").toInt(); -// QFile * f = new QFile(str); -// f->open(QIODevice::ReadOnly); -// QDataStream s(f); -// s >> Cells; -// if (Cells.size()>0) -// { -// mapsize.setWidth(Cells.size()); -// mapsize.setHeight(Cells[0].size()); -// //qDebug() << "Map size" << mapsize; -// //printMap(); -// } else qDebug("invalid size"); -// delete f; -// delete sreader; -//} - - void Map::printMap() { for (int i=0; i players) return false; QRect r(QPoint(),mapSize); if (!r.contains(pos) || - !r.contains(pos+QPoint(1,0)) || - !r.contains(pos+QPoint(0,1)) || - !r.contains(pos+QPoint(1,1))) return false; + !r.contains(pos-QPoint(1,0)) || + !r.contains(pos-QPoint(0,1)) || + !r.contains(pos-QPoint(1,1))) return false; if (Cells[pos.x()][pos.y()]==Player+playerId && - Cells[pos.x()+1][pos.y()]==Player+playerId && - Cells[pos.x()][pos.y()+1]==Player+playerId && - Cells[pos.x()+1][pos.y()+1]==Player+playerId) + Cells[pos.x()-1][pos.y()]==Player+playerId && + Cells[pos.x()][pos.y()-1]==Player+playerId && + Cells[pos.x()-1][pos.y()-1]==Player+playerId) return isReachable(playerId,pos); if ((Cells[pos.x()][pos.y()]==AlienPath+Player+playerId || Cells[pos.x()][pos.y()]==Player+playerId) && - (Cells[pos.x()+1][pos.y()]==AlienPath+Player+playerId || Cells[pos.x()+1][pos.y()]==Player+playerId) && - (Cells[pos.x()][pos.y()+1]==AlienPath+Player+playerId || Cells[pos.x()][pos.y()+1]==Player+playerId) && - (Cells[pos.x()+1][pos.y()+1]==AlienPath+Player+playerId || Cells[pos.x()+1][pos.y()+1]==Player+playerId)) + (Cells[pos.x()-1][pos.y()]==AlienPath+Player+playerId || Cells[pos.x()-1][pos.y()]==Player+playerId) && + (Cells[pos.x()][pos.y()-1]==AlienPath+Player+playerId || Cells[pos.x()][pos.y()-1]==Player+playerId) && + (Cells[pos.x()-1][pos.y()-1]==AlienPath+Player+playerId || Cells[pos.x()-1][pos.y()-1]==Player+playerId)) { if (!isReachable(playerId,pos)) return false; bool * pathOK; @@ -144,59 +121,12 @@ bool Map::addTowerOnMap(int playerId, QPoint pos) } -//void Map::CreateMapExample() -//{ -// Cells.resize(16); -// for (int i=0; iopen(QIODevice::ReadWrite); -// f->resize(0); -// QByteArray ba; -// QDataStream tmps(&ba,QIODevice::ReadWrite); -// tmps << Cells; -// ba = qCompress(ba); -// ba = ba.toBase64(); -// QString str(ba); -//// QDataStream s(f); -//// s << ba; -// f->write(str.toUtf8()); -// f->close(); -// delete f; -//} - - QVector Map::createPath(QPoint start, QPoint finish) { QPointF tp; QVector srcPath; QVector tmpPath; QVector path; - //PathIndex = 1; if (Cells[start.x()][start.y()] < 0) { qDebug("ERROR invalid start"); @@ -216,7 +146,6 @@ QVector Map::createPath(QPoint start, QPoint finish) tmpPath.push_back(QPointF(srcPath.at(i))); } srcPath.clear(); - //qDebug() << tmpPath.size(); if (!tmpPath.isEmpty()) { for (int j=0; j<4; j++) @@ -226,7 +155,6 @@ QVector Map::createPath(QPoint start, QPoint finish) path.push_back(tp); for (int i = 0; i < tmpPath.size() - 1; i++) { - //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); @@ -319,7 +247,6 @@ int Map::waveTrace(QPoint start, QPoint finish) step++; } qDebug() << "trace false"; - //qDebug() << TmpCells; return -1; } diff --git a/map.h b/map.h index 2fc4dea..e8a3806 100644 --- a/map.h +++ b/map.h @@ -5,6 +5,8 @@ // Maximum map is 65530 x 65530 because qHash may be repeat and towers has same Id +/// TODO: fix cell type - must be a union of int and bit flags + class Map : public QObject { Q_OBJECT @@ -43,10 +45,10 @@ public: void removeAliensPath(); QVector createPath(QPoint start, QPoint finish); QRect rect() const {return QRect(QPoint(),mapSize);} -// QString name() const {return mapName;} - QList starts() const {return Starts;} - QList finishs() const {return Finishs;} - QVector < QVector > cells() const {return Cells;} + const QString &name() const {return mapName;} + const QList &starts() const {return Starts;} + const QList &finishs() const {return Finishs;} + const QVector < QVector > &cells() const {return Cells;} int image() const {return imageType;} int maxPlayers() const {return players;} signals: @@ -60,16 +62,13 @@ private: int players; int imageType; QVector < QVector > Cells; - QList Starts; + QVector < QVector > TmpCells; + QList Starts; QList Finishs; - QVector < QVector > TmpCells; - QVector invWaveTrace(QPoint finish, int cnt); int waveTrace(QPoint start, QPoint finish); bool isReachable(int playerId, QPoint pos); - //void CreateMapExample(); - //void ReadSettings(); }; #endif // MAP_H diff --git a/player.cpp b/player.cpp index 74f0972..4ae812f 100644 --- a/player.cpp +++ b/player.cpp @@ -4,6 +4,6 @@ Player::Player(int Id, QObject *parent) : QObject(parent) { playerId = Id; - money = 100; + money = 50; selectAlienId = -1; } diff --git a/player.h b/player.h index fc8cbaf..7e2884c 100644 --- a/player.h +++ b/player.h @@ -8,7 +8,7 @@ class Player : public QObject Q_OBJECT public: explicit Player(int Id, QObject *parent = 0); - unsigned long int money; + long long int money; unsigned long int score; int selectAlienId; QList srcTowers; diff --git a/splashes.cpp b/splashes.cpp index e9478d5..32e0c1a 100644 --- a/splashes.cpp +++ b/splashes.cpp @@ -2,163 +2,206 @@ #include Splashes::Splashes(Game_Data *gd, QObject *parent) : - QObject(parent) + QObject(parent) { gameData = gd; nextId = 1; } -void Splashes::addSplash(int player, int srcTower, int srcId, QPointF pos, QPointF dest, int aim, QPoint curTower) +void Splashes::addSplash(int player, srcSplashType *src, QPointF pos, QPointF dest, int aim) { - srcSplashType src = gameData->players.at(player)->srcTowers.at(srcTower).splashes.at(srcId); SplashType spl; spl.id = nextId; - spl.TowerId = curTower; - spl.srcTower = srcTower; + spl.towerId = QPoint(-1,-1); + spl.srcTow = 0; spl.PlayerId = player; + spl.AlienId = 0; + spl.pos = pos; + spl.destination = dest; + spl.AlienId = aim; + spl.angle = 180.0f*(- std::atan2(spl.pos.x() - spl.destination.x(),spl.pos.y() - spl.destination.y()))/M_PI; + spl.life = 0; + spl.src = src; + addSplash(&spl); +} + + +void Splashes::addSplash(const TowerType &tower) +{ + SplashType spl; + spl.src = &(tower.src->splashes[0]); + spl.id = nextId; + spl.towerId = tower.pos; + spl.PlayerId = tower.PlayerId; + spl.AlienId = tower.aim; + spl.srcTow = tower.src; + spl.pos = QPointF(tower.pos); + if (gameData->aliens->curAliens.contains(spl.AlienId)) + spl.destination = gameData->aliens->curAliens.value(spl.AlienId).pos+QPointF(0.5,0.5); + else {qCritical("aim alien not exists(!)");return;} + spl.angle = 180.0f*(- std::atan2(spl.pos.x() - spl.destination.x(),spl.pos.y() - spl.destination.y()))/M_PI; + spl.life = 0; + addSplash(&spl); +} + + +void Splashes::addSplash(srcTowerType *srctower, int id, QPointF pos, QPointF dest, int aim, QPoint towerId) +{ + SplashType spl; + if (srctower != 0) + spl.src = &(srctower->splashes[id]); + else spl.src = &(gameData->srcSplashes[id]); + spl.id = nextId; + spl.towerId = towerId; + spl.PlayerId = srctower->PlayerId; spl.AlienId = aim; spl.pos = pos; spl.destination = dest; - if (src.autoControl) - { - if (gameData->curAliens.contains(aim)) - spl.destination = gameData->curAliens.value(aim).pos+QPointF(0.5,0.5); - } spl.angle = 180.0f*(- std::atan2(spl.pos.x() - spl.destination.x(),spl.pos.y() - spl.destination.y()))/M_PI; - spl.imgType = src.imgType; - spl.animIndex = 0; spl.life = 0; - spl.src = srcId; - srcTriggerType trig; - for(int i=0; isrc->triggerIndexes.size(); i++) { TriggerOnDest tod; TriggerOnAlien toa; TriggerOnTimer tot; - trig = gameData->players.at(player)->srcTowers.at(srcTower).triggers.at(src.triggerIndexes.at(i)); - switch (trig.type) + if (spl->srcTow != 0) + trig = &(spl->srcTow->triggers[spl->src->triggerIndexes.at(i)]); + else {qDebug("no parent tower"); trig = &(gameData->srcTriggers[spl->src->triggerIndexes.at(i)]);} + switch (trig->type) { case srcTriggerType::onDestination : - tod.src = src.triggerIndexes.at(i); - spl.trigsOnDest.append(tod); + tod.src = trig; + spl->trigsOnDest.append(tod); break; case srcTriggerType::onAlienInRadius : - toa.src = src.triggerIndexes.at(i); - spl.trigsOnAlien.append(toa); + toa.src = trig; + spl->trigsOnAlien.append(toa); break; case srcTriggerType::onTimer : - tot.src = src.triggerIndexes.at(i); + tot.src = trig; tot.timer = 0; - spl.trigsOnTimer.append(tot); + spl->trigsOnTimer.append(tot); break; } } - gameData->curSplashes.insert(spl.id,spl); + spl->item = new ADItem(spl->id, ADItem::Splash, spl->src->images); + spl->item->setPos(spl->pos*cellSize); + spl->item->setRotation(spl->angle); + scene->addItem(spl->item); + gameData->curSplashes.insert(spl->id,*spl); nextId++; } -void Splashes::addSplash(QPoint curTowerId) -{ - if (gameData->curTowers.contains(curTowerId)) - { - TowerType tw = gameData->curTowers.value(curTowerId); - addSplash(tw.PlayerId,tw.src,0,QPointF(tw.pos+QPoint(1,1)),gameData->curAliens.value(tw.aim).pos+QPointF(0.5,0.5),tw.aim,tw.pos); - } -} - - void Splashes::update() { QList deadIndexes; - // FIXME : rewrite loop with iterators, and correct onTrigTimer() - for (int i=0; icurSplashes.size(); i++) + for (QHash::iterator + spl = gameData->curSplashes.begin(); + spl != gameData->curSplashes.end(); ++spl) { - float arctg,angl; - SplashType spl = gameData->curSplashes.values().at(i); - srcSplashType src; - // FIXME : if splash is not tower's splash, will be some trouble - src = gameData->players.at(spl.PlayerId)->srcTowers.at(spl.srcTower).splashes.at(spl.src); - spl.life++; - if (spl.life > src.lifetime) - deadIndexes.append(spl.id); - if (!spl.trigsOnTimer.isEmpty()) - { - doTriggerOnTimer(&spl,&deadIndexes); - } - if (src.autoControl) + spl->life++; + if (spl->life > spl->src->lifetime) + deadIndexes.append(spl->id); + if (!spl->trigsOnTimer.isEmpty()) + doTriggerOnTimer(spl,&deadIndexes); + float arctg; + float speed = spl->src->speed; + float speed2 = speed*speed; + if (spl->src->autoControl && spl->AlienId != -1) { bool badAl = false; - if (gameData->curAliens.contains(spl.AlienId)) - spl.destination = gameData->curAliens.value(spl.AlienId).pos+QPointF(0.5,0.5); - else + if (gameData->aliens->curAliens.contains(spl->AlienId)) + spl->destination = gameData->aliens->curAliens.value(spl->AlienId).pos+QPointF(0.5,0.5); + else { badAl = true; - if (distance2(spl.pos, spl.destination) < src.speed*src.speed) + spl->AlienId = -1; + } + if (distance2(spl->pos, spl->destination) < speed2) { if (badAl) - deadIndexes.push_back(spl.id); - spl.pos = spl.destination; + deadIndexes.push_back(spl->id); + spl->pos = spl->destination; doTriggerOnDest(spl,&deadIndexes,badAl); } - arctg = std::atan2(spl.pos.x() - spl.destination.x(),spl.pos.y() - spl.destination.y()); - angl = 180.0f*(-arctg)/M_PI; - spl.angle = angl; - spl.pos.setX(spl.pos.x() - -src.speed*std::sin(arctg)); - spl.pos.setY(spl.pos.y() - -src.speed*std::cos(arctg)); + arctg = std::atan2(spl->pos.x() - spl->destination.x(),spl->pos.y() - spl->destination.y()); + spl->angle = 180.0f*(-arctg)/M_PI; } else { - if (!gameData->map->rect().contains(spl.pos.toPoint())) - deadIndexes.push_back(spl.id); - if (!spl.trigsOnDest.isEmpty()) + if (!gameData->map->rect().contains(spl->pos.toPoint())) + deadIndexes.push_back(spl->id); + if (!spl->trigsOnDest.isEmpty()) { - if (distance2(spl.pos, spl.destination) < src.speed*src.speed) + if (distance2(spl->pos, spl->destination) < speed2) { - spl.pos = spl.destination; + spl->pos = spl->destination; doTriggerOnDest(spl,&deadIndexes,true); } } - spl.pos.setX(spl.pos.x() - -src.speed*std::sin(-spl.angle*M_PI/180.f)); - spl.pos.setY(spl.pos.y() - -src.speed*std::cos(-spl.angle*M_PI/180.f)); + arctg = -spl->angle*M_PI/180.f; } - // TODO: smooth splash rotate - gameData->curSplashes.insert(spl.id,spl); + spl->pos.rx() -= speed*std::sin(arctg); + spl->pos.ry() -= speed*std::cos(arctg); + /// TODO: smooth splash rotate + spl->item->setPos(spl->pos*cellSize); + spl->item->setRotation(spl->angle); + if (speed == 0) spl->item->next(1.f); + else spl->item->next(speed*cellSize); } for (int j=0; j::iterator j = dead.begin(); + j != dead.end(); ++j) + emit killAlien(j->player,j->id); + dead.clear(); } void Splashes::delSplash(int Id) { + scene->removeItem(gameData->curSplashes[Id].item); + delete gameData->curSplashes[Id].item; gameData->curSplashes.remove(Id); } -void Splashes::doTriggerOnTimer(SplashType *spl, QList *deadIndexes) +/// FIXME : correct onTrigTimer() +void Splashes::doTriggerOnTimer(QHash::iterator + spl, QList *deadIndexes) { - for (int k=0; ktrigsOnTimer.size(); k++) + for(QList::iterator i = spl->trigsOnTimer.begin(); + i != spl->trigsOnTimer.end(); ++i) { - srcTriggerType strig = gameData->players. - at(spl->PlayerId)->srcTowers. - at(spl->srcTower).triggers. - at(spl->trigsOnTimer.at(k).src); - spl->trigsOnTimer[k].timer++; - if (spl->trigsOnTimer.at(k).timer >= strig.timer) + srcTriggerType * strig = i->src; + i->timer++; + if (i->timer >= strig->timer) { - spl->trigsOnTimer[k].timer = 0; - if (strig.delParent) deadIndexes->append(spl->id); - if (strig.count > 0 && strig.childId > 0) + /// TODO : add damage and other effects + i->timer = 0; + if (strig->delParent) deadIndexes->append(spl->id); + if (strig->count > 0 && strig->childId > 0) { - for (int j=0; jchildAim) + { + /// TODO : othet aim types + case srcTriggerType::parentAim : aim = spl->AlienId; break; + case srcTriggerType::noAim : aim = -1; break; + default : aim = -1; + } + for (int j=0; jcount; ++j) { - // TODO: randomRadiusPos - addSplash(spl->PlayerId,spl->srcTower,strig.childId, - spl->pos,spl->destination,-1,spl->TowerId); + /// TODO: randomRadiusPos + addSplash(spl->srcTow, strig->childId, spl->pos, spl->destination, aim, spl->towerId); } } } @@ -166,50 +209,54 @@ void Splashes::doTriggerOnTimer(SplashType *spl, QList *deadIndexes) } -void Splashes::doTriggerOnDest(const SplashType &spl, QList *deadIndexes, +void Splashes::doTriggerOnDest(QHash::iterator + spl, QList *deadIndexes, bool badAl) { - for (int k=0; ktrigsOnDest) { - // TODO: aims for childs - srcTriggerType strig = gameData->players. - at(spl.PlayerId)->srcTowers. - at(spl.srcTower).triggers. - at(spl.trigsOnDest.at(k).src); - if (strig.delParent) deadIndexes->append(spl.id); - if (strig.count > 0 && strig.childId > 0) + /// TODO: different aims for childs + if (t.src->delParent) deadIndexes->append(spl->id); + int aim; + switch(t.src->childAim) { - for (int j=0; jAlienId; break; + /// TODO : other aim types + case srcTriggerType::noAim : aim = -1; break; + default : aim = -1; + } + if (t.src->count > 0 && t.src->childId > 0) + { + for (int j=0; jcount; ++j) { - // TODO: randomRadiusPos - addSplash(spl.PlayerId,spl.srcTower,strig.childId, - spl.pos,spl.destination,-1,spl.TowerId); + /// TODO: randomRadiusPos + addSplash(spl->srcTow,t.src->childId, + spl->pos,spl->destination,aim,spl->towerId); } } - if (strig.damage > 0) + float dmg = t.src->damage; + float rad = t.src->radius; + if (dmg > 0) { - if (!(strig.radius > 0)) + float hh; + if (!(rad > 0)) { if (!badAl) { - float hh = gameData->curAliens[spl.AlienId].health -= strig.damage; - if (hh < 0) - gameData->curAliens.remove(spl.AlienId); + hh = gameData->aliens->curAliens[spl->AlienId].health -= dmg; + if (hh <= 0) dead.insert(spl->AlienId, DeadAlienIndex(spl->AlienId, spl->PlayerId)); } } else { - QList dead; for (QHash::iterator - i = gameData->curAliens.begin(); - i != gameData->curAliens.end(); ++i) + i = gameData->aliens->curAliens.begin(); + i != gameData->aliens->curAliens.end(); ++i) { - if (distance2(spl.pos, (*i).pos) < strig.radius*strig.radius) + if (distance2(spl->pos, i->pos) < rad*rad) { - (*i).health -= strig.damage; - if ((*i).health < 0) dead.append((*i).id); + hh = i->health -= dmg; + if (hh <= 0) dead.insert(i->id, DeadAlienIndex(i->id, spl->PlayerId)); } } - for (int i=0; icurAliens.remove(dead.at(i)); } } } diff --git a/splashes.h b/splashes.h index c5e24b2..31392f3 100644 --- a/splashes.h +++ b/splashes.h @@ -5,24 +5,41 @@ class Splashes : public QObject { -Q_OBJECT + Q_OBJECT public: explicit Splashes(Game_Data * gd, QObject *parent = 0); - void addSplash( int player, int srcTower, int srcId, QPointF pos, QPointF dest, int aim, QPoint curTower); void delSplash(int Id); void update(); + void setScene(QGraphicsScene * scene_) {scene = scene_;} + signals: + /// TODO : fix killalien - new function for adding alien in kill list and del aliens in finish update + void killAlien(int playerId, int alienId); public slots: - void addSplash(QPoint towerId); + void addSplash(int player, srcSplashType * src, QPointF pos, QPointF dest, int aim = -1); + void addSplash(const TowerType &tower); + void addSplash(srcTowerType * srctower, int id, QPointF pos, QPointF dest, int aim = -1, QPoint towerId = QPoint(-1,-1)); + void addSplash(SplashType *spl); private: + + QGraphicsScene * scene; Game_Data *gameData; int nextId; + struct DeadAlienIndex + { + DeadAlienIndex(int i, int p) {id = i; player = p;} + int player; + int id; + }; + QHash dead; - void doTriggerOnDest(const SplashType &spl, QList * deadIndexes, + void doTriggerOnDest(QHash::iterator + spl, QList * deadIndexes, bool badAl); - void doTriggerOnTimer(SplashType * spl, QList * deadIndexes); + void doTriggerOnTimer(QHash::iterator + spl, QList * deadIndexes); }; #endif // SPLASHES_H diff --git a/touch_butt.cpp b/touch_butt.cpp new file mode 100644 index 0000000..0f81cf1 --- /dev/null +++ b/touch_butt.cpp @@ -0,0 +1,108 @@ +#include "touch_butt.h" + +touch_butt::touch_butt(int butt_index, QString caption, QColor colr, QColor colg, QColor colw, QColor colp, QPushButton * parent) : QPushButton(parent) +{ + cr = colr; cg = colg; cw = colw; cp = colp; + aim_col = this->palette().button().color(); + next_col = aim_col; + index = butt_index; + this->setText(caption); + connect(this,SIGNAL(pressed()),SLOT(butt_push())); + connect(this,SIGNAL(clicked()),SLOT(butt_click())); + connect(this,SIGNAL(toggled(bool)),SLOT(butt_toggle(bool))); + connect(this,SIGNAL(released()),SLOT(butt_release())); + timer = new QTimer(this); + connect(timer,SIGNAL(timeout()),this,SLOT(timerDone())); + timer->start(10); + setWhite(); + is_push = false; +} + + +void touch_butt::setColor(QColor colr, QColor colg, QColor colw, QColor colp) +{ + cr = colr; cg = colg; cw = colw; cp = colp; + setWhite(); +} + + +void touch_butt::timerDone() +{ + float r = this->palette().button().color().redF(); + float g = this->palette().button().color().greenF(); + float b = this->palette().button().color().blueF(); + r += (aim_col.redF()-r)/9.0f; + g += (aim_col.greenF()-g)/9.0f; + b += (aim_col.blueF()-b)/9.0f; + QPalette pal; + pal.setColor(QPalette::Button,QColor(qRound(r*255.0f),qRound(g*255.0f),qRound(b*255.0f))); + this->setPalette(pal); + if (qAbs(aim_col.redF() - r) < 0.02 && qAbs(aim_col.greenF() - g) < 0.02 && qAbs(aim_col.blueF() - b) < 0.02) + { + pal.setColor(QPalette::Button,aim_col); + this->setPalette(pal); + aim_col = next_col; + } +} + + +void touch_butt::butt_click() +{ + is_push = false; + //setWhite(); + //timerDone(); + emit touch_click(index); +} + + +void touch_butt::butt_push() +{ + is_push = true; + setPush(); + emit touch_push(index); +} + + +void touch_butt::butt_release() +{ + is_push = false; + setWhite(); + timerDone(); +} + + +void touch_butt::butt_toggle(bool checked) +{ + if (!checked) { + setWhite(); + } + emit touch_toggle(index,checked); +} + + +void touch_butt::setRed() +{ + next_col = cr; + timerDone(); +} + + +void touch_butt::setGreen() +{ + //if (!is_push) + next_col = cg; +} + + +void touch_butt::setWhite() +{ + //if (!is_push) + next_col = cw; +} + + +void touch_butt::setPush() +{ + next_col = cp; + timerDone(); +} diff --git a/touch_butt.h b/touch_butt.h new file mode 100644 index 0000000..5f2971b --- /dev/null +++ b/touch_butt.h @@ -0,0 +1,40 @@ +#ifndef TOUCH_BUTT_H +#define TOUCH_BUTT_H + +#include +#include +#include +#include +#include + +class touch_butt : public QPushButton +{ + Q_OBJECT +public: + touch_butt(int butt_index, QString caption, QColor colr, QColor colg, QColor colw, QColor colp, QPushButton * parent = 0); + int index; + void setRed(); + void setGreen(); + void setWhite(); + void setColor(QColor colr, QColor colg, QColor colw, QColor colp); + QColor color() const {return next_col;} +public slots: + void butt_click(); + void butt_toggle(bool checked); +private: + QTimer * timer; + QColor aim_col, next_col; + QColor cr,cg,cw,cp; + bool is_push; + void setPush(); +private slots: + void butt_push(); + void butt_release(); + void timerDone(); +signals: + void touch_push(int index); + void touch_click(int index); + void touch_toggle(int index, bool checked); +}; + +#endif // TOUCH_BUTT_H diff --git a/touchbuttframe.cpp b/touchbuttframe.cpp new file mode 100644 index 0000000..8a77ca5 --- /dev/null +++ b/touchbuttframe.cpp @@ -0,0 +1,67 @@ +#include "touchbuttframe.h" + +TouchButtFrame::TouchButtFrame(QWidget *parent, QColor cr, QColor cg, QColor cw, QColor cp, Orientation orientation) : + QFrame(parent) +{ + colr = cr; colg = cg; colw = cw; colp = cp; + setFrameShape(QFrame::StyledPanel);; + if (orientation == Horizontal) lay = new QHBoxLayout(); + else lay = new QVBoxLayout(); + count = 0; + +} + +void TouchButtFrame::addButton(QString caption) +{ + touch_butt * butt = new touch_butt(count,caption,colr,colg,colw,colp); + butt->setMinimumHeight(50); + butt->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum); + butt->setCheckable(true); + butt->setAutoExclusive(true); + butt->setAutoFillBackground(true); + if (lay->isEmpty()) butt->setChecked(true); + connect(butt,SIGNAL(touch_click(int)),SLOT(butt_click(int))); + connect(butt,SIGNAL(touch_toggle(int,bool)),SLOT(butt_toggle(int,bool))); + lay->addWidget(butt); + this->setLayout(lay); + count++; +} + + +void TouchButtFrame::butt_click(int index) +{ + //qDebug() << "button" << index << "clicked!"; + //if (((touch_butt*)lay->itemAt(index)->widget())->isChecked()) + //((touch_butt*)lay->itemAt(index)->widget())->setGreen(); + emit click(index); +} + + +void TouchButtFrame::butt_toggle(int index,bool checked) +{ + //if (((touch_butt*)lay->itemAt(index)->widget())->isChecked()) + //qDebug() << "button" << index << "toggled" << checked; + emit toggle(index,checked); +} + + + +void TouchButtFrame::resetColors() +{ + for (int i=0; isetColor(colr,colg,colw,colp); + } +} + + +int TouchButtFrame::buttCount() +{ + return count; +} + + +touch_butt * TouchButtFrame::Button(int index) +{ + return ((touch_butt*)lay->itemAt(index)->widget()); +} diff --git a/touchbuttframe.h b/touchbuttframe.h new file mode 100644 index 0000000..97b9350 --- /dev/null +++ b/touchbuttframe.h @@ -0,0 +1,51 @@ +#ifndef TOUCHBUTTFRAME_H +#define TOUCHBUTTFRAME_H + +#include +#include +#include +#include "touch_butt.h" + + +class TouchButtFrame : public QFrame +{ + Q_OBJECT + Q_PROPERTY(QColor ColorYes READ colorYes WRITE setColorYes) + Q_PROPERTY(QColor ColorNo READ colorNo WRITE setColorNo) + Q_PROPERTY(QColor ColorDefault READ colorDefault WRITE setColorDefault) + Q_PROPERTY(QColor ColorPush READ colorPush WRITE setColorPush) +public: + enum Orientation { + Horizontal = 0, + Vertical = 1 + }; + TouchButtFrame(QWidget * parent = 0, + QColor red = Qt::red, QColor green = Qt::green, QColor white = Qt::white, QColor push = Qt::darkYellow, + Orientation orientation = Vertical); + void addButton(QString caption); + int buttCount(); + QColor colorYes() const {return colg;} + QColor colorNo() const {return colr;} + QColor colorDefault() const {return colw;} + QColor colorPush() const {return colp;} + void setColorYes(QColor ColorYes) {colg = ColorYes; resetColors();} + void setColorNo(QColor ColorNo) {colr = ColorNo; resetColors();} + void setColorDefault(QColor ColorDefault) {colw = ColorDefault; resetColors();} + void setColorPush(QColor ColorPush) {colp = ColorPush; resetColors();} + touch_butt * Button(int index); +public slots: + void set_green(int index) {Button(index)->setGreen();} +private slots: + void butt_click(int index); + void butt_toggle(int index, bool checked); +private: + QColor colr,colg,colw,colp; + QLayout * lay; + int count; + void resetColors(); +signals: + void click(int index); + void toggle(int index, bool checked); +}; + +#endif // TOUCHBUTTFRAME_H diff --git a/towers.cpp b/towers.cpp index 87918f8..7034574 100644 --- a/towers.cpp +++ b/towers.cpp @@ -3,45 +3,51 @@ #include Towers::Towers(Game_Data *gd, QObject *parent) : - QObject(parent) + QObject(parent) { gameData = gd; } bool Towers::addTower(int playerId, int srcId, QPoint pos) { - //QByteArray ar; - //ar << pos; if (playerId < 0 || playerId >= gameData->players.size()) { qCritical("ERROR out of players range"); return false; } - if (srcId < 0 || srcId >= gameData->players.at(playerId)->srcTowers.size()) + Player * p = gameData->players.at(playerId); + if (srcId < 0 || srcId >= p->srcTowers.size()) { qCritical("ERROR out of towers range"); return false; } + if (p->money < p->srcTowers.at(srcId).cost) + { + qDebug() << "not enought money!"; + return false; + } QRect alrec(pos,QSize(2,2)); - for (QHash::iterator i = gameData->curAliens.begin(); i != gameData->curAliens.end(); ++i) - if (alrec.contains((*i).pos.toPoint())) return false; + for (QHash::iterator i = gameData->aliens->curAliens.begin(); i != gameData->aliens->curAliens.end(); ++i) + if (alrec.contains(i->pos.toPoint())) return false; TowerType tw; - tw.src = srcId; - tw.imgType = gameData->players.at(playerId)->srcTowers.at(srcId).imgType; - tw.animIndex = 0; + tw.src = &(p->srcTowers[srcId]); tw.PlayerId = playerId; tw.angle = 0; tw.build = 0; tw.reload = 0; tw.aim = -1; tw.experience = 0; - //tw .oldAim = -1; + tw.isBilding = true; if (gameData->map->addTowerOnMap(playerId,pos)) { tw.pos = pos; - //qDebug() << "tw" << pos; - //gameData->map->printMap(); + tw.item = new ADItem(tw.pos,tw.src->image,QRectF(-cellSize,-cellSize,cellSize*2,cellSize*2)); + tw.item->setPos(tw.pos * cellSize); + tw.item->rotate(tw.angle); + tw.item->setOpacity(0.5); + scene->addItem(tw.item); gameData->curTowers.insert(tw.pos,tw); + p->money -= tw.src->cost; return true; } return false; @@ -50,10 +56,18 @@ bool Towers::addTower(int playerId, int srcId, QPoint pos) void Towers::delTower(QPoint pos) { - if (gameData->curTowers.contains(pos)) + QPoint p; + if (gameData->curTowers.contains(pos)) p = pos; + if (gameData->curTowers.contains(pos+QPoint(1,0))) p = pos+QPoint(1,0); + if (gameData->curTowers.contains(pos+QPoint(1,1))) p = pos+QPoint(1,1); + if (gameData->curTowers.contains(pos+QPoint(0,1))) p = pos+QPoint(0,1); + if (gameData->curTowers.contains(p)) { - gameData->map->delTowerOnMap(gameData->curTowers.value(pos).PlayerId,pos); - gameData->curTowers.remove(pos); + TowerType * t = &(gameData->curTowers[p]); + gameData->map->delTowerOnMap(t->PlayerId,p); + scene->removeItem(t->item); + delete t->item; + gameData->curTowers.remove(p); } } @@ -62,55 +76,58 @@ void Towers::update() { for (QHash::iterator i = gameData->curTowers.begin(); i != gameData->curTowers.end(); ++i) { - srcTowerType stw = gameData->players.at((*i).PlayerId)->srcTowers.at((*i).src); - if ((*i).build < stw.buildTime) + if (i->isBilding) { - (*i).build++; - //qDebug() << "building tower ..." << (*i).build; + if (i->build < i->src->buildTime) + { + i->build++; + i->item->setBarValue((float)(i->build)/i->src->buildTime); + } else { + i->isBilding = false; + i->item->hideBar(); + i->item->setOpacity(1.0); + } } else { - if (!(gameData->curAliens.contains((*i).aim))) - (*i).aim = -1; + float rad = i->src->radius; + float rad2 = rad*rad; + if (!(gameData->aliens->curAliens.contains(i->aim))) + i->aim = -1; else { - if (distance2((*i).pos,gameData->curAliens.value((*i).aim).pos) > stw.radius*stw.radius) - (*i).aim = -1; + if (distance2(i->pos,gameData->aliens->curAliens.value(i->aim).pos) > rad2) + i->aim = -1; } - if (gameData->players.at((*i).PlayerId)->selectAlienId > 0) + if (gameData->players.at(i->PlayerId)->selectAlienId > 0) { - AlienType al = gameData->curAliens.value(gameData->players.at((*i).PlayerId)->selectAlienId); - if (distance2(al.pos, (*i).pos) < stw.radius*stw.radius) (*i).aim = al.id; + AlienType al = gameData->aliens->curAliens.value(gameData->players.at(i->PlayerId)->selectAlienId); + if (distance2(al.pos, i->pos) < rad2) i->aim = al.id; } - if ((*i).aim < 0) + if (i->aim < 0) { - for (QHash::iterator j = gameData->curAliens.begin(); j != gameData->curAliens.end(); ++j) + for (QHash::iterator j = gameData->aliens->curAliens.begin(); j != gameData->aliens->curAliens.end(); ++j) { - float dist = distance2((*i).pos, (*j).pos); - //qDebug() << "dist=" << dist << "radius=" << stw.radius*stw.radius; - if (dist < stw.radius*stw.radius) (*i).aim = (*j).id; - //else (*i).aim = -1; + float dist = distance2(i->pos, (*j).pos); + if (dist < rad2) i->aim = (*j).id; } } - // TODO: calculate tower angl - if ((*i).aim > 0) + if (i->aim > 0) { - float arctg = std::atan2((*i).pos.x() - gameData->curAliens.value((*i).aim).pos.x(),(*i).pos.y() - gameData->curAliens.value((*i).aim).pos.y()); - //if (tmpdy < 0) arctg=arctg+M_PI; - (*i).angle = 180.0f*(-arctg)/M_PI; + float arctg = std::atan2(i->pos.x() - gameData->aliens->curAliens.value(i->aim).pos.x(),i->pos.y() - gameData->aliens->curAliens.value(i->aim).pos.y()); + i->angle = 180.0f*(-arctg)/M_PI; + i->item->setRotation(i->angle); } - // TODO: smooth rotate tower to aim - if ((*i).reload < stw.reload) - (*i).reload++; + /// TODO: smooth rotate tower to aim + if (i->reload < i->src->reload) + i->reload++; else { - if ((*i).aim > 0) + if (i->aim > 0) { - (*i).reload = 0; - (*i).experience += stw.expByShot; - emit shot((*i).pos); - //qDebug("shot!"); + i->reload = 0; + i->experience += i->src->expByShot; + emit shot((*i)); } } } - //qDebug() << "aim=" << (*i).aim; } } diff --git a/towers.h b/towers.h index 02bb8c4..fd8928b 100644 --- a/towers.h +++ b/towers.h @@ -11,12 +11,16 @@ public: bool addTower(int playerId, int srcId, QPoint pos); void delTower(QPoint pos); void update(); + void setScene(QGraphicsScene * scene_) {scene = scene_;} + signals: - void shot(QPoint towerId); + void shot(const TowerType &tow); + public slots: private: Game_Data * gameData; + QGraphicsScene * scene; }; #endif // TOWERS_H