import { _decorator, Component, Node, Vec2, director, Vec3 } from 'cc';
import { NoticeManager } from './NoticeManager';
const { ccclass, property } = _decorator;

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

    /**
     * 类型
     */
    type:string;
    /**
     * 开始位置
     */
    startPos:Vec3;
    /**
     * 持续时间
     */
    time:number;
    /**
     * 移动距离
     */
    dis:number;
    /**
     * 数据
     */
    data:any;

    /**
     *  0 纵向 1横向
     */
    moveType:number=0;

    protected startTime:number;
    protected endPos:Vec2;
    start () {
        
    }

    StartMove(data?:any):void{
        if(this.endPos==null){
            this.endPos=new Vec2();
        }
        this.data=data;
        this.startTime=director.getCurrentTime();
        if(this.moveType==0){
            this.endPos.x=this.startPos.x+this.dis;
            this.endPos.y=this.startPos.y;
        }else{
            this.endPos.x=this.startPos.x;
            this.endPos.y=this.startPos.y+this.dis;
        }
        this.OnStartMove();
    }

    protected OnStartMove():void{
        
    }

    update (deltaTime: number) {
        let currentTime:number=director.getCurrentTime();
        let passTime:number=currentTime-this.startTime;
        let disX:number=this.endPos.x-this.startPos.x;
        let disY:number=this.endPos.y-this.startPos.y;
        let value:number=passTime/this.time;
        this.node.setPosition(this.startPos.x+disX*value,this.startPos.y+disY*value,0);
        if(passTime>this.time){
            //结束
            NoticeManager.recycle(this);
        }
    }
}
