BlocksLayer.cpp 4.8 KB

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