* Tux now stops at crossroads on worldmaps
[supertux.git] / src / tile.hpp
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 #ifndef TILE_H
21 #define TILE_H
22
23 #include <vector>
24 #include <SDL.h>
25 #include <stdint.h>
26 #include "video/surface.hpp"
27 #include "math/rect.hpp"
28 #include "lisp/lisp.hpp"
29
30 class DrawingContext;
31
32 /**
33 Tile Class
34 */
35 class Tile
36 {
37 public:
38   /// bitset for tile attributes
39   enum {
40     /** solid tile that is indestructable by Tux */                         
41     SOLID     = 0x0001,
42     /** uni-directional solid tile */
43     UNISOLID  = 0x0002,
44     /** a brick that can be destroyed by jumping under it */
45     BRICK     = 0x0004,
46     /** the level should be finished when touching a goaltile.
47      * if data is 0 then the endsequence should be triggered, if data is 1
48      * then we can finish the level instantly.
49      */
50     GOAL      = 0x0008,
51     /** slope tile */
52     SLOPE     = 0x0010,
53     /** Bonusbox, content is stored in \a data */
54     FULLBOX   = 0x0020,
55     /** Tile is a coin */
56     COIN      = 0x0040,
57
58     /* interesting flags (the following are passed to gameobjects) */
59     FIRST_INTERESTING_FLAG = 0x0100,
60     
61     /** an ice brick that makes tux sliding more than usual */
62     ICE       = 0x0100,
63     /** a water tile in which tux starts to swim */                         
64     WATER     = 0x0200,
65     /** a tile that hurts the player if he touches it */
66     HURTS     = 0x0400,
67   };
68
69   /// worldmap flags
70   enum {
71     WORLDMAP_NORTH = 0x0001,
72     WORLDMAP_SOUTH = 0x0002,
73     WORLDMAP_EAST  = 0x0004,
74     WORLDMAP_WEST  = 0x0008,
75
76     WORLDMAP_STOP  = 0x0010,
77
78     // convenience values ("C" stands for crossroads)
79     WORLDMAP_CNSE  = WORLDMAP_NORTH | WORLDMAP_SOUTH | WORLDMAP_EAST,
80     WORLDMAP_CNSW  = WORLDMAP_NORTH | WORLDMAP_SOUTH | WORLDMAP_WEST,
81     WORLDMAP_CNEW  = WORLDMAP_NORTH | WORLDMAP_EAST  | WORLDMAP_WEST,
82     WORLDMAP_CSEW  = WORLDMAP_SOUTH | WORLDMAP_EAST  | WORLDMAP_WEST,
83     WORLDMAP_CNSEW = WORLDMAP_NORTH | WORLDMAP_SOUTH | WORLDMAP_EAST | WORLDMAP_WEST
84   };
85   
86   struct ImageSpec {
87     ImageSpec(const std::string& newfile, const Rect& newrect)
88       : file(newfile), rect(newrect)
89     { }
90
91     std::string file;
92     Rect rect;
93   };
94
95 private:
96   unsigned int id;
97
98   std::vector<ImageSpec> imagespecs;
99   std::vector<Surface*> images;
100
101   std::string editor_imagefile;
102   Surface* editor_image;
103   
104   /** tile attributes */
105   uint32_t attributes;
106   
107   /** General purpose data attached to a tile (content of a box, type of coin)*/
108   int data;
109   
110   float anim_fps;
111
112 public:
113   ~Tile();
114   
115   /** Draw a tile on the screen */
116   void draw(DrawingContext& context, const Vector& pos, int layer) const;
117
118   Surface* get_editor_image() const;
119
120   unsigned int getID() const
121   { return id; }
122
123   uint32_t getAttributes() const
124   { return attributes; }
125
126   int getData() const
127   { return data; }
128
129   /// returns the width of the tile in pixels
130   int getWidth() const
131   { 
132     if(!images.size())
133       return 0;
134     return (int) images[0]->get_width();
135   }
136
137   /// returns the height of the tiles in pixels
138   int getHeight() const
139   {
140     if(!images.size())
141       return 0;
142     return (int) images[0]->get_height();
143   }
144
145 protected:
146   friend class TileManager;
147   Tile();
148   Tile(unsigned int id, Uint32 attributes, const ImageSpec& imagespec);
149
150   void load_images(const std::string& tilesetpath);
151
152   /// parses the tile and returns it's id number
153   void parse(const lisp::Lisp& reader);
154   void parse_images(const lisp::Lisp& cur);
155 };
156
157 #endif