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