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