tile_map.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*************************************************************************/
  2. /* tile_map.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef TILE_MAP_H
  31. #define TILE_MAP_H
  32. #include "scene/2d/navigation2d.h"
  33. #include "scene/2d/node_2d.h"
  34. #include "scene/resources/tile_set.h"
  35. #include "self_list.h"
  36. #include "vset.h"
  37. class TileMap : public Node2D {
  38. GDCLASS(TileMap, Node2D);
  39. public:
  40. enum Mode {
  41. MODE_SQUARE,
  42. MODE_ISOMETRIC,
  43. MODE_CUSTOM
  44. };
  45. enum HalfOffset {
  46. HALF_OFFSET_X,
  47. HALF_OFFSET_Y,
  48. HALF_OFFSET_DISABLED,
  49. };
  50. enum TileOrigin {
  51. TILE_ORIGIN_TOP_LEFT,
  52. TILE_ORIGIN_CENTER,
  53. TILE_ORIGIN_BOTTOM_LEFT
  54. };
  55. private:
  56. Ref<TileSet> tile_set;
  57. Size2i cell_size;
  58. int quadrant_size;
  59. bool center_x, center_y;
  60. Mode mode;
  61. Transform2D custom_transform;
  62. HalfOffset half_offset;
  63. bool use_kinematic;
  64. Navigation2D *navigation;
  65. union PosKey {
  66. struct {
  67. int16_t x;
  68. int16_t y;
  69. };
  70. uint32_t key;
  71. //using a more precise comparison so the regions can be sorted later
  72. bool operator<(const PosKey &p_k) const { return (y == p_k.y) ? x < p_k.x : y < p_k.y; }
  73. PosKey(int16_t p_x, int16_t p_y) {
  74. x = p_x;
  75. y = p_y;
  76. }
  77. PosKey() {
  78. x = 0;
  79. y = 0;
  80. }
  81. };
  82. union Cell {
  83. struct {
  84. int32_t id : 24;
  85. bool flip_h : 1;
  86. bool flip_v : 1;
  87. bool transpose : 1;
  88. };
  89. uint32_t _u32t;
  90. Cell() { _u32t = 0; }
  91. };
  92. Map<PosKey, Cell> tile_map;
  93. struct Quadrant {
  94. Vector2 pos;
  95. List<RID> canvas_items;
  96. RID body;
  97. SelfList<Quadrant> dirty_list;
  98. struct NavPoly {
  99. int id;
  100. Transform2D xform;
  101. };
  102. struct Occluder {
  103. RID id;
  104. Transform2D xform;
  105. };
  106. Map<PosKey, NavPoly> navpoly_ids;
  107. Map<PosKey, Occluder> occluder_instances;
  108. VSet<PosKey> cells;
  109. void operator=(const Quadrant &q) {
  110. pos = q.pos;
  111. canvas_items = q.canvas_items;
  112. body = q.body;
  113. cells = q.cells;
  114. navpoly_ids = q.navpoly_ids;
  115. occluder_instances = q.occluder_instances;
  116. }
  117. Quadrant(const Quadrant &q)
  118. : dirty_list(this) {
  119. pos = q.pos;
  120. canvas_items = q.canvas_items;
  121. body = q.body;
  122. cells = q.cells;
  123. occluder_instances = q.occluder_instances;
  124. navpoly_ids = q.navpoly_ids;
  125. }
  126. Quadrant()
  127. : dirty_list(this) {}
  128. };
  129. Map<PosKey, Quadrant> quadrant_map;
  130. SelfList<Quadrant>::List dirty_quadrant_list;
  131. bool pending_update;
  132. Rect2 rect_cache;
  133. bool rect_cache_dirty;
  134. Rect2 used_size_cache;
  135. bool used_size_cache_dirty;
  136. bool quadrant_order_dirty;
  137. bool y_sort_mode;
  138. float fp_adjust;
  139. float friction;
  140. float bounce;
  141. uint32_t collision_layer;
  142. uint32_t collision_mask;
  143. TileOrigin tile_origin;
  144. int occluder_light_mask;
  145. void _fix_cell_transform(Transform2D &xform, const Cell &p_cell, const Vector2 &p_offset, const Size2 &p_sc);
  146. Map<PosKey, Quadrant>::Element *_create_quadrant(const PosKey &p_qk);
  147. void _erase_quadrant(Map<PosKey, Quadrant>::Element *Q);
  148. void _make_quadrant_dirty(Map<PosKey, Quadrant>::Element *Q);
  149. void _recreate_quadrants();
  150. void _clear_quadrants();
  151. void _update_dirty_quadrants();
  152. void _update_quadrant_space(const RID &p_space);
  153. void _update_quadrant_transform();
  154. void _recompute_rect_cache();
  155. void _update_all_items_material_state();
  156. _FORCE_INLINE_ void _update_item_material_state(const RID &p_canvas_item);
  157. _FORCE_INLINE_ int _get_quadrant_size() const;
  158. void _set_tile_data(const PoolVector<int> &p_data);
  159. PoolVector<int> _get_tile_data() const;
  160. void _set_old_cell_size(int p_size) { set_cell_size(Size2(p_size, p_size)); }
  161. int _get_old_cell_size() const { return cell_size.x; }
  162. _FORCE_INLINE_ Vector2 _map_to_world(int p_x, int p_y, bool p_ignore_ofs = false) const;
  163. protected:
  164. void _notification(int p_what);
  165. static void _bind_methods();
  166. public:
  167. enum {
  168. INVALID_CELL = -1
  169. };
  170. void set_tileset(const Ref<TileSet> &p_tileset);
  171. Ref<TileSet> get_tileset() const;
  172. void set_cell_size(Size2 p_size);
  173. Size2 get_cell_size() const;
  174. void set_quadrant_size(int p_size);
  175. int get_quadrant_size() const;
  176. void set_center_x(bool p_enable);
  177. bool get_center_x() const;
  178. void set_center_y(bool p_enable);
  179. bool get_center_y() const;
  180. void set_cell(int p_x, int p_y, int p_tile, bool p_flip_x = false, bool p_flip_y = false, bool p_transpose = false);
  181. int get_cell(int p_x, int p_y) const;
  182. bool is_cell_x_flipped(int p_x, int p_y) const;
  183. bool is_cell_y_flipped(int p_x, int p_y) const;
  184. bool is_cell_transposed(int p_x, int p_y) const;
  185. void set_cellv(const Vector2 &p_pos, int p_tile, bool p_flip_x = false, bool p_flip_y = false, bool p_transpose = false);
  186. int get_cellv(const Vector2 &p_pos) const;
  187. Rect2 get_item_rect() const;
  188. void set_collision_layer(uint32_t p_layer);
  189. uint32_t get_collision_layer() const;
  190. void set_collision_mask(uint32_t p_mask);
  191. uint32_t get_collision_mask() const;
  192. void set_collision_layer_bit(int p_bit, bool p_value);
  193. bool get_collision_layer_bit(int p_bit) const;
  194. void set_collision_mask_bit(int p_bit, bool p_value);
  195. bool get_collision_mask_bit(int p_bit) const;
  196. void set_collision_use_kinematic(bool p_use_kinematic);
  197. bool get_collision_use_kinematic() const;
  198. void set_collision_friction(float p_friction);
  199. float get_collision_friction() const;
  200. void set_collision_bounce(float p_bounce);
  201. float get_collision_bounce() const;
  202. void set_mode(Mode p_mode);
  203. Mode get_mode() const;
  204. void set_half_offset(HalfOffset p_half_offset);
  205. HalfOffset get_half_offset() const;
  206. void set_tile_origin(TileOrigin p_tile_origin);
  207. TileOrigin get_tile_origin() const;
  208. void set_custom_transform(const Transform2D &p_xform);
  209. Transform2D get_custom_transform() const;
  210. Transform2D get_cell_transform() const;
  211. Vector2 get_cell_draw_offset() const;
  212. Vector2 map_to_world(const Vector2 &p_pos, bool p_ignore_ofs = false) const;
  213. Vector2 world_to_map(const Vector2 &p_pos) const;
  214. void set_y_sort_mode(bool p_enable);
  215. bool is_y_sort_mode_enabled() const;
  216. Array get_used_cells() const;
  217. Array get_used_cells_by_id(int p_id) const;
  218. Rect2 get_used_rect(); // Not const because of cache
  219. void set_occluder_light_mask(int p_mask);
  220. int get_occluder_light_mask() const;
  221. virtual void set_light_mask(int p_light_mask);
  222. virtual void set_material(const Ref<Material> &p_material);
  223. virtual void set_use_parent_material(bool p_use_parent_material);
  224. void clear();
  225. TileMap();
  226. ~TileMap();
  227. };
  228. VARIANT_ENUM_CAST(TileMap::Mode);
  229. VARIANT_ENUM_CAST(TileMap::HalfOffset);
  230. VARIANT_ENUM_CAST(TileMap::TileOrigin);
  231. #endif // TILE_MAP_H