import { _decorator, Collider, Component, ICollisionEvent, math, ParticleSystem, RigidBody, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
const velocity = new Vec3();

@ccclass('Ball')
export class Ball extends Component {

    @property(ParticleSystem)
    penEffect:ParticleSystem | null = null;

    rigidBody:RigidBody | null = null;

    /**
     * 球的类型 0 ,我放,1,圈内
     */
    private ballType:number = 0;

    public isDead :boolean = false;

    public isMoving:boolean = false;

    public static collideWithBalls:number = 0;



    /**
     *  增加力使球移动
     */
    private isTorqueMove:boolean = false;


   
    start() {
        this.rigidBody = this.node.getComponent(RigidBody);
        // 监听物理更新事件
        this.getComponent(Collider)!.on('onCollisionEnter', this.onCollisionEnter, this);
        this.isMoving = false;
        this.isDead = false;
        if(this.penEffect){
            this.penEffect.node.active = false;
        }
    }

    playPenEffect(){
        if(this.penEffect){
            this.penEffect.node.active = true;
            this.penEffect.play();
        }
    }

    update(deltaTime: number) {
        switch(this.ballType){
            case 1:
                if(!this.isDead){
                    var pos = this.node.getPosition();
                    if(!this.isPointInCircle( Math.abs(pos.x), Math.abs(pos.z),0,0,6.6)){
                        this.isDead = true;
                    }
                }
                break;  
        }

        if(this.isMoving){
            this.moveEnd();
        }
    }

    /**
     * 给球加一个力度 
     * @param force 
     */
    public AddTorque(force:number){
        
        this.rigidBody = this.node.getComponent(RigidBody);
        if(this.rigidBody){
            this.rigidBody.applyForce(new math.Vec3(0,-1500,force));
            console.log("applyForce",force);
            this.isTorqueMove = true;
            this.schedule(this.ForqueMoveEnd.bind(this), 0.1);
        }
    }



    private ForqueMoveEnd(){
        if(this.isTorqueMove){
            this.moveEnd();
            if(!this.isMoving){
                this.unschedule(this.ForqueMoveEnd.bind(this));
                this.isTorqueMove = false;
            }
        }
    }

    /**
     * 判断点是否在圆内
     * @param x 
     * @param y 
     * @param cx 
     * @param cy 
     * @param r 
     * @returns 
     */
    isPointInCircle(x:number, y:number, cx:number, cy:number, r:number) {  
        // 计算点到圆心的距离的平方  
        const distanceSquared = (x - cx) ** 2 + (y - cy) ** 2;  
        // 比较距离的平方与半径的平方  
        // 如果距离的平方小于等于半径的平方,则点在圆内(包括边界)  
        return distanceSquared <= r * r;  
    }  

    onCollisionEnter(event:ICollisionEvent) {  
       this.moveEnd();
       if(this.ballType == 1){
        this.playPenEffect();
       }
    }

    private moveEnd(){
        if(this.rigidBody){
            // 检查物体的速度是否大于某个阈值  
            this.rigidBody.getLinearVelocity(velocity)

            if(Math.abs(velocity.z)>10){
                this.isMoving = false;
                return;
            }
            if (velocity.x > 0.01 || velocity.x < -0.01 ||  
                velocity.z > 0.01 || velocity.z < -0.01) {  
                if (!this.isMoving) {  
                    this.isMoving = true; // 物体开始移动  
                    // ... 执行相关逻辑 ...  
                    console.log("开始移动");
                }  
            } else {  
                if (this.isMoving) {  
                    this.isMoving = false; // 物体停止移动  
                    console.log("停止移动");
                }  
            }  
        }
    }


    /**
     * 设置球的类型 0 ,我放,1,圈内
     * @param type 
     */
    public setBodyType(type:number){
        this.ballType = type;
    }
}