2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef HEADER_SUPERTUX_OBJECT_TILEMAP_HPP
18 #define HEADER_SUPERTUX_OBJECT_TILEMAP_HPP
20 #include <boost/shared_ptr.hpp>
22 #include "object/path_walker.hpp"
23 #include "supertux/game_object.hpp"
24 #include "supertux/script_interface.hpp"
25 #include "video/drawing_context.hpp"
37 * This class is responsible for drawing the level tiles
39 class TileMap : public GameObject,
40 public ScriptInterface
43 TileMap(const TileSet *tileset);
44 TileMap(const Reader& reader);
45 TileMap(const TileSet *tileset, std::string name, int z_pos, bool solid_,
46 size_t width_, size_t height_);
49 virtual void update(float elapsed_time);
50 virtual void draw(DrawingContext& context);
52 /** Move tilemap until at given node, then stop */
53 void goto_node(int node_no);
55 /** Start moving tilemap */
58 /** Stop tilemap at next node */
61 virtual void expose(HSQUIRRELVM vm, SQInteger table_idx);
62 virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx);
64 void set(int width, int height, const std::vector<unsigned int>& vec,
65 int z_pos, bool solid);
67 /** resizes the tilemap to a new width and height (tries to not destroy the
70 void resize(int newwidth, int newheight, int fill_id = 0);
72 size_t get_width() const
75 size_t get_height() const
78 Vector get_offset() const
81 /** Get the movement of this tilemap. The collision detection code may need a
82 * non-negative y-movement. Passing `false' as the `actual' argument will
83 * provide that. Used exclusively in src/supertux/sector.cpp. */
84 Vector get_movement(bool actual) const
89 return Vector(movement.x, std::max(0.0f,movement.y));
93 boost::shared_ptr<Path> get_path()
96 boost::shared_ptr<PathWalker> get_walker()
99 void set_offset(const Vector &offset)
100 { this->offset = offset; }
102 /* Returns the position of the upper-left corner of
103 * tile (x, y) in the sector. */
104 Vector get_tile_position(int x, int y) const
105 { return offset + Vector(x,y) * 32; }
107 Rectf get_bbox() const
108 { return Rectf(get_tile_position(0, 0), get_tile_position(width, height)); }
110 Rectf get_tile_bbox(int x, int y) const
111 { return Rectf(get_tile_position(x, y), get_tile_position(x+1, y+1)); }
113 /* Returns the half-open rectangle of (x, y) tile indices
114 * that overlap the given rectangle in the sector. */
115 Rect get_tiles_overlapping(const Rectf &rect) const;
117 int get_layer() const
120 bool is_solid() const
121 { return real_solid && effective_solid; }
124 * Changes Tilemap's solidity, i.e. whether to consider it when doing collision detection.
126 void set_solid(bool solid = true);
128 /// returns tile in row y and column y (of the tilemap)
129 const Tile* get_tile(int x, int y) const;
130 /// returns tile at position pos (in world coordinates)
131 const Tile* get_tile_at(const Vector& pos) const;
132 /// returns tile in row y and column y (of the tilemap)
133 uint32_t get_tile_id(int x, int y) const;
134 /// returns tile at position pos (in world coordinates)
135 uint32_t get_tile_id_at(const Vector& pos) const;
137 void change(int x, int y, uint32_t newtile);
139 void change_at(const Vector& pos, uint32_t newtile);
141 /// changes all tiles with the given ID
142 void change_all(uint32_t oldtile, uint32_t newtile);
144 void set_drawing_effect(DrawingEffect effect)
146 drawing_effect = effect;
149 DrawingEffect get_drawing_effect()
151 return drawing_effect;
155 * Start fading the tilemap to opacity given by @c alpha.
156 * Destination opacity will be reached after @c seconds seconds. Also influences solidity.
158 void fade(float alpha, float seconds = 0);
161 * Instantly switch tilemap's opacity to @c alpha. Also influences solidity.
163 void set_alpha(float alpha);
166 * Return tilemap's opacity. Note that while the tilemap is fading in or out, this will return the current alpha value, not the target alpha.
171 const TileSet *tileset;
173 typedef std::vector<uint32_t> Tiles;
176 /* read solid: In *general*, is this a solid layer?
177 * effective solid: is the layer *currently* solid? A generally solid layer
178 * may be not solid when its alpha is low.
179 * See `is_solid' above. */
181 bool effective_solid;
182 void update_effective_solid (void);
189 Vector movement; /**< The movement that happened last frame */
191 DrawingEffect drawing_effect;
192 float alpha; /**< requested tilemap opacity */
193 float current_alpha; /**< current tilemap opacity */
194 float remaining_fade_time; /**< seconds until requested tilemap opacity is reached */
196 boost::shared_ptr<Path> path;
197 boost::shared_ptr<PathWalker> walker;
199 DrawingContext::Target draw_target; /**< set to LIGHTMAP to draw to lightmap */
202 TileMap(const TileMap&);
203 TileMap& operator=(const TileMap&);
206 #endif /*SUPERTUX_TILEMAP_H*/