import { EConfigConst } from "../cfg/ConfigDef";
import { BulletItem } from "../cfg/bulletItem";
import { ConfigMgr } from "../cfg/configMgr";
import { Utils } from "../common/utils";
import { Barrage } from "../nest/barrage";
import { GameLogic } from "../update/GameLogic";
import { GameSysLogic } from "../update/gameSysLogic";

enum EBulletPlayState{
    none,
    start,
    done,
}

export class BulletPlay extends GameLogic{
    private _bulletId: string;
    private _delay: number;
    private _state = EBulletPlayState.none;
    private _cd: number;
    private _nextTime: number;

    monsterZooid: number;

    init(bulletId: string, delay: number){
        this._bulletId = bulletId;
        this._delay = delay;
        let bulletData = ConfigMgr.inst.getCfgClassById(EConfigConst.bullet, bulletId, BulletItem);

        this._cd = bulletData.data.cd;
        this._nextTime = Date.now();
    }

    start(){
        this._state = EBulletPlayState.start;
    }

    protected gameUpdate(dt: any): void {
        if(this._state != EBulletPlayState.start) return;
        this._delay -= dt;
        if(this._delay > 0) return;

        let now = Date.now();
        if(now > this._nextTime){
            this._nextTime = now + this._cd * 1000;
            // 攻击
            Barrage.inst.createMonsterBulletByZooid(this.monsterZooid, this._bulletId);
        }
    }
}