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