92fa06e495ad0806a4881cefa8448ae6b3a01d78
[supertux.git] / src / supertux / tile.hpp
1 //  SuperTux
2 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
3 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 //  Copyright (C) 2010 Florian Forster <supertux at octo.it>
5 //
6 //  This program is free software: you can redistribute it and/or modify
7 //  it under the terms of the GNU General Public License as published by
8 //  the Free Software Foundation, either version 3 of the License, or
9 //  (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, see <http://www.gnu.org/licenses/>.
18
19 #ifndef HEADER_SUPERTUX_SUPERTUX_TILE_HPP
20 #define HEADER_SUPERTUX_SUPERTUX_TILE_HPP
21
22 #include <vector>
23 #include <stdint.h>
24
25 #include "math/rectf.hpp"
26 #include "video/surface.hpp"
27 #include "util/reader_fwd.hpp"
28
29 class DrawingContext;
30
31 class Tile
32 {
33 public:
34   static bool draw_editor_images;
35   /// bitset for tile attributes
36   enum {
37     /** solid tile that is indestructible by Tux */
38     SOLID     = 0x0001,
39     /** uni-directional solid tile */
40     UNISOLID  = 0x0002,
41     /** a brick that can be destroyed by jumping under it */
42     BRICK     = 0x0004,
43     /** the level should be finished when touching a goaltile.
44      * if data is 0 then the endsequence should be triggered, if data is 1
45      * then we can finish the level instantly.
46      */
47     GOAL      = 0x0008,
48     /** slope tile */
49     SLOPE     = 0x0010,
50     /** Bonusbox, content is stored in \a data */
51     FULLBOX   = 0x0020,
52     /** Tile is a coin */
53     COIN      = 0x0040,
54
55     /* interesting flags (the following are passed to gameobjects) */
56     FIRST_INTERESTING_FLAG = 0x0100,
57
58     /** an ice brick that makes tux sliding more than usual */
59     ICE       = 0x0100,
60     /** a water tile in which tux starts to swim */
61     WATER     = 0x0200,
62     /** a tile that hurts the player if he touches it */
63     HURTS     = 0x0400,
64     /** for lava: WATER, HURTS, FIRE */
65     FIRE      = 0x0800
66   };
67
68   /// worldmap flags
69   enum {
70     WORLDMAP_NORTH = 0x0001,
71     WORLDMAP_SOUTH = 0x0002,
72     WORLDMAP_EAST  = 0x0004,
73     WORLDMAP_WEST  = 0x0008,
74     WORLDMAP_DIR_MASK = 0x000f,
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 Rectf& newrect)
88       : file(newfile), rect(newrect)
89     { }
90
91     std::string file;
92     Rectf rect;
93   };
94
95   enum
96   {
97     UNI_DIR_NORTH = 0,
98     UNI_DIR_SOUTH = 1,
99     UNI_DIR_WEST  = 2,
100     UNI_DIR_EAST  = 3,
101     UNI_DIR_MASK  = 3
102   };
103
104 private:
105   std::vector<ImageSpec> imagespecs;
106   std::vector<SurfacePtr>  images;
107   std::vector<ImageSpec> editor_imagespecs;
108   std::vector<SurfacePtr>  editor_images;
109
110   /// tile attributes
111   uint32_t attributes;
112
113   /** General purpose data attached to a tile (content of a box, type of coin)*/
114   int data;
115
116   float fps;
117
118 public:
119   Tile();
120   Tile(const std::vector<ImageSpec>& images, const std::vector<ImageSpec>& editor_images,
121        uint32_t attributes, uint32_t data, float fps);
122   ~Tile();
123
124   /** load Surfaces, if not already loaded */
125   void load_images();
126
127   /** Draw a tile on the screen */
128   void draw(DrawingContext& context, const Vector& pos, int z_pos) const;
129
130   uint32_t getAttributes() const
131   { return attributes; }
132
133   int getData() const
134   { return data; }
135
136   /** Checks the SLOPE attribute. Returns "true" if set, "false" otherwise. */
137   bool is_slope() const
138   {
139     return attributes & SLOPE;
140   }
141
142   /** Determine the solidity of a tile. This version behaves correctly for
143    * unisolid tiles by taking position and movement of the object in question
144    * into account. Because creating the arguments for this function can be
145    * expensive, you should handle trivial cases using the "is_solid()" and
146    * "is_unisolid()" methods first. */
147   bool is_solid (const Rectf& tile_bbox, const Rectf& position, const Vector& movement) const;
148
149   /** This version only checks the SOLID flag to determine the solidity of a
150    * tile. This means it will always return "true" for unisolid tiles. To
151    * determine the *current* solidity of unisolid tiles, use the "is_solid"
152    * method that takes position and movement into account (see above). */
153   bool is_solid() const
154   {
155     return attributes & SOLID;
156   }
157
158   /** Checks the UNISOLID attribute. Returns "true" if set, "false" otherwise. */
159   bool is_unisolid() const
160   {
161     return attributes & UNISOLID;
162   }
163
164   void print_debug(int id) const;
165
166 private:
167   //Correct small oddities in attributes that naive people
168   //might miss (and rebuke them for it)
169   void correct_attributes();
170
171   /** Returns zero if a unisolid tile is non-solid due to the movement
172    * direction, non-zero if the tile is solid due to direction. */
173   bool check_movement_unisolid (const Vector movement) const;
174
175   /** Returns zero if a unisolid tile is non-solid due to the position of the
176    * tile and the object, non-zero if the tile is solid. */
177   bool check_position_unisolid (const Rectf& obj_bbox,
178                                 const Rectf& tile_bbox) const;
179
180 private:
181   Tile(const Tile&);
182   Tile& operator=(const Tile&);
183 };
184
185 #endif
186
187 /* EOF */