tile_map.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*************************************************************************/
  2. /* tile_map.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/self_list.h"
  33. #include "core/vset.h"
  34. #include "scene/2d/navigation2d.h"
  35. #include "scene/2d/node_2d.h"
  36. #include "scene/resources/tile_set.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. enum DataFormat {
  57. FORMAT_1 = 0,
  58. FORMAT_2
  59. };
  60. Ref<TileSet> tile_set;
  61. Size2i cell_size;
  62. int quadrant_size;
  63. Mode mode;
  64. Transform2D custom_transform;
  65. HalfOffset half_offset;
  66. bool use_kinematic;
  67. Navigation2D *navigation;
  68. union PosKey {
  69. struct {
  70. int16_t x;
  71. int16_t y;
  72. };
  73. uint32_t key;
  74. //using a more precise comparison so the regions can be sorted later
  75. bool operator<(const PosKey &p_k) const { return (y == p_k.y) ? x < p_k.x : y < p_k.y; }
  76. bool operator==(const PosKey &p_k) const { return (y == p_k.y && x == p_k.x); }
  77. PosKey(int16_t p_x, int16_t p_y) {
  78. x = p_x;
  79. y = p_y;
  80. }
  81. PosKey() {
  82. x = 0;
  83. y = 0;
  84. }
  85. };
  86. union Cell {
  87. struct {
  88. int32_t id : 24;
  89. bool flip_h : 1;
  90. bool flip_v : 1;
  91. bool transpose : 1;
  92. int16_t autotile_coord_x : 16;
  93. int16_t autotile_coord_y : 16;
  94. };
  95. uint64_t _u64t;
  96. Cell() { _u64t = 0; }
  97. };
  98. Map<PosKey, Cell> tile_map;
  99. List<PosKey> dirty_bitmask;
  100. struct Quadrant {
  101. Vector2 pos;
  102. List<RID> canvas_items;
  103. RID body;
  104. SelfList<Quadrant> dirty_list;
  105. struct NavPoly {
  106. int id;
  107. Transform2D xform;
  108. };
  109. struct Occluder {
  110. RID id;
  111. Transform2D xform;
  112. };
  113. Map<PosKey, NavPoly> navpoly_ids;
  114. Map<PosKey, Occluder> occluder_instances;
  115. VSet<PosKey> cells;
  116. void operator=(const Quadrant &q) {
  117. pos = q.pos;
  118. canvas_items = q.canvas_items;
  119. body = q.body;
  120. cells = q.cells;
  121. navpoly_ids = q.navpoly_ids;
  122. occluder_instances = q.occluder_instances;
  123. }
  124. Quadrant(const Quadrant &q) :
  125. dirty_list(this) {
  126. pos = q.pos;
  127. canvas_items = q.canvas_items;
  128. body = q.body;
  129. cells = q.cells;
  130. occluder_instances = q.occluder_instances;
  131. navpoly_ids = q.navpoly_ids;
  132. }
  133. Quadrant() :
  134. dirty_list(this) {}
  135. };
  136. Map<PosKey, Quadrant> quadrant_map;
  137. SelfList<Quadrant>::List dirty_quadrant_list;
  138. bool pending_update;
  139. Rect2 rect_cache;
  140. bool rect_cache_dirty;
  141. Rect2 used_size_cache;
  142. bool used_size_cache_dirty;
  143. bool quadrant_order_dirty;
  144. bool y_sort_mode;
  145. bool clip_uv;
  146. float fp_adjust;
  147. float friction;
  148. float bounce;
  149. uint32_t collision_layer;
  150. uint32_t collision_mask;
  151. mutable DataFormat format;
  152. TileOrigin tile_origin;
  153. int occluder_light_mask;
  154. void _fix_cell_transform(Transform2D &xform, const Cell &p_cell, const Vector2 &p_offset, const Size2 &p_sc);
  155. Map<PosKey, Quadrant>::Element *_create_quadrant(const PosKey &p_qk);
  156. void _erase_quadrant(Map<PosKey, Quadrant>::Element *Q);
  157. void _make_quadrant_dirty(Map<PosKey, Quadrant>::Element *Q, bool update = true);
  158. void _recreate_quadrants();
  159. void _clear_quadrants();
  160. void _update_quadrant_space(const RID &p_space);
  161. void _update_quadrant_transform();
  162. void _recompute_rect_cache();
  163. void _update_all_items_material_state();
  164. _FORCE_INLINE_ void _update_item_material_state(const RID &p_canvas_item);
  165. _FORCE_INLINE_ int _get_quadrant_size() const;
  166. void _set_tile_data(const PoolVector<int> &p_data);
  167. PoolVector<int> _get_tile_data() const;
  168. void _set_old_cell_size(int p_size) { set_cell_size(Size2(p_size, p_size)); }
  169. int _get_old_cell_size() const { return cell_size.x; }
  170. _FORCE_INLINE_ Vector2 _map_to_world(int p_x, int p_y, bool p_ignore_ofs = false) const;
  171. protected:
  172. bool _set(const StringName &p_name, const Variant &p_value);
  173. bool _get(const StringName &p_name, Variant &r_ret) const;
  174. void _get_property_list(List<PropertyInfo> *p_list) const;
  175. void _notification(int p_what);
  176. static void _bind_methods();
  177. virtual void _changed_callback(Object *p_changed, const char *p_prop);
  178. public:
  179. enum {
  180. INVALID_CELL = -1
  181. };
  182. virtual Rect2 _edit_get_rect() const;
  183. void set_tileset(const Ref<TileSet> &p_tileset);
  184. Ref<TileSet> get_tileset() const;
  185. void set_cell_size(Size2 p_size);
  186. Size2 get_cell_size() const;
  187. void set_quadrant_size(int p_size);
  188. int get_quadrant_size() const;
  189. 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, Vector2 p_autotile_coord = Vector2());
  190. int get_cell(int p_x, int p_y) const;
  191. bool is_cell_x_flipped(int p_x, int p_y) const;
  192. bool is_cell_y_flipped(int p_x, int p_y) const;
  193. bool is_cell_transposed(int p_x, int p_y) const;
  194. void set_cell_autotile_coord(int p_x, int p_y, const Vector2 &p_coord);
  195. Vector2 get_cell_autotile_coord(int p_x, int p_y) const;
  196. void _set_celld(const Vector2 &p_pos, const Dictionary &p_data);
  197. void set_cellv(const Vector2 &p_pos, int p_tile, bool p_flip_x = false, bool p_flip_y = false, bool p_transpose = false);
  198. int get_cellv(const Vector2 &p_pos) const;
  199. void make_bitmask_area_dirty(const Vector2 &p_pos);
  200. void update_bitmask_area(const Vector2 &p_pos);
  201. void update_bitmask_region(const Vector2 &p_start = Vector2(), const Vector2 &p_end = Vector2());
  202. void update_cell_bitmask(int p_x, int p_y);
  203. void update_dirty_bitmask();
  204. void update_dirty_quadrants();
  205. void set_collision_layer(uint32_t p_layer);
  206. uint32_t get_collision_layer() const;
  207. void set_collision_mask(uint32_t p_mask);
  208. uint32_t get_collision_mask() const;
  209. void set_collision_layer_bit(int p_bit, bool p_value);
  210. bool get_collision_layer_bit(int p_bit) const;
  211. void set_collision_mask_bit(int p_bit, bool p_value);
  212. bool get_collision_mask_bit(int p_bit) const;
  213. void set_collision_use_kinematic(bool p_use_kinematic);
  214. bool get_collision_use_kinematic() const;
  215. void set_collision_friction(float p_friction);
  216. float get_collision_friction() const;
  217. void set_collision_bounce(float p_bounce);
  218. float get_collision_bounce() const;
  219. void set_mode(Mode p_mode);
  220. Mode get_mode() const;
  221. void set_half_offset(HalfOffset p_half_offset);
  222. HalfOffset get_half_offset() const;
  223. void set_tile_origin(TileOrigin p_tile_origin);
  224. TileOrigin get_tile_origin() const;
  225. void set_custom_transform(const Transform2D &p_xform);
  226. Transform2D get_custom_transform() const;
  227. Transform2D get_cell_transform() const;
  228. Vector2 get_cell_draw_offset() const;
  229. Vector2 map_to_world(const Vector2 &p_pos, bool p_ignore_ofs = false) const;
  230. Vector2 world_to_map(const Vector2 &p_pos) const;
  231. void set_y_sort_mode(bool p_enable);
  232. bool is_y_sort_mode_enabled() const;
  233. Array get_used_cells() const;
  234. Array get_used_cells_by_id(int p_id) const;
  235. Rect2 get_used_rect(); // Not const because of cache
  236. void set_occluder_light_mask(int p_mask);
  237. int get_occluder_light_mask() const;
  238. virtual void set_light_mask(int p_light_mask);
  239. virtual void set_material(const Ref<Material> &p_material);
  240. virtual void set_use_parent_material(bool p_use_parent_material);
  241. void set_clip_uv(bool p_enable);
  242. bool get_clip_uv() const;
  243. void fix_invalid_tiles();
  244. void clear();
  245. TileMap();
  246. ~TileMap();
  247. };
  248. VARIANT_ENUM_CAST(TileMap::Mode);
  249. VARIANT_ENUM_CAST(TileMap::HalfOffset);
  250. VARIANT_ENUM_CAST(TileMap::TileOrigin);
  251. #endif // TILE_MAP_H