- some more savegame stuff
[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 class WorldMap;
81
82 class Tux
83 {
84 private:
85   WorldMap* worldmap;
86   Surface* sprite;
87
88   Direction input_direction;
89   Direction direction;
90   Point tile_pos;
91   /** Length by which tux is away from its current tile, length is in
92       input_direction direction */
93   float offset;
94   bool  moving;
95
96   void stop();
97 public: 
98   Tux(WorldMap* worldmap_);
99   
100   void draw(const Point& offset);
101   void update(float delta);
102
103   void set_direction(Direction d) { input_direction = d; }
104
105   bool is_moving() const { return moving; }
106   Point get_pos();
107   Point get_tile_pos() const { return tile_pos; } 
108   void  set_tile_pos(Point p) { tile_pos = p; } 
109 };
110
111 /** */
112 class WorldMap
113 {
114 private:
115   Tux* tux;
116
117   Surface* level_sprite;
118   Surface* leveldot_green;
119   Surface* leveldot_red;
120
121   bool quit;
122
123   std::string name;
124   std::string music;
125
126   std::vector<int> tilemap;
127   int width;
128   int height;
129
130   struct Level
131   {
132     int x;
133     int y;
134     std::string name;
135     bool solved;
136   };
137
138   typedef std::vector<Level> Levels;
139   Levels levels;
140
141   Mix_Music* song;
142
143   Direction input_direction;
144   bool enter_level;
145
146   Point offset;
147
148   void draw_status();
149 public:
150   WorldMap();
151   ~WorldMap();
152
153   /** Busy loop */
154   void display();
155
156   void load_map();
157   
158   void get_input();
159
160   /** Update Tux position */
161   void update();
162
163   /** Draw one frame */
164   void draw(const Point& offset);
165
166   Point get_next_tile(Point pos, Direction direction);
167   Tile* at(Point pos);
168
169   /** Check if it is possible to walk from \a pos into \a direction,
170       if possible, write the new position to \a new_pos */
171   bool path_ok(Direction direction, Point pos, Point* new_pos);
172
173   void savegame(const std::string& filename);
174 };
175
176 } // namespace WorldMapNS
177
178 void worldmap_run();
179
180 #endif
181
182 /* Local Variables: */
183 /* mode:c++ */
184 /* End: */