BlocksLayer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.1);
  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. //bool* indestructible = new bool[number_of_segments+1];
  41. for (int i = 0; i < number_of_segments; i++) {
  42. points[i] = 2.0*M_PI/number_of_segments * i;
  43. points[i] += rand_minus1_1()*0.9*M_PI/number_of_segments;
  44. }
  45. //std::sort(points, points+number_of_segments);
  46. points[number_of_segments] = points[0] + 2*M_PI;
  47. for (int i = 0; i < number_of_segments; i++) {
  48. auto seg = createSegment(interna, externa, points[i], points[i+1], false);
  49. node->addChild(seg);
  50. }
  51. auto back = DrawNode::create();
  52. back->drawDot(Vec2(), interna+2, Color4F(COLOR_back));
  53. node->addChild(back, 1);
  54. auto rotate = RotateBy::create(1, rand_minus1_1()*30.0);
  55. node->runAction(RepeatForever::create(rotate));
  56. delete [] points;
  57. return node;
  58. }
  59. Node* BlocksLayer::createSegment (double r_internal, double r_externa, double begin, double end, bool especial) {
  60. int i;
  61. double sinV, cosV, step;
  62. auto draw = DrawNode::create();
  63. // borda
  64. begin += 1/r_externa;
  65. end -= 1/r_externa;
  66. int ns_ex_draw; // numeros de segmentos externos do desenho
  67. int ns_in_phy; // numeros de segmentos internos da fisica
  68. int ns_ex_phy; // numeros de segmentos externos do da fisica
  69. ns_ex_draw = (end - begin)*r_externa/10.0;
  70. ns_ex_phy = (end - begin)*r_externa/50.0;
  71. ns_in_phy = (end - begin)*r_internal/50.0;
  72. // Previne segmentos muito errados!
  73. if (ns_ex_draw < 2) ns_ex_draw = 2;
  74. if (ns_ex_phy < 2) ns_ex_phy = 2;
  75. if (ns_in_phy < 2) ns_in_phy = 2;
  76. std::vector<Vec2> p_ex_draw(ns_ex_draw+2); // pontos do arco para desenho
  77. p_ex_draw[ns_ex_draw+1] = Vec2();
  78. std::vector<Vec2> p_physics(ns_in_phy+ns_ex_phy+2); // pontos da fisica
  79. step = (end - begin)/(double)ns_ex_draw;
  80. for (i = 0; i <= ns_ex_draw; i++) {
  81. sinV = sin(begin + step * (double) i) * r_externa;
  82. cosV = cos(begin + step * (double) i) * r_externa;
  83. p_ex_draw[i] = Vec2 (sinV, cosV);
  84. }
  85. step = (end - begin)/(double)ns_ex_phy;
  86. for (i = 0; i <= ns_ex_phy; i++) {
  87. sinV = sin(begin + step * (double) i) * r_externa;
  88. cosV = cos(begin + step * (double) i) * r_externa;
  89. p_physics[i] = Vec2 (sinV, cosV);
  90. }
  91. step = (end - begin)/(double)ns_in_phy;
  92. for (i = 0; i <= ns_in_phy; i++) {
  93. sinV = sin(begin + step * (double) i) * r_internal;
  94. cosV = cos(begin + step * (double) i) * r_internal;
  95. p_physics[ns_ex_phy - i + 1 + ns_in_phy] = Vec2 (sinV, cosV);
  96. }
  97. Color4B color = Color4B(102*(rand_0_1()/5+0.5), 217*(rand_0_1()/5+0.5), 239*(rand_0_1()/5+0.5), 255);
  98. draw->drawSolidPoly(p_ex_draw.data(), ns_ex_draw + 2, Color4F(color));
  99. auto physicsBody = PhysicsBody::createEdgePolygon(p_physics.data(), ns_in_phy+ns_ex_phy+2, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  100. physicsBody->setGravityEnable(false);
  101. physicsBody->setDynamic(false);
  102. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  103. draw->addComponent(physicsBody);
  104. draw->setTag(BLOCK_TAG);
  105. return draw;
  106. }