import { FFAttr } from "../data/FFCalAttr";
import FF from "./FF";
import FMap from "./map/FMap";
import FSprite from "./object/FSprite";
import MSprite from "./object/MSprite";
import PSprite from "./object/PSprite";

/**
 * 载入战斗数据
 */
export default class FLoad{
    /**
     * 
     * @param mapId 载入地图数据
     * @param callback 
     */
    public static loadMap(ff:FF,stageData,callback:(fmap:FMap)=>void){
        // cc.log('loadMap stageData = ',stageData);
        cc.resources.load('prefab/map/'+stageData.mapId, cc.Prefab, (err, prefab:cc.Prefab) =>{
            if(err){
                cc.error(err);
            }else{
                //加载结束
                let node:cc.Node = cc.instantiate(prefab);
                let fmap:FMap = node.getComponent(FMap);

                ff.mLietMap.loadMap(stageData.mapId)

                callback(fmap);
            }
        } );
    }

    /**
     * 载入角色列表
     * @param fDatas 
     * @param callback 
     */
    public static loadRole(ff:FF,fDatas:Array<FFAttr>,callback:(sprites:Array<FSprite>)=>void){
        if(fDatas.length <= 0){
            callback([]);
            return;
        }
        let paths = [];
        for (let i = 0; i < fDatas.length; i++) {
            const element = fDatas[i];
            paths.push(element.path);
        }
        cc.resources.load(paths, cc.Prefab, (err, prefas:any) =>{
            if(err){
                cc.error(err);
            }else{
                let player = ff.main.player
                let ss = [];
                for (let i = 0; i < fDatas.length; i++) {
                // for (let i = 0; i < 1; i++) {
                    const prefab = prefas[i];
                    let node:cc.Node = cc.instantiate(prefab);
                    let sp = null;
                    let data = fDatas[i]
                    // if(data.id != 2){
                    //     continue
                    // }
                    // player.role.leader = 2
                    if(data.id == player.role.leader){//此处需要根据用户设置
                        sp = node.addComponent(MSprite);
                    }else{
                        sp = node.addComponent(PSprite);
                    }
                    sp.ff = ff
                    sp.setAttrData(fDatas[i]);
                    sp.hp = sp.attrData.hp
                    ss.push(sp);
                }
                callback(ss);
        } });
    }

}
