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 "object/path_walker.hpp"
21 #include "supertux/game_object.hpp"
22 #include "supertux/script_interface.hpp"
23 #include "video/drawing_context.hpp"
35 * This class is responsible for drawing the level tiles
37 class TileMap : public GameObject,
38 public ScriptInterface
41 TileMap(const TileSet *tileset);
42 TileMap(const Reader& reader);
43 TileMap(const TileSet *tileset, std::string name, int z_pos, bool solid_,
44 size_t width_, size_t height_);
47 virtual void update(float elapsed_time);
48 virtual void draw(DrawingContext& context);
50 /** Move tilemap until at given node, then stop */
51 void goto_node(int node_no);
53 /** Start moving tilemap */
56 /** Stop tilemap at next node */
59 virtual void expose(HSQUIRRELVM vm, SQInteger table_idx);
60 virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx);
62 void set(int width, int height, const std::vector<unsigned int>& vec,
63 int z_pos, bool solid);
65 /** resizes the tilemap to a new width and height (tries to not destroy the
68 void resize(int newwidth, int newheight, int fill_id = 0);
70 size_t get_width() const
73 size_t get_height() const
76 Vector get_offset() const
79 /** Get the movement of this tilemap. The collision detection code may need a
80 * non-negative y-movement. Passing `false' as the `actual' argument will
81 * provide that. Used exclusively in src/supertux/sector.cpp. */
82 Vector get_movement(bool actual) const
87 return Vector(movement.x, std::max(0.0f,movement.y));
91 std::shared_ptr<Path> get_path()
94 std::shared_ptr<PathWalker> get_walker()
97 void set_offset(const Vector &offset_)
98 { this->offset = offset_; }
100 /* Returns the position of the upper-left corner of
101 * tile (x, y) in the sector. */
102 Vector get_tile_position(int x, int y) const
103 { return offset + Vector(x,y) * 32; }
105 Rectf get_bbox() const
106 { return Rectf(get_tile_position(0, 0), get_tile_position(width, height)); }
108 Rectf get_tile_bbox(int x, int y) const
109 { return Rectf(get_tile_position(x, y), get_tile_position(x+1, y+1)); }
111 /* Returns the half-open rectangle of (x, y) tile indices
112 * that overlap the given rectangle in the sector. */
113 Rect get_tiles_overlapping(const Rectf &rect) const;
115 int get_layer() const
118 bool is_solid() const
119 { return real_solid && effective_solid; }
122 * Changes Tilemap's solidity, i.e. whether to consider it when doing collision detection.
124 void set_solid(bool solid = true);
126 /// returns tile in row y and column y (of the tilemap)
127 const Tile* get_tile(int x, int y) const;
128 /// returns tile at position pos (in world coordinates)
129 const Tile* get_tile_at(const Vector& pos) const;
130 /// returns tile in row y and column y (of the tilemap)
131 uint32_t get_tile_id(int x, int y) const;
132 /// returns tile at position pos (in world coordinates)
133 uint32_t get_tile_id_at(const Vector& pos) const;
135 void change(int x, int y, uint32_t newtile);
137 void change_at(const Vector& pos, uint32_t newtile);
139 /// changes all tiles with the given ID
140 void change_all(uint32_t oldtile, uint32_t newtile);
142 void set_drawing_effect(DrawingEffect effect)
144 drawing_effect = effect;
147 DrawingEffect get_drawing_effect()
149 return drawing_effect;
153 * Start fading the tilemap to opacity given by @c alpha.
154 * Destination opacity will be reached after @c seconds seconds. Also influences solidity.
156 void fade(float alpha, float seconds = 0);
159 * Instantly switch tilemap's opacity to @c alpha. Also influences solidity.
161 void set_alpha(float alpha);
164 * 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.
169 const TileSet *tileset;
171 typedef std::vector<uint32_t> Tiles;
174 /* read solid: In *general*, is this a solid layer?
175 * effective solid: is the layer *currently* solid? A generally solid layer
176 * may be not solid when its alpha is low.
177 * See `is_solid' above. */
179 bool effective_solid;
180 void update_effective_solid (void);
187 Vector movement; /**< The movement that happened last frame */
189 DrawingEffect drawing_effect;
190 float alpha; /**< requested tilemap opacity */
191 float current_alpha; /**< current tilemap opacity */
192 float remaining_fade_time; /**< seconds until requested tilemap opacity is reached */
194 std::shared_ptr<Path> path;
195 std::shared_ptr<PathWalker> walker;
197 DrawingContext::Target draw_target; /**< set to LIGHTMAP to draw to lightmap */
200 TileMap(const TileMap&);
201 TileMap& operator=(const TileMap&);
204 #endif /*SUPERTUX_TILEMAP_H*/