import { _decorator, Component, Node, Tween, tween, Vec3 } from 'cc';
import { weiqi_manager } from './weiqi_manager';
const { ccclass, property } = _decorator;

@ccclass('weiqi_item')
export class weiqi_item extends Component {
    private mDur:number = 0
    private old_pos:Vec3 = null
    public init(){
        this.hide()
    }
    public play(duration: number){
        this.mDur = duration

        if(this.node.parent!=null){
            // let dir = this.node.parent.getComponent(weiqi_manager).getDir()
            this.old_pos =this.node.parent.getComponent(weiqi_manager).getOriginPos()
            this.node.position = new Vec3(this.old_pos.x,this.old_pos.y)
            // let nor = pos.subtract(dir).normalize()
            // let ndir = nor.multiplyScalar(dis)
            tween(this.node).delay(duration).call(()=>{
                this.show()
                this.old_pos=null
                this.play(this.mDur)
            })
            // .to(duration,{position:dir}).call(()=>{
            //     this.hide()
            //     this.play(this.mDur)
            // })
            .start()
        }
    }

    hide(){
        this.node.active = false
    }

    show(){
        this.node.active = true
    }

    protected onDestroy(): void {
        Tween.stopAllByTarget(this.node)
    }

    protected update(dt: number): void {
        if(this.old_pos!=null){
            this.node.position = new Vec3(this.node.position.x,this.node.position.y-dt*100)
            if(Vec3.distance(this.node.position,this.old_pos)>150){
                this.hide()
            }
        }
      
    }

}


