- replaced char* with string
[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 HEADER_WORLDMAP_HXX
21 #define HEADER_WORLDMAP_HXX
22
23 #include <vector>
24 #include <string>
25
26 namespace WorldMapNS {
27
28 struct Point
29 {
30   Point() : x(0), y(0) {}
31
32   Point(const Point& pos)
33     : x(pos.x), y(pos.y) {}
34
35   Point& operator=(const Point& pos)
36   { x = pos.x;
37     y = pos.y; 
38     return *this; }
39
40   Point(int x_, int y_)
41     : x(x_), y(y_) {}
42
43   int x;
44   int y;
45 };
46
47 struct Tile
48 {
49   texture_type sprite;
50
51   // Directions in which Tux is allowed to walk from this tile
52   bool north;
53   bool east;
54   bool south;
55   bool west;
56
57   /** Stop on this tile or walk over it? */
58   bool stop;
59 };
60
61 class TileManager
62 {
63 private:
64   typedef std::vector<Tile*> Tiles;
65   Tiles tiles;
66   static TileManager* instance_ ;
67
68  TileManager();
69 public:
70   static TileManager* instance() { return instance_ ? instance_ : instance_ = new TileManager(); }
71
72   void load();
73   Tile* get(int i);
74 };
75
76 enum Direction { NONE, WEST, EAST, NORTH, SOUTH };
77
78 class WorldMap;
79
80 class Tux
81 {
82 private:
83   WorldMap* worldmap;
84   texture_type sprite;
85
86   Direction input_direction;
87   Direction direction;
88   Point tile_pos;
89   /** Length by which tux is away from its current tile, length is in
90       input_direction direction */
91   float offset;
92   bool  moving;
93
94   void stop();
95 public: 
96   Tux(WorldMap* worldmap_);
97   
98   void draw();
99   void update(float delta);
100
101   void set_direction(Direction d) { input_direction = d; }
102
103   bool is_moving() const { return moving; }
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   texture_type level_sprite;
115   bool quit;
116
117   std::string name;
118   std::string music;
119
120   std::vector<int> tilemap;
121   int width;
122   int height;
123
124   struct Level
125   {
126     int x;
127     int y;
128     std::string name;
129   };
130
131   typedef std::vector<Level> Levels;
132   Levels levels;
133
134   Mix_Music* song;
135
136   Direction input_direction;
137   bool enter_level;
138
139 public:
140   WorldMap();
141   ~WorldMap();
142
143   /** Busy loop */
144   void display();
145
146   void load_map();
147   
148   void get_input();
149
150   /** Update Tux position */
151   void update();
152
153   /** Draw one frame */
154   void draw();
155
156   Point get_next_tile(Point pos, Direction direction);
157   Tile* at(Point pos);
158
159   /** Check if it is possible to walk from \a pos into \a direction,
160       if possible, write the new position to \a new_pos */
161   bool path_ok(Direction direction, Point pos, Point* new_pos);
162 };
163
164 } // namespace WorldMapNS
165
166 void worldmap_run();
167
168 #endif
169
170 /* Local Variables: */
171 /* mode:c++ */
172 /* End */