Board.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "Board.h"
  2. #include "Jewel.h"
  3. Board::Board(): _size(0, 0), _selected(0)
  4. {
  5. }
  6. Vector2 getPos(const Point& p)
  7. {
  8. Vector2 pos;
  9. pos.x = p.x * JewelSize.x;
  10. pos.y = p.y * JewelSize.y;
  11. pos += JewelSize / 2;
  12. return pos;
  13. }
  14. void Board::init(int w, int h)
  15. {
  16. _size.x = w;
  17. _size.y = h;
  18. _view = new Sprite;
  19. _field.resize(_size.x * _size.y);
  20. for (int y = 0; y < _size.y; ++y)
  21. {
  22. for (int x = 0; x < _size.x; ++x)
  23. {
  24. _field[x + y * _size.x].pos = Point(x, y);
  25. }
  26. }
  27. _view->setSize(_size.x * JewelSize.x, _size.y * JewelSize.y);
  28. _view->addEventListener(TouchEvent::TOUCH_DOWN, CLOSURE(this, &Board::touched));
  29. _view->setTouchChildrenEnabled(false);
  30. _view->setCallbackDoUpdate(CLOSURE(this, &Board::update));
  31. }
  32. struct swapData: public Object
  33. {
  34. swapData(space* A, space* B): a(A), b(B) {}
  35. space* a;
  36. space* b;
  37. };
  38. spTween Board::swap(space& a, space& b)
  39. {
  40. spTween tween;
  41. tween = a.jewel->swap(getPos(b.pos));
  42. tween = b.jewel->swap(getPos(a.pos));
  43. std::swap(a.jewel, b.jewel);
  44. tween->setDataObject(new swapData(&a, &b));
  45. return tween;
  46. }
  47. void Board::swapped(Event* event)
  48. {
  49. TweenEvent* te = safeCast<TweenEvent*>(event);
  50. spObject data = te->tween->getDataObject();
  51. swapData* sw = safeCast<swapData*>(data.get());
  52. space& a = *sw->a;
  53. space& b = *sw->b;
  54. std::vector<space*> spaces;
  55. findMatches(spaces, a);
  56. findMatches(spaces, b);
  57. if (spaces.empty())
  58. {
  59. a.jewel->swap(getPos(b.pos));
  60. b.jewel->swap(getPos(a.pos));
  61. std::swap(a.jewel, b.jewel);
  62. }
  63. }
  64. space* Board::getSpace(const Point& pos, bool check)
  65. {
  66. if (pos.x < 0 || pos.x >= _size.x)
  67. return 0;
  68. if (pos.y < 0 || pos.y >= _size.y)
  69. return 0;
  70. space& sp = _field[pos.x + pos.y * _size.x];
  71. if (check)
  72. {
  73. if (!sp.jewel)
  74. return 0;
  75. if (sp.jewel->isExploding())
  76. return 0;
  77. if (sp.jewel->isSwaping())
  78. return 0;
  79. if (sp.jewel->isDead())
  80. return 0;
  81. if (sp.jewel->isFalling())
  82. return 0;
  83. }
  84. return &sp;
  85. }
  86. const Point dirUp(0, -1);
  87. const Point dirDown(0, 1);
  88. const Point dirLeft(-1, 0);
  89. const Point dirRight(1, 0);
  90. void Board::findMatches(std::vector<space*>& spaces, space& sp)
  91. {
  92. std::vector<space*> hor;
  93. findMatches(hor, sp, dirUp);
  94. findMatches(hor, sp, dirDown);
  95. std::vector<space*> ver;
  96. findMatches(ver, sp, dirLeft);
  97. findMatches(ver, sp, dirRight);
  98. if (hor.size() >= 2)
  99. {
  100. spaces.insert(spaces.end(), hor.begin(), hor.end());
  101. }
  102. if (ver.size() >= 2)
  103. {
  104. spaces.insert(spaces.end(), ver.begin(), ver.end());
  105. }
  106. if (!spaces.empty())
  107. spaces.push_back(&sp);
  108. }
  109. void Board::findMatches(std::vector<space*>& spaces, space& sp, const Point& dir)
  110. {
  111. Point pos = sp.pos;
  112. while (true)
  113. {
  114. pos += dir;
  115. space* s = getSpace(pos);
  116. if (!s)
  117. return;
  118. if (sp.jewel->getType() != s->jewel->getType())
  119. return;
  120. spaces.push_back(s);
  121. }
  122. }
  123. void Board::update(const UpdateState& us)
  124. {
  125. for (int y = 0; y < _size.y; ++y)
  126. {
  127. for (int x = 0; x < _size.x; ++x)
  128. {
  129. space* sp = getSpace(Point(x, y));
  130. if (!sp)
  131. continue;
  132. std::vector<space*> spaces;
  133. findMatches(spaces, *sp);
  134. for (size_t i = 0; i < spaces.size(); ++i)
  135. {
  136. space* s = spaces[i];
  137. s->jewel->explode();
  138. }
  139. }
  140. }
  141. for (int y = 0; y < _size.y; ++y)
  142. {
  143. for (int x = 0; x < _size.x; ++x)
  144. {
  145. space& sp = _field[x + y * _size.x];
  146. spJewel jewel = sp.jewel;
  147. if (!jewel)
  148. continue;
  149. if (jewel->isDead())
  150. sp.jewel = 0;
  151. }
  152. }
  153. for (int x = 0; x < _size.x; ++x)
  154. {
  155. float last_y = 0;
  156. for (int y = _size.y - 1; y >= 0; --y)
  157. {
  158. space& sp = _field[x + y * _size.x];
  159. if (!sp.jewel)
  160. {
  161. spJewel fallJewel;
  162. for (int ty = y - 1; ty >= 0; --ty)
  163. {
  164. space* fall = getSpace(Point(x, ty), false);
  165. if (!fall->jewel)
  166. continue;
  167. fallJewel = fall->jewel;
  168. fall->jewel = 0;
  169. break;
  170. }
  171. Vector2 newPos = getPos(sp.pos);
  172. if (fallJewel)
  173. {
  174. OX_ASSERT(fallJewel->isDead() == false);
  175. }
  176. else
  177. {
  178. last_y -= JewelSize.y + 10 + rand() % 65;
  179. fallJewel = new Jewel;
  180. spActor view = fallJewel->getView();
  181. view->setPosition(Vector2(newPos.x, last_y));
  182. view->attachTo(_view);
  183. }
  184. fallJewel->fall(getPos(Point(x, y)));
  185. sp.jewel = fallJewel;
  186. }
  187. }
  188. }
  189. }
  190. void Board::touched(Event* event)
  191. {
  192. TouchEvent* te = safeCast<TouchEvent*>(event);
  193. Vector2 pos = te->localPosition;
  194. //log::messageln("%d - %d: %.2f, %.2f", event->target->getObjectID(), event->currentTarget->getObjectID(), pos.x, pos.y);
  195. Point spacePos;
  196. spacePos.x = (int)(pos.x / JewelSize.x);
  197. spacePos.y = (int)(pos.y / JewelSize.y);
  198. space* sp = getSpace(spacePos);
  199. if (_selected)
  200. {
  201. _selected->jewel->unselect();
  202. if (sp)
  203. {
  204. Point dir = _selected->pos - sp->pos;
  205. if (dir.x == 0 && abs(dir.y) == 1 ||
  206. dir.y == 0 && abs(dir.x) == 1)
  207. {
  208. spTween tween = swap(*_selected, *sp);
  209. tween->setDoneCallback(CLOSURE(this, &Board::swapped));
  210. }
  211. }
  212. _selected = 0;
  213. }
  214. else
  215. {
  216. _selected = sp;
  217. if (sp)
  218. sp->jewel->select();
  219. }
  220. }
  221. spActor Board::getView()
  222. {
  223. return _view;
  224. }
  225. void Board::free()
  226. {
  227. _field.clear();
  228. _view->detach();
  229. _view = 0;
  230. _current = 0;
  231. }