1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- //
- // Ball.cpp
- // RedCore2
- //
- // Created by Gabriel Capella on 31/05/17.
- //
- //
- #include "Ball.h"
- #include "params.h"
- #include "SimpleAudioEngine.h"
- USING_NS_CC;
- // on "init" you need to initialize your instance
- bool Ball::init() {
- auto ball_draw = DrawNode::create();
- ball_draw->drawDot(Vec2(0, 0), BALL_SIZE, Color4F(COLOR_grey));
- this->addChild(ball_draw);
-
- // ParticleSun* m_emitter = ParticleSun::create();
- // m_emitter->setPosition(Vec2(0, 0));
- // this->addChild(m_emitter);
- //
-
- auto material = PHYSICSBODY_MATERIAL_DEFAULT;
- material.density = 0.0f;
- material.restitution = 1.001f;
- material.friction = 0.0f; //set friction here
-
- auto physicsBody = PhysicsBody::createCircle(BALL_SIZE, material);
- physicsBody->setGravityEnable(false);
- physicsBody->setVelocity(Vec2(0,0));
- physicsBody->setLinearDamping(0.0);
- //physicsBody->setRotationEnable(false);
- physicsBody->setMass(1000.0f);
-
- physicsBody->setContactTestBitmask(0xFFFFFFFF);
-
- this->addComponent(physicsBody);
- this->setTag(BALL_TAG);
-
- auto contactListener = EventListenerPhysicsContact::create();
- contactListener->onContactBegin = CC_CALLBACK_1(Ball::onContactBegin, this);
- _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
-
- return true;
- }
- bool Ball::onContactBegin(PhysicsContact& contact) {
- auto nodeA = contact.getShapeA()->getBody()->getNode();
- auto nodeB = contact.getShapeB()->getBody()->getNode();
-
- if (nodeA && nodeB && nodeA->getTag() != nodeB->getTag()) {
- CCLOG("%d %d", nodeB->getTag(), nodeA->getTag());
-
- if (nodeA->getTag() == BLOCK_TAG) {
- nodeA->removeFromParentAndCleanup(true);
- } else if (nodeB->getTag() == BLOCK_TAG) {
- nodeB->removeFromParentAndCleanup(true);
- } else if (nodeA->getTag() == BOTTOM_TAG && nodeB->getTag() == BALL_TAG) {
- ParticleSun* m_emitter = ParticleSun::create();
- m_emitter->setPosition(nodeB->getPosition());
- m_emitter->setDuration(1);
- this->getScene()->addChild(m_emitter);
- nodeB->removeFromParentAndCleanup(true);
- } else if (nodeB->getTag() == BOTTOM_TAG && nodeA->getTag() == BALL_TAG) {
- ParticleSun* m_emitter = ParticleSun::create();
- m_emitter->setPosition(nodeA->getPosition());
- m_emitter->setDuration(1);
- this->getScene()->addChild(m_emitter);
- nodeA->removeFromParentAndCleanup(true);
- }
- }
-
- //bodies can collide
- return true;
- }
- void Ball::throwBall () {
- float ang = CC_DEGREES_TO_RADIANS(rand_minus1_1() * THROW_ANG);
- float x = sinf(ang) * INITIAL_SPEED;
- float y = cosf(ang) * INITIAL_SPEED;
- this->getPhysicsBody()->setVelocity(Vec2(x, y));
- }
|