1
0

Paddle.cpp 4.9 KB

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