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