tile_map.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*************************************************************************/
  2. /* tile_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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. #include "tile_map.h"
  30. #include "io/marshalls.h"
  31. #include "servers/physics_2d_server.h"
  32. void TileMap::_notification(int p_what) {
  33. switch(p_what) {
  34. case NOTIFICATION_ENTER_SCENE: {
  35. pending_update=true;
  36. _update_dirty_quadrants();
  37. RID space = get_world_2d()->get_space();
  38. _update_quadrant_transform();
  39. _update_quadrant_space(space);
  40. } break;
  41. case NOTIFICATION_EXIT_SCENE: {
  42. _update_quadrant_space(RID());
  43. } break;
  44. case NOTIFICATION_TRANSFORM_CHANGED: {
  45. //move stuff
  46. _update_quadrant_transform();
  47. } break;
  48. }
  49. }
  50. void TileMap::_update_quadrant_space(const RID& p_space) {
  51. for (Map<PosKey,Quadrant>::Element *E=quadrant_map.front();E;E=E->next()) {
  52. Quadrant &q=E->get();
  53. Physics2DServer::get_singleton()->body_set_space(q.static_body,p_space);
  54. }
  55. }
  56. void TileMap::_update_quadrant_transform() {
  57. if (!is_inside_scene())
  58. return;
  59. Matrix32 global_transform = get_global_transform();
  60. for (Map<PosKey,Quadrant>::Element *E=quadrant_map.front();E;E=E->next()) {
  61. Quadrant &q=E->get();
  62. Matrix32 xform;
  63. xform.set_origin( q.pos );
  64. xform = global_transform * xform;
  65. Physics2DServer::get_singleton()->body_set_state(q.static_body,Physics2DServer::BODY_STATE_TRANSFORM,xform);
  66. }
  67. }
  68. void TileMap::set_tileset(const Ref<TileSet>& p_tileset) {
  69. if (tile_set.is_valid())
  70. tile_set->disconnect("changed",this,"_recreate_quadrants");
  71. _clear_quadrants();
  72. tile_set=p_tileset;
  73. if (tile_set.is_valid())
  74. tile_set->connect("changed",this,"_recreate_quadrants");
  75. else
  76. clear();
  77. _recreate_quadrants();
  78. }
  79. Ref<TileSet> TileMap::get_tileset() const {
  80. return tile_set;
  81. }
  82. void TileMap::set_cell_size(int p_size) {
  83. ERR_FAIL_COND(p_size<1);
  84. _clear_quadrants();
  85. cell_size=p_size;
  86. _recreate_quadrants();
  87. }
  88. int TileMap::get_cell_size() const {
  89. return cell_size;
  90. }
  91. void TileMap::set_quadrant_size(int p_size) {
  92. ERR_FAIL_COND(p_size<1);
  93. _clear_quadrants();
  94. quadrant_size=p_size;
  95. _recreate_quadrants();
  96. }
  97. int TileMap::get_quadrant_size() const {
  98. return quadrant_size;
  99. }
  100. void TileMap::set_center_x(bool p_enable) {
  101. center_x=p_enable;
  102. _recreate_quadrants();
  103. }
  104. bool TileMap::get_center_x() const {
  105. return center_x;
  106. }
  107. void TileMap::set_center_y(bool p_enable) {
  108. center_y=p_enable;
  109. _recreate_quadrants();
  110. }
  111. bool TileMap::get_center_y() const {
  112. return center_y;
  113. }
  114. void TileMap::_update_dirty_quadrants() {
  115. if (!pending_update)
  116. return;
  117. if (!is_inside_scene())
  118. return;
  119. if (!tile_set.is_valid())
  120. return;
  121. VisualServer *vs = VisualServer::get_singleton();
  122. Physics2DServer *ps = Physics2DServer::get_singleton();
  123. while (dirty_quadrant_list.first()) {
  124. Quadrant &q = *dirty_quadrant_list.first()->self();
  125. vs->canvas_item_clear(q.canvas_item);
  126. ps->body_clear_shapes(q.static_body);
  127. for(int i=0;i<q.cells.size();i++) {
  128. Map<PosKey,Cell>::Element *E=tile_map.find( q.cells[i] );
  129. Cell &c=E->get();
  130. //moment of truth
  131. if (!tile_set->has_tile(c.id))
  132. continue;
  133. Ref<Texture> tex = tile_set->tile_get_texture(c.id);
  134. Vector2 tile_ofs = tile_set->tile_get_texture_offset(c.id);
  135. Vector2 offset = Point2( E->key().x, E->key().y )*cell_size - q.pos;
  136. if (!tex.is_valid())
  137. continue;
  138. Rect2 r = tile_set->tile_get_region(c.id);
  139. Size2 s = tex->get_size();
  140. if (r==Rect2())
  141. s = tex->get_size();
  142. else {
  143. s = r.size;
  144. r.pos.x+=fp_adjust;
  145. r.pos.y+=fp_adjust;
  146. r.size.x-=fp_adjust*2.0;
  147. r.size.y-=fp_adjust*2.0;
  148. }
  149. Rect2 rect;
  150. rect.pos=offset.floor();
  151. rect.size=s;
  152. rect.size.x+=fp_adjust;
  153. rect.size.y+=fp_adjust;
  154. if (c.flip_h)
  155. rect.size.x=-rect.size.x;
  156. if (c.flip_v)
  157. rect.size.y=-rect.size.y;
  158. rect.pos+=tile_ofs;
  159. if (r==Rect2()) {
  160. tex->draw_rect(q.canvas_item,rect);
  161. } else {
  162. tex->draw_rect_region(q.canvas_item,rect,r);
  163. }
  164. Vector< Ref<Shape2D> > shapes = tile_set->tile_get_shapes(c.id);
  165. for(int i=0;i<shapes.size();i++) {
  166. Ref<Shape2D> shape = shapes[i];
  167. if (shape.is_valid()) {
  168. Vector2 shape_ofs = tile_set->tile_get_shape_offset(c.id);
  169. Matrix32 xform;
  170. xform.set_origin(offset.floor()+shape_ofs);
  171. if (c.flip_h) {
  172. xform.elements[0]=-xform.elements[0];
  173. xform.elements[2].x+=s.x;
  174. }
  175. if (c.flip_v) {
  176. xform.elements[1]=-xform.elements[1];
  177. xform.elements[2].y+=s.y;
  178. }
  179. ps->body_add_shape(q.static_body,shape->get_rid(),xform);
  180. }
  181. }
  182. }
  183. dirty_quadrant_list.remove( dirty_quadrant_list.first() );
  184. }
  185. pending_update=false;
  186. _recompute_rect_cache();
  187. }
  188. void TileMap::_recompute_rect_cache() {
  189. #ifdef DEBUG_ENABLED
  190. if (!rect_cache_dirty)
  191. return;
  192. Rect2 r_total;
  193. for (Map<PosKey,Quadrant>::Element *E=quadrant_map.front();E;E=E->next()) {
  194. Rect2 r( Point2(E->key().x, E->key().y)*cell_size*quadrant_size, Size2(1,1)*cell_size*quadrant_size );
  195. if (E==quadrant_map.front())
  196. r_total=r;
  197. else
  198. r_total=r_total.merge(r);
  199. }
  200. if (r_total==Rect2()) {
  201. rect_cache=Rect2(-10,-10,20,20);
  202. } else {
  203. rect_cache=r_total;
  204. }
  205. item_rect_changed();
  206. rect_cache_dirty=false;
  207. #endif
  208. }
  209. Map<TileMap::PosKey,TileMap::Quadrant>::Element *TileMap::_create_quadrant(const PosKey& p_qk) {
  210. Matrix32 xform;
  211. xform.set_origin(Point2(p_qk.x,p_qk.y)*quadrant_size*cell_size);
  212. Quadrant q;
  213. q.canvas_item = VisualServer::get_singleton()->canvas_item_create();
  214. VisualServer::get_singleton()->canvas_item_set_parent( q.canvas_item, get_canvas_item() );
  215. VisualServer::get_singleton()->canvas_item_set_transform( q.canvas_item, xform );
  216. q.static_body=Physics2DServer::get_singleton()->body_create(Physics2DServer::BODY_MODE_STATIC);
  217. if (is_inside_scene()) {
  218. xform = get_global_transform() * xform;
  219. RID space = get_world_2d()->get_space();
  220. Physics2DServer::get_singleton()->body_set_space(q.static_body,space);
  221. }
  222. Physics2DServer::get_singleton()->body_set_state(q.static_body,Physics2DServer::BODY_STATE_TRANSFORM,xform);
  223. q.pos=Vector2(p_qk.x,p_qk.y)*quadrant_size*cell_size;
  224. rect_cache_dirty=true;
  225. return quadrant_map.insert(p_qk,q);
  226. }
  227. void TileMap::_erase_quadrant(Map<PosKey,Quadrant>::Element *Q) {
  228. Quadrant &q=Q->get();
  229. Physics2DServer::get_singleton()->free(q.static_body);
  230. VisualServer::get_singleton()->free(q.canvas_item);
  231. if (q.dirty_list.in_list())
  232. dirty_quadrant_list.remove(&q.dirty_list);
  233. quadrant_map.erase(Q);
  234. rect_cache_dirty=true;
  235. }
  236. void TileMap::_make_quadrant_dirty(Map<PosKey,Quadrant>::Element *Q) {
  237. Quadrant &q=Q->get();
  238. if (!q.dirty_list.in_list())
  239. dirty_quadrant_list.add(&q.dirty_list);
  240. if (pending_update)
  241. return;
  242. pending_update=true;
  243. if (!is_inside_scene())
  244. return;
  245. call_deferred("_update_dirty_quadrants");
  246. }
  247. void TileMap::set_cell(int p_x,int p_y,int p_tile,bool p_flip_x,bool p_flip_y) {
  248. PosKey pk(p_x,p_y);
  249. Map<PosKey,Cell>::Element *E=tile_map.find(pk);
  250. if (!E && p_tile==INVALID_CELL)
  251. return; //nothing to do
  252. PosKey qk(p_x/quadrant_size,p_y/quadrant_size);
  253. if (p_tile==INVALID_CELL) {
  254. //erase existing
  255. tile_map.erase(pk);
  256. Map<PosKey,Quadrant>::Element *Q = quadrant_map.find(qk);
  257. ERR_FAIL_COND(!Q);
  258. Quadrant &q=Q->get();
  259. q.cells.erase(pk);
  260. if (q.cells.size()==0)
  261. _erase_quadrant(Q);
  262. else
  263. _make_quadrant_dirty(Q);
  264. return;
  265. }
  266. Map<PosKey,Quadrant>::Element *Q = quadrant_map.find(qk);
  267. if (!E) {
  268. E=tile_map.insert(pk,Cell());
  269. if (!Q)
  270. Q=_create_quadrant(qk);
  271. Quadrant &q=Q->get();
  272. q.cells.insert(pk);
  273. } else {
  274. ERR_FAIL_COND(!Q); // quadrant should exist...
  275. if (E->get().id==p_tile && E->get().flip_h==p_flip_x && E->get().flip_v==p_flip_y)
  276. return; //nothing changed
  277. }
  278. Cell &c = E->get();
  279. c.id=p_tile;
  280. c.flip_h=p_flip_x;
  281. c.flip_v=p_flip_y;
  282. _make_quadrant_dirty(Q);
  283. }
  284. int TileMap::get_cell(int p_x,int p_y) const {
  285. PosKey pk(p_x,p_y);
  286. const Map<PosKey,Cell>::Element *E=tile_map.find(pk);
  287. if (!E)
  288. return INVALID_CELL;
  289. return E->get().id;
  290. }
  291. bool TileMap::is_cell_x_flipped(int p_x,int p_y) const {
  292. PosKey pk(p_x,p_y);
  293. const Map<PosKey,Cell>::Element *E=tile_map.find(pk);
  294. if (!E)
  295. return false;
  296. return E->get().flip_h;
  297. }
  298. bool TileMap::is_cell_y_flipped(int p_x,int p_y) const {
  299. PosKey pk(p_x,p_y);
  300. const Map<PosKey,Cell>::Element *E=tile_map.find(pk);
  301. if (!E)
  302. return false;
  303. return E->get().flip_v;
  304. }
  305. void TileMap::_recreate_quadrants() {
  306. _clear_quadrants();
  307. for (Map<PosKey,Cell>::Element *E=tile_map.front();E;E=E->next()) {
  308. PosKey qk(E->key().x/quadrant_size,E->key().y/quadrant_size);
  309. Map<PosKey,Quadrant>::Element *Q=quadrant_map.find(qk);
  310. if (!Q) {
  311. Q=_create_quadrant(qk);
  312. dirty_quadrant_list.add(&Q->get().dirty_list);
  313. }
  314. Q->get().cells.insert(E->key());
  315. }
  316. }
  317. void TileMap::_clear_quadrants() {
  318. while (quadrant_map.size()) {
  319. _erase_quadrant( quadrant_map.front() );
  320. }
  321. }
  322. void TileMap::clear() {
  323. _clear_quadrants();
  324. tile_map.clear();
  325. }
  326. void TileMap::_set_tile_data(const DVector<int>& p_data) {
  327. int c=p_data.size();
  328. DVector<int>::Read r = p_data.read();
  329. for(int i=0;i<c;i+=2) {
  330. const uint8_t *ptr=(const uint8_t*)&r[i];
  331. uint8_t local[8];
  332. for(int j=0;j<8;j++)
  333. local[j]=ptr[j];
  334. #ifdef BIG_ENDIAN_ENABLED
  335. SWAP(local[0],local[3]);
  336. SWAP(local[1],local[2]);
  337. SWAP(local[4],local[7]);
  338. SWAP(local[5],local[6]);
  339. #endif
  340. int x = decode_uint16(&local[0]);
  341. int y = decode_uint16(&local[2]);
  342. uint32_t v = decode_uint32(&local[4]);
  343. bool flip_h = v&(1<<29);
  344. bool flip_v = v&(1<<30);
  345. v&=(1<<29)-1;
  346. // if (x<-20 || y <-20 || x>4000 || y>4000)
  347. // continue;
  348. set_cell(x,y,v,flip_h,flip_v);
  349. }
  350. }
  351. DVector<int> TileMap::_get_tile_data() const {
  352. DVector<int> data;
  353. data.resize(tile_map.size()*2);
  354. DVector<int>::Write w = data.write();
  355. int idx=0;
  356. for(const Map<PosKey,Cell>::Element *E=tile_map.front();E;E=E->next()) {
  357. uint8_t *ptr = (uint8_t*)&w[idx];
  358. encode_uint16(E->key().x,&ptr[0]);
  359. encode_uint16(E->key().y,&ptr[2]);
  360. uint32_t val = E->get().id;
  361. if (E->get().flip_h)
  362. val|=(1<<29);
  363. if (E->get().flip_v)
  364. val|=(1<<30);
  365. encode_uint32(val,&ptr[4]);
  366. idx+=2;
  367. }
  368. w = DVector<int>::Write();
  369. return data;
  370. }
  371. Rect2 TileMap::get_item_rect() const {
  372. const_cast<TileMap*>(this)->_update_dirty_quadrants();
  373. return rect_cache;
  374. }
  375. void TileMap::_bind_methods() {
  376. ObjectTypeDB::bind_method(_MD("set_tileset","tileset:TileSet"),&TileMap::set_tileset);
  377. ObjectTypeDB::bind_method(_MD("get_tileset:TileSet"),&TileMap::get_tileset);
  378. ObjectTypeDB::bind_method(_MD("set_cell_size","size"),&TileMap::set_cell_size);
  379. ObjectTypeDB::bind_method(_MD("get_cell_size"),&TileMap::get_cell_size);
  380. ObjectTypeDB::bind_method(_MD("set_quadrant_size","size"),&TileMap::set_quadrant_size);
  381. ObjectTypeDB::bind_method(_MD("get_quadrant_size"),&TileMap::get_quadrant_size);
  382. ObjectTypeDB::bind_method(_MD("set_center_x","enable"),&TileMap::set_center_x);
  383. ObjectTypeDB::bind_method(_MD("get_center_x"),&TileMap::get_center_x);
  384. ObjectTypeDB::bind_method(_MD("set_center_y","enable"),&TileMap::set_center_y);
  385. ObjectTypeDB::bind_method(_MD("get_center_y"),&TileMap::get_center_y);
  386. ObjectTypeDB::bind_method(_MD("set_cell","x","y","tile","flip_x","flip_y"),&TileMap::set_cell,DEFVAL(false),DEFVAL(false));
  387. ObjectTypeDB::bind_method(_MD("get_cell","x","y"),&TileMap::get_cell);
  388. ObjectTypeDB::bind_method(_MD("is_cell_x_flipped","x","y"),&TileMap::is_cell_x_flipped);
  389. ObjectTypeDB::bind_method(_MD("is_cell_y_flipped","x","y"),&TileMap::is_cell_y_flipped);
  390. ObjectTypeDB::bind_method(_MD("clear"),&TileMap::clear);
  391. ObjectTypeDB::bind_method(_MD("_clear_quadrants"),&TileMap::_clear_quadrants);
  392. ObjectTypeDB::bind_method(_MD("_recreate_quadrants"),&TileMap::_recreate_quadrants);
  393. ObjectTypeDB::bind_method(_MD("_update_dirty_quadrants"),&TileMap::_update_dirty_quadrants);
  394. ObjectTypeDB::bind_method(_MD("_set_tile_data"),&TileMap::_set_tile_data);
  395. ObjectTypeDB::bind_method(_MD("_get_tile_data"),&TileMap::_get_tile_data);
  396. ADD_PROPERTY( PropertyInfo(Variant::INT,"cell_size",PROPERTY_HINT_RANGE,"1,8192,1"),_SCS("set_cell_size"),_SCS("get_cell_size"));
  397. ADD_PROPERTY( PropertyInfo(Variant::INT,"quadrant_size",PROPERTY_HINT_RANGE,"1,128,1"),_SCS("set_quadrant_size"),_SCS("get_quadrant_size"));
  398. ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"tile_set",PROPERTY_HINT_RESOURCE_TYPE,"TileSet"),_SCS("set_tileset"),_SCS("get_tileset"));
  399. ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"tile_data",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_tile_data"),_SCS("_get_tile_data"));
  400. BIND_CONSTANT( INVALID_CELL );
  401. }
  402. TileMap::TileMap() {
  403. rect_cache_dirty=true;
  404. pending_update=false;
  405. quadrant_size=16;
  406. cell_size=64;
  407. center_x=false;
  408. center_y=false;
  409. fp_adjust=0.4;
  410. fp_adjust=0.4;
  411. }
  412. TileMap::~TileMap() {
  413. clear();
  414. }