| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2012 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 | #include "startlights.hpp" |
| 17 | #include "inputhandler.hpp" |
| 18 | |
| 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 | |
| 37 | bool Startlights::timeElapsed(int limit) |
| 38 | { |
| 39 | if (++m_counter > limit) |
| 40 | { |
| 41 | m_counter = 0; |
| 42 | return true; |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | void Startlights::updateAnimation() |
| 48 | { |
| 49 | m_stateToFunctionMap[m_state](); |
| 50 | } |
| 51 | |
| 52 | void Startlights::beginAnimation() |
| 53 | { |
| 54 | m_state = State::Init; |
| 55 | m_timer.start(); |
| 56 | } |
| 57 | |
| 58 | void Startlights::setDimensions(int width, int height) |
| 59 | { |
| 60 | m_width = width; |
| 61 | m_height = height; |
| 62 | } |
| 63 | |
| 64 | Startlights::State Startlights::state() const |
| 65 | { |
| 66 | return m_state; |
| 67 | } |
| 68 | |
| 69 | const MCVector3dF & Startlights::pos() const |
| 70 | { |
| 71 | return m_pos; |
| 72 | } |
| 73 | |
| 74 | float Startlights::glowScale() const |
| 75 | { |
| 76 | return m_glowScale; |
| 77 | } |
| 78 | |
| 79 | void Startlights::stateInit() |
| 80 | { |
| 81 | const int 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 | |
| 94 | void Startlights::stateAppear() |
| 95 | { |
| 96 | m_animation.update(); |
| 97 | |
| 98 | if (const int second = m_stepsPerState; timeElapsed(second)) |
| 99 | { |
| 100 | m_state = State::FirstRow; |
| 101 | emit messageRequested("3"); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void Startlights::stateFirstRow() |
| 106 | { |
| 107 | if (const int second = m_stepsPerState; timeElapsed(second)) |
| 108 | { |
| 109 | m_state = State::SecondRow; |
| 110 | emit messageRequested("2"); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void Startlights::stateSecondRow() |
| 115 | { |
| 116 | if (const int second = m_stepsPerState; timeElapsed(second)) |
| 117 | { |
| 118 | m_state = State::ThirdRow; |
| 119 | emit messageRequested("1"); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void Startlights::stateThirdRow() |
| 124 | { |
| 125 | if (const int second = m_stepsPerState; timeElapsed(second)) |
| 126 | { |
| 127 | m_state = State::Go; |
| 128 | emit messageRequested(QObject::tr("GO!!!")); |
| 129 | emit raceStarted(); |
| 130 | InputHandler::setEnabled(true); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void Startlights::stateGo() |
| 135 | { |
| 136 | if (const int second = m_stepsPerState; timeElapsed(second)) |
| 137 | { |
| 138 | m_state = State::Disappear; |
| 139 | m_animation.init( |
| 140 | m_pos, |
| 141 | m_pos, |
| 142 | MCVector3dF(m_pos.i(), 3 * m_height / 2, 0), |
| 143 | second / 3); |
| 144 | } |
| 145 | |
| 146 | m_glowScale *= 0.75f; |
| 147 | } |
| 148 | |
| 149 | void Startlights::stateDisappear() |
| 150 | { |
| 151 | if (m_animation.update()) |
| 152 | { |
| 153 | m_state = State::End; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void Startlights::stateEnd() |
| 158 | { |
| 159 | m_timer.stop(); |
| 160 | emit animationEnded(); |
| 161 | } |