- Refactored worldmap a bit to reuse GameObject from the rest of the game
[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 #ifndef SUPERTUX_WORLDMAP_H
20 #define SUPERTUX_WORLDMAP_H
21
22 #include <vector>
23 #include <string>
24
25 #include "math/vector.h"
26 #include "audio/musicref.h"
27 #include "video/screen.h"
28 #include "lisp/lisp.h"
29 #include "control/controller.h"
30 #include "statistics.h"
31 #include "timer.h"
32 #include "tile_manager.h"
33 #include "game_object.h"
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 public: 
85   Tux(WorldMap* worldmap_);
86   ~Tux();
87   
88   void draw(DrawingContext& context);
89   void update(float elapsed_time);
90
91   void set_direction(Direction dir);
92
93   bool is_moving() const { return moving; }
94   Vector get_pos();
95   Vector get_tile_pos() const { return tile_pos; } 
96   void  set_tile_pos(Vector p) { tile_pos = p; } 
97 };
98
99 /** */
100 class WorldMap
101 {
102 private:
103   Tux* tux;
104
105   bool quit;
106
107   Surface* leveldot_green;
108   Surface* leveldot_red;
109   Surface* messagedot;
110   Surface* teleporterdot;
111
112   std::string name;
113   std::string music;
114
115   typedef std::vector<GameObject*> GameObjects;
116   GameObjects game_objects;
117   TileMap* solids;
118   
119   TileManager* tile_manager;
120
121 public:
122   struct SpecialTile
123   {
124     Vector pos;
125
126     /** Optional flags: */
127
128     /** Position to swap to player */
129     Vector teleport_dest;
130
131     /** Message to show in the Map */
132     std::string map_message;
133     bool passive_message;
134
135     /** Hide special tile */
136     bool invisible;
137
138     /** Only applies actions (ie. passive messages) when going to that direction */
139     bool apply_action_north;
140     bool apply_action_east;
141     bool apply_action_south;
142     bool apply_action_west;
143   };
144
145   struct Level
146   {
147     Vector pos;
148
149     std::string name;
150     std::string title;
151     bool solved;
152
153     /** Statistics for level tiles */
154     Statistics statistics;
155
156     /** Optional flags: */
157
158     /** Check if this level should be vertically flipped */
159     bool vertical_flip;
160
161     /** Filename of the extro text to show once the level is
162         successfully completed */
163     std::string extro_filename;
164
165     /** Go to this world */
166     std::string next_worldmap;
167
168     /** Quit the worldmap */
169     bool quit_worldmap;
170
171     /** If false, disables the auto walking after finishing a level */
172     bool auto_path;
173
174     // Directions which are walkable from this level
175     bool north;
176     bool east;
177     bool south;
178     bool west;
179   };
180
181   /** Variables to deal with the passive map messages */
182   Timer passive_message_timer;
183   std::string passive_message;
184
185 private:
186   std::string map_filename;
187   std::string levels_path;
188
189   typedef std::vector<SpecialTile> SpecialTiles;
190   SpecialTiles special_tiles;
191   typedef std::vector<Level> Levels;
192   Levels levels;
193   typedef std::vector<SpawnPoint*> SpawnPoints;
194   SpawnPoints spawn_points;
195
196   MusicRef song;
197
198   Vector offset;
199   std::string savegame_file;
200   
201   std::string intro_filename;
202   bool intro_displayed;
203
204   void get_level_title(Level& level);
205
206   void draw_status(DrawingContext& context);
207
208   // to avoid calculating total stats all the time. This way only
209   // when need, it is calculated.
210   Statistics total_stats;
211   void calculate_total_stats();
212
213 public:
214   WorldMap();
215   ~WorldMap();
216
217   /** Busy loop */
218   void display();
219
220   void load_map();
221   
222   void get_input();
223
224   void add_object(GameObject* object);
225   void clear_objects();
226
227   /** Update Tux position */
228   void update(float delta);
229
230   /** Draw one frame */
231   void draw(DrawingContext& context);
232
233   Vector get_next_tile(Vector pos, Direction direction);
234   const Tile* at(Vector pos);
235
236   WorldMap::Level* at_level();
237   WorldMap::SpecialTile* at_special_tile();
238
239   /** Check if it is possible to walk from \a pos into \a direction,
240       if possible, write the new position to \a new_pos */
241   bool path_ok(Direction direction, Vector pos, Vector* new_pos);
242
243   /* Save map to slot */
244   void savegame(const std::string& filename);
245   /* Load map from slot
246      You should call set_map_filename() before this */
247   void loadgame(const std::string& filename);
248   /* Load map directly from file */
249   void loadmap(const std::string& filename);
250
251   const std::string& get_world_title() const
252   { return name; }
253     
254   void set_map_filename(std::string filename)
255   { map_filename = filename; }
256
257 private:
258   void on_escape_press();
259   void parse_special_tile(const lisp::Lisp* lisp);
260   void parse_level_tile(const lisp::Lisp* lisp);
261 };
262
263 } // namespace WorldMapNS
264
265 #endif
266
267 /* Local Variables: */
268 /* mode:c++ */
269 /* End: */