SuperTuxKart
Loading...
Searching...
No Matches
world_status.hpp
1// SuperTuxKart - a fun racing game with go-kart
2// Copyright (C) 2004-2015 SuperTuxKart-Team
3//
4// This program is free software; you can redistribute it and/or
5// modify it under the terms of the GNU General Public License
6// as published by the Free Software Foundation; either version 3
7// of the License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18#ifndef HEADER_WORLD_STATUS_HPP
19#define HEADER_WORLD_STATUS_HPP
20
21#include "utils/cpp2011.hpp"
22#include <atomic>
23
24enum ProcessType : unsigned int;
25class SFXBase;
26
33{
34public:
37 {
38 CLOCK_NONE,
39 CLOCK_CHRONO, // counts up
40 CLOCK_COUNTDOWN
41 };
42
43 enum Phase {
44 // Time for a camera movement, and introductory song
45 TRACK_INTRO_PHASE,
46
47 // Game setup, e.g. track loading
48 SETUP_PHASE,
49
50 // Used in network games only: wait for the server to broadcast
51 // 'start'. This happens on a network client only
52 WAIT_FOR_SERVER_PHASE,
53
54 // Used in network games only: server is ready
55 SERVER_READY_PHASE,
56
57 // 'Ready' is displayed
58 READY_PHASE,
59
60 // 'Set' is displayed
61 SET_PHASE,
62
63 // 'Go' is displayed, but this is already race phase
64 GO_PHASE,
65
66 // Race is started, 'go' is gone, but music name is still there
67 MUSIC_PHASE,
68
69 // the actual race has started, no ready/set/go is displayed anymore
70 RACE_PHASE,
71
72 // All players have finished, now wait a certain amount of time for AI
73 // karts to finish. If they do not finish in that time, finish the race
74 // and estimate their arrival time.
75 DELAY_FINISH_PHASE,
76
77 // Display the results, while world is still being updated to
78 // show the end animation
79 RESULT_DISPLAY_PHASE,
80
81 // The player crossed the finishing line and his and the time of
82 // the other players is displayed, controll is automatic
83 FINISH_PHASE,
84
85 // Display the in-game menu, but no update of world or anything
86 IN_GAME_MENU_PHASE,
87
88 // Undefined, used in asserts to catch incorrect states.
89 UNDEFINED_PHASE,
90 };
91
92protected:
94 double m_time;
95
98
101
103 const ProcessType m_process_type;
104
105private:
113
115 uint64_t m_started_at;
116
119protected:
120 bool m_play_track_intro_sound;
121 bool m_play_ready_set_go_sounds;
122 std::atomic<Phase> m_phase;
123
124private:
125
130
136
137 int m_start_music_ticks;
138
139 int m_race_ticks;
140
141 int m_live_join_ticks;
142
146
147 bool m_engines_started;
148
149 bool m_live_join_world;
150
151 void startEngines();
152
153public:
154 WorldStatus();
155 virtual ~WorldStatus();
156
157 virtual void reset(bool restart);
158 virtual void updateTime(int ticks);
159 virtual void update(int ticks);
160 void startReadySetGo();
161 virtual void pause(Phase phase);
162 virtual void unpause();
163 virtual void enterRaceOverState();
164 virtual void terminateRace();
165 void setTime(const float time);
166 void setTicks(int ticks);
167 void setTicksForRewind(int ticks);
168 // ------------------------------------------------------------------------
169 // Note: GO_PHASE is both: start phase and race phase
170 bool isStartPhase() const { return m_phase<GO_PHASE; }
171 // ------------------------------------------------------------------------
172 bool isRacePhase() const { return m_phase>=GO_PHASE &&
173 m_phase<FINISH_PHASE; }
174 // ------------------------------------------------------------------------
175 bool isActiveRacePhase() const { return m_phase>=GO_PHASE &&
176 m_phase<DELAY_FINISH_PHASE; }
177 // ------------------------------------------------------------------------
180 bool isFinishPhase() const { return m_phase==FINISH_PHASE ||
181 (m_phase==IN_GAME_MENU_PHASE &&
182 m_previous_phase==FINISH_PHASE);}
183 // ------------------------------------------------------------------------
185 const Phase getPhase() const { return m_phase; }
186
187 // ------------------------------------------------------------------------
190 void setPhase(Phase phase) { m_phase = phase; }
191
192 // ------------------------------------------------------------------------
196 void setClockMode(const ClockType mode, const float initial_time=0.0f);
197
198 // ------------------------------------------------------------------------
200 int getClockMode() const { return m_clock_mode; }
201
202 // ------------------------------------------------------------------------
204 float getTime() const { return (float)m_time; }
205
206 // ------------------------------------------------------------------------
208 uint64_t getStart() const { return m_started_at; }
209
210 // ------------------------------------------------------------------------
213 int getTimeTicks() const { return m_time_ticks; }
214
215 // ------------------------------------------------------------------------
218 virtual void countdownReachedZero() {};
219
220 // ------------------------------------------------------------------------
222 virtual void onGo() {};
223
224 // ------------------------------------------------------------------------
226 int getTicksSinceStart() const { return m_count_up_ticks; }
227 // ------------------------------------------------------------------------
228 int getAuxiliaryTicks() const { return m_auxiliary_ticks; }
229 // ------------------------------------------------------------------------
230 bool isLiveJoinWorld() const { return m_live_join_world; }
231 // ------------------------------------------------------------------------
232 void setLiveJoinWorld(bool val) { m_live_join_world = val; }
233 // ------------------------------------------------------------------------
234 int getMusicDescriptionTicks() const
235 {
236 return m_live_join_ticks == -1 ?
237 m_count_up_ticks : m_count_up_ticks - m_live_join_ticks;
238 }
239 // ------------------------------------------------------------------------
240 void endLiveJoinWorld(int ticks_now);
241}; // WorldStatus
242
243
244#endif
The base class for sound effects.
Definition: sfx_base.hpp:43
A class that manages the clock (countdown, chrono, etc.) Also manages stuff like the 'ready/set/go' t...
Definition: world_status.hpp:33
void endLiveJoinWorld(int ticks_now)
Base on the network timer set current world count up ticks to tick_now.
Definition: world_status.cpp:574
float getTime() const
Returns the current race time.
Definition: world_status.hpp:204
double m_time
Elasped/remaining time in seconds.
Definition: world_status.hpp:94
virtual void onGo()
Called when the race actually starts.
Definition: world_status.hpp:222
virtual ~WorldStatus()
Destructor of WorldStatus.
Definition: world_status.cpp:122
SFXBase * m_start_sound
The third sound to be played in ready, set, go.
Definition: world_status.hpp:112
void setClockMode(const ClockType mode, const float initial_time=0.0f)
Call to specify what kind of clock you want.
Definition: world_status.cpp:158
int m_time_ticks
Time in number of ticks (in terms of physics time steps).
Definition: world_status.hpp:97
virtual void unpause()
Switches back from a pause state to the previous state.
Definition: world_status.cpp:554
virtual void update(int ticks)
Update, called once per frame.
Definition: world_status.cpp:199
uint64_t getStart() const
Returns the start time.
Definition: world_status.hpp:208
int getClockMode() const
Returns the current clock mode.
Definition: world_status.hpp:200
virtual void reset(bool restart)
Resets all status information, used when starting a new race.
Definition: world_status.cpp:73
ClockType m_clock_mode
The clock mode: normal counting forwards, or countdown.
Definition: world_status.hpp:118
void setTicks(int ticks)
Sets a new time for the world time, measured in ticks.
Definition: world_status.cpp:507
void setTime(const float time)
Sets the time for the clock.
Definition: world_status.cpp:496
int m_count_up_ticks
Special counter to count ticks since start (in terms of physics timestep size).
Definition: world_status.hpp:145
virtual void enterRaceOverState()
Called when the race is finished, but it still leaves some time for an end of race animation,...
Definition: world_status.cpp:170
virtual void terminateRace()
Called when it's really over (delay over if any).
Definition: world_status.cpp:190
int getTicksSinceStart() const
Get the ticks since start regardless of which way the clock counts.
Definition: world_status.hpp:226
void setPhase(Phase phase)
Sets the current race phase.
Definition: world_status.hpp:190
bool isFinishPhase() const
While the race menu is being displayed, m_phase is limbo, and m_previous_phase is finish.
Definition: world_status.hpp:180
virtual void updateTime(int ticks)
Updates the world time and clock (which might be running backwards), and all status information,...
Definition: world_status.cpp:209
ClockType
Different clock types for a race.
Definition: world_status.hpp:37
const ProcessType m_process_type
Process type of this world (main or child).
Definition: world_status.hpp:103
void startEngines()
Starts the kart engines.
Definition: world_status.cpp:140
int m_auxiliary_ticks
Counts time during the initial 'ready/set/go' phase, or at the end of a race.
Definition: world_status.hpp:135
bool m_play_racestart_sounds
If the start race should be played, disabled in cutscenes.
Definition: world_status.hpp:100
SFXBase * m_track_intro_sound
Sound to play at the beginning of a race, during which a a camera intro of the track can be shown.
Definition: world_status.hpp:108
Phase m_previous_phase
Remember previous phase e.g.
Definition: world_status.hpp:129
virtual void countdownReachedZero()
Will be called to notify your derived class that the clock, which is in COUNTDOWN mode,...
Definition: world_status.hpp:218
const Phase getPhase() const
Returns the current race phase.
Definition: world_status.hpp:185
SFXBase * m_prestart_sound
Sound used for the first two 'beeps' in ready, set, go.
Definition: world_status.hpp:110
int getTimeTicks() const
Returns the current race time in time ticks (i.e.
Definition: world_status.hpp:213
uint64_t m_started_at
(Unix) time when we started
Definition: world_status.hpp:115
virtual void pause(Phase phase)
Pauses the game and switches to the specified phase.
Definition: world_status.cpp:534
void setTicksForRewind(int ticks)
Sets a new time for the world time (used by rewind), measured in ticks.
Definition: world_status.cpp:517