From eaf133fd08c9708fe718ef47451bed7ea803a46a Mon Sep 17 00:00:00 2001 From: totycro Date: Tue, 19 Apr 2011 21:56:37 +0200 Subject: Added rounds rounds will end when all points are removed TODO: end round when a pacman gets eaten --- pacman-c++/mainwidget.cpp | 2 +- pacman-c++/sceneholder.cpp | 9 +++- pacman-c++/sceneholder.h | 3 ++ pacman-c++/server.cpp | 115 ++++++++++++++++++++++++++++++++++++--------- pacman-c++/server.h | 8 ++++ pacman-c++/util.cpp | 10 ++-- 6 files changed, 120 insertions(+), 27 deletions(-) diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 42abd01..f2e1b31 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -110,7 +110,7 @@ void MainWidget::updateScore(const ProtoBuf::MapUpdate& packet) turnPointsLbl->setText(QString::number(packet.round_points(i))); QLabel *allPointsLbl = dynamic_cast(score->itemAtPosition(1, 1)->widget()); - allPointsLbl->setText(QString::number(packet.round_points(i))); + allPointsLbl->setText(QString::number(packet.game_points(i))); } } diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp index dace711..2fcfb37 100644 --- a/pacman-c++/sceneholder.cpp +++ b/pacman-c++/sceneholder.cpp @@ -73,8 +73,9 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x } /* an item must be removed by an actor */ - if (actor == NULL) + if (actor == NULL) { Q_ASSERT(false); + } oldItem->onDie(actor); /* register item for removal in next update */ @@ -132,9 +133,11 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x m_actors[color] = actor; addItem(actor); actor->setPos(mapPositionToCoord(x, y)); + qDebug() << endl << "create act " << endl; } else { + qDebug() << endl << "act has pnts: " << actor->getRoundPoints() << actor->getGamePoints() << endl; Actor::Movement direction = Util::transmissionMovementToActor( cur & Transmission::direction_mask); /* WARNING: do NOT add actor to visualMap as visualMap-items may @@ -151,6 +154,7 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x qDebug() << "[SceneUpdate] actor moves: color=" << color << "direction=" << direction << "newpos=" << QPoint(x, y); } + actor->setPos(mapPositionToCoord(x, y)); } if (cur & Transmission::empty) @@ -195,6 +199,9 @@ unsigned int SceneHolder::pointsLeft() void SceneHolder::decrementPoints() { --m_pointsLeft; + if (m_pointsLeft == 0) { + emit allPointsRemoved(); + } } void SceneHolder::setEatingOrder(QList &order) diff --git a/pacman-c++/sceneholder.h b/pacman-c++/sceneholder.h index 69ee598..26ba942 100644 --- a/pacman-c++/sceneholder.h +++ b/pacman-c++/sceneholder.h @@ -24,6 +24,9 @@ public: void setEatingOrder(QList &order); QList &eatingOrder(); +signals: + void allPointsRemoved(); + private slots: void decrementPoints(); diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index 4051807..3cb422b 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp @@ -31,30 +31,20 @@ bool Server::run() if (!waitForClientConnections()) return false; - qDebug() << "[Server] Creating map..."; - Transmission::map_t map = Util::createDemoMap(); - Util::placeActors(map, m_maxplayers, Color::order); - Util::fillPoints(map); + initRoundMap(true); - /* save positions of blocks for later usage */ - for (unsigned int x = 0; x < Constants::map_size.width; ++x) - { - for (unsigned int y = 0; y < Constants::map_size.height; ++y) - { - Transmission::field_t &cur = map[x][y]; - if (cur & Transmission::block) - m_blocks.append(QPoint(x, y)); - } - } + m_tickTimer = new QTimer(this); + connect(m_tickTimer, SIGNAL(timeout()), this, SLOT(tick())); + m_tickTimer->start(Constants::tick); - updateMap(map); - sendUpdate(map, true); - Util::deleteMap(map); - map = NULL; + /* finish round every 3 sec + QTimer *timer2 = new QTimer(this); + connect(timer2, SIGNAL(timeout()), this, SLOT(onRoundFinished())); + timer2->start(3000); + / */ + + connect(this, SIGNAL(allPointsRemoved()), SLOT(onRoundFinished())); - QTimer *timer = new QTimer(this); - connect(timer, SIGNAL(timeout()), this, SLOT(tick())); - timer->start(Constants::tick); return true; } @@ -109,7 +99,6 @@ invalid_direction: // // TODO: support actors eating each other - // TODO: old item - REMOVE THAT? GameEntity *oldItem = visualMap[mapPosition.x()][mapPosition.y()]; /* check if there's an item at new location of actor */ @@ -501,6 +490,88 @@ void Server::keyPressUpdate() } } +void Server::onRoundFinished() +{ + foreach(Actor *actor, m_actors) { + actor->finishRound(); + } + + initRoundMap(); + ++m_curRound; + + if(m_curRound >= m_rounds) { + // end of game + m_tickTimer->stop(); + } +} + +void Server::initRoundMap(bool firstPacket) +{ + qDebug() << endl; + qDebug() << endl; + qDebug() << "[Server] Creating map..."; + qDebug() << endl; + qDebug() << endl; + qDebug() << endl; + + disconnect(this, SIGNAL(allPointsRemoved()), this, SLOT(onRoundFinished())); + if (!firstPacket) { + // clear old map + for (unsigned int i=0; i(e) == NULL) { + removeItem(e); + delete e; + } + } + visualMap[i][j] = 0; + } + } + } + + // create new map + Transmission::map_t map = Util::createDemoMap(); + // add content + Util::placeActors(map, m_maxplayers, Color::order); + /* + if (!firstPacket) { + for (unsigned int i=0; i(map[i][j] & Transmission::color_mask); + Actor *actor = m_actors.value(color, NULL); + qDebug() << "setting actor to " << i << j << mapPositionToCoord(i, j); + actor->setPos(mapPositionToCoord(i, j)); + } + } + } + } + */ + + Util::fillPoints(map); + + /* save positions of blocks for later usage */ + m_blocks.clear(); + for (unsigned int x = 0; x < Constants::map_size.width; ++x) + { + for (unsigned int y = 0; y < Constants::map_size.height; ++y) + { + Transmission::field_t &cur = map[x][y]; + if (cur & Transmission::block) + m_blocks.append(QPoint(x, y)); + } + } + + updateMap(map); + sendUpdate(map, firstPacket); + Util::deleteMap(map); + map = NULL; + + connect(this, SIGNAL(allPointsRemoved()), this, SLOT(onRoundFinished())); +} + bool Server::parseCommandline() { AnyOption opt; diff --git a/pacman-c++/server.h b/pacman-c++/server.h index fd1b571..dc8de88 100644 --- a/pacman-c++/server.h +++ b/pacman-c++/server.h @@ -38,6 +38,12 @@ protected: void colorizeBlocks(Transmission::map_t map); void botCalculate(Actor *actor); +protected slots: + // TODO: call this when a pacman get's eaten + void onRoundFinished(); // called when a round is finished +protected: + void initRoundMap(bool firstPacket = false); // creates new round map + protected: QMap m_clientConnections; QList m_bots; @@ -61,6 +67,8 @@ protected: unsigned int m_curRound; // current round starting with 0 + QTimer *m_tickTimer; + }; #endif // SERVER_H diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp index 708d005..fcc1aff 100644 --- a/pacman-c++/util.cpp +++ b/pacman-c++/util.cpp @@ -90,7 +90,7 @@ namespace Util QList actors; for(unsigned int i = 0; i < players; ++i) { - /* first remove formally placed actors from map */ + /* first remove formerly placed actors from map */ foreach(QPoint pos, actors) map[pos.x()][pos.y()] = Transmission::none; actors.clear(); @@ -139,15 +139,19 @@ namespace Util void fillPoints(Transmission::map_t map, Transmission::field_t type) { /* auto place normal points*/ + int i =0; for (unsigned int x = 0; x < Constants::map_size.width; ++x) { for (unsigned int y = 0; y < Constants::map_size.height; ++y) { Transmission::field_t &cur = map[x][y]; - if (cur == Transmission::none) + if (cur == Transmission::none) { + if (++i > 10 ) continue; + cur = type; - else if (cur == Transmission::point) + } else if (cur == Transmission::point) { cur = Transmission::none; + } } } } -- cgit v1.2.3