Debian Codegraph @

src/game/gearbox.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2019 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 "gearbox.hpp"
17
18 #include <cmath>
19
20 Gearbox::Gearbox()
21 {
22 }
23
24 Gearbox::Gear Gearbox::gear() const
25 {
26 return m_gear;
27 }
28
29 void Gearbox::update(int speedInKmh)
30 {
31 if (m_acceleratorEnabled && m_brakeEnabled)
32 {
33 m_gear = Gear::Neutral;
34 }
35 else if (m_brakeEnabled)
36 {
37 if (speedInKmh == 0)
38 {
39 if (m_gear != Gear::Stop && m_gear != Gear::Reverse)
40 {
41 m_gear = Gear::Stop;
42 m_stopCounter = 0;
43 }
44 else
45 {
46 if (++m_stopCounter > 30) // Wait ~0.5 sec before reverse engages
47 {
48 m_gear = Gear::Reverse;
49 }
50 }
51 }
52 }
53 else if (m_acceleratorEnabled)
54 {
55 if (m_gear == Gear::Neutral || m_gear == Gear::Stop)
56 {
57 m_gear = Gear::Forward;
58 }
59 }
60 else
61 {
62 m_gear = Gear::Neutral;
63 }
64 }
65
66 void Gearbox::setAcceleratorEnabled(bool enable)
67 {
68 m_acceleratorEnabled = enable;
69 }
70
71 void Gearbox::setBrakeEnabled(bool enable)
72 {
73 m_brakeEnabled = enable;
74 }