[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kmahjongg/qgraphic] /: Add first demo animation code.
From:       Christian Knippendorf <coding () christian-krippendorf ! de>
Date:       2012-09-21 20:56:48
Message-ID: 20120921205648.3B4C9A6141 () git ! kde ! org
[Download RAW message or body]

Git commit 78ea4e11292ca09a2c1bdabd21f018b12fb82a01 by Christian Knippendorf.
Committed on 03/07/2012 at 14:56.
Pushed by wrohdewald into branch 'qgraphic'.

Add first demo animation code.

svn path=/branches/work/krippendorf/kmahjongg/; revision=1303802

M  +59   -13   DemoAnimation.cpp
M  +28   -15   DemoAnimation.h
M  +10   -3    GameScene.cpp
M  +11   -2    GameScene.h
M  +70   -13   GameView.cpp
M  +44   -18   GameView.h
M  +1    -1    kmahjongg.cpp

http://commits.kde.org/kmahjongg/78ea4e11292ca09a2c1bdabd21f018b12fb82a01

diff --git a/DemoAnimation.cpp b/DemoAnimation.cpp
index 48b2534..634f38a 100644
--- a/DemoAnimation.cpp
+++ b/DemoAnimation.cpp
@@ -13,16 +13,18 @@
  * 02110-1301, USA. */
 
 #include "DemoAnimation.h"
-#include "GameItem.h"
+#include "GameData.h"
 
 #include <QList>
 
+#include <KDebug>
+
 
 DemoAnimation::DemoAnimation(QObject * pParent)
     : QTimer(pParent),
     m_iAnimationSpeed(0),
-    m_iRepetitions(0),
-    m_iFinishedRepetitions(0)
+    m_iStep(0),
+    m_pGameData(NULL)
 {
     connect(this, SIGNAL(timeout()), this, SLOT(timeoutOccurred()));
 }
@@ -31,11 +33,6 @@ DemoAnimation::~DemoAnimation()
 {
 }
 
-void DemoAnimation::setRepetitions(int iRepetitions)
-{
-    m_iRepetitions = iRepetitions;
-}
-
 void DemoAnimation::setAnimationSpeed(int iAnimationSpeed)
 {
     m_iAnimationSpeed = iAnimationSpeed;
@@ -46,21 +43,70 @@ int DemoAnimation::getAnimationSpeed() const
     return m_iAnimationSpeed;
 }
 
-int DemoAnimation::getRepetitions() const
+void DemoAnimation::start(GameData * pGameData)
 {
-    return m_iRepetitions;
-}
+    m_pGameData = pGameData;
 
-void DemoAnimation::start()
-{
     QTimer::start(m_iAnimationSpeed);
 }
 
 void DemoAnimation::stop()
 {
     QTimer::stop();
+
+    m_iStep = 0;
 }
 
 void DemoAnimation::timeoutOccurred()
 {
+    switch (m_iStep++ % 5) {
+    case 0:
+        // Test if we got a game data object.
+        if (m_pGameData == NULL) {
+            kDebug() << "m_pGameData is null";
+
+            stop();
+            return;
+        }
+
+        if (!m_pGameData->findMove(m_stFirst, m_stSecond)) {
+            // First stop the animation.
+            stop();
+
+            if (m_pGameData->TileNum == 0) {
+                // The computer has won the game.
+                emit gameOver(true);
+            } else {
+                // The computer lost the game.
+                // setStatusText(i18n("Your computer has lost the game."));
+                emit gameOver(false);
+
+                // while (Game->TileNum < Game->MaxTileNum) {
+                //     putTileInBoard(Game->MoveListData(Game->TileNum), false);
+                //     Game->TileNum++;
+                //     putTileInBoard(Game->MoveListData(Game->TileNum));
+                //     Game->TileNum++;
+                //     drawTileNumber();
+                // }
+            }
+        }
+
+        break;
+    case 1:
+    case 3:
+        emit changeItemSelectedState(m_stFirst, true);
+        emit changeItemSelectedState(m_stSecond, true);
+
+        break;
+    case 2:
+        emit changeItemSelectedState(m_stFirst, false);
+        emit changeItemSelectedState(m_stSecond, false);
+
+        break;
+    case 4:
+        emit removeItem(m_stFirst);
+        emit removeItem(m_stSecond);
+
+        break;
+    }
 }
diff --git a/DemoAnimation.h b/DemoAnimation.h
index 9755044..d9b2938 100644
--- a/DemoAnimation.h
+++ b/DemoAnimation.h
@@ -17,8 +17,11 @@
 
 #include <QTimer>
 
+#include "KmTypes.h"
+
 
 // Forward declarations...
+class GameData;
 
 /**
  * A class for a demo animation with the help of selection.
@@ -38,12 +41,6 @@ public:
     ~DemoAnimation();
 
     /**
-     * Set the count of repetitions before the items will be removed.
-     *
-     * @param iRepetitions The number of repetitions. */
-    void setRepetitions(int iRepetitions);
-
-    /**
      * Set the animation speed in milliseconds.
      *
      * @param iAnimationSpeed The animation speed in milliseconds. */
@@ -56,14 +53,10 @@ public:
     int getAnimationSpeed() const;
 
     /**
-     * Get the number of repetitions set.
+     * Override of QTimer.
      *
-     * @return The number of repetitions. */
-    int getRepetitions() const;
-
-    /**
-     * Override of QTimer. */
-    void start();
+     * @param pGameData The data object to handle with for this animation process. */
+    void start(GameData * pGameData);
 
     /**
      * Override of QTimer. */
@@ -72,6 +65,22 @@ public:
 public slots:
 
 signals:
+    /**
+     * Emits when the game is over.
+     *
+     * @param bWon True if computer won the game, else false. */
+    void gameOver(bool bWon);
+
+    /**
+     * Emit to remove the given item. */
+    void removeItem(POSITION & stItem);
+
+    /**
+     * Emit to set the selected state of the given item.
+     *
+     * @param stItem The position of the item to change the selected state.
+     * @param bSelected THe item should be selected on true, else deselected. */
+    void changeItemSelectedState(POSITION & stItem, bool bSelected);
 
 private slots:
     /**
@@ -79,9 +88,13 @@ private slots:
     void timeoutOccurred();
 
 private:
-    int m_iRepetitions;
-    int m_iFinishedRepetitions;
+    int m_iStep;
     int m_iAnimationSpeed;
+
+    POSITION m_stFirst;
+    POSITION m_stSecond;
+
+    GameData * m_pGameData;
 };
 
 
diff --git a/GameScene.cpp b/GameScene.cpp
index 5aa8556..e9a7563 100644
--- a/GameScene.cpp
+++ b/GameScene.cpp
@@ -58,7 +58,7 @@ void GameScene::initializeGameItemsArray()
     }
 }
 
-void GameScene::addItem(GameItem *pGameItem)
+void GameScene::addItem(GameItem * pGameItem)
 {
     QGraphicsScene::addItem(pGameItem);
 
@@ -70,7 +70,7 @@ void GameScene::addItem(GameItem *pGameItem)
     addItemToPositionArray(pGameItem);
 }
 
-void GameScene::removeItem(GameItem *pGameItem)
+void GameScene::removeItem(GameItem * pGameItem)
 {
     m_pGameItemsArray[pGameItem->getXPosition()][pGameItem->getYPosition()]
         [pGameItem->getZPosition()] = NULL;
@@ -78,7 +78,14 @@ void GameScene::removeItem(GameItem *pGameItem)
     QGraphicsScene::removeItem(pGameItem);
 }
 
-void GameScene::addItemToPositionArray(GameItem *pGameItem)
+void GameScene::removeItem(POSITION & stItemPos)
+{
+    GameItem * pGameItem = m_pGameItemsArray[stItemPos.x][stItemPos.y][stItemPos.e];
+
+    removeItem(pGameItem);
+}
+
+void GameScene::addItemToPositionArray(GameItem * pGameItem)
 {
     // Take a look, if the place is already taken.
     if (m_pGameItemsArray[pGameItem->getXPosition()][pGameItem->getYPosition()]
diff --git a/GameScene.h b/GameScene.h
index c62d841..8262079 100644
--- a/GameScene.h
+++ b/GameScene.h
@@ -17,11 +17,14 @@
 
 #include <QGraphicsScene>
 
+#include "KmTypes.h"
+
 #define BOARD_WIDTH 32
 #define BOARD_HEIGHT 16
 #define BOARD_DEPH 5
 
 
+// Forward declarations...
 class GameItem;
 class GameData;
 class GameWidget;
@@ -62,11 +65,17 @@ public:
 
     /**
      * Override from QGraphicsScene. */
-    void addItem(GameItem *pGameItem);
+    void addItem(GameItem * pGameItem);
 
     /**
      * Override from QGraphicsScene. */
-    void removeItem(GameItem *pGameItem);
+    void removeItem(GameItem * pGameItem);
+
+    /**
+     * Override from QGraphicsScene with POSITION parameter.
+     *
+     * @param stItemPos The item position. */
+    void removeItem(POSITION & stItemPos);
 
     /**
      * Override from QGraphicsScene. */
diff --git a/GameView.cpp b/GameView.cpp
index 7eff5ec..b723bf0 100644
--- a/GameView.cpp
+++ b/GameView.cpp
@@ -17,6 +17,7 @@
 #include "GameScene.h"
 #include "GameItem.h"
 #include "SelectionAnimation.h"
+#include "DemoAnimation.h"
 #include "kmahjongglayout.h"
 #include "kmahjonggtileset.h"
 #include "kmahjonggbackground.h"
@@ -42,6 +43,7 @@ GameView::GameView(GameScene *pGameScene, QWidget *pParent)
     m_pTiles(new KMahjonggTileset()),
     m_pSelectedItem(NULL),
     m_pHelpAnimation(new SelectionAnimation(this)),
+    m_pDemoAnimation(new DemoAnimation(this)),
     m_bMatch(false)
 {
     // Some settings to the QGraphicsView.
@@ -55,8 +57,15 @@ GameView::GameView(GameScene *pGameScene, QWidget *pParent)
     m_pHelpAnimation->setAnimationSpeed(ANIMATION_SPEED);
     m_pHelpAnimation->setRepetitions(2);
 
+    // Init DemoAnimation
+    m_pDemoAnimation->setAnimationSpeed(ANIMATION_SPEED);
+
     // Connections
     connect(scene(), SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
+
+    connect(m_pDemoAnimation, SIGNAL(changeItemSelectedState(POSITION &, bool)), this,
+        SLOT(changeItemSelectedState(POSITION &, bool)));
+    connect(m_pDemoAnimation, SIGNAL(removeItem(POSITION &)), this, SLOT(removeItem(POSITION &)));
 }
 
 GameView::~GameView()
@@ -120,7 +129,7 @@ void GameView::selectionChanged()
     QList<GameItem *> selectedGameItems = scene()->selectedItems();
 
     // When no item is selected or help animation is running, there is nothing to do.
-    if (selectedGameItems.size() < 1 || checkHelpAnimationActive(false)) {
+    if (selectedGameItems.size() < 1 || checkHelpAnimationActive() || checkDemoAnimationActive()) {
         return;
     }
 
@@ -145,22 +154,13 @@ void GameView::selectionChanged()
             // Update the removed tiles in GameData.
             m_pGameData->setRemovedTilePair(stFirstPos, stSecondPos);
 
-            // Clear the positions in the game data object and remove the items from the scene.
-            m_pGameData->putTile(stFirstPos.e, stFirstPos.y, stFirstPos.x, 0);
-            m_pGameData->putTile(stSecondPos.e, stSecondPos.y, stSecondPos.x, 0);
-
-            scene()->removeItem(m_pSelectedItem);
-            scene()->removeItem(selectedGameItems.at(0));
-
-            // Decrement the tilenum variable from GameData.
-            m_pGameData->TileNum -= 2;
+            // Remove the items.
+            removeItem(stFirstPos);
+            removeItem(stSecondPos);
 
             // Reset the selected item variable.
             m_pSelectedItem = NULL;
 
-            // The item numbers changed, so we need to populate the new informations.
-            populateItemNumber();
-
             // Test whether the game is over or not.
             if (m_pGameData->TileNum == 0) {
                 emit gameOver(m_pGameData->MaxTileNum, m_usCheatsUsed);
@@ -180,6 +180,46 @@ void GameView::selectionChanged()
     }
 }
 
+void GameView::removeItem(POSITION & stItemPos)
+{
+    // Put an empty item in the data object. (data part)
+    m_pGameData->putTile(stItemPos.e, stItemPos.y, stItemPos.x, 0);
+
+    // Remove the item from the scene object. (graphic part)
+    scene()->removeItem(stItemPos);
+
+    // Decrement the tilenum variable from GameData.
+    m_pGameData->TileNum -= 1;
+
+    // If TileNum is % 2 then update the number in the status bar.
+    if (!(m_pGameData->TileNum % 2)) {
+        // The item numbers changed, so we need to populate the new informations.
+        populateItemNumber();
+    }
+}
+
+void GameView::startDemo()
+{
+    kDebug() << "Starting demo mode";
+
+    // Stop any helping animation.
+    checkHelpAnimationActive(true);
+
+    // Stop demo animation, if anyone is running.
+    checkDemoAnimationActive(true);
+
+    // Create a new game with the actual game number.
+    createNewGame(m_lGameNumber);
+
+    // Start the demo mode.
+    m_pDemoAnimation->start(m_pGameData);
+}
+
+void GameView::changeItemSelectedState(POSITION & stItemPos, bool bSelected)
+{
+    getItemFromPosition(stItemPos)->setSelected(bSelected);
+}
+
 void GameView::helpMove()
 {
     POSITION stItem1;
@@ -237,6 +277,18 @@ bool GameView::checkHelpAnimationActive(bool bStop)
     return bActive;
 }
 
+bool GameView::checkDemoAnimationActive(bool bStop)
+{
+    bool bActive = m_pDemoAnimation->isActive();
+
+    // If animation is running and it should be closed, do so.
+    if (bActive && bStop) {
+        m_pDemoAnimation->stop();
+    }
+
+    return bActive;
+}
+
 bool GameView::validMovesAvailable(bool bSilent)
 {
     POSITION stItem1;
@@ -588,6 +640,11 @@ void GameView::angleSwitchCW()
 
 void GameView::mousePressEvent(QMouseEvent * pMouseEvent)
 {
+    // No mouse events when the demo mode is active.
+    if (checkDemoAnimationActive()) {
+        return;
+    }
+
     // If any help mode is active, ... stop it.
     checkHelpAnimationActive(true);
 
diff --git a/GameView.h b/GameView.h
index 15e4273..d62476e 100644
--- a/GameView.h
+++ b/GameView.h
@@ -26,6 +26,7 @@ class GameScene;
 class GameData;
 class GameItem;
 class SelectionAnimation;
+class DemoAnimation;
 class KMahjonggLayout;
 class KMahjonggTileset;
 class KMahjonggBackground;
@@ -52,6 +53,23 @@ public:
     ~GameView();
 
     /**
+     * Items where added to the scene and should now be layouted. */
+    void updateItemsPosition();
+
+    /**
+     * Get the POSITION struct copy of the GameItem positions.
+     *
+     * @param pGameItem The GameItem to get the position struct from. */
+    POSITION getPositionFromItem(GameItem * pGameItem);
+
+    /**
+     * Get the GameItem of a POSITION.
+     *
+     * @param stGamePos The position of the item.
+     * @return The GameItem object or NULL if no one exist with the given position. */
+    GameItem * getItemFromPosition(POSITION stGamePos);
+
+    /**
      * Sets the status text.
      *
      * @param rText The new status text. */
@@ -105,7 +123,14 @@ public:
      *
      * @param bStop Stop the help animation if running.
      * @return Return true if the help animation was running else false. */
-    bool checkHelpAnimationActive(bool bStop);
+    bool checkHelpAnimationActive(bool bStop = false);
+
+    /**
+     * Test for active demo animation and maybe close.
+     *
+     * @param bStop Stop the demo animation if running.
+     * @return Return true if the demo animation was running else false. */
+    bool checkDemoAnimationActive(bool bStop = false);
 
     /**
      * Set the match variable. If set to true, the matching items to the selected will be animated.
@@ -121,6 +146,16 @@ public:
 
 public slots:
     /**
+     * Remove the given item.
+     *
+     * @param stItemPos The item position. */
+    void removeItem(POSITION &stItemPos);
+
+    /**
+     * Starts the demo animation. */
+    void startDemo();
+
+    /**
      * Switch the view angle to the next right around. */
     void angleSwitchCCW();
 
@@ -159,23 +194,6 @@ public slots:
     void createNewGame(int iGameNumber = -1);
 
     /**
-     * Items where added to the scene and should now be layouted. */
-    void updateItemsPosition();
-
-    /**
-     * Get the POSITION struct copy of the GameItem positions.
-     *
-     * @param pGameItem The GameItem to get the position struct from. */
-    POSITION getPositionFromItem(GameItem * pGameItem);
-
-    /**
-     * Get the GameItem of a POSITION.
-     *
-     * @param stGamePos The position of the item.
-     * @return The GameItem object or NULL if no one exist with the given position. */
-    GameItem * getItemFromPosition(POSITION stGamePos);
-
-    /**
      * Populates the number of the items, by emit a signal: itemNumberChanged(...). */
     void populateItemNumber();
 
@@ -230,6 +248,13 @@ signals:
 
 private slots:
     /**
+     * Change the selected state of the given item.
+     *
+     * @param stItemPos The position of the item.
+     * @param bSelected The selection state to set. */
+    void changeItemSelectedState(POSITION & stItemPos, bool bSelected);
+
+    /**
      * Gets called when a pair was selected. */
     void selectionChanged();
 
@@ -282,6 +307,7 @@ private:
     QString * m_pBackgroundPath;
 
     SelectionAnimation * m_pHelpAnimation;
+    DemoAnimation * m_pDemoAnimation;
 
     KMahjonggLayout * m_pBoardLayout;
     KMahjonggTileset * m_pTiles;
diff --git a/kmahjongg.cpp b/kmahjongg.cpp
index 8e8acb2..c563776 100644
--- a/kmahjongg.cpp
+++ b/kmahjongg.cpp
@@ -310,7 +310,7 @@ void KMahjongg::demoMode()
         // we assume demo mode removes tiles so we can
         // disable redo here.
 //        bw->Game->allow_redo = false;
-//        bw->startDemoMode();
+        m_pGameView->startDemo();
     }
 
 }

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic