1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
/*
Copyright 2006 Pierre Ducroquet <pinaraf@pinaraf.info>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "localgame.h"
#include <KDebug>
#include <QApplication>
LocalGame::LocalGame(QObject *parent) :
Game(parent)
{
}
void LocalGame::start()
{
if (!m_gameMachine.isRunning()) {
buildMachine();
kDebug() << "Starting machine";
m_gameMachine.start();
qApp->processEvents(); // Really important : ignoring this will not apply the change soon enough
kDebug() << "Machine state" << m_gameMachine.isRunning();
}
}
void LocalGame::stop()
{
if (m_gameMachine.isRunning()) {
m_gameMachine.stop();
qApp->processEvents(); // Really important : ignoring this will not apply the change soon enough
kDebug() << "Machine state" << m_gameMachine.isRunning();
}
}
void LocalGame::addPlayer(Player *player)
{
player->setParent(&m_gameMachine);
if (!m_players.contains(player))
m_players << player;
}
void LocalGame::buildMachine()
{
kDebug() << "Building machine";
if (m_gameMachine.isRunning())
return;
// Player is a subclass of QState and the constructor of Player already adds
// the new Player object to m_gameMachine by passing it to the superclass
// constructor QState(QState *parent = 0).
// Accordingly, we can instantly go ahead with configuring the other
// parts of the machine.
m_gameMachine.setInitialState(m_neutral);
connect(m_neutral, SIGNAL(donePlaying()), this, SLOT(playerIsDone()));
m_neutral->addTransition(m_neutral, SIGNAL(donePlaying()), m_players[0]);
// Now add transitions
for (int i = 0 ; i < m_players.count() ; i++)
{
Player *player = m_players[i];
Player *nextPlayer;
if (i+1 >= m_players.count())
nextPlayer = m_neutral;
else
nextPlayer = m_players[i + 1];
kDebug() << "Adding transition from "
<< player->name() << " to " << nextPlayer->name();
player->addTransition(player, SIGNAL(donePlaying()), nextPlayer);
connect(player, SIGNAL(donePlaying()), this, SLOT(playerIsDone()));
}
}
void LocalGame::playerIsDone()
{
kDebug() << "It seems a player is done :" << currentPlayer()->name();
}
|