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 #include "startlights.hpp"
17 #include "inputhandler.hpp"
18
Startlights()19 Startlights::Startlights()
20 : m_state(State::Init)
21 , m_counter(0)
22 , m_stepsPerState(60)
23 {
24 m_stateToFunctionMap[State::Init] = std::bind(&Startlights::stateInit, this);
25 m_stateToFunctionMap[State::Appear] = std::bind(&Startlights::stateAppear, this);
26 m_stateToFunctionMap[State::FirstRow] = std::bind(&Startlights::stateFirstRow, this);
27 m_stateToFunctionMap[State::SecondRow] = std::bind(&Startlights::stateSecondRow, this);
28 m_stateToFunctionMap[State::ThirdRow] = std::bind(&Startlights::stateThirdRow, this);
29 m_stateToFunctionMap[State::Go] = std::bind(&Startlights::stateGo, this);
30 m_stateToFunctionMap[State::Disappear] = std::bind(&Startlights::stateDisappear, this);
31 m_stateToFunctionMap[State::End] = std::bind(&Startlights::stateEnd, this);
32
33 connect(&m_timer, &QTimer::timeout, this, &Startlights::updateAnimation);
34 m_timer.setInterval(1000 / m_stepsPerState);
35 }
36
timeElapsed(size_t limit)37 bool Startlights::timeElapsed(size_t limit)
38 {
39 if (++m_counter > limit)
40 {
41 m_counter = 0;
42 return true;
43 }
44 return false;
45 }
46
updateAnimation()47 void Startlights::updateAnimation()
48 {
49 m_stateToFunctionMap[m_state]();
50 }
51
beginAnimation()52 void Startlights::beginAnimation()
53 {
54 m_state = State::Init;
55 m_timer.start();
56 }
57
setDimensions(size_t width,size_t height)58 void Startlights::setDimensions(size_t width, size_t height)
59 {
60 m_width = width;
61 m_height = height;
62 }
63
state() const64 Startlights::State Startlights::state() const
65 {
66 return m_state;
67 }
68
pos() const69 const MCVector3dF & Startlights::pos() const
70 {
71 return m_pos;
72 }
73
glowScale() const74 float Startlights::glowScale() const
75 {
76 return m_glowScale;
77 }
78
stateInit()79 void Startlights::stateInit()
80 {
81 const size_t second = m_stepsPerState;
82
83 m_pos = MCVector3dF(m_width / 2, 3 * m_height / 2, 0);
84 m_animation.init(
85 m_pos,
86 m_pos,
87 MCVector3dF(m_pos.i(), m_height / 2, 0),
88 second / 3);
89 m_state = State::Appear;
90 m_glowScale = 1.0;
91 InputHandler::setEnabled(false);
92 }
93
stateAppear()94 void Startlights::stateAppear()
95 {
96 const size_t second = m_stepsPerState;
97
98 m_animation.update();
99 if (timeElapsed(second))
100 {
101 m_state = State::FirstRow;
102 emit messageRequested("3");
103 }
104 }
105
stateFirstRow()106 void Startlights::stateFirstRow()
107 {
108 const size_t second = m_stepsPerState;
109
110 if (timeElapsed(second))
111 {
112 m_state = State::SecondRow;
113 emit messageRequested("2");
114 }
115 }
116
stateSecondRow()117 void Startlights::stateSecondRow()
118 {
119 const size_t second = m_stepsPerState;
120
121 if (timeElapsed(second))
122 {
123 m_state = State::ThirdRow;
124 emit messageRequested("1");
125 }
126 }
127
stateThirdRow()128 void Startlights::stateThirdRow()
129 {
130 const size_t second = m_stepsPerState;
131
132 if (timeElapsed(second))
133 {
134 m_state = State::Go;
135 emit messageRequested(QObject::tr("GO!!!"));
136 emit raceStarted();
137 InputHandler::setEnabled(true);
138 }
139 }
140
stateGo()141 void Startlights::stateGo()
142 {
143 const size_t second = m_stepsPerState;
144
145 if (timeElapsed(second))
146 {
147 m_state = State::Disappear;
148 m_animation.init(
149 m_pos,
150 m_pos,
151 MCVector3dF(m_pos.i(), 3 * m_height / 2, 0),
152 second / 3);
153 }
154
155 m_glowScale *= 0.75f;
156 }
157
stateDisappear()158 void Startlights::stateDisappear()
159 {
160 if (m_animation.update())
161 {
162 m_state = State::End;
163 }
164 }
165
stateEnd()166 void Startlights::stateEnd()
167 {
168 m_timer.stop();
169 emit animationEnded();
170 }
171