Ball.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /********************************************************************
  2. * Nomes: Gabriel Capella Números USP: 8962078
  3. * João Herique Luciano 8535957
  4. *
  5. * Tarefa: RedCore - EP2 MAC0463
  6. * Arquivo: Ball.cpp
  7. * Descrição: Classe para auxiliar na criação da bola no jogo.
  8. ********************************************************************/
  9. #include "Ball.h"
  10. #include "params.h"
  11. #include "SimpleAudioEngine.h"
  12. USING_NS_CC;
  13. bool Ball::init() {
  14. ball_draw = DrawNode::create();
  15. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE, Color4F(COLOR_grey));
  16. addChild(ball_draw);
  17. superball_draw = DrawNode::create();
  18. superball_draw->drawDot(Vec2(0, 0), BALL_SIZE, Color4F(COLOR_orange));
  19. addChild(superball_draw);
  20. auto material = PHYSICSBODY_MATERIAL_DEFAULT;
  21. material.density = 0.0f;
  22. material.restitution = 1.005f;
  23. material.friction = 0.0f;
  24. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE, material);
  25. physicsBody->setGravityEnable(false);
  26. physicsBody->setVelocity(Vec2(0,0));
  27. physicsBody->setLinearDamping(0.0);
  28. physicsBody->setMass(1000.0f);
  29. addComponent(physicsBody);
  30. setTag(BALL_TAG);
  31. resetNormalball();
  32. return true;
  33. }
  34. void Ball::throwBall () {
  35. float ang = CC_DEGREES_TO_RADIANS(rand_minus1_1() * THROW_ANG);
  36. float x = sinf(ang) * INITIAL_SPEED;
  37. float y = cosf(ang) * INITIAL_SPEED;
  38. this->getPhysicsBody()->setVelocity(Vec2(x, y));
  39. }
  40. void Ball::setSuperball() {
  41. superball = true;
  42. ball_draw->setVisible(false);
  43. superball_draw->setVisible(true);
  44. }
  45. void Ball::resetNormalball() {
  46. superball = false;
  47. ball_draw->setVisible(true);
  48. superball_draw->setVisible(false);
  49. }
  50. void Ball::setPosition(float x, float y) {
  51. Node::setPosition(x, y);
  52. auto physicsBody = getPhysicsBody();
  53. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  54. physicsBody->setGroup(-5);
  55. }