1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2012 Jussi Lind <jussi.lind@iki.fi>
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 STARTLIGHTS_HPP
17 #define STARTLIGHTS_HPP
18 
19 #include <QObject>
20 #include <QString>
21 #include <QTimer>
22 
23 #include <MCVectorAnimation>
24 
25 #include <functional>
26 #include <map>
27 #include <string>
28 
29 class Race;
30 
31 //! Startlight model that controls the position and animation
32 //! of the startlights.
33 class Startlights : public QObject
34 {
35     Q_OBJECT
36 
37 public:
38     //! Startlights animation sequence states.
39     enum class State
40     {
41         Init,
42         Appear,
43         FirstRow,
44         SecondRow,
45         ThirdRow,
46         Go,
47         Disappear,
48         End
49     };
50 
51     //! Constructor.
52     Startlights();
53 
54     State state() const;
55 
56     void setDimensions(size_t width, size_t height);
57 
58     const MCVector3dF & pos() const;
59 
60     float glowScale() const;
61 
62 signals:
63 
64     void messageRequested(QString message);
65 
66     void raceStarted();
67 
68     void animationEnded();
69 
70 public slots:
71 
72     void beginAnimation();
73 
74 private slots:
75 
76     void updateAnimation();
77 
78 private:
79     typedef std::map<State, std::function<void()>> StateToFunctionMap;
80     StateToFunctionMap m_stateToFunctionMap;
81 
82     void stateInit();
83 
84     void stateAppear();
85 
86     void stateFirstRow();
87 
88     void stateSecondRow();
89 
90     void stateThirdRow();
91 
92     void stateGo();
93 
94     void stateDisappear();
95 
96     void stateEnd();
97 
98     bool timeElapsed(size_t limit);
99 
100     State m_state;
101 
102     size_t m_counter;
103 
104     size_t m_stepsPerState;
105 
106     MCVector3dF m_pos;
107 
108     size_t m_width;
109 
110     size_t m_height;
111 
112     float m_glowScale;
113 
114     MCVectorAnimation m_animation;
115 
116     QTimer m_timer;
117 };
118 
119 #endif // STARTLIGHTS_HPP
120