//
//  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);
    
    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));
}
