BlocksLayer.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // BlocksLayer.cpp
  3. // RedCore2
  4. //
  5. // Created by Gabriel Capella on 01/06/17.
  6. //
  7. //
  8. #include "BlocksLayer.h"
  9. #include "params.h"
  10. USING_NS_CC;
  11. void BlocksLayer::setLevel(int level) {
  12. auto visibleSize = Director::getInstance()->getVisibleSize();
  13. Vec2 center = Vec2(visibleSize.width/2, visibleSize.height/2);
  14. int count = 0, i, j;
  15. int circles_n;
  16. double p_indus = 0;
  17. double size = CORE_RADIUS*0.75;
  18. int delta = 15;
  19. for (i = 0; count <= level; i++) {
  20. for (j = 0; count <= level && j <= i; j++) {
  21. if (i + 1 > delta) {
  22. delta *= 1.5;
  23. size /= 1.5;
  24. i = 0;
  25. }
  26. circles_n = i + 1;
  27. p_indus = j * 0.035;
  28. CCLOG("> %d %d", i+1, j);
  29. count++;
  30. }
  31. }
  32. this->setPosition(center);
  33. // aumentar aqui
  34. for (int i = circles_n - 1; i >= 0; i--) {
  35. auto circle = createCircle(size, i, 10, p_indus);
  36. this->addChild(circle, 9);
  37. }
  38. auto physicsBody = PhysicsBody::createCircle(CORE_RADIUS, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  39. physicsBody->setGravityEnable(false);
  40. physicsBody->setVelocity(Vec2(0,0));
  41. physicsBody->setLinearDamping(0);
  42. physicsBody->setDynamic(false);
  43. physicsBody->setGroup(-1);
  44. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  45. //physicsBody->setPositionOffset(center);
  46. auto core = DrawNode::create();
  47. //core->drawCircle(center, CORE_RADIUS, M_PI, 30, false, Color4F(COLOR_blue));
  48. core->drawDot(Vec2(), CORE_RADIUS, COLOR_red);
  49. core->addComponent(physicsBody);
  50. core->setTag(CORE_TAG);
  51. this->addChild(core, 40);
  52. }
  53. // on "init" you need to initialize your instance
  54. bool BlocksLayer::init() {
  55. return true;
  56. }
  57. Node* BlocksLayer::createCircle (double size, int number, int number_of_segments, double p) {
  58. auto node = Node::create();
  59. double interna = number * size + CORE_RADIUS;
  60. double externa = (1 + number) * size + CORE_RADIUS;
  61. double* points = new double[number_of_segments+2];
  62. for (int i = 0; i < number_of_segments; i++) {
  63. points[i] = 2.0*M_PI/number_of_segments * i;
  64. points[i] += rand_minus1_1()*0.9*M_PI/number_of_segments;
  65. }
  66. //std::sort(points, points+number_of_segments);
  67. points[number_of_segments] = points[0] + 2*M_PI;
  68. for (int i = 0; i < number_of_segments; i++) {
  69. auto seg = createSegment(interna, externa, points[i], points[i+1], p >= rand_0_1());
  70. node->addChild(seg);
  71. }
  72. auto back = DrawNode::create();
  73. back->drawDot(Vec2(), interna+2, Color4F(COLOR_back));
  74. node->addChild(back, 1);
  75. auto rotate = RotateBy::create(1, rand_minus1_1()*30.0);
  76. node->runAction(RepeatForever::create(rotate));
  77. delete [] points;
  78. return node;
  79. }
  80. Node* BlocksLayer::createSegment (double r_internal, double r_externa, double begin, double end, bool especial) {
  81. int i;
  82. double sinV, cosV, step;
  83. auto draw = DrawNode::create();
  84. // borda
  85. begin += 1/r_externa;
  86. end -= 1/r_externa;
  87. int ns_ex_draw; // numeros de segmentos externos do desenho
  88. int ns_in_phy; // numeros de segmentos internos da fisica
  89. int ns_ex_phy; // numeros de segmentos externos do da fisica
  90. ns_ex_draw = (end - begin)*sqrt(r_externa)/1.5;
  91. ns_ex_phy = (end - begin)*sqrt(r_externa)/4.0;
  92. ns_in_phy = (end - begin)*sqrt(r_internal)/4.0;
  93. // Previne segmentos muito errados!
  94. if (ns_ex_draw < 2) ns_ex_draw = 2;
  95. if (ns_ex_phy < 2) ns_ex_phy = 2;
  96. if (ns_in_phy < 2) ns_in_phy = 2;
  97. std::vector<Vec2> p_ex_draw(ns_ex_draw+2); // pontos do arco para desenho
  98. p_ex_draw[ns_ex_draw+1] = Vec2();
  99. std::vector<Vec2> p_physics(ns_in_phy+ns_ex_phy+2); // pontos da fisica
  100. step = (end - begin)/(double)ns_ex_draw;
  101. for (i = 0; i <= ns_ex_draw; i++) {
  102. sinV = sin(begin + step * (double) i) * r_externa;
  103. cosV = cos(begin + step * (double) i) * r_externa;
  104. p_ex_draw[i] = Vec2 (sinV, cosV);
  105. }
  106. step = (end - begin)/(double)ns_ex_phy;
  107. for (i = 0; i <= ns_ex_phy; i++) {
  108. sinV = sin(begin + step * (double) i) * r_externa;
  109. cosV = cos(begin + step * (double) i) * r_externa;
  110. p_physics[i] = Vec2 (sinV, cosV);
  111. }
  112. step = (end - begin)/(double)ns_in_phy;
  113. for (i = 0; i <= ns_in_phy; i++) {
  114. sinV = sin(begin + step * (double) i) * r_internal;
  115. cosV = cos(begin + step * (double) i) * r_internal;
  116. p_physics[ns_ex_phy - i + 1 + ns_in_phy] = Vec2 (sinV, cosV);
  117. }
  118. Color4B color;
  119. if (!especial)
  120. color = Color4B(102*(rand_0_1()/5+0.5), 217*(rand_0_1()/5+0.5), 239*(rand_0_1()/5+0.5), 255);
  121. else
  122. color = Color4B(COLOR_orange);
  123. draw->drawSolidPoly(p_ex_draw.data(), ns_ex_draw + 2, Color4F(color));
  124. auto physicsBody = PhysicsBody::createEdgePolygon(p_physics.data(), ns_in_phy+ns_ex_phy+2, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  125. physicsBody->setGravityEnable(false);
  126. physicsBody->setDynamic(false);
  127. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  128. physicsBody->setGroup(-1);
  129. draw->addComponent(physicsBody);
  130. if (!especial)
  131. draw->setTag(BLOCK_TAG);
  132. else
  133. draw->setTag(INDESTRUCTIBLE_BLOCK_TAG);
  134. return draw;
  135. }