35315c17ab8baac6efd000ba39f4f9ee297a98ea
[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 Level
48 {
49   int x;
50   int y;
51   std::string name;
52 };
53
54 struct Tile
55 {
56   texture_type sprite;
57
58   // Directions in which Tux is allowed to walk from this tile
59   bool north;
60   bool east;
61   bool south;
62   bool west;
63
64   /** Stop on this tile or walk over it? */
65   bool stop;
66 };
67
68 class TileManager
69 {
70 private:
71   typedef std::vector<Tile*> Tiles;
72   Tiles tiles;
73   static TileManager* instance_ ;
74
75  TileManager();
76 public:
77   static TileManager* instance() { return instance_ ? instance_ : instance_ = new TileManager(); }
78
79   void load();
80   Tile* get(int i);
81 };
82
83 /** */
84 class WorldMap
85 {
86 private:
87   texture_type tux_sprite;
88   texture_type level_sprite;
89   bool quit;
90
91   std::string name;
92   std::string music;
93
94   std::vector<int> tilemap;
95   int width;
96   int height;
97
98   typedef std::vector<Level> Levels;
99   Levels levels;
100
101   Mix_Music* song;
102
103   enum Direction { NONE, WEST, EAST, NORTH, SOUTH };
104   Direction tux_direction;
105   Point tux_tile_pos;
106   /** Length by which tux is away from its current tile, length is in
107       input_direction direction */
108   float tux_offset;
109   bool tux_moving;
110
111   Direction input_direction;
112   bool enter_level;
113
114   Tile* at(Point pos);
115   Point get_next_tile(Point pos, Direction direction);
116   bool path_ok(Direction direction, Point old_pos, Point* new_pos);
117 public:
118   WorldMap();
119   ~WorldMap();
120
121   /** Busy loop */
122   void display();
123
124   void load_map();
125   
126   void get_input();
127
128   /** Update Tux position */
129   void update();
130
131   /** Draw one frame */
132   void draw();
133 };
134
135 } // namespace WorldMapNS
136
137 void worldmap_run();
138
139 #endif
140
141 /* Local Variables: */
142 /* mode:c++ */
143 /* End */