From 41a31420cf091aeb4e986503387855d41e550106 Mon Sep 17 00:00:00 2001 From: manuel Date: Mon, 25 Apr 2011 14:39:00 +0200 Subject: - add intro sound on every round - add dieing sound - add dieing animation - add die on moving onto colorized block --- pacman-c++/server.cpp | 143 +++++++++++++++++++++++++++++++------------------- 1 file changed, 90 insertions(+), 53 deletions(-) (limited to 'pacman-c++/server.cpp') diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index be45a66..ef271f0 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp @@ -8,11 +8,12 @@ #include #include #include +#include Server::Server(QWidget *parent) : SceneHolder(parent), m_bindaddress(QHostAddress::Any), m_port(Constants::Networking::port), m_numbots(0), - m_rounds(3), m_curRound(0), m_roundFinished(false) + m_rounds(3), m_curRound(0), m_running(false), m_finishRound(false) { /* determine max players by using order array */ for(m_maxplayers = 0; Color::order[m_maxplayers] != Color::none; ++m_maxplayers); @@ -36,54 +37,43 @@ bool Server::run() if (!waitForClientConnections()) return false; - connect(this, SIGNAL(allPointsRemoved()), this, SLOT(setRoundFinished())); + connect(this, SIGNAL(allPointsRemoved()), this, SLOT(setFinishRound())); initRoundMap(); return true; } void Server::tick() { - //qDebug() << "[Tick] Doing server update"; - if (m_roundFinished) + qDebug() << "[Tick] Doing server update"; + if (m_finishRound) + stopGame(true); + if (!m_running) { - // TODO: call this when a pacman get's eaten - /* first finish previous round */ - foreach(Actor *actor, m_actors) - actor->finishRound(); - - ++m_curRound; - if(m_curRound < m_rounds) - initRoundMap(); - else - { - /* end of game */ - qDebug() << "All round finished. Exiting..."; - m_tickTimer->stop(); - qApp->quit(); - } + Transmission::map_t map = Util::createEmptyMap(); + sendUpdate(map); + Util::deleteMap(map); + return; } - else - { - /* let the bots move */ - foreach (Color::Color color, m_bots) - botCalculate(m_actors[color]); - /* move on the virtual map */ - Transmission::map_t map = calculateUpdates(); - updateMap(map); + /* let the bots move */ + foreach (Color::Color color, m_bots) + botCalculate(m_actors[color]); - /* add a random bonus point */ - QPoint pos = addRandomPoint(map, Transmission::bonuspoint); - if (!pos.isNull()) - updateMap(map, pos.x(), pos.y()); + /* move on the virtual map */ + Transmission::map_t map = calculateUpdates(); + updateMap(map); - /* add/remove random colorized block */ - if (this->property("coloredblocks").toBool()) - colorizeBlocks(map); + /* add a random bonus point */ + QPoint pos = addRandomPoint(map, Transmission::bonuspoint); + if (!pos.isNull()) + updateMap(map, pos.x(), pos.y()); - sendUpdate(map); - Util::deleteMap(map); - } + /* add/remove random colorized block */ + if (this->property("coloredblocks").toBool()) + colorizeBlocks(map); + + sendUpdate(map); + Util::deleteMap(map); } Transmission::map_t Server::calculateUpdates() @@ -94,14 +84,15 @@ Transmission::map_t Server::calculateUpdates() while (i.hasNext()) { i.next(); - Actor *actor = m_actors.value(i.key()); + Color::Color color = i.key(); + Actor *actor = m_actors[color]; QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint()); Actor::Movement direction = i.value(); int turn = 0; invalid_direction: ++turn; - qDebug() << "[Calc] Actor wants to move: color=" << i.key() + qDebug() << "[Calc] Actor wants to move: color=" << color << "pos=" << mapPosition << "direction=" << direction; QPoint newMapPosition = mapPosition + Actor::movementToPoint(direction); @@ -132,10 +123,15 @@ invalid_direction: else { /* apply actions of entering this field */ - bool survive = item->enter(actor); - if (!survive) - { + GameEntity::EnteredState survive = item->enter(actor); + if (survive == GameEntity::DestroyedEntity) map[newMapPosition.x()][newMapPosition.y()] = Transmission::empty | actor->color(); + else if (survive == GameEntity::DestroyedActor) + { + m_actors[item->color()]->addRoundPoints(actor->getRoundPoints()); + actor->finishRound(true); + map[newMapPosition.x()][newMapPosition.y()] = Transmission::death | actor->color(); + setFinishRound(); } } } @@ -161,7 +157,7 @@ invalid_direction: } map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | - i.key() | Util::actorMovementToTransmission(direction); + color | Util::actorMovementToTransmission(direction); /* DEBUG: uncomments to disable auto-movement */ //direction = Actor::None; @@ -169,7 +165,7 @@ invalid_direction: if (direction == Actor::None) { /* set actor to non-moving */ - m_actorMovements[i.key()] = Actor::None; + m_actorMovements[color] = Actor::None; i.remove(); } } @@ -228,11 +224,12 @@ void Server::colorizeBlocks(Transmission::map_t map) i.setValue(--val); else { + QPoint block = i.key(); /* check for actor collision */ bool skip = false; foreach (Actor *actor, m_actors) { - if (CoordToMapPosition(actor->pos().toPoint()) == i.key()) + if (CoordToMapPosition(actor->pos().toPoint()) == block) skip = true; if (skip) break; @@ -240,7 +237,6 @@ void Server::colorizeBlocks(Transmission::map_t map) if (skip) continue; - QPoint block = i.key(); map[block.x()][block.y()] |= Transmission::block | Color::none; updateMap(map, block.x(), block.y()); m_blocks.append(block); @@ -510,16 +506,14 @@ void Server::keyPressUpdate() } } -void Server::setRoundFinished(bool value) -{ - m_roundFinished = value; -} - void Server::initRoundMap() { qDebug() << "[initRoundMap] New round starts..."; m_tickTimer->stop(); + /* reset scene and clean up items */ + reset(); + /* create new map */ Transmission::map_t map = Util::createDemoMap(); Util::placeActors(map, m_maxplayers, Color::order); @@ -542,11 +536,54 @@ void Server::initRoundMap() Util::deleteMap(map); map = NULL; - m_roundFinished = false; m_actorMovements.clear(); + + disconnect(AudioManager::self()->audioPlayer(), NULL, this, NULL); + connect(AudioManager::self()->audioPlayer(), SIGNAL(finished()), this, SLOT(startGame())); + AudioManager::self()->play(Sound::Intro, true); m_tickTimer->start(); } +void Server::startGame() +{ + m_running = true; +} + +void Server::stopGame(bool delay) +{ + /* first finish previous round */ + foreach(Actor *actor, m_actors) + actor->finishRound(); + m_finishRound = false; + m_running = false; + + /* add delay if requested */ + if (delay) + { + disconnect(AudioManager::self()->audioPlayer(), NULL, this, NULL); + connect(AudioManager::self()->audioPlayer(), SIGNAL(finished()), this, SLOT(stopGame())); + AudioManager::self()->play(Sound::Die, true); + return; + } + + /* do next-round work */ + ++m_curRound; + if(m_rounds == 0 || m_curRound < m_rounds) + initRoundMap(); + else + { + /* end of game */ + qDebug() << "All round finished. Exiting..."; + qApp->quit(); + } +} + + +void Server::setFinishRound() +{ + m_finishRound = true; +} + bool Server::parseCommandline() { AnyOption opt; @@ -583,7 +620,7 @@ bool Server::parseCommandline() << " Disable random colorized blocks" << endl << endl; opt.setOption("rounds", 'r'); - out << " -r, --rounds [1..n]" << endl + out << " -r, --rounds [0 | 1..n]" << endl << " Number of rounds to play" << endl << " Default: " << m_rounds << endl << endl; -- cgit v1.2.3