import Global from "./Global";
import SoundManager from "./SoundManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Ball extends cc.Component {

    private rigidbody: cc.RigidBody;

    private isHitBottom: boolean = false;

    private timer: number = 0;
    private speedupTimes: number = 0;
    private currSpeed: number = Global.BALL_SPEED;
    private collider: cc.PhysicsCircleCollider;
    private sprite: cc.Sprite = null;
    private hitWallCounter = 0;

    private manager = null;

    onLoad() {
        this.rigidbody = this.getComponent(cc.RigidBody);
        this.collider = this.getComponent('cc.PhysicsCircleCollider');
        //this.ballAudio = this.getComponent(cc.AudioSource);
        this.sprite = this.getComponent(cc.Sprite);
    }

    reuse(manager) {
        this.manager = manager;
    }

    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.Collider, otherCollider: cc.Collider) {
        //保持速度
        this.remainSpeed();
        if (otherCollider.tag == Global.TAG_WALL_BOTTOM) {//球到底了
            this.isHitBottom = true;
            this.rigidbody.linearVelocity = cc.v2();

            this.node.runAction(cc.callFunc(this.recycle, this));
        } else if (otherCollider.tag == Global.TAG_WALL || otherCollider.tag == Global.TAG_Obstacle) {
            this.countHitWall();
        } else {
            this.hitWallCounter = 0;
        }

        SoundManager.playBall();
    }

    countHitWall() {
        this.hitWallCounter += 1;//
        if (this.hitWallCounter > 10) {//一直在墙中反弹，添加向下的力，避免无限反弹
            let currVelocity = this.rigidbody.linearVelocity;
            currVelocity.addSelf(cc.v2(Math.random() * 10 - 5, Math.random() * 10 - 5));
            this.rigidbody.linearVelocity = currVelocity;
            //this._hitWallCounter = 0;
        }
    }

    updateSkin(spriteFrame: cc.SpriteFrame) {
        this.sprite.spriteFrame = spriteFrame;
    }

    remainSpeed() {
        this.rigidbody.linearVelocity = this.rigidbody.linearVelocity.normalize().mul(this.currSpeed);
    }

    disablePhysics() {
        this.rigidbody.linearVelocity = cc.v2();
        if (this.collider) {
            this.collider.sensor = true;
            this.collider.apply();
        }
    }

    enablePhysics() {
        if (this.collider && this.collider.sensor) {
            this.collider.sensor = false;
            this.collider.apply();
        }
    }

    recycle() {
        this.manager.recycleBall(this);
    }

    fire(direction: cc.Vec2) {
        this.node.stopAllActions();
        this.timer = 0;
        this.speedupTimes = 0;
        this.isHitBottom = false;
        this.currSpeed = Global.BALL_SPEED;
        this.enablePhysics();
        this.rigidbody.linearVelocity = direction.mul(this.currSpeed);
    }

    update(dt) {
        //cc.log("ball alive. pos = " + this.node.position);
        if (this.isHitBottom) {
            return;
        }

        //速度太快会穿透刚体
        if (this.node.position.x > 400 || this.node.position.x < -400 || this.node.position.y > 600 || this.node.position.y < -600) {
            this.recycle();
        }
    }
}
