- Allow custom leveldots
[supertux.git] / src / worldmap.hpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.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_WORLDMAP_H
20 #define SUPERTUX_WORLDMAP_H
21
22 #include <vector>
23 #include <string>
24
25 #include "math/vector.hpp"
26 #include "video/screen.hpp"
27 #include "lisp/lisp.hpp"
28 #include "control/controller.hpp"
29 #include "statistics.hpp"
30 #include "timer.hpp"
31 #include "tile_manager.hpp"
32 #include "game_object.hpp"
33
34 class Sprite;
35 class Menu;
36 class SpawnPoint;
37 class GameObject;
38 class TileMap;
39 extern Menu* worldmap_menu;
40
41 namespace WorldMapNS {
42
43 enum WorldMapMenuIDs {
44   MNID_RETURNWORLDMAP,
45   MNID_QUITWORLDMAP
46 };
47
48 // For one way tiles
49 enum {
50   BOTH_WAYS,
51   NORTH_SOUTH_WAY,
52   SOUTH_NORTH_WAY,
53   EAST_WEST_WAY,
54   WEST_EAST_WAY
55 };
56
57 enum Direction { D_NONE, D_WEST, D_EAST, D_NORTH, D_SOUTH };
58
59 std::string direction_to_string(Direction d);
60 Direction   string_to_direction(const std::string& d);
61 Direction reverse_dir(Direction d);
62
63 class WorldMap;
64
65 class Tux : public GameObject
66 {
67 public:
68   Direction back_direction;
69 private:
70   WorldMap* worldmap;
71   Sprite* tux_sprite;
72   Controller* controller;
73
74   Direction input_direction;
75   Direction direction;
76   Vector tile_pos;
77   /** Length by which tux is away from its current tile, length is in
78       input_direction direction */
79   float offset;
80   bool  moving;
81
82   void stop();
83 public: 
84   Tux(WorldMap* worldmap_);
85   ~Tux();
86   
87   void draw(DrawingContext& context);
88   void update(float elapsed_time);
89
90   void set_direction(Direction dir);
91
92   bool is_moving() const { return moving; }
93   Vector get_pos();
94   Vector get_tile_pos() const { return tile_pos; } 
95   void  set_tile_pos(Vector p) { tile_pos = p; } 
96 };
97
98 /** */
99 class WorldMap
100 {
101 private:
102   Tux* tux;
103
104   bool quit;
105
106   Surface* leveldot_green;
107   Surface* leveldot_red;
108   Surface* messagedot;
109   Surface* teleporterdot;
110
111   std::string name;
112   std::string music;
113
114   typedef std::vector<GameObject*> GameObjects;
115   GameObjects game_objects;
116   TileMap* solids;
117   
118   TileManager* tile_manager;
119
120 public:
121   struct SpecialTile
122   {
123     Vector pos;
124
125     /** Optional flags: */
126
127     /** Position to swap to player */
128     Vector teleport_dest;
129
130     /** Message to show in the Map */
131     std::string map_message;
132     bool passive_message;
133
134     /** Hide special tile */
135     bool invisible;
136
137     /** Only applies actions (ie. passive messages) when going to that direction */
138     bool apply_action_north;
139     bool apply_action_east;
140     bool apply_action_south;
141     bool apply_action_west;
142   };
143
144   struct Level
145   {
146     Vector pos;
147
148     std::string name;
149     std::string title;
150     bool solved;
151
152     Sprite* sprite;
153
154     /** Statistics for level tiles */
155     Statistics statistics;
156
157     /** Optional flags: */
158
159     /** Check if this level should be vertically flipped */
160     bool vertical_flip;
161
162     /** Script that is run when the level is successfully finished */
163     std::string extro_script;
164
165     /** Go to this world */
166     std::string next_worldmap;
167
168     /** Quit the worldmap */
169     bool quit_worldmap;
170
171     /** If false, disables the auto walking after finishing a level */
172     bool auto_path;
173
174     // Directions which are walkable from this level
175     bool north;
176     bool east;
177     bool south;
178     bool west;
179   };
180
181   /** Variables to deal with the passive map messages */
182   Timer passive_message_timer;
183   std::string passive_message;
184
185 private:
186   std::string map_filename;
187   std::string levels_path;
188
189   typedef std::vector<SpecialTile> SpecialTiles;
190   SpecialTiles special_tiles;
191   typedef std::vector<Level> Levels;
192   Levels levels;
193   typedef std::vector<SpawnPoint*> SpawnPoints;
194   SpawnPoints spawn_points;
195
196   Vector offset;
197   std::string savegame_file;
198   
199   std::string intro_script;
200   bool intro_displayed;
201
202   void get_level_title(Level& level);
203
204   void draw_status(DrawingContext& context);
205
206   // to avoid calculating total stats all the time. This way only
207   // when need, it is calculated.
208   Statistics total_stats;
209   void calculate_total_stats();
210
211 public:
212   WorldMap();
213   ~WorldMap();
214
215   /** Busy loop */
216   void display();
217
218   void load_map();
219   
220   void get_input();
221
222   void add_object(GameObject* object);
223   void clear_objects();
224
225   /** Update Tux position */
226   void update(float delta);
227
228   /** Draw one frame */
229   void draw(DrawingContext& context);
230
231   Vector get_next_tile(Vector pos, Direction direction);
232   const Tile* at(Vector pos);
233
234   WorldMap::Level* at_level();
235   WorldMap::SpecialTile* at_special_tile();
236
237   /** Check if it is possible to walk from \a pos into \a direction,
238       if possible, write the new position to \a new_pos */
239   bool path_ok(Direction direction, Vector pos, Vector* new_pos);
240
241   /* Save map to slot */
242   void savegame(const std::string& filename);
243   /* Load map from slot
244      You should call set_map_filename() before this */
245   void loadgame(const std::string& filename);
246   /* Load map directly from file */
247   void loadmap(const std::string& filename);
248
249   const std::string& get_world_title() const
250   { return name; }
251     
252   void set_map_filename(std::string filename)
253   { map_filename = filename; }
254
255 private:
256   void on_escape_press();
257   void parse_special_tile(const lisp::Lisp* lisp);
258   void parse_level_tile(const lisp::Lisp* lisp);
259 };
260
261 } // namespace WorldMapNS
262
263 #endif
264
265 /* Local Variables: */
266 /* mode:c++ */
267 /* End: */