tile.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #ifndef HEADER_SUPERTUX_SUPERTUX_TILE_HPP
  19. #define HEADER_SUPERTUX_SUPERTUX_TILE_HPP
  20. #include <vector>
  21. #include <stdint.h>
  22. #include "math/rectf.hpp"
  23. #include "video/color.hpp"
  24. #include "video/surface_ptr.hpp"
  25. class Canvas;
  26. class DrawingContext;
  27. class Tile final
  28. {
  29. public:
  30. static bool draw_editor_images;
  31. public:
  32. /** bitset for tile attributes */
  33. enum {
  34. /** solid tile that is indestructible by Tux */
  35. SOLID = 0x0001,
  36. /** uni-directional solid tile */
  37. UNISOLID = 0x0002,
  38. /** a brick that can be destroyed by jumping under it */
  39. BRICK = 0x0004, //Marked for removal, DO NOT USE!
  40. /** the level should be finished when touching a goaltile.
  41. * if data is 0 then the endsequence should be triggered, if data is 1
  42. * then we can finish the level instantly.
  43. */
  44. GOAL = 0x0008, //Marked for removal, DO NOT USE!
  45. /** slope tile */
  46. SLOPE = 0x0010,
  47. /** Bonusbox, content is stored in \a data */
  48. FULLBOX = 0x0020, //Marked for removal, DO NOT USE!
  49. /** Tile is a coin */
  50. COIN = 0x0040, //Marked for removal, DO NOT USE!
  51. /* interesting flags (the following are passed to gameobjects) */
  52. FIRST_INTERESTING_FLAG = 0x0100,
  53. /** an ice brick that makes tux sliding more than usual */
  54. ICE = 0x0100,
  55. /** a water tile in which tux starts to swim */
  56. WATER = 0x0200,
  57. /** a tile that hurts Tux if he touches it */
  58. HURTS = 0x0400,
  59. /** for lava: WATER, HURTS, FIRE */
  60. FIRE = 0x0800,
  61. /** a walljumping trigger tile */
  62. WALLJUMP = 0x1000
  63. };
  64. /** worldmap flags */
  65. enum {
  66. WORLDMAP_NORTH = 0x0001,
  67. WORLDMAP_SOUTH = 0x0002,
  68. WORLDMAP_EAST = 0x0004,
  69. WORLDMAP_WEST = 0x0008,
  70. WORLDMAP_DIR_MASK = 0x000f,
  71. WORLDMAP_STOP = 0x0010,
  72. // convenience values ("C" stands for crossroads)
  73. WORLDMAP_CNSE = WORLDMAP_NORTH | WORLDMAP_SOUTH | WORLDMAP_EAST,
  74. WORLDMAP_CNSW = WORLDMAP_NORTH | WORLDMAP_SOUTH | WORLDMAP_WEST,
  75. WORLDMAP_CNEW = WORLDMAP_NORTH | WORLDMAP_EAST | WORLDMAP_WEST,
  76. WORLDMAP_CSEW = WORLDMAP_SOUTH | WORLDMAP_EAST | WORLDMAP_WEST,
  77. WORLDMAP_CNSEW = WORLDMAP_NORTH | WORLDMAP_SOUTH | WORLDMAP_EAST | WORLDMAP_WEST
  78. };
  79. enum
  80. {
  81. UNI_DIR_NORTH = 0,
  82. UNI_DIR_SOUTH = 1,
  83. UNI_DIR_WEST = 2,
  84. UNI_DIR_EAST = 3,
  85. UNI_DIR_MASK = 3
  86. };
  87. public:
  88. Tile();
  89. Tile(const std::vector<SurfacePtr>& images,
  90. const std::vector<SurfacePtr>& editor_images,
  91. uint32_t attributes, uint32_t data, float fps,
  92. bool deprecated = false,
  93. const std::string& obj_name = "", const std::string& obj_data = "");
  94. /** Draw a tile on the screen */
  95. void draw(Canvas& canvas, const Vector& pos, int z_pos, const Color& color = Color(1, 1, 1)) const;
  96. void draw_debug(Canvas& canvas, const Vector& pos, int z_pos, const Color& color = Color(1.0f, 0.f, 1.0f, 0.5f)) const;
  97. SurfacePtr get_current_surface() const;
  98. SurfacePtr get_current_editor_surface() const;
  99. uint32_t get_attributes() const { return m_attributes; }
  100. int get_data() const { return m_data; }
  101. /** Checks the SLOPE attribute. Returns "true" if set, "false" otherwise. */
  102. bool is_slope() const { return (m_attributes & SLOPE) != 0; }
  103. /** Determine the solidity of a tile. This version behaves correctly
  104. for unisolid tiles by taking position and movement of the object
  105. in question into account. Because creating the arguments for
  106. this function can be expensive, you should handle trivial cases
  107. using the "is_solid()" and "is_unisolid()" methods first. */
  108. bool is_solid (const Rectf& tile_bbox, const Rectf& position, const Vector& movement) const;
  109. /** This version only checks the SOLID flag to determine the
  110. solidity of a tile. This means it will always return "true" for
  111. unisolid tiles. To determine the *current* solidity of unisolid
  112. tiles, use the "is_solid" method that takes position and
  113. movement into account (see above). */
  114. bool is_solid() const { return (m_attributes & SOLID) != 0; }
  115. /** Determines whether the tile's attributes are important to
  116. calculate the collisions. The tile may be unisolid and therefore
  117. the collision with that tile don't matter.*/
  118. bool is_collisionful(const Rectf& tile_bbox, const Rectf& position, const Vector& movement) const;
  119. /** Checks the UNISOLID attribute. Returns "true" if set, "false" otherwise. */
  120. bool is_unisolid() const { return (m_attributes & UNISOLID) != 0; }
  121. bool is_deprecated() const { return m_deprecated; }
  122. const std::string& get_object_name() const { return m_object_name; }
  123. const std::string& get_object_data() const { return m_object_data; }
  124. private:
  125. /** Returns zero if a unisolid tile is non-solid due to the movement
  126. direction, non-zero if the tile is solid due to direction. */
  127. bool check_movement_unisolid (const Vector& movement) const;
  128. /** Returns zero if a unisolid tile is non-solid due to the position
  129. of the tile and the object, non-zero if the tile is solid. */
  130. bool check_position_unisolid (const Rectf& obj_bbox,
  131. const Rectf& tile_bbox) const;
  132. private:
  133. std::vector<SurfacePtr> m_images;
  134. std::vector<SurfacePtr> m_editor_images;
  135. /** tile attributes */
  136. uint32_t m_attributes;
  137. /** General purpose data attached to a tile (content of a box, type of coin)*/
  138. int m_data;
  139. float m_fps;
  140. std::string m_object_name;
  141. std::string m_object_data;
  142. /** Prevent the addition of this tile to tilegroups, place restrictions in editor. */
  143. bool m_deprecated;
  144. private:
  145. Tile(const Tile&) = delete;
  146. Tile& operator=(const Tile&) = delete;
  147. };
  148. #endif
  149. /* EOF */