File indexing completed on 2024-12-01 03:47:55

0001 /*
0002     SPDX-FileCopyrightText: 2006 Pierre Ducroquet <pinaraf@pinaraf.info>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "localgame.h"
0008 #include <QDebug>
0009 #include <QApplication>
0010 
0011 LocalGame::LocalGame(QObject *parent) :
0012     Game(parent)
0013 {
0014 }
0015 
0016 void LocalGame::start()
0017 {
0018     if (!m_gameMachine.isRunning()) {
0019         buildMachine();
0020         //qDebug() << "Starting machine";
0021         m_gameMachine.start();
0022         qApp->processEvents();  // Really important : ignoring this will not apply the change soon enough
0023         //qDebug() << "Machine state" << m_gameMachine.isRunning();
0024     }
0025 }
0026 
0027 void LocalGame::stop()
0028 {
0029     if (m_gameMachine.isRunning()) {
0030         m_gameMachine.stop();
0031         qApp->processEvents();  // Really important : ignoring this will not apply the change soon enough
0032         //qDebug() << "Machine state" << m_gameMachine.isRunning();
0033     }
0034 }
0035 
0036 void LocalGame::addPlayer(Player *player)
0037 {
0038     player->setParent(&m_gameMachine);
0039     if (!m_players.contains(player))
0040         m_players << player;
0041 }
0042 
0043 void LocalGame::buildMachine()
0044 {
0045     //qDebug() << "Building machine";
0046     if (m_gameMachine.isRunning())
0047         return;
0048 
0049     // Player is a subclass of QState and the constructor of Player already adds
0050     // the new Player object to m_gameMachine by passing it to the superclass
0051     // constructor QState(QState *parent = 0).
0052     // Accordingly, we can instantly go ahead with configuring the other
0053     // parts of the machine.
0054 
0055     m_gameMachine.setInitialState(m_neutral);
0056 
0057     connect(m_neutral, &NeutralPlayer::donePlaying, this, &LocalGame::playerIsDone);
0058     m_neutral->addTransition(m_neutral, &NeutralPlayer::donePlaying, m_players[0]);
0059 
0060     // Now add transitions
0061     for (int i = 0 ; i < m_players.count() ; i++)
0062     {
0063         Player *player = m_players[i];
0064         Player *nextPlayer;
0065         if (i+1 >= m_players.count())
0066             nextPlayer = m_neutral;
0067         else
0068             nextPlayer = m_players[i + 1];
0069 
0070         //qDebug() << "Adding transition from "
0071                  //<< player->name() << " to " << nextPlayer->name();
0072         player->addTransition(player, &Player::donePlaying, nextPlayer);
0073         connect(player, &Player::donePlaying, this, &LocalGame::playerIsDone);
0074     }
0075 }
0076 
0077 void LocalGame::playerIsDone()
0078 {
0079     //qDebug() << "It seems a player is done :" << currentPlayer()->name();
0080 }