59a4642806954ec9addfcbe4d5a61c8b2412b893
[supertux.git] / src / game_session.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #ifndef SUPERTUX_GAMELOOP_H
20 #define SUPERTUX_GAMELOOP_H
21
22 #include <string>
23 #include <SDL.h>
24 #include "screen.hpp"
25 #include "timer.hpp"
26 #include "statistics.hpp"
27 #include "math/vector.hpp"
28 #include "console.hpp"
29 #include "video/surface.hpp"
30
31 class Level;
32 class Sector;
33 class Statistics;
34 class DrawingContext;
35 class CodeController;
36 class Menu;
37
38 /**
39  * The GameSession class controlls the controll flow of the Game (the part
40  * where you actually play a level)
41  */
42 class GameSession : public Screen
43 {
44 public:
45   GameSession(const std::string& levelfile, Statistics* statistics = NULL);
46   ~GameSession();
47
48   void record_demo(const std::string& filename);
49   int get_demo_random_seed(const std::string& filename);
50   void play_demo(const std::string& filename);
51
52   void draw(DrawingContext& context);
53   void update(float frame_ratio);
54   void setup();
55
56   void set_current()
57   { current_ = this; }
58   static GameSession* current()
59   { return current_; }
60
61   /// ends the current level
62   void finish(bool win = true);
63   void respawn(const std::string& sectorname, const std::string& spawnpointname);
64   void set_reset_point(const std::string& sectorname, const Vector& pos);
65   void display_info_box(const std::string& text);
66   
67   Sector* get_current_sector()
68   { return currentsector; }
69
70   Level* get_current_level()
71   { return level.get(); }
72
73   void start_sequence(const std::string& sequencename);
74
75   /**
76    * returns the "working directory" usually this is the directory where the
77    * currently played level resides. This is used when locating additional
78    * resources for the current level/world
79    */
80   std::string get_working_directory();
81   void restart_level(bool fromBeginning = true);
82
83   void toggle_pause();
84
85 private:
86   void check_end_conditions();
87   void process_events();
88   void capture_demo_step();
89
90   void levelintro();
91   void drawstatus(DrawingContext& context);
92   void draw_pause(DrawingContext& context);
93
94   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
95   void on_escape_press();
96   void process_menu();
97
98   Timer endsequence_timer;
99   std::auto_ptr<Level> level;
100   std::auto_ptr<Surface> statistics_backdrop;
101
102   // scripts
103   typedef std::vector<HSQOBJECT> ScriptList;
104   ScriptList scripts;
105
106   Sector* currentsector;
107
108   int levelnb;
109   int pause_menu_frame;
110
111   /** If true the end_sequence will be played, user input will be
112       ignored while doing that */
113   enum EndSequenceState {
114     NO_ENDSEQUENCE,
115     ENDSEQUENCE_RUNNING, // tux is running right
116     ENDSEQUENCE_WAITING  // waiting for the end of the music
117   };
118   EndSequenceState end_sequence;
119   float last_x_pos;
120   CodeController* end_sequence_controller;
121
122   bool game_pause;
123
124   std::string levelfile;
125
126   // reset point (the point where tux respawns if he dies)
127   std::string reset_sector;
128   Vector reset_pos;
129
130   // the sector and spawnpoint we should spawn after this frame
131   std::string newsector;
132   std::string newspawnpoint;
133
134   static GameSession* current_;
135
136   Statistics* best_level_statistics;
137
138   std::ostream* capture_demo_stream;
139   std::string capture_file;
140   std::istream* playback_demo_stream;
141   CodeController* demo_controller;
142
143   std::auto_ptr<Menu> game_menu;
144
145   float play_time; /**< total time in seconds that this session ran interactively */
146 };
147
148 #endif /*SUPERTUX_GAMELOOP_H*/
149