Ball.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // Ball.cpp
  3. // RedCore2
  4. //
  5. // Created by Gabriel Capella on 31/05/17.
  6. //
  7. //
  8. #include "Ball.h"
  9. #include "params.h"
  10. #include "SimpleAudioEngine.h"
  11. USING_NS_CC;
  12. // on "init" you need to initialize your instance
  13. bool Ball::init() {
  14. auto ball_draw = DrawNode::create();
  15. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE, Color4F(COLOR_grey));
  16. this->addChild(ball_draw);
  17. // ParticleSun* m_emitter = ParticleSun::create();
  18. // m_emitter->setPosition(Vec2(0, 0));
  19. // this->addChild(m_emitter);
  20. //
  21. auto material = PHYSICSBODY_MATERIAL_DEFAULT;
  22. material.density = 0.0f;
  23. material.restitution = 1.001f;
  24. material.friction = 0.0f; //set friction here
  25. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE, material);
  26. physicsBody->setGravityEnable(false);
  27. physicsBody->setVelocity(Vec2(0,0));
  28. physicsBody->setLinearDamping(0.0);
  29. //physicsBody->setRotationEnable(false);
  30. physicsBody->setMass(1000.0f);
  31. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  32. this->addComponent(physicsBody);
  33. this->setTag(BALL_TAG);
  34. auto contactListener = EventListenerPhysicsContact::create();
  35. contactListener->onContactBegin = CC_CALLBACK_1(Ball::onContactBegin, this);
  36. _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
  37. return true;
  38. }
  39. bool Ball::onContactBegin(PhysicsContact& contact) {
  40. auto nodeA = contact.getShapeA()->getBody()->getNode();
  41. auto nodeB = contact.getShapeB()->getBody()->getNode();
  42. if (nodeA && nodeB && nodeA->getTag() != nodeB->getTag()) {
  43. CCLOG("%d %d", nodeB->getTag(), nodeA->getTag());
  44. if (nodeA->getTag() == BLOCK_TAG) {
  45. nodeA->removeFromParentAndCleanup(true);
  46. } else if (nodeB->getTag() == BLOCK_TAG) {
  47. nodeB->removeFromParentAndCleanup(true);
  48. } else if (nodeA->getTag() == BOTTOM_TAG && nodeB->getTag() == BALL_TAG) {
  49. ParticleSun* m_emitter = ParticleSun::create();
  50. m_emitter->setPosition(nodeB->getPosition());
  51. m_emitter->setDuration(1);
  52. this->getScene()->addChild(m_emitter);
  53. nodeB->removeFromParentAndCleanup(true);
  54. } else if (nodeB->getTag() == BOTTOM_TAG && nodeA->getTag() == BALL_TAG) {
  55. ParticleSun* m_emitter = ParticleSun::create();
  56. m_emitter->setPosition(nodeA->getPosition());
  57. m_emitter->setDuration(1);
  58. this->getScene()->addChild(m_emitter);
  59. nodeA->removeFromParentAndCleanup(true);
  60. }
  61. }
  62. //bodies can collide
  63. return true;
  64. }
  65. void Ball::throwBall () {
  66. float ang = CC_DEGREES_TO_RADIANS(rand_minus1_1() * THROW_ANG);
  67. float x = sinf(ang) * INITIAL_SPEED;
  68. float y = cosf(ang) * INITIAL_SPEED;
  69. this->getPhysicsBody()->setVelocity(Vec2(x, y));
  70. }