yeti cleanup and death animation rework, hitbox fix ups
[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 "screen.hpp"
33 #include "tile_manager.hpp"
34 #include "game_object.hpp"
35 #include "console.hpp"
36
37 class Sprite;
38 class Menu;
39 class SpawnPoint;
40 class GameObject;
41 class TileMap;
42 extern Menu* worldmap_menu;
43
44 namespace WorldMapNS {
45
46 enum WorldMapMenuIDs {
47   MNID_RETURNWORLDMAP,
48   MNID_QUITWORLDMAP
49 };
50
51 // For one way tiles
52 enum {
53   BOTH_WAYS,
54   NORTH_SOUTH_WAY,
55   SOUTH_NORTH_WAY,
56   EAST_WEST_WAY,
57   WEST_EAST_WAY
58 };
59
60 enum Direction { D_NONE, D_WEST, D_EAST, D_NORTH, D_SOUTH };
61
62 std::string direction_to_string(Direction d);
63 Direction   string_to_direction(const std::string& d);
64 Direction reverse_dir(Direction d);
65
66 class WorldMap;
67
68 class Tux : public GameObject
69 {
70 public:
71   Direction back_direction;
72 private:
73   WorldMap* worldmap;
74   Sprite* tux_sprite;
75   Controller* controller;
76
77   Direction input_direction;
78   Direction direction;
79   Vector tile_pos;
80   /** Length by which tux is away from its current tile, length is in
81       input_direction direction */
82   float offset;
83   bool  moving;
84
85   void stop();
86
87   bool canWalk(const Tile* tile, Direction dir); /**< check if we can leave "tile" in direction "dir" */
88   void updateInputDirection(); /**< if controller was pressed, update input_direction */
89   void tryStartWalking(); /**< try starting to walk in input_direction */
90   void tryContinueWalking(float elapsed_time); /**< try to continue walking in current direction */
91
92 public: 
93   Tux(WorldMap* worldmap_);
94   ~Tux();
95   
96   void draw(DrawingContext& context);
97   void update(float elapsed_time);
98
99   void set_direction(Direction dir);
100
101   bool is_moving() const { return moving; }
102   Vector get_pos();
103   Vector get_tile_pos() const { return tile_pos; } 
104   void  set_tile_pos(Vector p) { tile_pos = p; } 
105 };
106
107 /** */
108 class WorldMap : public Screen
109 {
110 private:
111   Tux* tux;
112
113   Surface* leveldot_green;
114   Surface* leveldot_red;
115   Surface* messagedot;
116   Sprite* teleporterdot;
117   static WorldMap* current_;
118
119   Vector camera_offset;
120
121   std::string name;
122   std::string music;
123
124   typedef std::vector<GameObject*> GameObjects;
125   GameObjects game_objects;
126   TileMap* solids;
127   
128   TileManager* tile_manager;
129   
130   Console* console;
131
132 public:
133   struct SpecialTile
134   {
135     Vector pos;
136
137     /** Optional flags: */
138
139     /** Sprite to render instead of guessing what image to draw */
140     Sprite* sprite;
141
142     /** Position to swap to player */
143     Vector teleport_dest;
144
145     /** Message to show in the Map */
146     std::string map_message;
147     bool passive_message;
148
149     /** Hide special tile */
150     bool invisible;
151
152     /** Only applies actions (ie. passive messages) when going to that direction */
153     bool apply_action_north;
154     bool apply_action_east;
155     bool apply_action_south;
156     bool apply_action_west;
157   };
158
159   struct Level
160   {
161     Vector pos;
162
163     std::string name;
164     std::string title;
165     bool solved;
166
167     Sprite* sprite;
168
169     /** Statistics for level tiles */
170     Statistics statistics;
171
172     /** Optional flags: */
173
174     /** Check if this level should be vertically flipped */
175     bool vertical_flip;
176
177     /** Script that is run when the level is successfully finished */
178     std::string extro_script;
179
180     /** Go to this world */
181     std::string next_worldmap;
182
183     /** Quit the worldmap */
184     bool quit_worldmap;
185
186     /** If false, disables the auto walking after finishing a level */
187     bool auto_path;
188
189     // Directions which are walkable from this level
190     bool north;
191     bool east;
192     bool south;
193     bool west;
194   };
195
196   /** Variables to deal with the passive map messages */
197   Timer passive_message_timer;
198   std::string passive_message;
199
200 private:
201   std::string map_filename;
202   std::string levels_path;
203
204   typedef std::vector<SpecialTile> SpecialTiles;
205   SpecialTiles special_tiles;
206   typedef std::vector<Level> Levels;
207   Levels levels;
208   typedef std::vector<SpawnPoint*> SpawnPoints;
209   SpawnPoints spawn_points;
210
211   Vector offset;
212   std::string savegame_file;
213   
214   std::string intro_script;
215   bool intro_displayed;
216
217   void get_level_title(Level& level);
218
219   void draw_status(DrawingContext& context);
220
221   // to avoid calculating total stats all the time. This way only
222   // when need, it is calculated.
223   Statistics total_stats;
224   void calculate_total_stats();
225
226 public:
227   WorldMap();
228   ~WorldMap();
229
230   void load_map();
231   
232   void add_object(GameObject* object);
233   void clear_objects();
234
235   static WorldMap* current()
236   { return current_; }
237
238   void setup();
239
240   /** Update Tux position */
241   void update(float delta);
242
243   /** Draw one frame */
244   void draw(DrawingContext& context);
245
246   Vector get_next_tile(Vector pos, Direction direction);
247   const Tile* at(Vector pos);
248
249   size_t level_count();
250   size_t solved_level_count();
251
252   /**
253    * gets called from the GameSession when a level has been successfully
254    * finished
255    */
256   void finished_level(const std::string& filename);
257
258   WorldMap::Level* at_level();
259   WorldMap::SpecialTile* at_special_tile();
260
261   /** Check if it is possible to walk from \a pos into \a direction,
262       if possible, write the new position to \a new_pos */
263   bool path_ok(Direction direction, Vector pos, Vector* new_pos);
264
265   /**
266    * Save worldmap state to squirrel state table
267    */
268   void save_state();
269   /**
270    * Load worldmap state from squirrel state table
271    */
272   void load_state();
273   /**
274    * Load a worldmap
275    */
276   void loadmap(const std::string& filename);
277
278   const std::string& get_title() const
279   { return name; }
280     
281   void set_map_filename(std::string filename)
282   { map_filename = filename; }
283
284 private:
285   void on_escape_press();
286   void parse_special_tile(const lisp::Lisp* lisp);
287   void parse_level_tile(const lisp::Lisp* lisp);
288 };
289
290 } // namespace WorldMapNS
291
292 #endif
293
294 /* Local Variables: */
295 /* mode:c++ */
296 /* End: */