supertux
game_session.hpp
1 // SuperTux
2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 // This program 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 //
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, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_SUPERTUX_GAME_SESSION_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_GAME_SESSION_HPP
19 
20 #include "supertux/screen.hpp"
21 #include "util/currenton.hpp"
22 
23 #include <memory>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include <simplesquirrel/table.hpp>
28 
29 #include "math/vector.hpp"
30 #include "squirrel/squirrel_scheduler.hpp"
31 #include "squirrel/squirrel_util.hpp"
32 #include "supertux/game_object.hpp"
33 #include "supertux/player_status.hpp"
34 #include "supertux/screen_fade.hpp"
35 #include "supertux/sequence.hpp"
36 #include "supertux/timer.hpp"
37 #include "video/surface_ptr.hpp"
38 
39 class CodeController;
40 class DrawingContext;
41 class EndSequence;
42 class Level;
43 class Player;
44 class Sector;
45 class Statistics;
46 class Savegame;
47 
49 class GameSession final : public Screen,
50  public Currenton<GameSession>
51 {
52 private:
53  struct SpawnPoint
54  {
55  /* If a spawnpoint is set, the spawn position shall not, and vice versa. */
56  SpawnPoint(const std::string& sector_,
57  const Vector& position_,
58  bool is_checkpoint_ = false) :
59  sector(sector_),
60  spawnpoint(),
61  position(position_),
62  is_checkpoint(is_checkpoint_)
63  {}
64  SpawnPoint(const std::string& sector_,
65  const std::string& spawnpoint_,
66  bool is_checkpoint_ = false) :
67  sector(sector_),
68  spawnpoint(spawnpoint_),
69  position(),
70  is_checkpoint(is_checkpoint_)
71  {}
72 
73  std::string sector;
74  std::string spawnpoint;
75  Vector position;
76  bool is_checkpoint;
77  };
78 
79 public:
80  GameSession(const std::string& levelfile, Savegame& savegame, Statistics* statistics = nullptr,
81  bool preserve_music = false);
82 
83  virtual void draw(Compositor& compositor) override;
84  virtual void update(float dt_sec, const Controller& controller) override;
85  virtual void setup() override;
86  virtual void leave() override;
87  virtual IntegrationStatus get_status() const override;
88 
90  void finish(bool win = true);
91  void respawn(const std::string& sectorname, const std::string& spawnpointname);
92  void respawn_with_fade(const std::string& sectorname,
93  const std::string& spawnpointname,
94  const ScreenFade::FadeType fade_type,
95  const Vector &fade_point,
96  const bool make_invincible = false);
97  void reset_level();
98 
99  void set_start_point(const std::string& sectorname,
100  const std::string& spawnpointname);
101  void set_start_pos(const std::string& sectorname, const Vector& pos);
102  void set_respawn_point(const std::string& sectorname,
103  const std::string& spawnpointname);
104  void set_respawn_pos(const std::string& sectorname, const Vector& pos);
105  void clear_respawn_points();
106 
107  const SpawnPoint& get_last_spawnpoint() const;
108 
109  void set_checkpoint_pos(const std::string& sectorname, const Vector& pos);
110  const SpawnPoint* get_active_checkpoint_spawnpoint() const;
111 
112  Sector& get_current_sector() const { return *m_currentsector; }
113  Level& get_current_level() const { return *m_level; }
114 
115  void start_sequence(Player* caller, Sequence seq, const SequenceData* data = nullptr);
116  void set_target_timer_paused(bool paused);
117 
123  std::string get_working_directory() const;
124  const std::string& get_level_file() const { return m_levelfile; }
125  bool has_active_sequence() const;
126  int restart_level(bool after_death = false, bool preserve_music = false);
127 
128  void toggle_pause();
129  void abort_level();
130  bool is_active() const;
131 
132  Savegame& get_savegame() const { return m_savegame; }
133 
134  void set_scheduler(SquirrelScheduler& new_scheduler);
135 
136 private:
137  void check_end_conditions();
138 
139  void drawstatus(DrawingContext& context);
140  void draw_pause(DrawingContext& context);
141 
142  void on_escape_press(bool force_quick_respawn);
143 
144  Vector get_fade_point() const;
145  Vector get_fade_point(const Vector& position) const;
146 
147 public:
148  bool reset_button;
149  bool reset_checkpoint_button;
150 
153 private:
154  std::unique_ptr<Level> m_level;
155  SurfacePtr m_statistics_backdrop;
156 
157  ssq::Table m_data_table;
158 
159  Sector* m_currentsector;
160 
161  EndSequence* m_end_sequence;
162 
163  bool m_game_pause;
164  float m_speed_before_pause;
165 
166  std::string m_levelfile;
167 
168  // Spawnpoints
169  std::vector<SpawnPoint> m_spawnpoints;
170  const SpawnPoint* m_activated_checkpoint;
171 
172  // the sector and spawnpoint we should spawn after this frame
173  std::string m_newsector;
174  std::string m_newspawnpoint;
175  ScreenFade::FadeType m_spawn_fade_type;
176  Timer m_spawn_fade_timer;
177  bool m_spawn_with_invincibility;
178 
179  Statistics* m_best_level_statistics;
180  Savegame& m_savegame;
181 
182  // Note: m_play_time should reset when a level is restarted from the beginning
183  // but NOT if Tux respawns at a checkpoint (for LevelTimes to work)
184  float m_play_time;
186  bool m_levelintro_shown;
188  int m_coins_at_start;
189  std::vector<BonusType> m_boni_at_start;
190  std::vector<int> m_max_fire_bullets_at_start;
191  std::vector<int> m_max_ice_bullets_at_start;
193  bool m_active;
195  bool m_end_seq_started;
196  bool m_pause_target_timer;
197 
198  std::unique_ptr<GameObject> m_current_cutscene_text;
199 
200  Timer m_endsequence_timer;
201 
202 private:
203  GameSession(const GameSession&) = delete;
204  GameSession& operator=(const GameSession&) = delete;
205 };
206 
207 #endif
208 
209 /* EOF */
bool m_prevent_death
true if players should enter ghost mode instead of dying
Definition: game_session.hpp:151
Definition: controller.hpp:57
void finish(bool win=true)
ends the current level
Definition: game_session.cpp:690
Abstract base class for code the MainLoop runs exclusively and full-screen.
Definition: screen.hpp:31
virtual void update(float dt_sec, const Controller &controller) override
gets called for once (per logical) frame.
Definition: game_session.cpp:496
Definition: sequence.hpp:33
This class is a layer between level and worldmap to keep track of stuff like scores, and minor, but funny things, like number of jumps and stuff.
Definition: statistics.hpp:36
std::string get_working_directory() const
returns the "working directory" usually this is the directory where the currently played level reside...
Definition: game_session.cpp:824
Represents one of (potentially) multiple, separate parts of a Level.
Definition: sector.hpp:61
This class keeps a list of squirrel threads that are scheduled for a certain time.
Definition: squirrel_scheduler.hpp:26
Definition: endsequence.hpp:25
virtual void draw(Compositor &compositor) override
gets called once per frame.
Definition: game_session.cpp:437
virtual void setup() override
gets called before this screen gets activated (which is at least once before the first draw or update...
Definition: game_session.cpp:462
A &#39;Currenton&#39; allows access to the currently active instance of a class via the static current() func...
Definition: currenton.hpp:30
Represents a collection of Sectors running in a single GameSession.
Definition: level.hpp:30
virtual void leave() override
gets called when the current screen is temporarily suspended
Definition: game_session.cpp:490
Definition: compositor.hpp:29
Definition: integration.hpp:26
Definition: savegame.hpp:69
This is a dummy controller that doesn&#39;t react to any user input but should be controlled by code...
Definition: codecontroller.hpp:24
Simple timer designed to be used in the update functions of objects.
Definition: timer.hpp:24
virtual IntegrationStatus get_status() const override
Gives details about what the user is doing right now.
Definition: game_session.cpp:677
This module contains methods controlling the player.
Definition: player.hpp:47
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
Screen that runs a Level, where Players run and jump through Sectors.
Definition: game_session.hpp:49