- fixed 'When you jump into the roof or a bonus and fall back down you collide with...
[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 "musicref.h"
27 #include "screen.h"
28
29 namespace WorldMapNS {
30
31 class Tile
32 {
33 public:
34   Tile();
35   ~Tile();
36   
37   Surface* sprite;
38
39   // Directions in which Tux is allowed to walk from this tile
40   bool north;
41   bool east;
42   bool south;
43   bool west;
44
45   /** Stop on this tile or walk over it? */
46   bool stop;
47
48   /** When set automatically turn directions when walked over such a
49       tile (ie. walk smoothly a curve) */
50   bool auto_walk;
51 };
52
53 class TileManager
54 {
55 private:
56   typedef std::vector<Tile*> Tiles;
57   Tiles tiles;
58
59 public:
60   TileManager();
61   ~TileManager();
62
63   Tile* get(int i);
64 };
65
66 enum Direction { D_NONE, D_WEST, D_EAST, D_NORTH, D_SOUTH };
67
68 std::string direction_to_string(Direction d);
69 Direction   string_to_direction(const std::string& d);
70 Direction reverse_dir(Direction d);
71
72 class WorldMap;
73
74 class Tux
75 {
76 public:
77   Direction back_direction;
78 private:
79   WorldMap* worldmap;
80   Surface* largetux_sprite;
81   Surface* firetux_sprite;
82   Surface* smalltux_sprite;
83
84   Direction input_direction;
85   Direction direction;
86   Point tile_pos;
87   /** Length by which tux is away from its current tile, length is in
88       input_direction direction */
89   float offset;
90   bool  moving;
91
92   void stop();
93 public: 
94   Tux(WorldMap* worldmap_);
95   ~Tux();
96   
97   void draw(const Point& offset);
98   void update(float delta);
99
100   void set_direction(Direction d) { input_direction = d; }
101
102   bool is_moving() const { return moving; }
103   Point get_pos();
104   Point get_tile_pos() const { return tile_pos; } 
105   void  set_tile_pos(Point p) { tile_pos = p; } 
106 };
107
108 /** */
109 class WorldMap
110 {
111 private:
112   Tux* tux;
113
114   bool quit;
115
116   Surface* level_sprite;
117   Surface* leveldot_green;
118   Surface* leveldot_red;
119
120   std::string name;
121   std::string music;
122
123   std::vector<int> tilemap;
124   int width;
125   int height;
126
127   TileManager* tile_manager;
128
129 public:
130   struct Level
131   {
132     int x;
133     int y;
134     std::string name;
135     std::string title;
136     bool solved;
137
138     /** Filename of the extro text to show once the level is
139         successfully completed */
140     std::string extro_filename;
141
142     // Directions which are walkable from this level
143     bool north;
144     bool east;
145     bool south;
146     bool west;
147   };
148
149 private:
150   typedef std::vector<Level> Levels;
151   Levels levels;
152
153   MusicRef song;
154
155   Direction input_direction;
156   bool enter_level;
157
158   Point offset;
159   std::string savegame_file;
160
161   void get_level_title(Levels::pointer level);
162
163   void draw_status();
164 public:
165   WorldMap();
166   ~WorldMap();
167
168   /** Busy loop */
169   void display();
170
171   void load_map();
172   
173   void get_input();
174
175   /** Update Tux position */
176   void update(float delta);
177
178   /** Draw one frame */
179   void draw(const Point& offset);
180
181   Point get_next_tile(Point pos, Direction direction);
182   Tile* at(Point pos);
183   WorldMap::Level* at_level();
184
185   /** Check if it is possible to walk from \a pos into \a direction,
186       if possible, write the new position to \a new_pos */
187   bool path_ok(Direction direction, Point pos, Point* new_pos);
188
189   void savegame(const std::string& filename);
190   void loadgame(const std::string& filename);
191 private:
192   void on_escape_press();
193 };
194
195 } // namespace WorldMapNS
196
197 #endif
198
199 /* Local Variables: */
200 /* mode:c++ */
201 /* End: */