Ball.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // Ball.cpp
  3. // RedCore
  4. //
  5. #include "Ball.h"
  6. #include "params.h"
  7. #include "SimpleAudioEngine.h"
  8. USING_NS_CC;
  9. bool Ball::init() {
  10. ball_draw = DrawNode::create();
  11. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE, Color4F(COLOR_grey));
  12. addChild(ball_draw);
  13. superball_draw = DrawNode::create();
  14. superball_draw->drawDot(Vec2(0, 0), BALL_SIZE, Color4F(COLOR_orange));
  15. addChild(superball_draw);
  16. auto material = PHYSICSBODY_MATERIAL_DEFAULT;
  17. material.density = 0.0f;
  18. material.restitution = 1.005f;
  19. material.friction = 0.0f; //set friction here
  20. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE, material);
  21. physicsBody->setGravityEnable(false);
  22. physicsBody->setVelocity(Vec2(0,0));
  23. physicsBody->setLinearDamping(0.0);
  24. physicsBody->setMass(1000.0f);
  25. addComponent(physicsBody);
  26. setTag(BALL_TAG);
  27. resetNormalball();
  28. return true;
  29. }
  30. void Ball::throwBall () {
  31. float ang = CC_DEGREES_TO_RADIANS(rand_minus1_1() * THROW_ANG);
  32. float x = sinf(ang) * INITIAL_SPEED;
  33. float y = cosf(ang) * INITIAL_SPEED;
  34. this->getPhysicsBody()->setVelocity(Vec2(x, y));
  35. }
  36. void Ball::setSuperball() {
  37. superball = true;
  38. ball_draw->setVisible(false);
  39. superball_draw->setVisible(true);
  40. }
  41. void Ball::resetNormalball() {
  42. superball = false;
  43. ball_draw->setVisible(true);
  44. superball_draw->setVisible(false);
  45. }
  46. void Ball::setPosition(float x, float y) {
  47. Node::setPosition(x, y);
  48. auto physicsBody = getPhysicsBody();
  49. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  50. physicsBody->setGroup(-5);
  51. }