File indexing completed on 2024-04-14 03:59:49

0001 /*
0002     SPDX-FileCopyrightText: 2012 Christian Krippendorf <Coding@Christian-Krippendorf.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 // own
0008 #include "demoanimation.h"
0009 
0010 // KMahjongg
0011 #include "gamedata.h"
0012 #include "kmahjongg_debug.h"
0013 
0014 DemoAnimation::DemoAnimation(QObject * parent)
0015     : QTimer(parent)
0016     , m_step(0)
0017     , m_animationSpeed(0)
0018     , m_gameData(nullptr)
0019 {
0020     connect(this, &DemoAnimation::timeout, this, &DemoAnimation::timeoutOccurred);
0021 }
0022 
0023 DemoAnimation::~DemoAnimation()
0024 {
0025 }
0026 
0027 void DemoAnimation::setAnimationSpeed(int animationSpeed)
0028 {
0029     m_animationSpeed = animationSpeed;
0030 }
0031 
0032 int DemoAnimation::getAnimationSpeed() const
0033 {
0034     return m_animationSpeed;
0035 }
0036 
0037 void DemoAnimation::start(GameData * gameData)
0038 {
0039     m_gameData = gameData;
0040 
0041     QTimer::start(m_animationSpeed);
0042 }
0043 
0044 void DemoAnimation::stop()
0045 {
0046     QTimer::stop();
0047 
0048     m_step = 0;
0049 }
0050 
0051 void DemoAnimation::timeoutOccurred()
0052 {
0053     switch (m_step++ % 5) {
0054         case 0:
0055             // Test if we got a game data object.
0056             if (m_gameData == nullptr) {
0057                 qCDebug(KMAHJONGG_LOG) << "m_pGameData is null";
0058 
0059                 stop();
0060                 return;
0061             }
0062 
0063             if (!m_gameData->findMove(m_stFirst, m_stSecond)) {
0064                 // First stop the animation.
0065                 stop();
0066 
0067                 if (m_gameData->m_tileNum == 0) {
0068                     // The computer has won the game.
0069                     Q_EMIT gameOver(true);
0070                 } else {
0071                     // The computer lost the game.
0072                     // setStatusText(i18n("Your computer has lost the game."));
0073                     Q_EMIT gameOver(false);
0074                 }
0075             }
0076 
0077             break;
0078         case 1:
0079         case 3:
0080             Q_EMIT changeItemSelectedState(m_stFirst, true);
0081             Q_EMIT changeItemSelectedState(m_stSecond, true);
0082 
0083             break;
0084         case 2:
0085             Q_EMIT changeItemSelectedState(m_stFirst, false);
0086             Q_EMIT changeItemSelectedState(m_stSecond, false);
0087 
0088             break;
0089         case 4:
0090             m_stFirst.f -= TILE_OFFSET;
0091             Q_EMIT removeItem(m_stFirst);
0092             m_stSecond.f -= TILE_OFFSET;
0093             Q_EMIT removeItem(m_stSecond);
0094 
0095             break;
0096     }
0097 }
0098 
0099 #include "moc_demoanimation.cpp"