leveleditor menu IDisation and little improvements
[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   /** When set automatically turn directions when walked over such a
67       tile (ie. walk smoothly a curve) */
68   bool auto_walk;
69 };
70
71 class TileManager
72 {
73 private:
74   typedef std::vector<Tile*> Tiles;
75   Tiles tiles;
76
77 public:
78   TileManager();
79   ~TileManager();
80
81   Tile* get(int i);
82 };
83
84 enum Direction { NONE, WEST, EAST, NORTH, SOUTH };
85
86 std::string direction_to_string(Direction d);
87 Direction   string_to_direction(const std::string& d);
88 Direction reverse_dir(Direction d);
89
90 class WorldMap;
91
92 class Tux
93 {
94 public:
95   Direction back_direction;
96 private:
97   WorldMap* worldmap;
98   Surface* largetux_sprite;
99   Surface* firetux_sprite;
100   Surface* smalltux_sprite;
101
102   Direction input_direction;
103   Direction direction;
104   Point tile_pos;
105   /** Length by which tux is away from its current tile, length is in
106       input_direction direction */
107   float offset;
108   bool  moving;
109
110   void stop();
111 public: 
112   Tux(WorldMap* worldmap_);
113   ~Tux();
114   
115   void draw(const Point& offset);
116   void update(float delta);
117
118   void set_direction(Direction d) { input_direction = d; }
119
120   bool is_moving() const { return moving; }
121   Point get_pos();
122   Point get_tile_pos() const { return tile_pos; } 
123   void  set_tile_pos(Point p) { tile_pos = p; } 
124 };
125
126 /** */
127 class WorldMap
128 {
129 private:
130   Tux* tux;
131
132   bool quit;
133
134   Surface* level_sprite;
135   Surface* leveldot_green;
136   Surface* leveldot_red;
137
138   std::string name;
139   std::string music;
140
141   std::vector<int> tilemap;
142   int width;
143   int height;
144
145   TileManager* tile_manager;
146
147 public:
148   struct Level
149   {
150     int x;
151     int y;
152     std::string name;
153     std::string title;
154     bool solved;
155
156     /** Filename of the extro text to show once the level is
157         successfully completed */
158     std::string extro_filename;
159
160     // Directions which are walkable from this level
161     bool north;
162     bool east;
163     bool south;
164     bool west;
165   };
166
167 private:
168   typedef std::vector<Level> Levels;
169   Levels levels;
170
171   MusicRef song;
172
173   Direction input_direction;
174   bool enter_level;
175
176   Point offset;
177   std::string savegame_file;
178
179   void get_level_title(Levels::pointer level);
180
181   void draw_status();
182 public:
183   WorldMap();
184   ~WorldMap();
185
186   /** Busy loop */
187   void display();
188
189   void load_map();
190   
191   void get_input();
192
193   /** Update Tux position */
194   void update(float delta);
195
196   /** Draw one frame */
197   void draw(const Point& offset);
198
199   Point get_next_tile(Point pos, Direction direction);
200   Tile* at(Point pos);
201   WorldMap::Level* at_level();
202
203   /** Check if it is possible to walk from \a pos into \a direction,
204       if possible, write the new position to \a new_pos */
205   bool path_ok(Direction direction, Point pos, Point* new_pos);
206
207   void savegame(const std::string& filename);
208   void loadgame(const std::string& filename);
209 private:
210   void on_escape_press();
211 };
212
213 } // namespace WorldMapNS
214
215 #endif
216
217 /* Local Variables: */
218 /* mode:c++ */
219 /* End: */