- Avoid some expensive SDL_GetTicks() calls
[supertux.git] / src / worldmap / worldmap.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
5 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 #ifndef SUPERTUX_WORLDMAP_H
21 #define SUPERTUX_WORLDMAP_H
22
23 #include <vector>
24 #include <string>
25
26 #include "math/vector.hpp"
27 #include "lisp/lisp.hpp"
28 #include "control/controller.hpp"
29 #include "statistics.hpp"
30 #include "timer.hpp"
31 #include "screen.hpp"
32 #include "tile_manager.hpp"
33 #include "game_object.hpp"
34 #include "console.hpp"
35 #include "../level.hpp"
36
37 class Sprite;
38 class Menu;
39 class SpawnPoint;
40 class GameObject;
41 class TileMap;
42
43 namespace WorldMapNS {
44
45 class Tux;
46 class LevelTile;
47 class SpecialTile;
48 class SpriteChange;
49
50 // For one way tiles
51 enum {
52   BOTH_WAYS,
53   NORTH_SOUTH_WAY,
54   SOUTH_NORTH_WAY,
55   EAST_WEST_WAY,
56   WEST_EAST_WAY
57 };
58
59 enum Direction { D_NONE, D_WEST, D_EAST, D_NORTH, D_SOUTH };
60
61 std::string direction_to_string(Direction d);
62 Direction   string_to_direction(const std::string& d);
63 Direction reverse_dir(Direction d);
64
65 /**
66  * Screen that displays a worldmap
67  */
68 class WorldMap : public Screen
69 {
70 private:
71   Tux* tux;
72
73   static WorldMap* current_;
74
75   std::auto_ptr<Menu> worldmap_menu;
76
77   Vector camera_offset;
78
79   std::string name;
80   std::string music;
81
82   typedef std::vector<GameObject*> GameObjects;
83   GameObjects game_objects;
84   TileMap* solids;
85   
86   std::auto_ptr<TileManager> tile_manager;
87   
88 public:
89   /** Variables to deal with the passive map messages */
90   Timer passive_message_timer;
91   std::string passive_message;
92
93 private:
94   std::string map_filename;
95   std::string levels_path;
96
97   typedef std::vector<SpecialTile*> SpecialTiles;
98   SpecialTiles special_tiles;
99   typedef std::vector<LevelTile*> LevelTiles;
100   LevelTiles levels;
101   typedef std::vector<SpriteChange*> SpriteChanges;
102   SpriteChanges sprite_changes;
103   typedef std::vector<SpawnPoint*> SpawnPoints;
104   SpawnPoints spawn_points;
105
106   Statistics total_stats;
107
108   typedef std::vector<HSQOBJECT> ScriptList;
109   ScriptList scripts;      
110
111 public:
112   WorldMap(const std::string& filename);
113   ~WorldMap();
114
115   void add_object(GameObject* object);
116
117   static WorldMap* current()
118   { return current_; }
119
120   virtual void setup();
121
122   /** Update worldmap state */
123   virtual void update(float delta);
124   /** Draw worldmap */
125   virtual void draw(DrawingContext& context);
126
127   Vector get_next_tile(Vector pos, Direction direction);
128   const Tile* at(Vector pos);
129
130   size_t level_count();
131   size_t solved_level_count();
132
133   /**
134    * gets called from the GameSession when a level has been successfully
135    * finished
136    */
137   void finished_level(Level* level);
138
139   LevelTile* at_level();
140   SpecialTile* at_special_tile();
141   SpriteChange* at_sprite_change(const Vector& pos);
142
143   /** Check if it is possible to walk from \a pos into \a direction,
144       if possible, write the new position to \a new_pos */
145   bool path_ok(Direction direction, const Vector& pos, Vector* new_pos);
146
147   /**
148    * Save worldmap state to squirrel state table
149    */
150   void save_state();
151
152   /**
153    * Load worldmap state from squirrel state table
154    */
155   void load_state();
156
157   const std::string& get_title() const
158   { return name; }
159
160   /**
161    * runs a script in the context of the worldmap (and keeps a reference to 
162    * the script (so the script gets destroyed when the worldmap is destroyed)
163    */
164   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
165     
166 private:
167   void get_level_title(LevelTile& level);
168   void draw_status(DrawingContext& context);
169   void calculate_total_stats();
170
171   void load(const std::string& filename);  
172   void on_escape_press();
173   void parse_special_tile(const lisp::Lisp* lisp);
174   void parse_sprite_change(const lisp::Lisp* lisp);
175 };
176
177 } // namespace WorldMapNS
178
179 #endif