Fade out and pause music on death and resume on restart of level, fixes #1064
[supertux.git] / src / 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 <memory>
21 #include <vector>
22 #include <squirrel.h>
23
24 #include "object/endsequence.hpp"
25 #include "supertux/screen.hpp"
26 #include "supertux/player_status.hpp"
27 #include "util/currenton.hpp"
28 #include "video/surface.hpp"
29
30 class CodeController;
31 class DrawingContext;
32 class Level;
33 class Menu;
34 class PlayerStatus;
35 class Sector;
36 class Statistics;
37 class Savegame;
38
39 /**
40  * Screen that runs a Level, where Players run and jump through Sectors.
41  */
42 class GameSession : public Screen,
43                     public Currenton<GameSession>
44 {
45 public:
46   GameSession(const std::string& levelfile, Savegame& savegame, Statistics* statistics = NULL);
47   ~GameSession();
48
49   void record_demo(const std::string& filename);
50   int get_demo_random_seed(const std::string& filename);
51   void play_demo(const std::string& filename);
52
53   void draw(DrawingContext& context);
54   void update(float frame_ratio);
55   void setup() override;
56   void leave() override;
57
58   /// ends the current level
59   void finish(bool win = true);
60   void respawn(const std::string& sectorname, const std::string& spawnpointname);
61   void set_reset_point(const std::string& sectorname, const Vector& pos);
62   std::string get_reset_point_sectorname()
63   { return reset_sector; }
64
65   Vector get_reset_point_pos()
66   { return reset_pos; }
67
68   Sector* get_current_sector()
69   { return currentsector; }
70
71   Level* get_current_level()
72   { return level.get(); }
73
74   void start_sequence(const std::string& sequencename);
75
76   /**
77    * returns the "working directory" usually this is the directory where the
78    * currently played level resides. This is used when locating additional
79    * resources for the current level/world
80    */
81   std::string get_working_directory();
82   int restart_level(bool after_death = false);
83
84   void toggle_pause();
85   void abort_level();
86
87   /**
88    * Enters or leaves level editor mode
89    */
90   void set_editmode(bool edit_mode = true);
91
92   /**
93    * Forces all Players to enter ghost mode
94    */
95   void force_ghost_mode();
96
97   Savegame& get_savegame() { return m_savegame; }
98
99 private:
100   void check_end_conditions();
101   void process_events();
102   void capture_demo_step();
103
104   void drawstatus(DrawingContext& context);
105   void draw_pause(DrawingContext& context);
106
107   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
108   void on_escape_press();
109
110   std::unique_ptr<Level> level;
111   SurfacePtr statistics_backdrop;
112
113   // scripts
114   typedef std::vector<HSQOBJECT> ScriptList;
115   ScriptList scripts;
116
117   Sector* currentsector;
118
119   int levelnb;
120   int pause_menu_frame;
121
122   std::shared_ptr<EndSequence> end_sequence;
123
124   bool  game_pause;
125   float speed_before_pause;
126
127   std::string levelfile;
128
129   // reset point (the point where tux respawns if he dies)
130   std::string reset_sector;
131   Vector reset_pos;
132
133   // the sector and spawnpoint we should spawn after this frame
134   std::string newsector;
135   std::string newspawnpoint;
136
137   Statistics* best_level_statistics;
138   Savegame& m_savegame;
139
140   std::ostream* capture_demo_stream;
141   std::string capture_file;
142   std::istream* playback_demo_stream;
143   CodeController* demo_controller;
144
145   float play_time; /**< total time in seconds that this session ran interactively */
146
147   bool edit_mode; /**< true if GameSession runs in level editor mode */
148   bool levelintro_shown; /**< true if the LevelIntro screen was already shown */
149
150   int coins_at_start; /** How many coins does the player have at the start */
151   BonusType bonus_at_start; /** What bonuses does the player have at the start */
152   int max_fire_bullets_at_start; /** How many fire bullets does the player have */
153   int max_ice_bullets_at_start; /** How many ice bullets does the player have */
154
155 private:
156   GameSession(const GameSession&);
157   GameSession& operator=(const GameSession&);
158 };
159
160 #endif /*SUPERTUX_GAMELOOP_H*/
161
162 /* EOF */