| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2015 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D 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 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #ifndef STATEMACHINE_HPP |
| 17 | #define STATEMACHINE_HPP |
| 18 | |
| 19 | #include "renderer.hpp" |
| 20 | #include "updateableif.hpp" |
| 21 | |
| 22 | #include <QObject> |
| 23 | #include <QTimer> |
| 24 | |
| 25 | #include <functional> |
| 26 | #include <map> |
| 27 | |
| 28 | class InputHandler; |
| 29 | |
| 30 | //! The main state machine of the game. |
| 31 | class StateMachine : public QObject, public UpdateableIf |
| 32 | { |
| 33 | Q_OBJECT |
| 34 | |
| 35 | public: |
| 36 | enum class State |
| 37 | { |
| 38 | Init, |
| 39 | DoIntro, |
| 40 | Menu, |
| 41 | MenuTransitionIn, |
| 42 | MenuTransitionOut, |
| 43 | GameTransitionIn, // Active when a race starts |
| 44 | GameTransitionOut, // Active when a race is over |
| 45 | DoStartlights, |
| 46 | Play |
| 47 | }; |
| 48 | |
| 49 | //! Constructor. |
| 50 | StateMachine(InputHandler & inputHandler); |
| 51 | |
| 52 | //! Destructor. |
| 53 | virtual ~StateMachine(); |
| 54 | |
| 55 | //! Return the singleton instance. |
| 56 | static StateMachine & instance(); |
| 57 | |
| 58 | void quit(); |
| 59 | |
| 60 | StateMachine::State state() const; |
| 61 | |
| 62 | //! \reimp |
| 63 | virtual bool update() override; |
| 64 | |
| 65 | //! \reimp |
| 66 | virtual void reset() override; |
| 67 | |
| 68 | public slots: |
| 69 | |
| 70 | void endFadeIn(); |
| 71 | |
| 72 | void endFadeOut(); |
| 73 | |
| 74 | void endStartlightAnimation(); |
| 75 | |
| 76 | void finishRace(); |
| 77 | |
| 78 | signals: |
| 79 | |
| 80 | void fadeInRequested(int, int, int); |
| 81 | |
| 82 | void fadeOutRequested(int, int, int); |
| 83 | |
| 84 | void fadeOutFlashRequested(int, int, int); |
| 85 | |
| 86 | void soundsStopped(); |
| 87 | |
| 88 | void startlightAnimationRequested(); |
| 89 | |
| 90 | void renderingEnabled(bool); |
| 91 | |
| 92 | void exitGameRequested(); |
| 93 | |
| 94 | private: |
| 95 | void stateInit(); |
| 96 | |
| 97 | void stateDoIntro(); |
| 98 | |
| 99 | void stateMenu(); |
| 100 | |
| 101 | void stateMenuTransitionIn(); |
| 102 | |
| 103 | void stateMenuTransitionOut(); |
| 104 | |
| 105 | void stateGameTransitionIn(); |
| 106 | |
| 107 | void stateGameTransitionOut(); |
| 108 | |
| 109 | void stateDoStartlights(); |
| 110 | |
| 111 | void statePlay(); |
| 112 | |
| 113 | static StateMachine * m_instance; |
| 114 | |
| 115 | typedef std::map<State, std::function<void()>> StateToFunctionMap; |
| 116 | |
| 117 | StateToFunctionMap m_stateToFunctionMap; |
| 118 | |
| 119 | State m_state; |
| 120 | |
| 121 | State m_oldState; |
| 122 | |
| 123 | bool m_raceFinished; |
| 124 | |
| 125 | InputHandler & m_inputHandler; |
| 126 | |
| 127 | QTimer m_timer; |
| 128 | }; |
| 129 | |
| 130 | #endif // STATEMACHINE_HPP |