1
0

Paddle.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // BlocksLayer.cpp
  3. // RedCore
  4. //
  5. #include "Paddle.h"
  6. #include "../params.h"
  7. USING_NS_CC;
  8. bool Paddle::init() {
  9. // Double size paddle
  10. double_paddle = DrawNode::create();
  11. float py = PADDLE_HEIGHT/2.0;
  12. float pxl = - PADDLE_WIDTH/2;
  13. float pxr = PADDLE_WIDTH/2;
  14. double_paddle->drawSegment(Vec2(pxl*2, py), Vec2(pxr*2, py), py, Color4F(COLOR_grey));
  15. double_paddle->setPositionY(PADDLE_ALTURA);
  16. auto double_size = Size((PADDLE_WIDTH+PADDLE_HEIGHT)*2, PADDLE_HEIGHT);
  17. auto doubleBody = PhysicsBody::createBox(double_size, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  18. doubleBody->setPositionOffset(Vec2(0, PADDLE_HEIGHT/2));
  19. doubleBody->setGravityEnable(false);
  20. doubleBody->setDynamic(false);
  21. doubleBody->setContactTestBitmask(0xFFFFFFFF);
  22. double_paddle->setTag(PADDLE_TAG);
  23. double_paddle->addComponent(doubleBody);
  24. addChild(double_paddle);
  25. // Normal size paddle
  26. normal_paddle = DrawNode::create();
  27. normal_paddle->drawSegment(Vec2(pxl, py), Vec2(pxr, py), py, Color4F(COLOR_grey));
  28. normal_paddle->setPositionY(PADDLE_ALTURA);
  29. auto normal_size = Size(PADDLE_WIDTH+PADDLE_HEIGHT, PADDLE_HEIGHT);
  30. auto normalBody = PhysicsBody::createBox(normal_size, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  31. normalBody->setPositionOffset(Vec2(0, PADDLE_HEIGHT/2));
  32. normalBody->setGravityEnable(false);
  33. normalBody->setDynamic(false);
  34. normalBody->setContactTestBitmask(0xFFFFFFFF);
  35. normal_paddle->setTag(PADDLE_TAG);
  36. normal_paddle->addComponent(normalBody);
  37. addChild(normal_paddle);
  38. // Half size paddle
  39. half_paddle = DrawNode::create();
  40. half_paddle->drawSegment(Vec2(pxl/2.0, py), Vec2(pxr/2.0, py), py, Color4F(COLOR_grey));
  41. half_paddle->setPositionY(PADDLE_ALTURA);
  42. auto half__size = Size((PADDLE_WIDTH+PADDLE_HEIGHT)/2.0, PADDLE_HEIGHT);
  43. auto half_Body = PhysicsBody::createBox(half__size, PhysicsMaterial(0.1f, 1.0f, 0.0f));
  44. half_Body->setPositionOffset(Vec2(0, PADDLE_HEIGHT/2));
  45. half_Body->setGravityEnable(false);
  46. half_Body->setDynamic(false);
  47. half_Body->setContactTestBitmask(0xFFFFFFFF);
  48. half_paddle->setTag(PADDLE_TAG);
  49. half_paddle->addComponent(half_Body);
  50. addChild(half_paddle);
  51. normalSize();
  52. return true;
  53. }
  54. void Paddle::listen(double width) {
  55. // Registra eventos touch
  56. auto listener = EventListenerTouchOneByOne::create();
  57. // trigger when you push down
  58. listener->onTouchBegan = [=](Touch* touch, Event* event){
  59. setPX(touch->getLocation().x, width);
  60. return true; // if you are consuming it
  61. };
  62. // trigger when moving touch
  63. listener->onTouchMoved = [=](Touch* touch, Event* event){
  64. setPX(touch->getLocation().x, width);
  65. };
  66. // Add listener
  67. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  68. }
  69. void Paddle::setPX(double px, double width) {
  70. double paddle_x = PADDLE_WIDTH/2;
  71. switch (stade) {
  72. case HALF:
  73. paddle_x /= 2.0;
  74. break;
  75. case DOUBLE:
  76. paddle_x *= 2.0;
  77. break;
  78. }
  79. if (px >= paddle_x && px < width - paddle_x)
  80. this->setPositionX(px);
  81. }
  82. void Paddle::doubleSize() {
  83. if (stade == HALF) {
  84. normalSize();
  85. return;
  86. }
  87. stade = DOUBLE;
  88. double_paddle->getPhysicsBody()->setEnabled(true);
  89. double_paddle->setVisible(true);
  90. normal_paddle->getPhysicsBody()->setEnabled(false);
  91. normal_paddle->setVisible(false);
  92. half_paddle->getPhysicsBody()->setEnabled(false);
  93. half_paddle->setVisible(false);
  94. startNormalTimer();
  95. }
  96. void Paddle::halfSize() {
  97. if (stade == DOUBLE) {
  98. normalSize();
  99. return;
  100. }
  101. stade = HALF;
  102. double_paddle->getPhysicsBody()->setEnabled(false);
  103. double_paddle->setVisible(false);
  104. normal_paddle->getPhysicsBody()->setEnabled(false);
  105. normal_paddle->setVisible(false);
  106. half_paddle->getPhysicsBody()->setEnabled(true);
  107. half_paddle->setVisible(true);
  108. startNormalTimer();
  109. }
  110. void Paddle::normalSize() {
  111. stade = NORMAL;
  112. double_paddle->getPhysicsBody()->setEnabled(false);
  113. double_paddle->setVisible(false);
  114. normal_paddle->getPhysicsBody()->setEnabled(true);
  115. normal_paddle->setVisible(true);
  116. half_paddle->getPhysicsBody()->setEnabled(false);
  117. half_paddle->setVisible(false);
  118. }
  119. void Paddle::startNormalTimer() {
  120. auto delay = DelayTime::create(POWER_UP_PADDLE_SECONDS);
  121. CallFunc *runCallback = CallFunc::create(CC_CALLBACK_0(Paddle::normalSize, this));
  122. auto seq = Sequence::create(delay, runCallback, nullptr);
  123. runAction(seq);
  124. }