a94a19c36db043026e75c57410d58b41b3df88f6
[supertux.git] / src / tile.h
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@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
19 //  02111-1307, USA.
20
21 #ifndef TILE_H
22 #define TILE_H
23
24 #include <set>
25 #include <map>
26 #include <vector>
27 #include "texture.h"
28 #include "globals.h"
29 #include "lispreader.h"
30 #include "setup.h"
31 #include "vector.h"
32
33 /**
34 Tile Class
35 */
36 class Tile
37 {
38 public:
39   Tile();
40   ~Tile();
41
42   int id;
43
44   std::vector<Surface*> images;
45   std::vector<Surface*> editor_images;
46   
47   std::vector<std::string>  filenames;
48   std::vector<std::string> editor_filenames;
49   
50   /** solid tile that is indestructable by Tux */
51   bool solid;
52
53   /** a brick that can be destroyed by jumping under it */
54   bool brick;
55
56   /** FIXME: ? */
57   bool ice;
58
59   /** water */
60   bool water;
61
62   /** spike - kills player on contact */
63   bool spike;
64
65   /** Bonusbox, content is stored in \a data */
66   bool fullbox;
67
68   /** Tile is a distro/coin */
69   bool distro;
70
71
72   /** the level should be finished when touching a goaltile.
73    * if data is 0 then the endsequence should be triggered, if data is 1
74    * then we can finish the level instantly.
75    */
76   bool goal;
77
78   /** General purpose data attached to a tile (content of a box, type of coin) */
79   int data;
80
81   /** Id of the tile that is going to replace this tile once it has
82       been collected or jumped at */
83   int next_tile;
84
85   int anim_speed;
86   
87   /** Draw a tile on the screen: */
88   static void draw(float x, float y, unsigned int c, Uint8 alpha = 255);
89   static void draw_stretched(float x, float y, int w, int h, unsigned int c, Uint8 alpha = 255);
90
91   static void draw(const Vector& pos, unsigned int c, Uint8 alpha = 255)
92   {
93     draw(pos.x, pos.y, c, alpha);
94   }
95 };
96
97 struct TileGroup
98 {
99   friend bool operator<(const TileGroup& lhs, const TileGroup& rhs)
100   { return lhs.name < rhs.name; };
101   friend bool operator>(const TileGroup& lhs, const TileGroup& rhs)
102   { return lhs.name > rhs.name; };
103
104   std::string name;
105   std::vector<int> tiles;
106 };
107
108 class TileManager
109 {
110  private:
111   TileManager();
112   ~TileManager();
113   
114   std::vector<Tile*> tiles;
115   static TileManager* instance_ ;
116   static std::set<TileGroup>* tilegroups_;
117   void load_tileset(std::string filename);
118
119   std::string current_tileset;
120   
121  public:
122   static TileManager* instance() { return instance_ ? instance_ : instance_ = new TileManager(); }
123   static void destroy_instance() { delete instance_; instance_ = 0; }
124   
125   static std::set<TileGroup>* tilegroups() { if(!instance_) { instance_ = new TileManager(); } return tilegroups_ ? tilegroups_ : tilegroups_ = new std::set<TileGroup>; }
126   Tile* get(unsigned int id) {
127     if(id < tiles.size())
128       {
129         return tiles[id]; 
130       }
131     else
132       {
133         // Never return 0, but return the 0th tile instead so that
134         // user code doesn't have to check for NULL pointers all over
135         // the place
136         return tiles[0]; 
137       } 
138   }
139 };
140
141 #endif