| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #ifndef STATES_H |
| 52 | #define STATES_H |
| 53 | |
| 54 | //Qt |
| 55 | #include <QKeyEventTransition> |
| 56 | #include <QSignalTransition> |
| 57 | #include <QState> |
| 58 | |
| 59 | class GraphicsScene; |
| 60 | QT_BEGIN_NAMESPACE |
| 61 | class QStateMachine; |
| 62 | QT_END_NAMESPACE |
| 63 | |
| 64 | class PlayState : public QState |
| 65 | { |
| 66 | public: |
| 67 | explicit PlayState(GraphicsScene *scene, QState *parent = nullptr); |
| 68 | ~PlayState(); |
| 69 | |
| 70 | protected: |
| 71 | void onEntry(QEvent *) override; |
| 72 | |
| 73 | private : |
| 74 | GraphicsScene *scene; |
| 75 | QStateMachine *machine; |
| 76 | int currentLevel; |
| 77 | int score; |
| 78 | |
| 79 | friend class UpdateScoreState; |
| 80 | friend class UpdateScoreTransition; |
| 81 | friend class WinTransition; |
| 82 | friend class CustomSpaceTransition; |
| 83 | friend class WinState; |
| 84 | friend class LostState; |
| 85 | friend class LevelState; |
| 86 | }; |
| 87 | |
| 88 | class LevelState : public QState |
| 89 | { |
| 90 | public: |
| 91 | LevelState(GraphicsScene *scene, PlayState *game, QState *parent = nullptr); |
| 92 | protected: |
| 93 | void onEntry(QEvent *) override; |
| 94 | private : |
| 95 | void initializeLevel(); |
| 96 | GraphicsScene *scene; |
| 97 | PlayState *game; |
| 98 | }; |
| 99 | |
| 100 | class PauseState : public QState |
| 101 | { |
| 102 | public: |
| 103 | explicit PauseState(GraphicsScene *scene, QState *parent = nullptr); |
| 104 | |
| 105 | protected: |
| 106 | void onEntry(QEvent *) override; |
| 107 | void onExit(QEvent *) override; |
| 108 | private : |
| 109 | GraphicsScene *scene; |
| 110 | }; |
| 111 | |
| 112 | class LostState : public QState |
| 113 | { |
| 114 | public: |
| 115 | LostState(GraphicsScene *scene, PlayState *game, QState *parent = nullptr); |
| 116 | |
| 117 | protected: |
| 118 | void onEntry(QEvent *) override; |
| 119 | void onExit(QEvent *) override; |
| 120 | private : |
| 121 | GraphicsScene *scene; |
| 122 | PlayState *game; |
| 123 | }; |
| 124 | |
| 125 | class WinState : public QState |
| 126 | { |
| 127 | public: |
| 128 | WinState(GraphicsScene *scene, PlayState *game, QState *parent = nullptr); |
| 129 | |
| 130 | protected: |
| 131 | void onEntry(QEvent *) override; |
| 132 | void onExit(QEvent *) override; |
| 133 | private : |
| 134 | GraphicsScene *scene; |
| 135 | PlayState *game; |
| 136 | }; |
| 137 | |
| 138 | class UpdateScoreState : public QState |
| 139 | { |
| 140 | public: |
| 141 | UpdateScoreState(QState *parent); |
| 142 | private: |
| 143 | }; |
| 144 | |
| 145 | //These transtion is used to update the score |
| 146 | class UpdateScoreTransition : public QSignalTransition |
| 147 | { |
| 148 | public: |
| 149 | UpdateScoreTransition(GraphicsScene *scene, PlayState *game, QAbstractState *target); |
| 150 | protected: |
| 151 | bool eventTest(QEvent *event) override; |
| 152 | private: |
| 153 | PlayState *game; |
| 154 | GraphicsScene *scene; |
| 155 | }; |
| 156 | |
| 157 | //These transtion test if we have won the game |
| 158 | class WinTransition : public QSignalTransition |
| 159 | { |
| 160 | public: |
| 161 | WinTransition(GraphicsScene *scene, PlayState *game, QAbstractState *target); |
| 162 | protected: |
| 163 | bool eventTest(QEvent *event) override; |
| 164 | private: |
| 165 | PlayState *game; |
| 166 | GraphicsScene *scene; |
| 167 | }; |
| 168 | |
| 169 | //These transtion is true if one level has been completed and the player want to continue |
| 170 | class CustomSpaceTransition : public QKeyEventTransition |
| 171 | { |
| 172 | public: |
| 173 | CustomSpaceTransition(QWidget *widget, PlayState *game, QEvent::Type type, int key); |
| 174 | protected: |
| 175 | bool eventTest(QEvent *event) override; |
| 176 | private: |
| 177 | PlayState *game; |
| 178 | }; |
| 179 | |
| 180 | #endif // STATES_H |
| 181 | |