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++/server.cpp | 115 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 22 deletions(-) (limited to 'pacman-c++/server.cpp') 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; -- cgit v1.2.3