From fa68b5a120951ca1da4babbabb46e73bdb431f23 Mon Sep 17 00:00:00 2001 From: blizer Date: Sat, 20 Aug 2011 10:12:16 +0400 Subject: [PATCH] fixed few critical bugs --- ad_graphics.cpp | 131 ++++----- ad_graphics.h | 44 +-- adcore.cpp | 115 ++++---- adcore.h | 29 +- aliendefender.pro.user | 400 +++++++++++++++++--------- aliens.cpp | 156 +++++----- map.cpp | 636 ++++++++++++++++++++--------------------- splashes.cpp | 387 ++++++++++++------------- splashes.h | 36 +-- 9 files changed, 1027 insertions(+), 907 deletions(-) diff --git a/ad_graphics.cpp b/ad_graphics.cpp index 46c3cd6..2c80e7d 100644 --- a/ad_graphics.cpp +++ b/ad_graphics.cpp @@ -6,65 +6,65 @@ AD_Graphics::AD_Graphics(AD_Core *adcore, QWidget *parent) : QGraphicsView(parent) { - data = adcore->addata; - core = adcore; - setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); - setAlignment(Qt::AlignLeft | Qt::AlignTop); - scene = new QGraphicsScene(); - scene->setItemIndexMethod(QGraphicsScene::NoIndex); - core->setScene(scene); - setScene(scene); - //setCacheMode(); - //setViewportUpdateMode(QGraphicsView::FullViewportUpdate); - mapimg = new QImage(data->map->rect().size()*cellSize,QImage::Format_ARGB32); - //setDragMode(RubberBandDrag); - QPainter p(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::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)); - brush.setColor(QColor(qAbs(cel+2)*345%255,(qAbs(cel+2)*721)%255,(qAbs(cel+2)*75)%255,80)); - } - p.setPen(pen); - p.setBrush(brush); - p.drawRect(i*cellSize,j*cellSize,cellSize,cellSize); - } - } - p.end(); - scene->setSceneRect(mapimg->rect()); + data = adcore->addata; + core = adcore; + setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); + setAlignment(Qt::AlignLeft | Qt::AlignTop); + scene = new QGraphicsScene(); + scene->setItemIndexMethod(QGraphicsScene::NoIndex); + core->setScene(scene); + setScene(scene); + //setCacheMode(); + //setViewportUpdateMode(QGraphicsView::FullViewportUpdate); + mapimg = new QImage(data->map->rect().size()*cellSize,QImage::Format_ARGB32); + //setDragMode(RubberBandDrag); + QPainter p(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::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)); + brush.setColor(QColor(qAbs(cel+2)*345%255,(qAbs(cel+2)*721)%255,(qAbs(cel+2)*75)%255,80)); + } + p.setPen(pen); + p.setBrush(brush); + p.drawRect(i*cellSize,j*cellSize,cellSize,cellSize); + } + } + p.end(); + scene->setSceneRect(mapimg->rect()); scene->setBackgroundBrush(*mapimg); - setCacheMode(QGraphicsView::CacheBackground); + setCacheMode(QGraphicsView::CacheBackground); resize(mapimg->size()); -// startTimer(50); + startTimer(50); } AD_Graphics::~AD_Graphics() { - delete scene; + delete scene; } void AD_Graphics::mousePressEvent(QMouseEvent *event) { - scene->clearSelection(); - if (event->button() == Qt::LeftButton) - { + scene->clearSelection(); + if (event->button() == Qt::LeftButton) + { if (m_building) emit add_tow((QPointF(event->pos())/cellSize).toPoint()); - else - { + else + { QList list = scene->items(event->pos(), Qt::IntersectsItemBoundingRect, Qt::AscendingOrder); ADItem * al = 0; foreach(QGraphicsItem* x, list) @@ -87,17 +87,18 @@ void AD_Graphics::mousePressEvent(QMouseEvent *event) emit alien_select(al->id()); return; } - } - } - if (event->button() == Qt::RightButton) - emit cancel(); + } + } + if (event->button() == Qt::RightButton) + emit cancel(); } -//void AD_Graphics::timerEvent(QTimerEvent * ) -//{ -// scene->update(); -//} +void AD_Graphics::timerEvent(QTimerEvent * ) +{ + if (!core->isWaveEnd()) + scene->update(); +} void AD_Graphics::drawBackground(QPainter * p, const QRectF & ) @@ -108,16 +109,16 @@ void AD_Graphics::drawBackground(QPainter * p, const QRectF & ) void AD_Graphics::drawForeground(QPainter *p, const QRectF &rect) { - 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); - } - } + 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 7e30aed..d69b67e 100644 --- a/ad_graphics.h +++ b/ad_graphics.h @@ -10,44 +10,44 @@ class AD_Graphics: public QGraphicsView { - Q_OBJECT - Q_PROPERTY(bool building READ building WRITE setBuilding) + Q_OBJECT + Q_PROPERTY(bool building READ building WRITE setBuilding) public: - explicit AD_Graphics(AD_Core * core, QWidget * parent = 0); - ~AD_Graphics(); + explicit AD_Graphics(AD_Core * core, QWidget * parent = 0); + ~AD_Graphics(); - bool building() const - { - return m_building; - } + bool building() const + { + return m_building; + } public slots: - void setBuilding(bool arg) - { - m_building = arg; - } + void setBuilding(bool arg) + { + m_building = arg; + } private: void drawBackground(QPainter * p, const QRectF &); - void drawForeground(QPainter *painter, const QRectF &rect); - void mousePressEvent(QMouseEvent *event); + void drawForeground(QPainter *painter, const QRectF &rect); + void mousePressEvent(QMouseEvent *event); - QGraphicsScene * scene; - AD_Core * core; - QImage * mapimg; - Game_Data * data; + QGraphicsScene * scene; + AD_Core * core; + QImage * mapimg; + Game_Data * data; - bool m_building; + bool m_building; private slots: protected: -// void timerEvent(QTimerEvent * ); + void timerEvent(QTimerEvent * ); signals: - void cancel(); - void add_tow(QPoint pnt); + void cancel(); + void add_tow(QPoint pnt); void tower_select(QPoint id); void alien_select(int id); }; diff --git a/adcore.cpp b/adcore.cpp index bf6cc74..b49a6f0 100644 --- a/adcore.cpp +++ b/adcore.cpp @@ -1,93 +1,94 @@ #include "adcore.h" AD_Core::AD_Core(QObject *parent) : - QThread(parent) + QThread(parent) { - step = 0; - Loader * adloader = new Loader(); - adloader->load("data2.xml"); - addata = adloader->loadlevel(1); - delete adloader; - if (addata != 0) qDebug("Sucsess"); - else qFatal("Error"); - adtowers = new Towers(addata); - adsplashes = new Splashes(addata); - 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; - waveEnd = false; + step = 0; + Loader * adloader = new Loader(); + adloader->load("data2.xml"); + addata = adloader->loadlevel(1); + delete adloader; + if (addata != 0) qDebug("Sucsess"); + else qFatal("Error"); + adtowers = new Towers(addata); + adsplashes = new Splashes(addata); + 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; + waveEnd = false; startTimer(50); } void AD_Core::next() { - adtowers->update(); - adsplashes->update(); - addata->aliens->update(); - waveProcessing(); - step++; + adtowers->update(); + adsplashes->update(); + addata->aliens->update(); + waveProcessing(); + step++; } void AD_Core::timerEvent(QTimerEvent * ) { - next(); + next(); } 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; - qDebug() << tr("Next wave %1").arg(addata->curWave+1); - wavetime = 0; - waveInProgress = true; + if (waveInProgress) return; + if (addata->curWave >= addata->waves.size()) { + qDebug("level finished"); + waveEnd = false; + return; + } + //wavetime = addata->waves.at(addata->curWave).timeout; + qDebug() << tr("Next wave %1").arg(addata->curWave+1); + wavetime = 0; + waveInProgress = true; } 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) { - 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; - } - } - addata->curWave++; - waveInProgress = false; - } else { - wavetime--; - } + /// FIXME : resolve problem with prise for finish wave, it is calculated wrong! + if (addata->aliens->curAliens.isEmpty() && waveEnd) + { + if(addata->curWave > 0) 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) { + 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; + } + } + addata->curWave++; + waveInProgress = false; + } else { + wavetime--; + } } void AD_Core::killAlien(int playerId, int alienId) { - addata->players.at(playerId)->money += addata->aliens->curAliens[alienId].src->prise; - addata->aliens->delAlien(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); + addata->aliens->setScene(scene); + adtowers->setScene(scene); + adsplashes->setScene(scene); } diff --git a/adcore.h b/adcore.h index 99781af..78aaf5e 100644 --- a/adcore.h +++ b/adcore.h @@ -12,28 +12,29 @@ class AD_Core : public QThread { Q_OBJECT public: - explicit AD_Core(QObject *parent = 0); - Game_Data * addata; - Towers * adtowers; - Splashes * adsplashes; - long int step; + explicit AD_Core(QObject *parent = 0); + Game_Data * addata; + Towers * adtowers; + Splashes * adsplashes; + long int step; - void setScene(QGraphicsScene * scene); + void setScene(QGraphicsScene * scene); + bool isWaveEnd() {return waveEnd;} public slots: - void next(); - void nextWave(); + void next(); + void nextWave(); private: - void timerEvent(QTimerEvent * ); - void waveProcessing(); + void timerEvent(QTimerEvent * ); + void waveProcessing(); - bool waveInProgress; - bool waveEnd; - int wavetime; + bool waveInProgress; + bool waveEnd; + int wavetime; private slots: - void killAlien(int playerId, int alienId); + void killAlien(int playerId, int alienId); signals: }; diff --git a/aliendefender.pro.user b/aliendefender.pro.user index de64635..b682870 100644 --- a/aliendefender.pro.user +++ b/aliendefender.pro.user @@ -1,4 +1,6 @@ + + ProjectExplorer.Project.ActiveTarget @@ -7,166 +9,280 @@ ProjectExplorer.Project.EditorSettings - true - false - System - false - false - 4 - true - 1 - true - false - false - 0 - 4 - true - 1 - true - true - true - false + true + false + + Cpp + + true + CppProject + false + false + true + false + false + false + false + true + false + true + false + false + false + true + false + + + 1 + UTF-8 + Project + 4 + true + 1 + true + false + true + + Cpp + + true + false + CppGlobal + 4 + 1 + false + true + 0 + 8 + + + + QmlJS + + true + false + QmlJSGlobal + 4 + 1 + false + true + 0 + 8 + + + 2 + 0 + 4 + false + 1 + true + true + true + true ProjectExplorer.Project.Target.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.Target.DesktopTarget + 0 + 0 + 0 + + ProjectExplorer.ToolChain.Mingw:D:/Qt/qtcreator-2.2.84/mingw/bin/gcc.exe.x86-windows-msys-pe-32bit.D:/Qt/qtcreator-2.2.84/pythongdb/gdb-i686-pc-mingw32.exe + + + qmake + + QtProjectManager.QMakeBuildStep + false + false + + false - - Сборка - - Qt4ProjectManager.MakeStep - false - - + + Сборка + + Qt4ProjectManager.MakeStep + false + + - 2 - Build - Build - ProjectExplorer.BuildSteps.Build + 2 + Build + Build + ProjectExplorer.BuildSteps.Build - - - Сборка - - Qt4ProjectManager.MakeStep - true - clean - + + + Сборка + + Qt4ProjectManager.MakeStep + true + clean + - 1 - Clean - Clean - ProjectExplorer.BuildSteps.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 + 2 + false + + Debug + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + D:/Dropbox/My Dropbox/projects/aliendefender-build-desktop + 7 + 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 + + INVALID + + + qmake + + QtProjectManager.QMakeBuildStep + false + false + + false - - Сборка - - Qt4ProjectManager.MakeStep - false - - + + Сборка + + Qt4ProjectManager.MakeStep + false + + - 2 - Build - Build - ProjectExplorer.BuildSteps.Build + 2 + Build + Build + ProjectExplorer.BuildSteps.Build - - - Сборка - - Qt4ProjectManager.MakeStep - true - - + + + Сборка + + Qt4ProjectManager.MakeStep + true + + - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean + 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 + false + + Release + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + D:/Dropbox/My Dropbox/projects/aliendefender-build-desktop + -1 + false - 2 - - - 0 - Установка - Deploy - ProjectExplorer.BuildSteps.Deploy + 2 + + + 0 + Установка + Deploy + ProjectExplorer.BuildSteps.Deploy - 1 - Без установки - Без установки - ProjectExplorer.DefaultDeployConfiguration + 1 + Без установки + Без установки + ProjectExplorer.DefaultDeployConfiguration - 1 - - Конфигурация запуска Qt4 - aliendefender - Qt4ProjectManager.Qt4RunConfiguration - 2 - - aliendefender.pro - false - false - - - 3768 - true - false + 1 + + true + true + + + false + false + false + false + false + false + false + false + true + true + 0.01 + 0.01 + 10 + 10 + true + true + 25 + 25 + + + true + true + valgrind + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + Конфигурация запуска Qt4 + aliendefender + Qt4ProjectManager.Qt4RunConfiguration + 2 + + aliendefender.pro + false + false + + + 3768 + true + false + false - 1 + 1 @@ -175,10 +291,10 @@ ProjectExplorer.Project.Updater.EnvironmentId - {00000000-0000-0000-0000-000000000000} + {6c550070-c96c-41ad-865e-c5b9b5c3b79f} ProjectExplorer.Project.Updater.FileVersion - 9 + 10 diff --git a/aliens.cpp b/aliens.cpp index e014f9a..431dfbd 100644 --- a/aliens.cpp +++ b/aliens.cpp @@ -4,67 +4,67 @@ Aliens::Aliens(Map * map_, QObject *parent) : QObject(parent) { - map = map_; - nextId = 1; - m_missingAliens = 0; + map = map_; + nextId = 1; + m_missingAliens = 0; } void Aliens::addAlien(int srcId) { - if (srcId < 0 || srcId >= srcAliens.size()) - { - qCritical("ERROR out of aliens range"); - return; - } + if (srcId < 0 || srcId >= srcAliens.size()) + { + qCritical("ERROR out of aliens range"); + return; + } AlienType al; - al.id = nextId; - 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; - if (al.path.isEmpty()) - { - qCritical("ERROR create path"); - return; // FIXME: this - } - //qDebug() << al.path; - 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++; + al.id = nextId; + 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; + if (al.path.isEmpty()) + { + qCritical("ERROR create path"); + return; // FIXME: this + } + //qDebug() << al.path; + 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) { - map->removeAliensPath(); - for (QHash::iterator i = curAliens.begin(); i != curAliens.end(); ++i) - { - i->path = map->createPath(i->pos.toPoint(),i->finish); - i->pathIndex = 1; - if (i->path.isEmpty()) - { - *OK = false; - qDebug("Aliens retrace = false"); - } - } + map->removeAliensPath(); + for (QHash::iterator i = curAliens.begin(); i != curAliens.end(); ++i) + { + i->path = map->createPath(i->pos.toPoint(),i->finish); + i->pathIndex = 1; + if (i->path.isEmpty()) + { + *OK = false; + qDebug("Aliens retrace = false"); + } + } } void Aliens::delAlien(int id, bool missed) { - if (missed) - { - m_missingAliens++; - qDebug() << tr("Missing aliens = %1!").arg(m_missingAliens); - } + if (missed) + { + m_missingAliens++; + qDebug() << tr("Missing aliens = %1!").arg(m_missingAliens); + } scene->removeItem(curAliens[id].item); delete curAliens[id].item; curAliens.remove(id); @@ -73,37 +73,37 @@ void Aliens::delAlien(int id, bool missed) void Aliens::update() { - QList missIndex; - for (QHash::iterator i = curAliens.begin(); i != curAliens.end(); ++i) - { - float angl,arctg = 0; - int curMiss = -1; - while (distance2(i->pos, i->path.at(i->pathIndex)) < i->speed * i->speed) - { - i->pathIndex++; - if (i->pathIndex >= i->path.size()) - { - missIndex.push_back(i->id); - curMiss = i->id; - break; - } - } - 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()); - angl = 180.0f*(-arctg)/M_PI; - 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; j missIndex; + for (QHash::iterator i = curAliens.begin(); i != curAliens.end(); ++i) + { + float angl,arctg = 0; + int curMiss = -1; + while (distance2(i->pos, i->path.at(i->pathIndex)) < i->speed * i->speed) + { + i->pathIndex++; + if (i->pathIndex >= i->path.size()) + { + missIndex.push_back(i->id); + curMiss = i->id; + break; + } + } + 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()); + angl = 180.0f*(-arctg)/M_PI; + 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; j> Cells; - int chei; - if (Cells.size() <= 0) - qFatal("Map has invalid width"); - if (Cells.size() != size.width()) - qCritical("Map has wrong width"); - mapSize.setWidth(Cells.size()); - chei = Cells.at(0).size(); - for (int i=0; i> Cells; + int chei; + if (Cells.size() <= 0) + qFatal("Map has invalid width"); + if (Cells.size() != size.width()) + qCritical("Map has wrong width"); + mapSize.setWidth(Cells.size()); + chei = Cells.at(0).size(); + for (int i=0; i 1000 && Cells[i][j] < 2000) Cells[i][j]=Cells[i][j]-1000; - } - } + for (int i=0; i 1000 && Cells[i][j] < 2000) Cells[i][j]=Cells[i][j]-1000; + } + } } void Map::delTowerOnMap(int playerId, QPoint pos) { - Cells[pos.x()][pos.y()]=Player+playerId; + 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; - emit recreateAlienPath(new bool()); + emit recreateAlienPath(new bool()); } bool Map::isReachable(int playerId, QPoint pos) { - Cells[pos.x()][pos.y()]=PlayerTower-Player-playerId; + Cells[pos.x()][pos.y()]=PlayerTower-Player-playerId; Cells[pos.x()-1][pos.y()]=PlayerTower-Player-playerId; Cells[pos.x()][pos.y()-1]=PlayerTower-Player-playerId; Cells[pos.x()-1][pos.y()-1]=PlayerTower-Player-playerId; - for (int i=0; i players) return false; - QRect r(QPoint(),mapSize); - if (!r.contains(pos) || + if (playerId < 0 || playerId > 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; - if (Cells[pos.x()][pos.y()]==Player+playerId && + 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) - return isReachable(playerId,pos); - if ((Cells[pos.x()][pos.y()]==AlienPath+Player+playerId || Cells[pos.x()][pos.y()]==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)) - { - if (!isReachable(playerId,pos)) return false; - bool * pathOK; - pathOK = new bool(true); - emit recreateAlienPath(pathOK); - qDebug() << *pathOK; - if (*pathOK) - return true; - else - delTowerOnMap(playerId,pos); - } - qDebug()<<"Dont buid here" << pos; - return false; + { + if (!isReachable(playerId,pos)) return false; + bool * pathOK; + pathOK = new bool(true); + emit recreateAlienPath(pathOK); +// qDebug() << *pathOK; + if (*pathOK) + return true; + else + delTowerOnMap(playerId,pos); + } + qWarning("Dont buid here"); + return false; } QVector Map::createPath(QPoint start, QPoint finish) { - QPointF tp; - QVector srcPath; - QVector tmpPath; - QVector path; - if (Cells[start.x()][start.y()] < 0) - { - qDebug("ERROR invalid start"); - return path; - } - if (Cells[finish.x()][finish.y()] < 0) - { - qDebug("ERROR invalid finish"); - return path; - } - srcPath = invWaveTrace(finish,waveTrace(start,finish)); - for (int i=0; i 0 && x < 1000) x = x+1000; - Cells[srcPath.at(i).x()][srcPath.at(i).y()] = x; - tmpPath.push_back(QPointF(srcPath.at(i))); - } - srcPath.clear(); - if (!tmpPath.isEmpty()) - { - for (int j=0; j<4; j++) - { - path.clear(); - tp = tmpPath.at(0); - path.push_back(tp); - for (int i = 0; i < tmpPath.size() - 1; 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.at(tmpPath.size() - 1); - path.push_back(tp); - tmpPath = path; - } - tmpPath.clear(); - if (path.size() >= 6) - { - path.remove(1); - path.remove(1); - path.remove(path.size()-2); - path.remove(path.size()-2); - } - return path; - } - path.clear(); - return path; + QPointF tp; + QVector srcPath; + QVector tmpPath; + QVector path; + if (Cells[start.x()][start.y()] < 0) + { + qCritical("ERROR invalid start"); + return path; + } + if (Cells[finish.x()][finish.y()] < 0) + { + qCritical("ERROR invalid finish"); + return path; + } + srcPath = invWaveTrace(finish,waveTrace(start,finish)); + for (int i=0; i 0 && x < 1000) x = x+1000; + Cells[srcPath.at(i).x()][srcPath.at(i).y()] = x; + tmpPath.push_back(QPointF(srcPath.at(i))); + } + srcPath.clear(); + if (!tmpPath.isEmpty()) + { + for (int j=0; j<4; j++) + { + path.clear(); + tp = tmpPath.at(0); + path.push_back(tp); + for (int i = 0; i < tmpPath.size() - 1; 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.at(tmpPath.size() - 1); + path.push_back(tp); + tmpPath = path; + } + tmpPath.clear(); + if (path.size() >= 6) + { + path.remove(1); + path.remove(1); + path.remove(path.size()-2); + path.remove(path.size()-2); + } + return path; + } + path.clear(); + return path; } int Map::waveTrace(QPoint start, QPoint finish) { - bool stop = false; - int step = 2; - QPoint cp, tp; - QRect fr(QPoint(), mapSize); - QVector tmpp, curp; - TmpCells << Cells; - for (int i = 0; i < Cells.size(); i++) - { - for (int j = 0; j < Cells.at(i).size(); j++) - { - if (Cells[i][j] < 0 ) TmpCells[i][j] = -1; - else TmpCells[i][j] = 0; - } - } - cp = start; - curp.push_back(cp); - TmpCells[cp.x()][cp.y()] = 1; - //qDebug() << "trace"; - while (!stop) { - tmpp = curp; - curp.clear(); - stop = true; - for (int i = 0; i < tmpp.size(); i++) { - cp = tmpp.at(i); - if (cp == finish) { - TmpCells[cp.x()][cp.y()] = step; - //qDebug() << "trace done!"; - return step; - } - tp.setX(cp.x() - 1); - tp.setY(cp.y()); - if (fr.contains(tp)) { - if (TmpCells[tp.x()][tp.y()] == 0) { - TmpCells[tp.x()][tp.y()] = step; - curp.push_back(tp); - stop = false; - } - } - tp.setX(cp.x() + 1); - if (fr.contains(tp)) { - if (TmpCells[tp.x()][tp.y()] == 0) { - TmpCells[tp.x()][tp.y()] = step; - curp.push_back(tp); - stop = false; - } - } - tp.setX(cp.x()); - tp.setY(cp.y() - 1); - if (fr.contains(tp)) { - if (TmpCells[tp.x()][tp.y()] == 0) { - TmpCells[tp.x()][tp.y()] = step; - curp.push_back(tp); - stop = false; - } - } - tp.setY(cp.y() + 1); - if (fr.contains(tp)) { - if (TmpCells[tp.x()][tp.y()] == 0) { - TmpCells[tp.x()][tp.y()] = step; - curp.push_back(tp); - stop = false; - } - } - } - step++; - } - qDebug() << "trace false"; - return -1; + bool stop = false; + int step = 2; + QPoint cp, tp; + QRect fr(QPoint(), mapSize); + QVector tmpp, curp; + TmpCells << Cells; + for (int i = 0; i < Cells.size(); i++) + { + for (int j = 0; j < Cells.at(i).size(); j++) + { + if (Cells[i][j] < 0 ) TmpCells[i][j] = -1; + else TmpCells[i][j] = 0; + } + } + cp = start; + curp.push_back(cp); + TmpCells[cp.x()][cp.y()] = 1; + //qDebug() << "trace"; + while (!stop) { + tmpp = curp; + curp.clear(); + stop = true; + for (int i = 0; i < tmpp.size(); i++) { + cp = tmpp.at(i); + if (cp == finish) { + TmpCells[cp.x()][cp.y()] = step; + //qDebug() << "trace done!"; + return step; + } + tp.setX(cp.x() - 1); + tp.setY(cp.y()); + if (fr.contains(tp)) { + if (TmpCells[tp.x()][tp.y()] == 0) { + TmpCells[tp.x()][tp.y()] = step; + curp.push_back(tp); + stop = false; + } + } + tp.setX(cp.x() + 1); + if (fr.contains(tp)) { + if (TmpCells[tp.x()][tp.y()] == 0) { + TmpCells[tp.x()][tp.y()] = step; + curp.push_back(tp); + stop = false; + } + } + tp.setX(cp.x()); + tp.setY(cp.y() - 1); + if (fr.contains(tp)) { + if (TmpCells[tp.x()][tp.y()] == 0) { + TmpCells[tp.x()][tp.y()] = step; + curp.push_back(tp); + stop = false; + } + } + tp.setY(cp.y() + 1); + if (fr.contains(tp)) { + if (TmpCells[tp.x()][tp.y()] == 0) { + TmpCells[tp.x()][tp.y()] = step; + curp.push_back(tp); + stop = false; + } + } + } + step++; + } + qDebug() << "trace false"; + return -1; } QVector Map::invWaveTrace(QPoint finish, int cnt) { - QPoint wp, Ppnt; - QVector alpath; - if (cnt < 2) return alpath; - int Ind, c, xpp, ypp, xnn, ynn; - unsigned char chk; - Ppnt = wp = finish; - xnn=0; - xpp=0; - ynn=0; - ypp=0; - cnt--; - alpath.push_front(Ppnt); - while (cnt > 1) - { - cnt--; - chk = 0; - Ind = 0; - c = 0; - if (wp.x() - 1 >= 0 && TmpCells[wp.x()-1][wp.y()] == cnt) - { - chk = chk | 0x01; - c++; - } - if (wp.x() + 1 < mapSize.width() && TmpCells[wp.x()+1][wp.y()] == cnt) - { - chk = chk | 0x02; - c++; - } - if (wp.y() - 1 >= 0 && TmpCells[wp.x()][wp.y()-1] == cnt) - { - chk = chk | 0x04; - c++; - } - if (wp.y() + 1 < mapSize.height() && TmpCells[wp.x()][wp.y()+1] == cnt) - { - chk = chk | 0x08; - c++; - } - if (c == 0 || chk == 0) qDebug("ERROR in InvWaveTrace"),exit(-1); - if (c > 1) - { - if ((chk & 0x01)==0x01 && (chk & 0x04)==0x04) - { - if (xnn <= ynn && Ind == 0){ - wp.rx()--; - xnn++; - if (xnn == ynn) xnn++; - Ind = 1; - } else if (Ind == 0) { - wp.ry()--; - ynn++; - ynn++; - Ind = 1; - } - } - if ((chk & 0x02)==0x02 && (chk & 0x04)==0x04) - { - if (xpp <= ynn && Ind == 0){ - wp.rx()++; - xpp++; - if (xpp == ynn) xpp++; - Ind = 1; - } else if (Ind == 0) { - wp.ry()--; - ynn++; - ynn++; - Ind = 1; - } - } - if ((chk & 0x01)==0x01 && (chk & 0x08)==0x08) - { - if (xnn <= ypp && Ind == 0){ - wp.rx()--; - xnn++; - if (xnn == ypp) xnn++; - Ind = 1; - } else if (Ind == 0) { - wp.ry()++; - ypp++; - ypp++; - Ind = 1; - } - } - if ((chk & 0x02)==0x02 && (chk & 0x08)==0x08) - { - if (xpp <= ypp && Ind == 0){ - wp.rx()++; - xpp++; - if (xpp == ypp) xpp++; - Ind = 1; - } else if (Ind == 0) { - wp.ry()++; - ypp++; - ypp++; - Ind = 1; - } - } - } - if (c == 1 || Ind == 0) - { - xnn=0; - xpp=0; - ynn=0; - ypp=0; - if ((chk & 0x01)==0x01) { - wp.rx()--; - xnn++; - } - else if ((chk & 0x02)==0x02) { - wp.rx()++; - xpp++; - } - else if ((chk & 0x04)==0x04) { - wp.ry()--; - ynn++; - } - else if ((chk & 0x08)==0x08) { - wp.ry()++; - ypp++; - } - } - Ppnt = wp; - alpath.push_front(Ppnt); - } - return alpath; + QPoint wp, Ppnt; + QVector alpath; + if (cnt < 2) return alpath; + int Ind, c, xpp, ypp, xnn, ynn; + unsigned char chk; + Ppnt = wp = finish; + xnn=0; + xpp=0; + ynn=0; + ypp=0; + cnt--; + alpath.push_front(Ppnt); + while (cnt > 1) + { + cnt--; + chk = 0; + Ind = 0; + c = 0; + if (wp.x() - 1 >= 0 && TmpCells[wp.x()-1][wp.y()] == cnt) + { + chk = chk | 0x01; + c++; + } + if (wp.x() + 1 < mapSize.width() && TmpCells[wp.x()+1][wp.y()] == cnt) + { + chk = chk | 0x02; + c++; + } + if (wp.y() - 1 >= 0 && TmpCells[wp.x()][wp.y()-1] == cnt) + { + chk = chk | 0x04; + c++; + } + if (wp.y() + 1 < mapSize.height() && TmpCells[wp.x()][wp.y()+1] == cnt) + { + chk = chk | 0x08; + c++; + } + if (c == 0 || chk == 0) qDebug("ERROR in InvWaveTrace"),exit(-1); + if (c > 1) + { + if ((chk & 0x01)==0x01 && (chk & 0x04)==0x04) + { + if (xnn <= ynn && Ind == 0){ + wp.rx()--; + xnn++; + if (xnn == ynn) xnn++; + Ind = 1; + } else if (Ind == 0) { + wp.ry()--; + ynn++; + ynn++; + Ind = 1; + } + } + if ((chk & 0x02)==0x02 && (chk & 0x04)==0x04) + { + if (xpp <= ynn && Ind == 0){ + wp.rx()++; + xpp++; + if (xpp == ynn) xpp++; + Ind = 1; + } else if (Ind == 0) { + wp.ry()--; + ynn++; + ynn++; + Ind = 1; + } + } + if ((chk & 0x01)==0x01 && (chk & 0x08)==0x08) + { + if (xnn <= ypp && Ind == 0){ + wp.rx()--; + xnn++; + if (xnn == ypp) xnn++; + Ind = 1; + } else if (Ind == 0) { + wp.ry()++; + ypp++; + ypp++; + Ind = 1; + } + } + if ((chk & 0x02)==0x02 && (chk & 0x08)==0x08) + { + if (xpp <= ypp && Ind == 0){ + wp.rx()++; + xpp++; + if (xpp == ypp) xpp++; + Ind = 1; + } else if (Ind == 0) { + wp.ry()++; + ypp++; + ypp++; + Ind = 1; + } + } + } + if (c == 1 || Ind == 0) + { + xnn=0; + xpp=0; + ynn=0; + ypp=0; + if ((chk & 0x01)==0x01) { + wp.rx()--; + xnn++; + } + else if ((chk & 0x02)==0x02) { + wp.rx()++; + xpp++; + } + else if ((chk & 0x04)==0x04) { + wp.ry()--; + ynn++; + } + else if ((chk & 0x08)==0x08) { + wp.ry()++; + ypp++; + } + } + Ppnt = wp; + alpath.push_front(Ppnt); + } + return alpath; } diff --git a/splashes.cpp b/splashes.cpp index 32e0c1a..d80d0e9 100644 --- a/splashes.cpp +++ b/splashes.cpp @@ -2,163 +2,164 @@ #include Splashes::Splashes(Game_Data *gd, QObject *parent) : - QObject(parent) + QObject(parent) { - gameData = gd; - nextId = 1; + gameData = gd; + nextId = 1; } void Splashes::addSplash(int player, srcSplashType *src, QPointF pos, QPointF dest, int aim) { - SplashType spl; - spl.id = nextId; - 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); + SplashType spl; + spl.id = nextId; + 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; + 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); + 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; - spl.angle = 180.0f*(- std::atan2(spl.pos.x() - spl.destination.x(),spl.pos.y() - spl.destination.y()))/M_PI; - spl.life = 0; - spl.srcTow = srctower; - addSplash(&spl); + 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; + spl.angle = 180.0f*(- std::atan2(spl.pos.x() - spl.destination.x(),spl.pos.y() - spl.destination.y()))/M_PI; + spl.life = 0; + spl.srcTow = srctower; + addSplash(&spl); } void Splashes::addSplash(SplashType * spl) { - srcTriggerType * trig; - for(int i=0; isrc->triggerIndexes.size(); i++) - { - TriggerOnDest tod; - TriggerOnAlien toa; - TriggerOnTimer tot; - 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 = trig; - spl->trigsOnDest.append(tod); - break; - case srcTriggerType::onAlienInRadius : - toa.src = trig; - spl->trigsOnAlien.append(toa); - break; - case srcTriggerType::onTimer : - tot.src = trig; - tot.timer = 0; - spl->trigsOnTimer.append(tot); - break; - } + srcTriggerType * trig; + for(int i=0; isrc->triggerIndexes.size(); i++) + { + TriggerOnDest tod; + TriggerOnAlien toa; + TriggerOnTimer tot; + 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 = trig; + spl->trigsOnDest.append(tod); + break; + case srcTriggerType::onAlienInRadius : + toa.src = trig; + spl->trigsOnAlien.append(toa); + break; + case srcTriggerType::onTimer : + tot.src = trig; + tot.timer = 0; + spl->trigsOnTimer.append(tot); + break; + } - } - 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++; + } + 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::update() { - QList deadIndexes; - for (QHash::iterator - spl = gameData->curSplashes.begin(); - spl != gameData->curSplashes.end(); ++spl) - { - 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->aliens->curAliens.contains(spl->AlienId)) - spl->destination = gameData->aliens->curAliens.value(spl->AlienId).pos+QPointF(0.5,0.5); - else { - badAl = true; - spl->AlienId = -1; - } - if (distance2(spl->pos, spl->destination) < speed2) - { - if (badAl) - 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()); - 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 (distance2(spl->pos, spl->destination) < speed2) - { - spl->pos = spl->destination; - doTriggerOnDest(spl,&deadIndexes,true); - } - } - arctg = -spl->angle*M_PI/180.f; - } - 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 deadIndexes; + for (QHash::iterator + spl = gameData->curSplashes.begin(); + spl != gameData->curSplashes.end(); ++spl) + { + spl->life++; + if (spl->life > spl->src->lifetime) + deadIndexes.insert(spl->id,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->aliens->curAliens.contains(spl->AlienId)) + spl->destination = gameData->aliens->curAliens.value(spl->AlienId).pos+QPointF(0.5,0.5); + else { + badAl = true; + spl->AlienId = -1; + } + if (distance2(spl->pos, spl->destination) < speed2) + { + if (badAl) + deadIndexes.insert(spl->id,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()); + spl->angle = 180.0f*(-arctg)/M_PI; + } else { + if (!gameData->map->rect().contains(spl->pos.toPoint())) + deadIndexes.insert(spl->id,spl->id); + if (!spl->trigsOnDest.isEmpty()) + { + if (distance2(spl->pos, spl->destination) < speed2) + { + spl->pos = spl->destination; + doTriggerOnDest(spl,&deadIndexes,true); + } + } + arctg = -spl->angle*M_PI/180.f; + } + 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 (QHash::iterator j = deadIndexes.begin(); + j != deadIndexes.end(); ++j) + delSplash(*j); for (QHash::iterator j = dead.begin(); j != dead.end(); ++j) emit killAlien(j->player,j->id); @@ -168,28 +169,28 @@ void Splashes::update() void Splashes::delSplash(int Id) { - scene->removeItem(gameData->curSplashes[Id].item); - delete gameData->curSplashes[Id].item; - gameData->curSplashes.remove(Id); + scene->removeItem(gameData->curSplashes[Id].item); + delete gameData->curSplashes[Id].item; + gameData->curSplashes.remove(Id); } /// FIXME : correct onTrigTimer() void Splashes::doTriggerOnTimer(QHash::iterator - spl, QList *deadIndexes) + spl, QHash *deadIndexes) { - for(QList::iterator i = spl->trigsOnTimer.begin(); - i != spl->trigsOnTimer.end(); ++i) - { + for(QList::iterator i = spl->trigsOnTimer.begin(); + i != spl->trigsOnTimer.end(); ++i) + { srcTriggerType * strig = i->src; - i->timer++; - if (i->timer >= strig->timer) - { + i->timer++; + if (i->timer >= strig->timer) + { /// TODO : add damage and other effects i->timer = 0; - if (strig->delParent) deadIndexes->append(spl->id); - if (strig->count > 0 && strig->childId > 0) - { + if (strig->delParent) deadIndexes->insert(spl->id,spl->id); + if (strig->count > 0 && strig->childId > 0) + { int aim; switch(strig->childAim) { @@ -199,65 +200,65 @@ void Splashes::doTriggerOnTimer(QHash::iterator default : aim = -1; } for (int j=0; jcount; ++j) - { - /// TODO: randomRadiusPos - addSplash(spl->srcTow, strig->childId, spl->pos, spl->destination, aim, spl->towerId); - } - } - } - } + { + /// TODO: randomRadiusPos + addSplash(spl->srcTow, strig->childId, spl->pos, spl->destination, aim, spl->towerId); + } + } + } + } } void Splashes::doTriggerOnDest(QHash::iterator - spl, QList *deadIndexes, - bool badAl) + spl, QHash *deadIndexes, + bool badAl) { - foreach(TriggerOnDest t, spl->trigsOnDest) - { - /// TODO: different aims for childs - if (t.src->delParent) deadIndexes->append(spl->id); - int aim; - switch(t.src->childAim) - { - case srcTriggerType::parentAim : aim = spl->AlienId; 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->srcTow,t.src->childId, - spl->pos,spl->destination,aim,spl->towerId); - } - } - float dmg = t.src->damage; - float rad = t.src->radius; - if (dmg > 0) - { + foreach(TriggerOnDest t, spl->trigsOnDest) + { + /// TODO: different aims for childs + if (t.src->delParent) deadIndexes->insert(spl->id,spl->id); + int aim; + switch(t.src->childAim) + { + case srcTriggerType::parentAim : aim = spl->AlienId; 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->srcTow,t.src->childId, + spl->pos,spl->destination,aim,spl->towerId); + } + } + float dmg = t.src->damage; + float rad = t.src->radius; + if (dmg > 0) + { float hh; - if (!(rad > 0)) - { - if (!badAl) - { + if (!(rad > 0)) + { + if (!badAl) + { hh = gameData->aliens->curAliens[spl->AlienId].health -= dmg; if (hh <= 0) dead.insert(spl->AlienId, DeadAlienIndex(spl->AlienId, spl->PlayerId)); - } - } else { - for (QHash::iterator - i = gameData->aliens->curAliens.begin(); - i != gameData->aliens->curAliens.end(); ++i) - { - if (distance2(spl->pos, i->pos) < rad*rad) - { + } + } else { + for (QHash::iterator + i = gameData->aliens->curAliens.begin(); + i != gameData->aliens->curAliens.end(); ++i) + { + if (distance2(spl->pos, i->pos) < rad*rad) + { hh = i->health -= dmg; if (hh <= 0) dead.insert(i->id, DeadAlienIndex(i->id, spl->PlayerId)); - } - } - } - } - } + } + } + } + } + } } diff --git a/splashes.h b/splashes.h index 31392f3..9a5f7d6 100644 --- a/splashes.h +++ b/splashes.h @@ -5,28 +5,28 @@ class Splashes : public QObject { - Q_OBJECT + Q_OBJECT public: - explicit Splashes(Game_Data * gd, QObject *parent = 0); - void delSplash(int Id); - void update(); - void setScene(QGraphicsScene * scene_) {scene = scene_;} + explicit Splashes(Game_Data * gd, QObject *parent = 0); + 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); + void killAlien(int playerId, int alienId); public slots: - 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); + 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; + QGraphicsScene * scene; + Game_Data *gameData; + int nextId; struct DeadAlienIndex { DeadAlienIndex(int i, int p) {id = i; player = p;} @@ -35,11 +35,11 @@ private: }; QHash dead; - void doTriggerOnDest(QHash::iterator - spl, QList * deadIndexes, - bool badAl); - void doTriggerOnTimer(QHash::iterator - spl, QList * deadIndexes); + void doTriggerOnDest(QHash::iterator + spl, QHash * deadIndexes, + bool badAl); + void doTriggerOnTimer(QHash::iterator + spl, QHash * deadIndexes); }; #endif // SPLASHES_H