//
//  Ball.cpp
//  RedCore
//

#include "Ball.h"
#include "params.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

bool Ball::init() {
    ball_draw = DrawNode::create();
    ball_draw->drawDot(Vec2(0, 0), BALL_SIZE, Color4F(COLOR_grey));
    addChild(ball_draw);
    
    superball_draw = DrawNode::create();
    superball_draw->drawDot(Vec2(0, 0), BALL_SIZE, Color4F(COLOR_orange));
    addChild(superball_draw);
    
    auto material = PHYSICSBODY_MATERIAL_DEFAULT;
    material.density = 0.0f;
    material.restitution = 1.005f;
    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->setMass(1000.0f);
    
    addComponent(physicsBody);
    setTag(BALL_TAG);
    
    resetNormalball();
    
    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));
}


void Ball::setSuperball() {
    superball = true;
    ball_draw->setVisible(false);
    superball_draw->setVisible(true);
}

void Ball::resetNormalball() {
    superball = false;
    ball_draw->setVisible(true);
    superball_draw->setVisible(false);
}

void Ball::setPosition(float x, float y) {
    Node::setPosition(x, y);
    auto physicsBody = getPhysicsBody();
    physicsBody->setContactTestBitmask(0xFFFFFFFF);
    physicsBody->setGroup(-5);
}
