| 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 TIMING_HPP |
| 17 | #define TIMING_HPP |
| 18 | |
| 19 | #include <QObject> |
| 20 | |
| 21 | #include <chrono> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | using namespace std::chrono_literals; |
| 26 | |
| 27 | class Timing : public QObject |
| 28 | { |
| 29 | Q_OBJECT |
| 30 | |
| 31 | public: |
| 32 | //! Constructor. |
| 33 | explicit Timing(size_t cars, QObject * parent = nullptr); |
| 34 | |
| 35 | //! Completes the current lap for the given car. |
| 36 | void setLapCompleted(size_t index, bool isHuman); |
| 37 | |
| 38 | //! Completes the race for the given car. |
| 39 | void setRaceCompleted(size_t index, bool state, bool isHuman); |
| 40 | |
| 41 | //! \return if the race is completed for the given car. |
| 42 | bool raceCompleted(size_t index) const; |
| 43 | |
| 44 | //! Toggle timing activity of the given car. |
| 45 | void setIsActive(size_t index, bool state); |
| 46 | |
| 47 | //! \return timing activity of the given car. |
| 48 | bool isActive(size_t index) const; |
| 49 | |
| 50 | //! \return the current lap for the given car. |
| 51 | size_t lap(size_t index) const; |
| 52 | |
| 53 | //! \return the lap for the leading car. |
| 54 | size_t leadersLap() const; |
| 55 | |
| 56 | std::chrono::milliseconds raceTime() const; |
| 57 | std::chrono::milliseconds raceTime(size_t index) const; |
| 58 | |
| 59 | /*! \return the record race time for the given car in msecs or |
| 60 | * -1 if invalid car or time not set. */ |
| 61 | int recordRaceTime(size_t index) const; |
| 62 | |
| 63 | /*! \return last lap time in msecs for the given car or |
| 64 | * -1 if invalid car or time not set. */ |
| 65 | int lastLapTime(size_t index) const; |
| 66 | |
| 67 | /*! \return the current lap time in msecs for the given car or |
| 68 | * -1 if invalid car or time not set. */ |
| 69 | int currentLapTime(size_t index) const; |
| 70 | |
| 71 | /*! \return the record lap time in msecs for the given car or |
| 72 | * -1 if invalid car or time not set. */ |
| 73 | int recordLapTime(size_t index) const; |
| 74 | |
| 75 | //! \return the current lap record or -1 if not set. |
| 76 | int lapRecord() const; |
| 77 | |
| 78 | //! Set the current lap record. |
| 79 | void setLapRecord(int msecs); |
| 80 | |
| 81 | //! \return the current race record or -1 if not set. |
| 82 | int raceRecord() const; |
| 83 | |
| 84 | //! Set the current race record. |
| 85 | void setRaceRecord(int msecs); |
| 86 | |
| 87 | void start(); |
| 88 | void stop(); |
| 89 | void reset(); |
| 90 | |
| 91 | void tick(std::chrono::milliseconds timeStep); |
| 92 | |
| 93 | //! Converts msecs to string "mm:ss.zz". |
| 94 | static std::wstring msecsToString(int msec); |
| 95 | |
| 96 | signals: |
| 97 | |
| 98 | void lapCompleted(size_t index, int msec); |
| 99 | |
| 100 | void lapRecordAchieved(int msec); |
| 101 | void raceRecordAchieved(int msec); |
| 102 | |
| 103 | private: |
| 104 | //! Timing structure. |
| 105 | struct Times |
| 106 | { |
| 107 | int lastLapTime = -1; |
| 108 | int recordLapTime = -1; // Personal best |
| 109 | |
| 110 | std::chrono::milliseconds raceTime = 0ms; |
| 111 | int recordRaceTime = 0; // Personal best |
| 112 | |
| 113 | size_t lap = 0; |
| 114 | |
| 115 | bool raceCompleted = false; |
| 116 | |
| 117 | bool isActive = true; |
| 118 | }; |
| 119 | |
| 120 | std::vector<Timing::Times> m_times; |
| 121 | std::chrono::milliseconds m_time; |
| 122 | |
| 123 | bool m_started; |
| 124 | |
| 125 | int m_lapRecord; |
| 126 | int m_raceRecord; |
| 127 | }; |
| 128 | |
| 129 | #endif // TIMING_HPP |