const {ccclass, property} = cc._decorator;

@ccclass
export default class DaoJiShiProgress extends cc.Component {

    @property({
        type:cc.Node,
        displayName: "进度条上面的人物",
    })
    slider: cc.Node = null;

    @property({
        displayName: "总时间s",
    })
    allTimeNum:number = 60

    _dt:number = 0
    isStartTimer:boolean = true//是否开始倒计时
    curTimeNum:number = 0
    progressHeight:number = 0
    finishCallFunc = null//结束回调
    
    start () {
        this.progressHeight = this.node.height
        this.updateProgress()
    }

    update (dt) {
        if(this.isStartTimer){
            this.curTimeNum += dt
            this.updateProgress()
            //倒计时结束
            if(this.curTimeNum >= this.allTimeNum){
                this.curTimeNum = this.allTimeNum
                this.isStartTimer = false
                this.finishCallFunc && this.finishCallFunc()
            }
        }
    }
    //设置开始/结束倒计时
    setIsStartTimer(bStart,finishCallFunc?){
        this.isStartTimer = bStart
        this.finishCallFunc = finishCallFunc
    }
    updateProgress(){
        //未设置倒计时时间
        if(this.allTimeNum <= 0){
            return
        }
        let rate = this.curTimeNum/this.allTimeNum
        this.node.getComponent(cc.ProgressBar).progress = rate
        this.slider.y = rate*this.progressHeight
    }
}