10b86d7fb6c21cf36384adf22cb3eebd1c82ef3d
[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
30 extern Menu* worldmap_menu;
31
32 namespace WorldMapNS {
33
34 enum WorldMapMenuIDs {
35   MNID_RETURNWORLDMAP,
36   MNID_QUITWORLDMAP
37   };
38
39 class Tile
40 {
41 public:
42   Tile();
43   ~Tile();
44   
45   Surface* sprite;
46
47   // Directions in which Tux is allowed to walk from this tile
48   bool north;
49   bool east;
50   bool south;
51   bool west;
52
53   /** Stop on this tile or walk over it? */
54   bool stop;
55
56   /** When set automatically turn directions when walked over such a
57       tile (ie. walk smoothly a curve) */
58   bool auto_walk;
59 };
60
61 class TileManager
62 {
63 private:
64   typedef std::vector<Tile*> Tiles;
65   Tiles tiles;
66
67 public:
68   TileManager();
69   ~TileManager();
70
71   Tile* get(int i);
72 };
73
74 enum Direction { D_NONE, D_WEST, D_EAST, D_NORTH, D_SOUTH };
75
76 std::string direction_to_string(Direction d);
77 Direction   string_to_direction(const std::string& d);
78 Direction reverse_dir(Direction d);
79
80 class WorldMap;
81
82 class Tux
83 {
84 public:
85   Direction back_direction;
86 private:
87   WorldMap* worldmap;
88   Surface* largetux_sprite;
89   Surface* firetux_sprite;
90   Surface* smalltux_sprite;
91
92   Direction input_direction;
93   Direction direction;
94   Vector tile_pos;
95   /** Length by which tux is away from its current tile, length is in
96       input_direction direction */
97   float offset;
98   bool  moving;
99
100   void stop();
101 public: 
102   Tux(WorldMap* worldmap_);
103   ~Tux();
104   
105   void draw(DrawingContext& context, const Vector& offset);
106   void action(float elapsed_time);
107
108   void set_direction(Direction dir);
109
110   bool is_moving() const { return moving; }
111   Vector get_pos();
112   Vector get_tile_pos() const { return tile_pos; } 
113   void  set_tile_pos(Vector p) { tile_pos = p; } 
114 };
115
116 /** */
117 class WorldMap
118 {
119 private:
120   Tux* tux;
121
122   bool quit;
123
124   Surface* leveldot_green;
125   Surface* leveldot_red;
126   Surface* messagedot;
127
128   std::string name;
129   std::string music;
130
131   std::vector<int> tilemap;
132   int width;
133   int height;
134   
135   int start_x;
136   int start_y;
137
138   TileManager* tile_manager;
139
140 public:
141   struct SpecialTile
142   {
143     int x;
144     int y;
145     std::string level_name;
146     std::string title;
147     bool solved;
148
149     /** Optional flags: */
150
151     /** Check if this level should be vertically flipped */
152     bool vertical_flip;
153
154     /** Filename of the extro text to show once the level is
155         successfully completed */
156     std::string extro_filename;
157
158     /** Position to swap player */
159     int swap_x, swap_y;
160
161     /** Message to show in the Map */
162     std::string display_map_message;
163     bool passive_message;
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   Timer passive_message_timer;
183   std::string passive_message;
184
185 private:
186   std::string map_filename;
187
188   typedef std::vector<SpecialTile> SpecialTiles;
189   SpecialTiles special_tiles;
190
191   MusicRef song;
192
193   bool enter_level;
194
195   Vector offset;
196   std::string savegame_file;
197
198   void get_level_title(SpecialTile& special_tile);
199
200   void draw_status(DrawingContext& context);
201 public:
202   WorldMap();
203   ~WorldMap();
204
205   /** Busy loop */
206   void display();
207
208   void load_map();
209   
210   void get_input();
211
212   /** Update Tux position */
213   void update(float delta);
214
215   /** Draw one frame */
216   void draw(DrawingContext& context, const Vector& offset);
217
218   Vector get_next_tile(Vector pos, Direction direction);
219   Tile* at(Vector pos);
220   WorldMap::SpecialTile* at_special_tile();
221
222   /** Check if it is possible to walk from \a pos into \a direction,
223       if possible, write the new position to \a new_pos */
224   bool path_ok(Direction direction, Vector pos, Vector* new_pos);
225
226   /* Save map to slot */
227   void savegame(const std::string& filename);
228   /* Load map from slot */
229   void loadgame(const std::string& filename);
230   /* Load map directly from file */
231   void loadmap(const std::string& filename);
232
233   const std::string& get_world_title() const
234     { return name; }
235     
236   const int& get_start_x() const
237     { return start_x; }
238   
239   const int& get_start_y() const
240     { return start_y; }
241
242 private:
243   void on_escape_press();
244 };
245
246 } // namespace WorldMapNS
247
248 #endif
249
250 /* Local Variables: */
251 /* mode:c++ */
252 /* End: */