tile_map.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**************************************************************************/
  2. /* tile_map.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/navigation_2d.h"
  35. #include "scene/2d/node_2d.h"
  36. #include "scene/resources/tile_set.h"
  37. class CollisionObject2D;
  38. class TileMap : public Node2D {
  39. GDCLASS(TileMap, Node2D);
  40. public:
  41. enum Mode {
  42. MODE_SQUARE,
  43. MODE_ISOMETRIC,
  44. MODE_CUSTOM
  45. };
  46. enum HalfOffset {
  47. HALF_OFFSET_X,
  48. HALF_OFFSET_Y,
  49. HALF_OFFSET_DISABLED,
  50. HALF_OFFSET_NEGATIVE_X,
  51. HALF_OFFSET_NEGATIVE_Y,
  52. };
  53. enum TileOrigin {
  54. TILE_ORIGIN_TOP_LEFT,
  55. TILE_ORIGIN_CENTER,
  56. TILE_ORIGIN_BOTTOM_LEFT
  57. };
  58. private:
  59. enum DataFormat {
  60. FORMAT_1 = 0,
  61. FORMAT_2
  62. };
  63. Ref<TileSet> tile_set;
  64. Size2i cell_size;
  65. int quadrant_size;
  66. Mode mode;
  67. Transform2D custom_transform;
  68. HalfOffset half_offset;
  69. bool use_parent;
  70. CollisionObject2D *collision_parent;
  71. bool use_kinematic;
  72. Navigation2D *navigation;
  73. bool bake_navigation = false;
  74. uint32_t navigation_layers = 1;
  75. bool show_collision = false;
  76. union PosKey {
  77. struct {
  78. int16_t x;
  79. int16_t y;
  80. };
  81. uint32_t key;
  82. //using a more precise comparison so the regions can be sorted later
  83. bool operator<(const PosKey &p_k) const { return (y == p_k.y) ? x < p_k.x : y < p_k.y; }
  84. bool operator==(const PosKey &p_k) const { return (y == p_k.y && x == p_k.x); }
  85. PosKey to_quadrant(const int &p_quadrant_size) const {
  86. // rounding down, instead of simply rounding towards zero (truncating)
  87. return PosKey(
  88. x > 0 ? x / p_quadrant_size : (x - (p_quadrant_size - 1)) / p_quadrant_size,
  89. y > 0 ? y / p_quadrant_size : (y - (p_quadrant_size - 1)) / p_quadrant_size);
  90. }
  91. PosKey(int16_t p_x, int16_t p_y) {
  92. x = p_x;
  93. y = p_y;
  94. }
  95. PosKey() {
  96. x = 0;
  97. y = 0;
  98. }
  99. };
  100. union Cell {
  101. struct {
  102. int32_t id : 24;
  103. bool flip_h : 1;
  104. bool flip_v : 1;
  105. bool transpose : 1;
  106. int16_t autotile_coord_x : 16;
  107. int16_t autotile_coord_y : 16;
  108. };
  109. uint64_t _u64t;
  110. Cell() { _u64t = 0; }
  111. };
  112. Map<PosKey, Cell> tile_map;
  113. List<PosKey> dirty_bitmask;
  114. struct Quadrant {
  115. Vector2 pos;
  116. List<RID> canvas_items;
  117. RID body;
  118. uint32_t shape_owner_id;
  119. SelfList<Quadrant> dirty_list;
  120. struct NavPoly {
  121. RID region;
  122. Transform2D xform;
  123. };
  124. struct Occluder {
  125. RID id;
  126. Transform2D xform;
  127. };
  128. Map<PosKey, NavPoly> navpoly_ids;
  129. Map<PosKey, Occluder> occluder_instances;
  130. VSet<PosKey> cells;
  131. void clear_navpoly();
  132. void operator=(const Quadrant &q) {
  133. pos = q.pos;
  134. canvas_items = q.canvas_items;
  135. body = q.body;
  136. shape_owner_id = q.shape_owner_id;
  137. cells = q.cells;
  138. navpoly_ids = q.navpoly_ids;
  139. occluder_instances = q.occluder_instances;
  140. }
  141. Quadrant(const Quadrant &q) :
  142. dirty_list(this) {
  143. pos = q.pos;
  144. canvas_items = q.canvas_items;
  145. body = q.body;
  146. shape_owner_id = q.shape_owner_id;
  147. cells = q.cells;
  148. occluder_instances = q.occluder_instances;
  149. navpoly_ids = q.navpoly_ids;
  150. }
  151. Quadrant() :
  152. dirty_list(this) {}
  153. };
  154. Map<PosKey, Quadrant> quadrant_map;
  155. SelfList<Quadrant>::List dirty_quadrant_list;
  156. bool pending_update;
  157. Rect2 rect_cache;
  158. bool rect_cache_dirty;
  159. Rect2 used_size_cache;
  160. bool used_size_cache_dirty;
  161. bool quadrant_order_dirty;
  162. bool y_sort_mode;
  163. bool compatibility_mode;
  164. bool centered_textures;
  165. bool clip_uv;
  166. float fp_adjust;
  167. float friction;
  168. float bounce;
  169. uint32_t collision_layer;
  170. uint32_t collision_mask;
  171. mutable DataFormat format;
  172. TileOrigin tile_origin;
  173. int occluder_light_mask;
  174. void _fix_cell_transform(Transform2D &xform, const Cell &p_cell, const Vector2 &p_offset, const Size2 &p_sc);
  175. void _add_shape(int &shape_idx, const Quadrant &p_q, const Ref<Shape2D> &p_shape, const TileSet::ShapeData &p_shape_data, const Transform2D &p_xform, const Vector2 &p_metadata);
  176. Map<PosKey, Quadrant>::Element *_create_quadrant(const PosKey &p_qk);
  177. void _erase_quadrant(Map<PosKey, Quadrant>::Element *Q);
  178. void _make_quadrant_dirty(Map<PosKey, Quadrant>::Element *Q, bool update = true);
  179. void _recreate_quadrants();
  180. void _clear_quadrants();
  181. void _update_quadrant_space(const RID &p_space);
  182. void _update_quadrant_transform();
  183. void _recompute_rect_cache();
  184. void _update_all_items_material_state();
  185. _FORCE_INLINE_ void _update_item_material_state(const RID &p_canvas_item);
  186. _FORCE_INLINE_ int _get_quadrant_size() const;
  187. void _set_tile_data(const PoolVector<int> &p_data);
  188. PoolVector<int> _get_tile_data() const;
  189. void _set_old_cell_size(int p_size) { set_cell_size(Size2(p_size, p_size)); }
  190. int _get_old_cell_size() const { return cell_size.x; }
  191. _FORCE_INLINE_ Vector2 _map_to_world(int p_x, int p_y, bool p_ignore_ofs = false) const;
  192. protected:
  193. bool _set(const StringName &p_name, const Variant &p_value);
  194. bool _get(const StringName &p_name, Variant &r_ret) const;
  195. void _get_property_list(List<PropertyInfo> *p_list) const;
  196. void _notification(int p_what);
  197. static void _bind_methods();
  198. virtual void _validate_property(PropertyInfo &property) const;
  199. virtual void _changed_callback(Object *p_changed, const char *p_prop);
  200. public:
  201. enum {
  202. INVALID_CELL = -1
  203. };
  204. #ifdef TOOLS_ENABLED
  205. virtual Rect2 _edit_get_rect() const;
  206. #endif
  207. void set_tileset(const Ref<TileSet> &p_tileset);
  208. Ref<TileSet> get_tileset() const;
  209. void set_cell_size(Size2 p_size);
  210. Size2 get_cell_size() const;
  211. void set_quadrant_size(int p_size);
  212. int get_quadrant_size() const;
  213. 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, const Vector2 &p_autotile_coord = Vector2());
  214. int get_cell(int p_x, int p_y) const;
  215. bool is_cell_x_flipped(int p_x, int p_y) const;
  216. bool is_cell_y_flipped(int p_x, int p_y) const;
  217. bool is_cell_transposed(int p_x, int p_y) const;
  218. void set_cell_autotile_coord(int p_x, int p_y, const Vector2 &p_coord);
  219. Vector2 get_cell_autotile_coord(int p_x, int p_y) const;
  220. void _set_celld(const Vector2 &p_pos, const Dictionary &p_data);
  221. void set_cellv(const Vector2 &p_pos, int p_tile, bool p_flip_x = false, bool p_flip_y = false, bool p_transpose = false, const Vector2 &p_autotile_coord = Vector2());
  222. int get_cellv(const Vector2 &p_pos) const;
  223. void make_bitmask_area_dirty(const Vector2 &p_pos);
  224. void update_bitmask_area(const Vector2 &p_pos);
  225. void update_bitmask_region(const Vector2 &p_start = Vector2(), const Vector2 &p_end = Vector2());
  226. void update_cell_bitmask(int p_x, int p_y);
  227. void update_dirty_bitmask();
  228. void update_dirty_quadrants();
  229. void set_show_collision(bool p_value);
  230. bool is_show_collision_enabled() const;
  231. void set_collision_layer(uint32_t p_layer);
  232. uint32_t get_collision_layer() const;
  233. void set_collision_mask(uint32_t p_mask);
  234. uint32_t get_collision_mask() const;
  235. void set_collision_layer_bit(int p_bit, bool p_value);
  236. bool get_collision_layer_bit(int p_bit) const;
  237. void set_collision_mask_bit(int p_bit, bool p_value);
  238. bool get_collision_mask_bit(int p_bit) const;
  239. void set_collision_use_kinematic(bool p_use_kinematic);
  240. bool get_collision_use_kinematic() const;
  241. void set_collision_use_parent(bool p_use_parent);
  242. bool get_collision_use_parent() const;
  243. void set_collision_friction(float p_friction);
  244. float get_collision_friction() const;
  245. void set_collision_bounce(float p_bounce);
  246. float get_collision_bounce() const;
  247. void set_bake_navigation(bool p_bake_navigation);
  248. bool is_baking_navigation();
  249. void set_navigation_layers(uint32_t p_navigation_layers);
  250. uint32_t get_navigation_layers();
  251. void set_mode(Mode p_mode);
  252. Mode get_mode() const;
  253. void set_half_offset(HalfOffset p_half_offset);
  254. HalfOffset get_half_offset() const;
  255. void set_tile_origin(TileOrigin p_tile_origin);
  256. TileOrigin get_tile_origin() const;
  257. void set_custom_transform(const Transform2D &p_xform);
  258. Transform2D get_custom_transform() const;
  259. Transform2D get_cell_transform() const;
  260. Vector2 get_cell_draw_offset() const;
  261. Vector2 map_to_world(const Vector2 &p_pos, bool p_ignore_ofs = false) const;
  262. Vector2 world_to_map(const Vector2 &p_pos) const;
  263. void set_y_sort_mode(bool p_enable);
  264. bool is_y_sort_mode_enabled() const;
  265. void set_compatibility_mode(bool p_enable);
  266. bool is_compatibility_mode_enabled() const;
  267. void set_centered_textures(bool p_enable);
  268. bool is_centered_textures_enabled() const;
  269. Array get_used_cells() const;
  270. Array get_used_cells_by_id(int p_id) const;
  271. Rect2 get_used_rect(); // Not const because of cache
  272. void set_occluder_light_mask(int p_mask);
  273. int get_occluder_light_mask() const;
  274. virtual void set_light_mask(int p_light_mask);
  275. virtual void set_material(const Ref<Material> &p_material);
  276. virtual void set_use_parent_material(bool p_use_parent_material);
  277. void set_clip_uv(bool p_enable);
  278. bool get_clip_uv() const;
  279. String get_configuration_warning() const;
  280. void fix_invalid_tiles();
  281. void clear();
  282. TileMap();
  283. ~TileMap();
  284. };
  285. VARIANT_ENUM_CAST(TileMap::Mode);
  286. VARIANT_ENUM_CAST(TileMap::HalfOffset);
  287. VARIANT_ENUM_CAST(TileMap::TileOrigin);
  288. #endif // TILE_MAP_H