BlocksLayer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /********************************************************************
  2. * Nomes: Gabriel Capella Números USP: 8962078
  3. * João Herique Luciano 8535957
  4. *
  5. * Tarefa: RedCore - EP2 MAC0463
  6. * Arquivo: BlocksLayer.cpp
  7. * Descrição: Implementação da classe de construção dos blocos
  8. ********************************************************************/
  9. #include "BlocksLayer.h"
  10. #include "../params.h"
  11. USING_NS_CC;
  12. void BlocksLayer::setLevel(int level) {
  13. auto visibleSize = Director::getInstance()->getVisibleSize();
  14. Vec2 center = Vec2(visibleSize.width/2, visibleSize.height/2);
  15. int count = 0, i, j;
  16. int circles_n;
  17. double p_indus = 0;
  18. double size = CORE_RADIUS*0.75;
  19. int delta = 15;
  20. // Codigo feio para gerar fases aleatorias
  21. for (i = 0; count <= level; i++) {
  22. for (j = 0; count <= level && j <= i; j++) {
  23. if (i + 1 > delta) {
  24. delta *= 1.5;
  25. size /= 1.5;
  26. i = 0;
  27. }
  28. circles_n = i + 1;
  29. p_indus = j * 0.05/delta*8.0;
  30. count++;
  31. }
  32. }
  33. CCLOG(">> %f", p_indus);
  34. this->setPosition(center);
  35. for (int i = circles_n - 1; i >= 0; i--) {
  36. auto circle = createCircle(size, i, 10, p_indus);
  37. this->addChild(circle, 9);
  38. }
  39. auto physicsBody = PhysicsBody::createCircle(CORE_RADIUS, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  40. physicsBody->setGravityEnable(false);
  41. physicsBody->setVelocity(Vec2(0,0));
  42. physicsBody->setLinearDamping(0);
  43. physicsBody->setDynamic(false);
  44. physicsBody->setGroup(-1);
  45. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  46. auto core = DrawNode::create();
  47. core->drawDot(Vec2(), CORE_RADIUS, COLOR_red);
  48. core->addComponent(physicsBody);
  49. core->setTag(CORE_TAG);
  50. this->addChild(core, 40);
  51. }
  52. // on "init" you need to initialize your instance
  53. bool BlocksLayer::init() {
  54. return true;
  55. }
  56. Node* BlocksLayer::createCircle (double size, int number, int number_of_segments, double p) {
  57. auto node = Node::create();
  58. double interna = number * size + CORE_RADIUS;
  59. double externa = (1 + number) * size + CORE_RADIUS;
  60. double* points = new double[number_of_segments+2];
  61. for (int i = 0; i < number_of_segments; i++) {
  62. points[i] = 2.0*M_PI/number_of_segments * i;
  63. points[i] += rand_minus1_1()*0.9*M_PI/number_of_segments;
  64. }
  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. }