import { _decorator, Component, instantiate, Node, Sprite, Vec3 } from 'cc';
import { ModulerBase } from '../../GameFrameWork/ModulerBase';
import { resMgr } from '../../../Frames/ResourcesMgr';
import { Enemy } from './Enemy';
import { LevelData, RoleData } from '../../../DataItem/ItemData';
import { dataMgr } from '../../../Frames/DataManager';
import { GameInfo } from '../../../GameInfo';
import { GameMgr } from '../../GameFrameWork/GameMgr';
import { TouchGame } from '../TouchGame';
import { Tools } from '../../Tools/Tools';
import { Top } from './Top';
const { ccclass, property } = _decorator;

@ccclass('EnemyMgr')
export class EnemyMgr extends ModulerBase {
    private _lvDts: LevelData[] = [];
    private _lvDt: LevelData = null;
    private _roleDatas: RoleData[] = [];
    
    private _hight: number = 0;

    private _top: Top = null;
    //地图背景
    private _bg: Node = null;

    protected onLoad(): void {
        this._bg = this.node.parent.getChildByName("Bg");
        this._lvDts = dataMgr.getAllDataByName("LevelDt");
        this._hight = GameMgr.Instance.getModuler(TouchGame)._hight;
        this._roleDatas = dataMgr.getAllDataByName("RoleCardData");
        this._top = GameMgr.Instance.getModuler(Top);
    }
    init() {
        //第几关的数据
        this._lvDt = this._lvDts[GameInfo.Instance.getCurlv() - 1];
        this._createEnemy();
        this.schedule(() => {
            this._createEnemy();
        }, 10)
        //设置敌方防御塔血量
        this._top.enemyTotalHp = this._lvDt.enemyTowerHp;
        //设置背景
        this._bg.getComponent(Sprite).spriteFrame = resMgr.getSpriteFrame(this._lvDt.bgName);
    }

    private _createEnemy() {
        const enemy: Node = instantiate(resMgr.getPrefab("Enemy"));
        enemy.parent = this.node;
        const enemyTS = enemy.getComponent(Enemy);
        const imgNameArray = this._lvDt.imgName;
        if (imgNameArray && imgNameArray.length > 0) {
            //随机获取图片名
            const name = imgNameArray[Tools.randomNumber(imgNameArray.length)];
            //0 1 2 3 4
            const y = 180 + (Tools.randomNumber(5) + 1) * this._hight - this._hight / 2;

            enemyTS.init(name, new Vec3(1230, y, 0), this._roleDatas, -1);
        } else {
            Error("imgNameArray is null");
        }
    }
}


