tile_map.h 7.4 KB

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