fix bonus1 worldmap
[supertux.git] / src / worldmap.hpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
5 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 // 
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 #ifndef SUPERTUX_WORLDMAP_H
21 #define SUPERTUX_WORLDMAP_H
22
23 #include <vector>
24 #include <string>
25
26 #include "math/vector.hpp"
27 #include "video/screen.hpp"
28 #include "lisp/lisp.hpp"
29 #include "control/controller.hpp"
30 #include "statistics.hpp"
31 #include "timer.hpp"
32 #include "tile_manager.hpp"
33 #include "game_object.hpp"
34
35 class Sprite;
36 class Menu;
37 class SpawnPoint;
38 class GameObject;
39 class TileMap;
40 extern Menu* worldmap_menu;
41
42 namespace WorldMapNS {
43
44 enum WorldMapMenuIDs {
45   MNID_RETURNWORLDMAP,
46   MNID_QUITWORLDMAP
47 };
48
49 // For one way tiles
50 enum {
51   BOTH_WAYS,
52   NORTH_SOUTH_WAY,
53   SOUTH_NORTH_WAY,
54   EAST_WEST_WAY,
55   WEST_EAST_WAY
56 };
57
58 enum Direction { D_NONE, D_WEST, D_EAST, D_NORTH, D_SOUTH };
59
60 std::string direction_to_string(Direction d);
61 Direction   string_to_direction(const std::string& d);
62 Direction reverse_dir(Direction d);
63
64 class WorldMap;
65
66 class Tux : public GameObject
67 {
68 public:
69   Direction back_direction;
70 private:
71   WorldMap* worldmap;
72   Sprite* tux_sprite;
73   Controller* controller;
74
75   Direction input_direction;
76   Direction direction;
77   Vector tile_pos;
78   /** Length by which tux is away from its current tile, length is in
79       input_direction direction */
80   float offset;
81   bool  moving;
82
83   void stop();
84
85   bool canWalk(const Tile* tile, Direction dir); /**< check if we can leave "tile" in direction "dir" */
86   void updateInputDirection(); /**< if controller was pressed, update input_direction */
87   void tryStartWalking(); /**< try starting to walk in input_direction */
88   void tryContinueWalking(float elapsed_time); /**< try to continue walking in current direction */
89
90 public: 
91   Tux(WorldMap* worldmap_);
92   ~Tux();
93   
94   void draw(DrawingContext& context);
95   void update(float elapsed_time);
96
97   void set_direction(Direction dir);
98
99   bool is_moving() const { return moving; }
100   Vector get_pos();
101   Vector get_tile_pos() const { return tile_pos; } 
102   void  set_tile_pos(Vector p) { tile_pos = p; } 
103 };
104
105 /** */
106 class WorldMap
107 {
108 private:
109   Tux* tux;
110
111   bool quit;
112
113   Surface* leveldot_green;
114   Surface* leveldot_red;
115   Surface* messagedot;
116   Sprite* teleporterdot;
117
118   std::string name;
119   std::string music;
120
121   typedef std::vector<GameObject*> GameObjects;
122   GameObjects game_objects;
123   TileMap* solids;
124   
125   TileManager* tile_manager;
126
127 public:
128   struct SpecialTile
129   {
130     Vector pos;
131
132     /** Optional flags: */
133
134     /** Sprite to render instead of guessing what image to draw */
135     Sprite* sprite;
136
137     /** Position to swap to player */
138     Vector teleport_dest;
139
140     /** Message to show in the Map */
141     std::string map_message;
142     bool passive_message;
143
144     /** Hide special tile */
145     bool invisible;
146
147     /** Only applies actions (ie. passive messages) when going to that direction */
148     bool apply_action_north;
149     bool apply_action_east;
150     bool apply_action_south;
151     bool apply_action_west;
152   };
153
154   struct Level
155   {
156     Vector pos;
157
158     std::string name;
159     std::string title;
160     bool solved;
161
162     Sprite* sprite;
163
164     /** Statistics for level tiles */
165     Statistics statistics;
166
167     /** Optional flags: */
168
169     /** Check if this level should be vertically flipped */
170     bool vertical_flip;
171
172     /** Script that is run when the level is successfully finished */
173     std::string extro_script;
174
175     /** Go to this world */
176     std::string next_worldmap;
177
178     /** Quit the worldmap */
179     bool quit_worldmap;
180
181     /** If false, disables the auto walking after finishing a level */
182     bool auto_path;
183
184     // Directions which are walkable from this level
185     bool north;
186     bool east;
187     bool south;
188     bool west;
189   };
190
191   /** Variables to deal with the passive map messages */
192   Timer passive_message_timer;
193   std::string passive_message;
194
195 private:
196   std::string map_filename;
197   std::string levels_path;
198
199   typedef std::vector<SpecialTile> SpecialTiles;
200   SpecialTiles special_tiles;
201   typedef std::vector<Level> Levels;
202   Levels levels;
203   typedef std::vector<SpawnPoint*> SpawnPoints;
204   SpawnPoints spawn_points;
205
206   Vector offset;
207   std::string savegame_file;
208   
209   std::string intro_script;
210   bool intro_displayed;
211
212   void get_level_title(Level& level);
213
214   void draw_status(DrawingContext& context);
215
216   // to avoid calculating total stats all the time. This way only
217   // when need, it is calculated.
218   Statistics total_stats;
219   void calculate_total_stats();
220
221 public:
222   WorldMap();
223   ~WorldMap();
224
225   /** Busy loop */
226   void display();
227
228   void load_map();
229   
230   void get_input();
231
232   void add_object(GameObject* object);
233   void clear_objects();
234
235   /** Update Tux position */
236   void update(float delta);
237
238   /** Draw one frame */
239   void draw(DrawingContext& context);
240
241   Vector get_next_tile(Vector pos, Direction direction);
242   const Tile* at(Vector pos);
243
244   WorldMap::Level* at_level();
245   WorldMap::SpecialTile* at_special_tile();
246
247   /** Check if it is possible to walk from \a pos into \a direction,
248       if possible, write the new position to \a new_pos */
249   bool path_ok(Direction direction, Vector pos, Vector* new_pos);
250
251   /* Save map to slot */
252   void savegame(const std::string& filename);
253   /* Load map from slot
254      You should call set_map_filename() before this */
255   void loadgame(const std::string& filename);
256   /* Load map directly from file */
257   void loadmap(const std::string& filename);
258
259   const std::string& get_world_title() const
260   { return name; }
261     
262   void set_map_filename(std::string filename)
263   { map_filename = filename; }
264
265 private:
266   void on_escape_press();
267   void parse_special_tile(const lisp::Lisp* lisp);
268   void parse_level_tile(const lisp::Lisp* lisp);
269 };
270
271 } // namespace WorldMapNS
272
273 #endif
274
275 /* Local Variables: */
276 /* mode:c++ */
277 /* End: */