BlocksLayer.cpp 4.6 KB

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