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