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