SuperTuxKart
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 
24 class SFXBase;
25 
32 {
33 public:
35  enum ClockType
36  {
37  CLOCK_NONE,
38  CLOCK_CHRONO, // counts up
39  CLOCK_COUNTDOWN
40  };
41 
42  enum Phase {
43  // Time for a camera movement, and introductory song
44  TRACK_INTRO_PHASE,
45 
46  // Game setup, e.g. track loading
47  SETUP_PHASE,
48 
49  // Used in network games only: wait for the server to broadcast
50  // 'start'. This happens on a network client only
51  WAIT_FOR_SERVER_PHASE,
52 
53  // Used in network games only: server is ready
54  SERVER_READY_PHASE,
55 
56  // 'Ready' is displayed
57  READY_PHASE,
58 
59  // 'Set' is displayed
60  SET_PHASE,
61 
62  // 'Go' is displayed, but this is already race phase
63  GO_PHASE,
64 
65  // Race is started, 'go' is gone, but music name is still there
66  MUSIC_PHASE,
67 
68  // the actual race has started, no ready/set/go is displayed anymore
69  RACE_PHASE,
70 
71  // All players have finished, now wait a certain amount of time for AI
72  // karts to finish. If they do not finish in that time, finish the race
73  // and estimate their arrival time.
74  DELAY_FINISH_PHASE,
75 
76  // Display the results, while world is still being updated to
77  // show the end animation
78  RESULT_DISPLAY_PHASE,
79 
80  // The player crossed the finishing line and his and the time of
81  // the other players is displayed, controll is automatic
82  FINISH_PHASE,
83 
84  // Display the in-game menu, but no update of world or anything
85  IN_GAME_MENU_PHASE,
86 
87  // Undefined, used in asserts to catch incorrect states.
88  UNDEFINED_PHASE,
89  };
90 
91 protected:
93  double m_time;
94 
97 
100 
101 private:
109 
112 protected:
113  bool m_play_track_intro_sound;
114  bool m_play_ready_set_go_sounds;
115  std::atomic<Phase> m_phase;
116 
117 private:
118 
123 
129 
130  int m_start_music_ticks;
131 
132  int m_race_ticks;
133 
134  int m_live_join_ticks;
135 
139 
140  bool m_engines_started;
141 
142  bool m_live_join_world;
143 
144  void startEngines();
145 
146 public:
147  WorldStatus();
148  virtual ~WorldStatus();
149 
150  virtual void reset(bool restart);
151  virtual void updateTime(int ticks);
152  virtual void update(int ticks);
153  void startReadySetGo();
154  virtual void pause(Phase phase);
155  virtual void unpause();
156  virtual void enterRaceOverState();
157  virtual void terminateRace();
158  void setTime(const float time);
159  void setTicks(int ticks);
160  void setTicksForRewind(int ticks);
161  // ------------------------------------------------------------------------
162  // Note: GO_PHASE is both: start phase and race phase
163  bool isStartPhase() const { return m_phase<GO_PHASE; }
164  // ------------------------------------------------------------------------
165  bool isRacePhase() const { return m_phase>=GO_PHASE &&
166  m_phase<FINISH_PHASE; }
167  // ------------------------------------------------------------------------
168  bool isActiveRacePhase() const { return m_phase>=GO_PHASE &&
169  m_phase<DELAY_FINISH_PHASE; }
170  // ------------------------------------------------------------------------
173  bool isFinishPhase() const { return m_phase==FINISH_PHASE ||
174  (m_phase==IN_GAME_MENU_PHASE &&
175  m_previous_phase==FINISH_PHASE);}
176  // ------------------------------------------------------------------------
178  const Phase getPhase() const { return m_phase; }
179 
180  // ------------------------------------------------------------------------
183  void setPhase(Phase phase) { m_phase = phase; }
184 
185  // ------------------------------------------------------------------------
189  void setClockMode(const ClockType mode, const float initial_time=0.0f);
190 
191  // ------------------------------------------------------------------------
193  int getClockMode() const { return m_clock_mode; }
194 
195  // ------------------------------------------------------------------------
197  float getTime() const { return (float)m_time; }
198 
199  // ------------------------------------------------------------------------
202  int getTimeTicks() const { return m_time_ticks; }
203 
204  // ------------------------------------------------------------------------
207  virtual void countdownReachedZero() {};
208 
209  // ------------------------------------------------------------------------
211  virtual void onGo() {};
212 
213  // ------------------------------------------------------------------------
215  int getTicksSinceStart() const { return m_count_up_ticks; }
216  // ------------------------------------------------------------------------
217  int getAuxiliaryTicks() const { return m_auxiliary_ticks; }
218  // ------------------------------------------------------------------------
219  bool isLiveJoinWorld() const { return m_live_join_world; }
220  // ------------------------------------------------------------------------
221  void setLiveJoinWorld(bool val) { m_live_join_world = val; }
222  // ------------------------------------------------------------------------
223  int getMusicDescriptionTicks() const
224  {
225  return m_live_join_ticks == -1 ?
226  m_count_up_ticks : m_count_up_ticks - m_live_join_ticks;
227  }
228  // ------------------------------------------------------------------------
229  void endLiveJoinWorld(int ticks_now);
230 }; // WorldStatus
231 
232 
233 #endif
bool isFinishPhase() const
While the race menu is being displayed, m_phase is limbo, and m_previous_phase is finish.
Definition: world_status.hpp:173
double m_time
Elasped/remaining time in seconds.
Definition: world_status.hpp:93
bool m_play_racestart_sounds
If the start race should be played, disabled in cutscenes.
Definition: world_status.hpp:99
virtual ~WorldStatus()
Destructor of WorldStatus.
Definition: world_status.cpp:109
void setPhase(Phase phase)
Sets the current race phase.
Definition: world_status.hpp:183
virtual void onGo()
Called when the race actually starts.
Definition: world_status.hpp:211
void startEngines()
Starts the kart engines.
Definition: world_status.cpp:123
int m_auxiliary_ticks
Counts time during the initial 'ready/set/go' phase, or at the end of a race.
Definition: world_status.hpp:128
int getTimeTicks() const
Returns the current race time in time ticks (i.e.
Definition: world_status.hpp:202
virtual void unpause()
Switches back from a pause state to the previous state.
Definition: world_status.cpp:536
Phase m_previous_phase
Remember previous phase e.g.
Definition: world_status.hpp:122
virtual void countdownReachedZero()
Will be called to notify your derived class that the clock, which is in COUNTDOWN mode,...
Definition: world_status.hpp:207
int getTicksSinceStart() const
Get the ticks since start regardless of which way the clock counts.
Definition: world_status.hpp:215
virtual void updateTime(int ticks)
Updates the world time and clock (which might be running backwards), and all status information,...
Definition: world_status.cpp:192
ClockType
Different clock types for a race.
Definition: world_status.hpp:35
virtual void terminateRace()
Called when it's really over (delay over if any).
Definition: world_status.cpp:173
A class that manages the clock (countdown, chrono, etc.) Also manages stuff like the 'ready/set/go' t...
Definition: world_status.hpp:31
virtual void pause(Phase phase)
Pauses the game and switches to the specified phase.
Definition: world_status.cpp:520
virtual void update(int ticks)
Update, called once per frame.
Definition: world_status.cpp:182
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:153
void setTicks(int ticks)
Sets a new time for the world time, measured in ticks.
Definition: world_status.cpp:493
int m_time_ticks
Time in number of ticks (in terms of physics time steps).
Definition: world_status.hpp:96
ClockType m_clock_mode
The clock mode: normal counting forwards, or countdown.
Definition: world_status.hpp:111
SFXBase * m_start_sound
The third sound to be played in ready, set, go.
Definition: world_status.hpp:108
void setClockMode(const ClockType mode, const float initial_time=0.0f)
Call to specify what kind of clock you want.
Definition: world_status.cpp:141
int getClockMode() const
Returns the current clock mode.
Definition: world_status.hpp:193
void setTime(const float time)
Sets the time for the clock.
Definition: world_status.cpp:482
void endLiveJoinWorld(int ticks_now)
Base on the network timer set current world count up ticks to tick_now.
Definition: world_status.cpp:552
virtual void reset(bool restart)
Resets all status information, used when starting a new race.
Definition: world_status.cpp:66
int m_count_up_ticks
Special counter to count ticks since start (in terms of physics timestep size).
Definition: world_status.hpp:138
The base class for sound effects.
Definition: sfx_base.hpp:42
float getTime() const
Returns the current race time.
Definition: world_status.hpp:197
void setTicksForRewind(int ticks)
Sets a new time for the world time (used by rewind), measured in ticks.
Definition: world_status.cpp:503
SFXBase * m_prestart_sound
Sound used for the first two 'beeps' in ready, set, go.
Definition: world_status.hpp:106
const Phase getPhase() const
Returns the current race phase.
Definition: world_status.hpp:178
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:104