55eddfce3f0a182aa94b945d6565c3e5e49711fd
[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 "lisp/lisp.h"
29 #include "statistics.h"
30 #include "timer.h"
31 #include "tile_manager.h"
32
33 namespace SuperTux {
34   class Menu;
35   class Sprite;
36 }
37
38 extern Menu* worldmap_menu;
39
40 namespace WorldMapNS {
41
42 enum WorldMapMenuIDs {
43   MNID_RETURNWORLDMAP,
44   MNID_QUITWORLDMAP
45 };
46
47 // For one way tiles
48 enum {
49   BOTH_WAYS,
50   NORTH_SOUTH_WAY,
51   SOUTH_NORTH_WAY,
52   EAST_WEST_WAY,
53   WEST_EAST_WAY
54 };
55
56 enum Direction { D_NONE, D_WEST, D_EAST, D_NORTH, D_SOUTH };
57
58 std::string direction_to_string(Direction d);
59 Direction   string_to_direction(const std::string& d);
60 Direction reverse_dir(Direction d);
61
62 class WorldMap;
63
64 class Tux
65 {
66 public:
67   Direction back_direction;
68 private:
69   WorldMap* worldmap;
70   Sprite* tux_sprite;
71
72   Direction input_direction;
73   Direction direction;
74   Vector tile_pos;
75   /** Length by which tux is away from its current tile, length is in
76       input_direction direction */
77   float offset;
78   bool  moving;
79
80   void stop();
81 public: 
82   Tux(WorldMap* worldmap_);
83   ~Tux();
84   
85   void draw(DrawingContext& context);
86   void action(float elapsed_time);
87
88   void set_direction(Direction dir);
89
90   bool is_moving() const { return moving; }
91   Vector get_pos();
92   Vector get_tile_pos() const { return tile_pos; } 
93   void  set_tile_pos(Vector p) { tile_pos = p; } 
94 };
95
96 /** */
97 class WorldMap
98 {
99 private:
100   Tux* tux;
101
102   bool quit;
103
104   Surface* leveldot_green;
105   Surface* leveldot_red;
106   Surface* messagedot;
107   Surface* teleporterdot;
108
109   std::string name;
110   std::string music;
111
112   std::vector<int> tilemap;
113   int width;
114   int height;
115   
116   int start_x;
117   int start_y;
118
119   TileManager* tile_manager;
120
121 public:
122   struct SpecialTile
123   {
124     Vector pos;
125
126     /** Optional flags: */
127
128     /** Position to swap to player */
129     Vector teleport_dest;
130
131     /** Message to show in the Map */
132     std::string map_message;
133     bool passive_message;
134
135     /** Hide special tile */
136     bool invisible;
137
138     /** Only applies actions (ie. passive messages) when going to that direction */
139     bool apply_action_north;
140     bool apply_action_east;
141     bool apply_action_south;
142     bool apply_action_west;
143   };
144
145   struct Level
146   {
147     Vector pos;
148
149     std::string name;
150     std::string title;
151     bool solved;
152
153     /** Statistics for level tiles */
154     Statistics statistics;
155
156     /** Optional flags: */
157
158     /** Check if this level should be vertically flipped */
159     bool vertical_flip;
160
161     /** Filename of the extro text to show once the level is
162         successfully completed */
163     std::string extro_filename;
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   Timer2 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
192   typedef std::vector<Level> Levels;
193   Levels levels;
194
195   MusicRef song;
196
197   bool enter_level;
198
199   Vector offset;
200   std::string savegame_file;
201   
202   std::string intro_filename;
203   bool intro_displayed;
204
205   void get_level_title(Level& level);
206
207   void draw_status(DrawingContext& context);
208
209   // to avoid calculating total stats all the time. This way only
210   // when need, it is calculated.
211   Statistics total_stats;
212   void calculate_total_stats();
213
214 public:
215   WorldMap();
216   ~WorldMap();
217
218   /** Busy loop */
219   void display();
220
221   void load_map();
222   
223   void get_input();
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   const int& get_start_x() const
253     { return start_x; }
254   
255   const int& get_start_y() const
256     { return start_y; }
257
258   void set_map_filename(std::string filename)
259     { map_filename = filename; }
260
261 private:
262   void on_escape_press();
263   void parse_special_tiles(const lisp::Lisp* lisp);
264 };
265
266 } // namespace WorldMapNS
267
268 #endif
269
270 /* Local Variables: */
271 /* mode:c++ */
272 /* End: */