import { _decorator, Component, Node, sp, UITransform, Vec2, Vec3 ,Animation, Color, tween} from 'cc';
import { BattleNodeBase } from './BattleNodeBase';
import { RenderPriority } from '../data/BattleEnum';
const { ccclass, property } = _decorator;

// 子弹基类
@ccclass('Bullet')
export class Bullet extends BattleNodeBase {


    @property({ type: sp.Skeleton, tooltip: '骨骼动画' })
    modelSpine: sp.Skeleton = null;

    speedVector: Vec2 = new Vec2(0, 0);

    bStand: boolean = false;

    target: Node = null;

    dieCallback: Function = null;

    protected onLoad(): void {
        this.priority = RenderPriority.Bullet;
        this.modelSpine.setCompleteListener(this.actionComplete.bind(this))

    }

    start() {
    
    }
    //用自定义更新,不用系统更新
    battleUpdate(deltaTime: number) {
        this.node.position = new Vec3(this.node.position.x+this.speedVector.x*deltaTime,this.node.position.y+this.speedVector.y*deltaTime,0)
    }


    resetData(typeID:number,ID:number,pos:Vec3,life:number,lifeMax:number) {
        this.typeID = typeID;
        this.ID = ID;
        this.node.position = pos
        this.bStand = false;
        this.dieCallback = null;

    }
    
    startMove(animationName:string){
        this.node.active = true;
        // this.modelSpine.setAnimation(0, 'idle', true);

    }


    stand() {
        this.bStand = true;
        if(!this.modelSpine.node.active) return;
        this.modelSpine.setAnimation(0, 'idle', true);
        this.modelSpine.color = Color.WHITE;
        // this._attackCallback = null;
    }

   
    clearData() {
        this.node.active = false
    }
    
    die(callback?:Function) {
        
        this.dieCallback = callback;
        tween(this.node).delay(0.5).call(() =>  {
            this.node.active = false
            this.modelSpine.color = Color.WHITE;
            this.dieCallback && this.dieCallback()
        }).start()
    }




    actionComplete(trackEntry:sp.spine.TrackEntry){
        // console.log("动作完成:",trackEntry.animation.name,this._posID)
        // if (trackEntry.animation.name && trackEntry.animation.name.includes("hurt")) {
        //     this._bHurtAnimation = false;
        //     this.modelSpine.addAnimation(0, 'idle', true);
        //     this.modelSpine.color = Color.WHITE;
        // } 
    }

    

}