1
0

BlocksLayer.cpp 5.3 KB

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