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