45431d786dbf2fe8e4751f0cb0d3619bbe0f05b3
[supertux.git] / src / worldmap.h
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
20 #ifndef SUPERTUX_WORLDMAP_H
21 #define SUPERTUX_WORLDMAP_H
22
23 #include <vector>
24 #include <string>
25
26 #include "math/vector.h"
27 #include "audio/musicref.h"
28 #include "video/screen.h"
29 #include "statistics.h"
30 #include "timer.h"
31
32 namespace SuperTux {
33   class Menu;
34 }
35
36 extern Menu* worldmap_menu;
37
38 namespace WorldMapNS {
39
40 enum WorldMapMenuIDs {
41   MNID_RETURNWORLDMAP,
42   MNID_QUITWORLDMAP
43   };
44
45 // For one way tiles
46 enum {
47   BOTH_WAYS,
48   NORTH_SOUTH_WAY,
49   SOUTH_NORTH_WAY,
50   EAST_WEST_WAY,
51   WEST_EAST_WAY
52   };
53
54 class Tile
55 {
56 public:
57   Tile();
58   ~Tile();
59
60   void draw(DrawingContext& context, Vector pos);
61
62   std::vector<Surface*> images;
63   int anim_speed;
64
65   // Directions in which Tux is allowed to walk from this tile
66   bool north;
67   bool east;
68   bool south;
69   bool west;
70
71   /** One way tile */
72   int one_way;
73
74   /** Stop on this tile or walk over it? */
75   bool stop;
76
77   /** When set automatically turn directions when walked over such a
78       tile (ie. walk smoothly a curve) */
79   bool auto_walk;
80 };
81
82 class TileManager
83 {
84 private:
85   typedef std::vector<Tile*> Tiles;
86   Tiles tiles;
87
88 public:
89   TileManager();
90   ~TileManager();
91
92   Tile* get(int i);
93 };
94
95 enum Direction { D_NONE, D_WEST, D_EAST, D_NORTH, D_SOUTH };
96
97 std::string direction_to_string(Direction d);
98 Direction   string_to_direction(const std::string& d);
99 Direction reverse_dir(Direction d);
100
101 class WorldMap;
102
103 class Tux
104 {
105 public:
106   Direction back_direction;
107 private:
108   WorldMap* worldmap;
109   Surface* largetux_sprite;
110   Surface* firetux_sprite;
111   Surface* smalltux_sprite;
112
113   Direction input_direction;
114   Direction direction;
115   Vector tile_pos;
116   /** Length by which tux is away from its current tile, length is in
117       input_direction direction */
118   float offset;
119   bool  moving;
120
121   void stop();
122 public: 
123   Tux(WorldMap* worldmap_);
124   ~Tux();
125   
126   void draw(DrawingContext& context, const Vector& offset);
127   void action(float elapsed_time);
128
129   void set_direction(Direction dir);
130
131   bool is_moving() const { return moving; }
132   Vector get_pos();
133   Vector get_tile_pos() const { return tile_pos; } 
134   void  set_tile_pos(Vector p) { tile_pos = p; } 
135 };
136
137 /** */
138 class WorldMap
139 {
140 private:
141   Tux* tux;
142
143   bool quit;
144
145   Surface* leveldot_green;
146   Surface* leveldot_red;
147   Surface* messagedot;
148   Surface* teleporterdot;
149
150   std::string name;
151   std::string music;
152
153   std::vector<int> tilemap;
154   int width;
155   int height;
156   
157   int start_x;
158   int start_y;
159
160   TileManager* tile_manager;
161
162 public:
163   struct SpecialTile
164   {
165     Vector pos;
166
167     /** Optional flags: */
168
169     /** Position to swap to player */
170     Vector teleport_dest;
171
172     /** Message to show in the Map */
173     std::string map_message;
174     bool passive_message;
175
176     /** Hide special tile */
177     bool invisible;
178
179     /** Only applies actions (ie. passive messages) when going to that direction */
180     bool apply_action_north;
181     bool apply_action_east;
182     bool apply_action_south;
183     bool apply_action_west;
184   };
185
186   struct Level
187   {
188     Vector pos;
189
190     std::string name;
191     std::string title;
192     bool solved;
193
194     /** Statistics for level tiles */
195     Statistics statistics;
196
197     /** Optional flags: */
198
199     /** Check if this level should be vertically flipped */
200     bool vertical_flip;
201
202     /** Filename of the extro text to show once the level is
203         successfully completed */
204     std::string extro_filename;
205
206     /** Go to this world */
207     std::string next_worldmap;
208
209     /** Quit the worldmap */
210     bool quit_worldmap;
211
212     /** If false, disables the auto walking after finishing a level */
213     bool auto_path;
214
215     // Directions which are walkable from this level
216     bool north;
217     bool east;
218     bool south;
219     bool west;
220   };
221
222   /** Variables to deal with the passive map messages */
223   Timer2 passive_message_timer;
224   std::string passive_message;
225
226 private:
227   std::string map_filename;
228
229   typedef std::vector<SpecialTile> SpecialTiles;
230   SpecialTiles special_tiles;
231
232   typedef std::vector<Level> Levels;
233   Levels levels;
234
235   MusicRef song;
236
237   bool enter_level;
238
239   Vector offset;
240   std::string savegame_file;
241
242   void get_level_title(Level& level);
243
244   void draw_status(DrawingContext& context);
245
246   // to avoid calculating total stats all the time. This way only
247   // when need, it is calculated.
248   Statistics total_stats;
249   void calculate_total_stats();
250
251   Timer2 frame_timer;
252
253 public:
254   WorldMap();
255   ~WorldMap();
256
257   /** Busy loop */
258   void display();
259
260   void load_map();
261   
262   void get_input();
263
264   /** Update Tux position */
265   void update(float delta);
266
267   /** Draw one frame */
268   void draw(DrawingContext& context, const Vector& offset);
269
270   Vector get_next_tile(Vector pos, Direction direction);
271   Tile* at(Vector pos);
272
273   WorldMap::Level* at_level();
274   WorldMap::SpecialTile* at_special_tile();
275
276   /** Check if it is possible to walk from \a pos into \a direction,
277       if possible, write the new position to \a new_pos */
278   bool path_ok(Direction direction, Vector pos, Vector* new_pos);
279
280   /* Save map to slot */
281   void savegame(const std::string& filename);
282   /* Load map from slot
283      You should call set_map_filename() before this */
284   void loadgame(const std::string& filename);
285   /* Load map directly from file */
286   void loadmap(const std::string& filename);
287
288   const std::string& get_world_title() const
289     { return name; }
290     
291   const int& get_start_x() const
292     { return start_x; }
293   
294   const int& get_start_y() const
295     { return start_y; }
296
297   void set_map_filename(std::string filename)
298     { map_filename = filename; }
299
300 private:
301   void on_escape_press();
302 };
303
304 } // namespace WorldMapNS
305
306 #endif
307
308 /* Local Variables: */
309 /* mode:c++ */
310 /* End: */