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