- added shurtcut function to create menu items
[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 #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   texture_type 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   texture_type 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();
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_tile_pos() const { return tile_pos; } 
107   void  set_tile_pos(Point p) { tile_pos = p; } 
108 };
109
110 /** */
111 class WorldMap
112 {
113 private:
114   Tux* tux;
115
116   texture_type level_sprite;
117   bool quit;
118
119   std::string name;
120   std::string music;
121
122   std::vector<int> tilemap;
123   int width;
124   int height;
125
126   struct Level
127   {
128     int x;
129     int y;
130     std::string name;
131   };
132
133   typedef std::vector<Level> Levels;
134   Levels levels;
135
136   Mix_Music* song;
137
138   Direction input_direction;
139   bool enter_level;
140
141 public:
142   WorldMap();
143   ~WorldMap();
144
145   /** Busy loop */
146   void display();
147
148   void load_map();
149   
150   void get_input();
151
152   /** Update Tux position */
153   void update();
154
155   /** Draw one frame */
156   void draw();
157
158   Point get_next_tile(Point pos, Direction direction);
159   Tile* at(Point pos);
160
161   /** Check if it is possible to walk from \a pos into \a direction,
162       if possible, write the new position to \a new_pos */
163   bool path_ok(Direction direction, Point pos, Point* new_pos);
164 };
165
166 } // namespace WorldMapNS
167
168 void worldmap_run();
169
170 #endif
171
172 /* Local Variables: */
173 /* mode:c++ */
174 /* End */