Renamed Level by SpecialTile.
[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 d) { input_direction = d; }
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* level_sprite;
125   Surface* leveldot_green;
126   Surface* leveldot_red;
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
164     /** Go to this world */
165     std::string next_worldmap;
166
167     /** Quit the worldmap */
168     bool quit_worldmap;
169
170     /** If false, disables the auto walking after finishing a level */
171     bool auto_path;
172
173     // Directions which are walkable from this level
174     bool north;
175     bool east;
176     bool south;
177     bool west;
178   };
179
180 private:
181   std::string map_filename;
182
183   typedef std::vector<SpecialTile> SpecialTiles;
184   SpecialTiles special_tiles;
185
186   MusicRef song;
187
188   Direction input_direction;
189   bool enter_level;
190
191   Vector offset;
192   std::string savegame_file;
193
194   void get_level_title(SpecialTile& special_tile);
195
196   void draw_status(DrawingContext& context);
197 public:
198   WorldMap();
199   ~WorldMap();
200
201   /** Busy loop */
202   void display();
203
204   void load_map();
205   
206   void get_input();
207
208   /** Update Tux position */
209   void update(float delta);
210
211   /** Draw one frame */
212   void draw(DrawingContext& context, const Vector& offset);
213
214   Vector get_next_tile(Vector pos, Direction direction);
215   Tile* at(Vector pos);
216   WorldMap::SpecialTile* at_special_tile();
217
218   /** Check if it is possible to walk from \a pos into \a direction,
219       if possible, write the new position to \a new_pos */
220   bool path_ok(Direction direction, Vector pos, Vector* new_pos);
221
222   /* Save map to slot */
223   void savegame(const std::string& filename);
224   /* Load map from slot */
225   void loadgame(const std::string& filename);
226   /* Load map directly from file */
227   void loadmap(const std::string& filename);
228
229   const std::string& get_world_title() const
230     { return name; }
231     
232   const int& get_start_x() const
233     { return start_x; }
234   
235   const int& get_start_y() const
236     { return start_y; }
237
238 private:
239   void on_escape_press();
240 };
241
242 } // namespace WorldMapNS
243
244 #endif
245
246 /* Local Variables: */
247 /* mode:c++ */
248 /* End: */