supertux
tilemap.hpp
1 // SuperTux
2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16 
17 #ifndef HEADER_SUPERTUX_OBJECT_TILEMAP_HPP
18 #define HEADER_SUPERTUX_OBJECT_TILEMAP_HPP
19 
20 #include <algorithm>
21 #include <unordered_set>
22 
23 #include "math/rect.hpp"
24 #include "math/rectf.hpp"
25 #include "math/size.hpp"
26 #include "object/path_object.hpp"
27 #include "object/path_walker.hpp"
28 #include "supertux/autotile.hpp"
29 #include "supertux/game_object.hpp"
30 #include "video/color.hpp"
31 #include "video/flip.hpp"
32 #include "video/drawing_target.hpp"
33 
34 class CollisionObject;
36 class DrawingContext;
37 class Tile;
38 class TileSet;
39 
49 class TileMap final : public GameObject,
50  public PathObject
51 {
52 public:
53  static void register_class(ssq::VM& vm);
54 
55 public:
56  TileMap(const TileSet *tileset);
57  TileMap(const TileSet *tileset, const ReaderMapping& reader);
58  ~TileMap() override;
59 
60  virtual void finish_construction() override;
61 
62  static std::string class_name() { return "tilemap"; }
63  virtual std::string get_class_name() const override { return class_name(); }
64  virtual std::string get_exposed_class_name() const override { return "TileMap"; }
65  virtual const std::string get_icon_path() const override { return "images/engine/editor/tilemap.png"; }
66  static std::string display_name() { return _("Tilemap"); }
67  virtual std::string get_display_name() const override { return display_name(); }
68  virtual GameObjectClasses get_class_types() const override { return GameObject::get_class_types().add(typeid(PathObject)).add(typeid(TileMap)); }
69 
70  virtual ObjectSettings get_settings() override;
71  virtual void after_editor_set() override;
72 
73  void save_state() override;
74  void check_state() override;
75 
76  virtual void update(float dt_sec) override;
77  virtual void draw(DrawingContext& context) override;
78 
79  virtual void editor_update() override;
80 
81  virtual void on_flip(float height) override;
82 
83  void set(int width, int height, const std::vector<unsigned int>& vec,
84  int z_pos, bool solid);
85 
88  void resize(int newwidth, int newheight, int fill_id = 0,
89  int xoffset = 0, int yoffset = 0);
90  void resize(const Size& newsize, const Size& resize_offset);
91 
92  int get_width() const { return m_width; }
93  int get_height() const { return m_height; }
94  Size get_size() const { return Size(m_width, m_height); }
95 
96  void set_offset(const Vector &offset_) { m_offset = offset_; }
97  Vector get_offset() const { return m_offset; }
98 
99  void set_ground_movement_manager(const std::shared_ptr<CollisionGroundMovementManager>& movement_manager)
100  {
101  m_ground_movement_manager = movement_manager;
102  }
103 
104  void move_by(const Vector& pos);
105 
110  Vector get_movement(bool actual) const
111  {
112  if (actual) {
113  return m_movement;
114  }
115 
116  return Vector(m_movement.x, std::max(0.0f, m_movement.y));
117  }
118 
121  Vector get_tile_position(int x, int y) const
122  { return m_offset + Vector(static_cast<float>(x), static_cast<float>(y)) * 32.0f; }
123 
124  Rectf get_bbox() const {
125  return Rectf(get_tile_position(0, 0),
126  get_tile_position(m_width, m_height));
127  }
128 
129  Rectf get_tile_bbox(int x, int y) const {
130  return Rectf(get_tile_position(x, y),
131  get_tile_position(x + 1, y + 1));
132  }
133 
136  Rect get_tiles_overlapping(const Rectf &rect) const;
137 
140  void hits_object_bottom(CollisionObject& object);
141  void notify_object_removal(CollisionObject* other);
142 
143  int get_layer() const { return m_z_pos; }
144  void set_layer(int layer) { m_z_pos = layer; }
145 
146  bool is_solid() const { return m_real_solid && m_effective_solid; }
147 
154  void set_solid(bool solid = true);
155 
156  bool is_outside_bounds(const Vector& pos) const;
157  const Tile& get_tile(int x, int y) const;
158  const Tile& get_tile_at(const Vector& pos) const;
166  uint32_t get_tile_id(int x, int y) const;
173  uint32_t get_tile_id_at(float x, float y) const;
174  uint32_t get_tile_id_at(const Vector& pos) const;
175 
184  void change(int x, int y, uint32_t newtile);
192  void change_at(float x, float y, uint32_t newtile);
193  void change_at(const Vector& pos, uint32_t newtile);
200  void change_all(uint32_t oldtile, uint32_t newtile);
201 
203  void autotile(int x, int y, uint32_t tile);
204 
205  enum class AutotileCornerOperation {
206  ADD_TOP_LEFT,
207  ADD_TOP_RIGHT,
208  ADD_BOTTOM_LEFT,
209  ADD_BOTTOM_RIGHT,
210  REMOVE_TOP_LEFT,
211  REMOVE_TOP_RIGHT,
212  REMOVE_BOTTOM_LEFT,
213  REMOVE_BOTTOM_RIGHT,
214  };
215 
217  void autotile_corner(int x, int y, uint32_t tile, AutotileCornerOperation op);
218 
220  void autotile_erase(const Vector& pos, const Vector& corner_pos);
221 
223  AutotileSet* get_autotileset(uint32_t tile) const;
224 
225  void set_flip(Flip flip) { m_flip = flip; }
226  Flip get_flip() const { return m_flip; }
227 
235  void fade(float alpha, float time);
236 
239  void tint_fade(const Color& new_tint, float time = 0.f);
250  void tint_fade(float time, float red, float green, float blue, float alpha);
251 
252  Color get_current_tint() const { return m_current_tint; }
253 
259  void set_alpha(float alpha);
265  float get_alpha() const;
266 
267  float get_target_alpha() const { return m_alpha; }
268 
269  void set_tileset(const TileSet* new_tileset);
270 
271  const std::vector<uint32_t>& get_tiles() const { return m_tiles; }
272 
273 private:
274  void update_effective_solid(bool update_manager = true);
275  void float_channel(float target, float &current, float remaining_time, float dt_sec);
276 
277  bool is_corner(uint32_t tile) const;
278 
279  void apply_offset_x(int fill_id, int xoffset);
280  void apply_offset_y(int fill_id, int yoffset);
281 
282 public:
283  bool m_editor_active;
284 
285 private:
286  const TileSet* m_tileset;
287 
288  typedef std::vector<uint32_t> Tiles;
289  Tiles m_tiles;
290 
291  /* read solid: In *general*, is this a solid layer? effective solid:
292  is the layer *currently* solid? A generally solid layer may be
293  not solid when its alpha is low. See `is_solid' above. */
294  bool m_real_solid;
295  bool m_effective_solid;
296 
297  float m_speed_x;
298  float m_speed_y;
299  int m_width;
300  int m_height;
301  int m_z_pos;
302  Vector m_offset;
303  Vector m_movement;
306  std::unordered_set<CollisionObject*> m_objects_hit_bottom;
307 
308  std::shared_ptr<CollisionGroundMovementManager> m_ground_movement_manager;
309 
310  Flip m_flip;
311  float m_alpha;
312  float m_current_alpha;
313  float m_remaining_fade_time;
318  Color m_tint;
319  Color m_current_tint;
320  float m_remaining_tint_fade_time;
323  DrawingTarget m_draw_target;
324 
325  int m_new_size_x;
326  int m_new_size_y;
327  int m_new_offset_x;
328  int m_new_offset_y;
329  bool m_add_path;
330 
331  int m_starting_node;
332 
333 private:
334  TileMap(const TileMap&) = delete;
335  TileMap& operator=(const TileMap&) = delete;
336 };
337 
338 #endif
339 
340 /* EOF */
void change_at(float x, float y, uint32_t newtile)
Changes the tile at the given position (in-world coordinates) to ""newtile"".
Definition: tilemap.cpp:704
uint32_t get_tile_id(int x, int y) const
Returns the ID of the tile at the given coordinates or 0 if out of bounds.
Definition: tilemap.cpp:636
void autotile_corner(int x, int y, uint32_t tile, AutotileCornerOperation op)
Puts the correct autotile blocks at the tiles around the given corner.
Definition: tilemap.cpp:771
Definition: rect.hpp:30
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: tilemap.hpp:68
virtual void finish_construction() override
Called after all objects have been added to the Sector and the Sector is fully constructed.
Definition: tilemap.cpp:196
void change(int x, int y, uint32_t newtile)
Changes the tile at the given coordinates to ""newtile"".
Definition: tilemap.cpp:688
Definition: object_settings.hpp:39
Vector get_tile_position(int x, int y) const
Returns the position of the upper-left corner of tile (x, y) in the sector.
Definition: tilemap.hpp:121
Definition: size.hpp:24
This class takes care of moving objects that have collided on top of other moving objects or on top o...
Definition: collision_movement_manager.hpp:32
Definition: rectf.hpp:31
virtual void update(float dt_sec) override
This function is called once per frame and allows the object to update it&#39;s state.
Definition: tilemap.cpp:346
void save_state() override
Save/check the current state of the object.
Definition: tilemap.cpp:332
Vector get_movement(bool actual) const
Get the movement of this tilemap.
Definition: tilemap.hpp:110
void tint_fade(const Color &new_tint, float time=0.f)
Start fading the tilemap to tint given by RGBA.
Definition: tilemap.cpp:920
This class is responsible for: Updating and drawing the object.
Definition: game_object.hpp:83
void resize(int newwidth, int newheight, int fill_id=0, int xoffset=0, int yoffset=0)
resizes the tilemap to a new width and height (tries to not destroy the existing map) ...
Definition: tilemap.cpp:552
void set_solid(bool solid=true)
Switches the tilemap&#39;s real solidity to ""solid"".
Definition: tilemap.cpp:629
void change_all(uint32_t oldtile, uint32_t newtile)
Changes all tiles with the given ID.
Definition: tilemap.cpp:710
Definition: tile.hpp:32
void hits_object_bottom(CollisionObject &object)
Called by the collision mechanism to indicate that this tilemap has been hit on the top...
Definition: tilemap.cpp:617
void autotile_erase(const Vector &pos, const Vector &corner_pos)
Erases in autotile mode.
Definition: tilemap.cpp:829
virtual void on_flip(float height) override
When level is flipped vertically.
Definition: tilemap.cpp:418
float get_alpha() const
Returns the tilemap&#39;s opacity.
Definition: tilemap.cpp:942
uint32_t get_tile_id_at(float x, float y) const
Returns the ID of the tile at the given position (in world coordinates).
Definition: tilemap.cpp:675
void autotile(int x, int y, uint32_t tile)
Puts the correct autotile block at the given position.
Definition: tilemap.cpp:723
Rect get_tiles_overlapping(const Rectf &rect) const
Returns the half-open rectangle of (x, y) tile indices that overlap the given rectangle in the sector...
Definition: tilemap.cpp:604
virtual GameObjectClasses get_class_types() const
List notable classes in inheritance hierarchy of class.
Definition: game_object.cpp:113
Definition: color.hpp:26
A base class for all objects that contain, or make use of a path.
Definition: path_object.hpp:36
A helper structure to list all the type_indexes of the classes in the type hierarchy of a given class...
Definition: game_object.hpp:57
virtual std::string get_display_name() const override
Returns the display name of the object, translated to the user&#39;s locale.
Definition: tilemap.hpp:67
void fade(float alpha, float time)
Starts fading the tilemap to the opacity given by ""alpha"".
Definition: tilemap.cpp:913
void set_alpha(float alpha)
Instantly switches the tilemap&#39;s opacity to ""alpha"".
Definition: tilemap.cpp:933
Definition: collision_object.hpp:33
AutotileSet * get_autotileset(uint32_t tile) const
Returns the Autotileset associated with the given tile.
Definition: tilemap.cpp:907
This class is responsible for managing an array of tiles.
Definition: tilemap.hpp:49
Definition: reader_mapping.hpp:32
Definition: tile_set.hpp:44
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
Definition: autotile.hpp:79
virtual void draw(DrawingContext &context) override
The GameObject should draw itself onto the provided DrawingContext if this function is called...
Definition: tilemap.cpp:438
virtual void editor_update() override
Called each frame in the editor, used to keep linked objects together (e.g.
Definition: tilemap.cpp:396