tile_map.h
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 
32 #include "scene/2d/node_2d.h"
33 #include "scene/2d/navigation2d.h"
34 #include "scene/resources/tile_set.h"
35 #include "self_list.h"
36 #include "vset.h"
37 
38 class TileMap : public Node2D {
39 
40  OBJ_TYPE( TileMap, Node2D );
41 public:
42 
43  enum Mode {
44  MODE_SQUARE,
45  MODE_ISOMETRIC,
46  MODE_CUSTOM
47  };
48 
49  enum HalfOffset {
50  HALF_OFFSET_X,
51  HALF_OFFSET_Y,
52  HALF_OFFSET_DISABLED,
53  };
54 
55  enum TileOrigin {
56  TILE_ORIGIN_TOP_LEFT,
57  TILE_ORIGIN_CENTER
58  };
59 
60 
61 private:
62 
63  Ref<TileSet> tile_set;
64  Size2i cell_size;
65  int quadrant_size;
66  bool center_x,center_y;
67  Mode mode;
68  Matrix32 custom_transform;
69  HalfOffset half_offset;
70  bool use_kinematic;
71  Navigation2D *navigation;
72 
73 
74  union PosKey {
75 
76  struct {
77  int16_t x;
78  int16_t y;
79  };
80  uint32_t key;
81 
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 
85  PosKey(int16_t p_x, int16_t p_y) { x=p_x; y=p_y; }
86  PosKey() { x=0; y=0; }
87 
88  };
89 
90 
91  union Cell {
92 
93  struct {
94  int32_t id:24;
95  bool flip_h:1;
96  bool flip_v:1;
97  bool transpose:1;
98  };
99 
100  uint32_t _u32t;
101  Cell() { _u32t=0; }
102  };
103 
104 
105  Map<PosKey,Cell> tile_map;
106  struct Quadrant {
107 
108  Vector2 pos;
109  List<RID> canvas_items;
110  RID body;
111 
112  SelfList<Quadrant> dirty_list;
113 
114  struct NavPoly {
115  int id;
116  Matrix32 xform;
117  };
118 
119  struct Occluder {
120  RID id;
121  Matrix32 xform;
122  };
123 
124 
125  Map<PosKey,NavPoly> navpoly_ids;
126  Map<PosKey,Occluder> occluder_instances;
127 
128  VSet<PosKey> cells;
129 
130  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; }
131  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;}
132  Quadrant() : dirty_list(this) {}
133  };
134 
135  Map<PosKey,Quadrant> quadrant_map;
136 
137  SelfList<Quadrant>::List dirty_quadrant_list;
138 
139  bool pending_update;
140 
141  Rect2 rect_cache;
142  bool rect_cache_dirty;
143  bool quadrant_order_dirty;
144  bool y_sort_mode;
145  float fp_adjust;
146  float friction;
147  float bounce;
148  uint32_t collision_layer;
149  uint32_t collision_mask;
150 
151  TileOrigin tile_origin;
152 
153  int occluder_light_mask;
154 
155  void _fix_cell_transform(Matrix32& xform, const Cell& p_cell, const Vector2 &p_offset, const Size2 &p_sc);
156 
157  Map<PosKey,Quadrant>::Element *_create_quadrant(const PosKey& p_qk);
158  void _erase_quadrant(Map<PosKey,Quadrant>::Element *Q);
159  void _make_quadrant_dirty(Map<PosKey,Quadrant>::Element *Q);
160  void _recreate_quadrants();
161  void _clear_quadrants();
162  void _update_dirty_quadrants();
163  void _update_quadrant_space(const RID& p_space);
164  void _update_quadrant_transform();
165  void _recompute_rect_cache();
166 
167  _FORCE_INLINE_ int _get_quadrant_size() const;
168 
169 
170  void _set_tile_data(const DVector<int>& p_data);
171  DVector<int> _get_tile_data() const;
172 
173  void _set_old_cell_size(int p_size) { set_cell_size(Size2(p_size,p_size)); }
174  int _get_old_cell_size() const { return cell_size.x; }
175 
176  _FORCE_INLINE_ Vector2 _map_to_world(int p_x,int p_y,bool p_ignore_ofs=false) const;
177 
178  Array get_used_cells() const;
179 
180 protected:
181 
182 
183  void _notification(int p_what);
184  static void _bind_methods();
185 
186 public:
187 
188  enum {
189  INVALID_CELL=-1
190  };
191 
192 
193  void set_tileset(const Ref<TileSet>& p_tileset);
194  Ref<TileSet> get_tileset() const;
195 
196  void set_cell_size(Size2 p_size);
197  Size2 get_cell_size() const;
198 
199  void set_quadrant_size(int p_size);
200  int get_quadrant_size() const;
201 
202  void set_center_x(bool p_enable);
203  bool get_center_x() const;
204  void set_center_y(bool p_enable);
205  bool get_center_y() const;
206 
207  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);
208  int get_cell(int p_x,int p_y) const;
209  bool is_cell_x_flipped(int p_x,int p_y) const;
210  bool is_cell_y_flipped(int p_x,int p_y) const;
211  bool is_cell_transposed(int p_x,int p_y) const;
212 
213  void set_cellv(const Vector2& p_pos,int p_tile,bool p_flip_x=false,bool p_flip_y=false,bool p_transpose=false);
214  int get_cellv(const Vector2& p_pos) const;
215 
216  Rect2 get_item_rect() const;
217 
218  void set_collision_layer(uint32_t p_layer);
219  uint32_t get_collision_layer() const;
220 
221  void set_collision_mask(uint32_t p_mask);
222  uint32_t get_collision_mask() const;
223 
224  void set_collision_use_kinematic(bool p_use_kinematic);
225  bool get_collision_use_kinematic() const;
226 
227  void set_collision_friction(float p_friction);
228  float get_collision_friction() const;
229 
230  void set_collision_bounce(float p_bounce);
231  float get_collision_bounce() const;
232 
233  void set_mode(Mode p_mode);
234  Mode get_mode() const;
235 
236  void set_half_offset(HalfOffset p_half_offset);
237  HalfOffset get_half_offset() const;
238 
239  void set_tile_origin(TileOrigin p_tile_origin);
240  TileOrigin get_tile_origin() const;
241 
242  void set_custom_transform(const Matrix32& p_xform);
243  Matrix32 get_custom_transform() const;
244 
245  Matrix32 get_cell_transform() const;
246  Vector2 get_cell_draw_offset() const;
247 
248  Vector2 map_to_world(const Vector2& p_pos, bool p_ignore_ofs=false) const;
249  Vector2 world_to_map(const Vector2& p_pos) const;
250 
251  void set_y_sort_mode(bool p_enable);
252  bool is_y_sort_mode_enabled() const;
253 
254  void set_occluder_light_mask(int p_mask);
255  int get_occluder_light_mask() const;
256 
257  virtual void set_light_mask(int p_light_mask);
258 
259  void clear();
260 
261  TileMap();
262  ~TileMap();
263 };
264 
265 VARIANT_ENUM_CAST(TileMap::Mode);
266 VARIANT_ENUM_CAST(TileMap::HalfOffset);
267 VARIANT_ENUM_CAST(TileMap::TileOrigin);
268 
269 #endif // TILE_MAP_H
Definition: tween_interpolaters.cpp:313
Definition: array.h:38
Definition: math_2d.h:369
Definition: math_2d.h:204
Definition: navigation2d.h:7
Definition: rid.h:47
Definition: tile_map.h:119
Definition: math_2d.h:65
Definition: tile_map.h:114
Definition: math_2d.h:554
Definition: node_2d.h:34
Definition: tile_map.h:38