4ad8243cc3742edea6342c25be2bd14d8816a376
[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 Level
142   {
143     int x;
144     int y;
145     std::string 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     // Directions which are walkable from this level
171     bool north;
172     bool east;
173     bool south;
174     bool west;
175   };
176
177 private:
178   std::string map_filename;
179
180   typedef std::vector<Level> Levels;
181   Levels levels;
182
183   MusicRef song;
184
185   Direction input_direction;
186   bool enter_level;
187
188   Vector offset;
189   std::string savegame_file;
190
191   void get_level_title(Level& level);
192
193   void draw_status(DrawingContext& context);
194 public:
195   WorldMap();
196   ~WorldMap();
197
198   /** Busy loop */
199   void display();
200
201   void load_map();
202   
203   void get_input();
204
205   /** Update Tux position */
206   void update(float delta);
207
208   /** Draw one frame */
209   void draw(DrawingContext& context, const Vector& offset);
210
211   Vector get_next_tile(Vector pos, Direction direction);
212   Tile* at(Vector pos);
213   WorldMap::Level* at_level();
214
215   /** Check if it is possible to walk from \a pos into \a direction,
216       if possible, write the new position to \a new_pos */
217   bool path_ok(Direction direction, Vector pos, Vector* new_pos);
218
219   /* Save map to slot */
220   void savegame(const std::string& filename);
221   /* Load map from slot */
222   void loadgame(const std::string& filename);
223   /* Load map directly from file */
224   void loadmap(const std::string& filename);
225
226   const std::string& get_world_title() const
227     { return name; }
228     
229   const int& get_start_x() const
230     { return start_x; }
231   
232   const int& get_start_y() const
233     { return start_y; }
234
235 private:
236   void on_escape_press();
237 };
238
239 } // namespace WorldMapNS
240
241 #endif
242
243 /* Local Variables: */
244 /* mode:c++ */
245 /* End: */