acbbedb66aa838554fe102152e48494a81ca030e
[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 #include "worldmap/special_tile.hpp"
37 #include "worldmap/sprite_change.hpp"
38 #include "worldmap/teleporter.hpp"
39 #include "worldmap/spawn_point.hpp"
40 #include "worldmap/direction.hpp"
41
42 class Sprite;
43 class Menu;
44 class GameObject;
45 class TileMap;
46
47 namespace WorldMapNS {
48
49 class Tux;
50 class LevelTile;
51 class SpecialTile;
52 class SpriteChange;
53
54 // For one way tiles
55 enum {
56   BOTH_WAYS,
57   NORTH_SOUTH_WAY,
58   SOUTH_NORTH_WAY,
59   EAST_WEST_WAY,
60   WEST_EAST_WAY
61 };
62
63 std::string direction_to_string(Direction d);
64 Direction   string_to_direction(const std::string& d);
65 Direction reverse_dir(Direction d);
66
67 /**
68  * Screen that displays a worldmap
69  */
70 class WorldMap : public Screen
71 {
72 private:
73   Tux* tux;
74
75   static WorldMap* current_;
76
77   std::auto_ptr<Menu> worldmap_menu;
78
79   Vector camera_offset;
80
81   std::string name;
82   std::string music;
83   std::string init_script;
84
85   typedef std::vector<GameObject*> GameObjects;
86   GameObjects game_objects;
87   TileMap* solids;
88
89   std::auto_ptr<TileManager> tile_manager;
90
91 public:
92   /** Variables to deal with the passive map messages */
93   Timer passive_message_timer;
94   std::string passive_message;
95
96 private:
97   std::string map_filename;
98   std::string levels_path;
99
100   typedef std::vector<SpecialTile*> SpecialTiles;
101   SpecialTiles special_tiles;
102   typedef std::vector<LevelTile*> LevelTiles;
103   LevelTiles levels;
104   typedef std::vector<SpriteChange*> SpriteChanges;
105   SpriteChanges sprite_changes;
106   typedef std::vector<SpawnPoint*> SpawnPoints;
107   SpawnPoints spawn_points;
108   std::vector<Teleporter*> teleporters;
109
110   Statistics total_stats;
111
112   HSQOBJECT worldmap_table;
113   typedef std::vector<HSQOBJECT> ScriptList;
114   ScriptList scripts;
115
116   std::string force_spawnpoint; /**< if set, spawnpoint will be forced to this value */
117
118 public:
119   WorldMap(const std::string& filename, const std::string& force_spawnpoint = "");
120   ~WorldMap();
121
122   void add_object(GameObject* object);
123
124   static WorldMap* current()
125   { return current_; }
126
127   virtual void setup();
128   virtual void leave();
129
130   /** Update worldmap state */
131   virtual void update(float delta);
132   /** Draw worldmap */
133   virtual void draw(DrawingContext& context);
134
135   Vector get_next_tile(Vector pos, Direction direction);
136   const Tile* at(Vector pos);
137
138   size_t level_count();
139   size_t solved_level_count();
140
141   /**
142    * gets called from the GameSession when a level has been successfully
143    * finished
144    */
145   void finished_level(Level* level);
146
147   LevelTile* at_level();
148   SpecialTile* at_special_tile();
149   SpriteChange* at_sprite_change(const Vector& pos);
150   Teleporter* at_teleporter(const Vector& pos);
151
152   /** Check if it is possible to walk from \a pos into \a direction,
153       if possible, write the new position to \a new_pos */
154   bool path_ok(Direction direction, const Vector& pos, Vector* new_pos);
155
156   /**
157    * Save worldmap state to squirrel state table
158    */
159   void save_state();
160
161   /**
162    * Load worldmap state from squirrel state table
163    */
164   void load_state();
165
166   const std::string& get_title() const
167   { return name; }
168
169   /**
170    * runs a script in the context of the worldmap (and keeps a reference to
171    * the script (so the script gets destroyed when the worldmap is destroyed)
172    */
173   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
174
175   /**
176    * switch to another worldmap.
177    * filename is relative to data root path
178    */
179   void change(const std::string& filename, const std::string& force_spawnpoint="");
180
181   /**
182    * moves Tux to the given spawnpoint
183    */
184   void move_to_spawnpoint(const std::string& spawnpoint);
185
186 private:
187   void get_level_title(LevelTile& level);
188   void draw_status(DrawingContext& context);
189   void calculate_total_stats();
190
191   void load(const std::string& filename);
192   void on_escape_press();
193
194 };
195
196 } // namespace WorldMapNS
197
198 #endif