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