import { _decorator, Component, Node, game, director, LabelComponent } from 'cc';
import { GameModel } from '../../models/GameModel';
import { GameController } from './GameController';
const { ccclass, property } = _decorator;

@ccclass('KillCountController')
export class KillCountController extends Component {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;

    @property({
        type:LabelComponent
    })
    killCountText:LabelComponent=null;

    @property({
        type:Node
    })
    killCountGroup:Node=null;
    
    private lastTime:number;
    start () {
        // Your initialization goes here.
        this.lastTime=director.getCurrentTime();
        this.RefrechText();
    }

    AddCount(value:number):void{
        this.lastTime=director.getCurrentTime();
        GameModel.single.killCount++;
        this.RefrechText();
    }

    Clear():void{

    }
    
    update (deltaTime: number) {
        if(GameController.single.GameOver){
            return;
        }
        if(GameModel.single.killCount>0){
            let currentTime:number=director.getCurrentTime();
            if(currentTime-this.lastTime>5000){
                GameModel.single.killCount--;
                this.lastTime=currentTime;
                this.RefrechText();
            }
        }
    }

    RefrechText():void{
        let count:number;
        if(GameModel.single.killCount>999){
            count=999;
        }else{
            count=GameModel.single.killCount;
        }
        this.killCountText.string=count.toString();
        if(count==0){
            this.killCountGroup.active=false;
        }else{
            this.killCountGroup.active=true;
        }
    }
}
