From 58ba349f19f98fe3af5332188f5d3dfe4d076807 Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 19 Apr 2011 23:03:50 +0200 Subject: nicer method of ending rounds --- pacman-c++/bonuspoint.cpp | 2 +- pacman-c++/mainwidget.cpp | 3 +-- pacman-c++/sceneholder.cpp | 16 ++++++++++------ pacman-c++/sceneholder.h | 4 +++- pacman-c++/server.cpp | 12 +++++++----- pacman-c++/server.h | 7 +++---- pacman-c++/util.cpp | 7 +++++++ 7 files changed, 32 insertions(+), 19 deletions(-) diff --git a/pacman-c++/bonuspoint.cpp b/pacman-c++/bonuspoint.cpp index 8cb8c7e..c90cccc 100644 --- a/pacman-c++/bonuspoint.cpp +++ b/pacman-c++/bonuspoint.cpp @@ -26,7 +26,7 @@ BonusPoint::BonusPoint(QGraphicsItem *parent) setSprite(rand * 20 + Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); } -bool BonusPoint::enter(Actor* actor) +bool BonusPoint::enter(Actor *actor) { actor->addRoundPoints(Constants::Game::bonus_point_value); m_eaten = true; diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index f81961f..82099c4 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -80,8 +80,6 @@ void MainWidget::createGui() scoreLayout->addWidget(scoreBox); } layout->addLayout(scoreLayout); - /* add some margin to scorebox hopefully won't resize the window */ - //setMinimumWidth(scoreLayout->minimumSize().width() + 50); QGraphicsView *window = new QGraphicsView(m_scene, this); window->setFrameStyle(0); @@ -151,6 +149,7 @@ void MainWidget::tick() if (m_updatepacket.eating_order_size() > 0) { Q_ASSERT(m_scene != NULL); + m_scene->removeActors(); QList order; for(int i = 0; i < m_updatepacket.eating_order_size(); ++i) order.append(static_cast(m_updatepacket.eating_order(i) & Transmission::color_mask)); diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp index 03abf7f..1610b7e 100644 --- a/pacman-c++/sceneholder.cpp +++ b/pacman-c++/sceneholder.cpp @@ -150,11 +150,6 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x qDebug() << "[SceneUpdate] actor moves: color=" << color << "direction=" << direction << "newpos=" << QPoint(x, y); } - - QPoint distance = QPoint(x, y) - CoordToMapPosition(actor->pos().x(), actor->pos().y()); - if (distance.manhattanLength() > 1) { - actor->setPos(mapPositionToCoord(x, y)); - } } if (cur & Transmission::empty) @@ -213,6 +208,16 @@ QList &SceneHolder::eatingOrder() return m_eatingorder; } +void SceneHolder::removeActors() +{ + foreach(Actor *actor, m_actors) + { + removeItem(actor); + delete actor; + } + m_actors.clear(); +} + QPoint SceneHolder::mapPositionToCoord(unsigned int x, unsigned int y) { return QPoint(x * Constants::field_size.width, y * Constants::field_size.height); @@ -232,4 +237,3 @@ QPoint SceneHolder::CoordToMapPosition(QPoint point) { return CoordToMapPosition(point.x(), point.y()); } - diff --git a/pacman-c++/sceneholder.h b/pacman-c++/sceneholder.h index 26ba942..c61de58 100644 --- a/pacman-c++/sceneholder.h +++ b/pacman-c++/sceneholder.h @@ -23,6 +23,7 @@ public: Color::Color color(); void setEatingOrder(QList &order); QList &eatingOrder(); + void removeActors(); signals: void allPointsRemoved(); @@ -37,11 +38,12 @@ protected: QPoint CoordToMapPosition(unsigned int x, unsigned int y); QPoint CoordToMapPosition(QPoint point); +protected: /* map of all pixmap instances */ QVector< QVector > visualMap; /* map of actors in order to keep track of those instances */ - QMap m_actors; + QMap m_actors; /* items that got removed/has been eaten * must be remove one tick later diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index f2c0f1c..72ee995 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp @@ -31,7 +31,7 @@ bool Server::run() if (!waitForClientConnections()) return false; - initRoundMap(true); + initRoundMap(); m_tickTimer = new QTimer(this); connect(m_tickTimer, SIGNAL(timeout()), this, SLOT(tick())); @@ -484,6 +484,7 @@ void Server::keyPressUpdate() void Server::onRoundFinished() { + // TODO: call this when a pacman get's eaten foreach(Actor *actor, m_actors) actor->finishRound(); @@ -495,8 +496,10 @@ void Server::onRoundFinished() m_tickTimer->stop(); } -void Server::initRoundMap(bool firstPacket) +void Server::initRoundMap() { + /* delete actors first */ + removeActors(); /* create new map */ Transmission::map_t map = Util::createDemoMap(); @@ -516,12 +519,11 @@ void Server::initRoundMap(bool firstPacket) } updateMap(map); - sendUpdate(map, firstPacket); + sendUpdate(map, true); Util::deleteMap(map); map = NULL; - if (firstPacket) - connect(this, SIGNAL(allPointsRemoved()), this, SLOT(onRoundFinished())); + connect(this, SIGNAL(allPointsRemoved()), this, SLOT(onRoundFinished()), Qt::UniqueConnection); } bool Server::parseCommandline() diff --git a/pacman-c++/server.h b/pacman-c++/server.h index dc8de88..6dd138b 100644 --- a/pacman-c++/server.h +++ b/pacman-c++/server.h @@ -37,12 +37,11 @@ protected: QPoint addRandomPoint(Transmission::map_t map, Transmission::field_t type = Transmission::bonuspoint); void colorizeBlocks(Transmission::map_t map); void botCalculate(Actor *actor); + void initRoundMap(); 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 + /* called when a round is finished */ + void onRoundFinished(); protected: QMap m_clientConnections; diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp index d83ea3c..f8396f9 100644 --- a/pacman-c++/util.cpp +++ b/pacman-c++/util.cpp @@ -145,7 +145,14 @@ namespace Util { Transmission::field_t &cur = map[x][y]; if (cur == Transmission::none) + { +#if 0 + /* use for endround testing */ + if (x > 0) + continue; +#endif cur = type; + } else if (cur == Transmission::point) cur = Transmission::none; } -- cgit v1.2.3