/*
文件名:AssetManager.nut
路径:User/Asset/AssetManager.nut
创建日期:2024-11-24	08:44
文件用途: 资源管理器
*/
class _AssetManager_ {

    //角色列表
    CharacterList = null;
    //角色信息表
    CharacterInfoList = null;
    //地图列表
    MapList = null;
    //装备列表
    EquipmentList = null;

    function InitMapList() {
        MapList = ScriptData.GetFileData("map/map.lst", function(DataTable, Data) {
            while (!Data.Eof()) {
                local Key = Data.Get();
                DataTable.rawset(Key, Data.Get());
            }
            if (_DEBUG_) print("加载地图List完成, 共" + DataTable.len() + "个");
        });
    }

    function InitCharacter() {
        CharacterList = ScriptData.GetFileData("character/character.lst", function(DataTable, Data) {
            local dirpath = DataTable.filepath.slice(0, DataTable.filepath.lastfind("/") + 1);
            while (!Data.Eof()) {
                local Index = Data.Get();
                local Path = Data.Get();
                DataTable.rawset(Index, dirpath + Path.tolower());
            }
            if (_DEBUG_) print("加载角色List完成, 共" + DataTable.len() + "个");
        });
        CharacterInfoList = [];
        foreach(Index, Path in CharacterList) {
            if (Index == "filepath") continue;
            local Info = ScriptData.GetFileData(Path, function(DataTable, Data) {
                local dirpath = DataTable.filepath.slice(0, DataTable.filepath.lastfind("/") + 1);
                while (!Data.Eof()) {
                    local Key = Data.Get();
                    if (Key == "[job]") {
                        DataTable.job <- Data.Get().slice(1, -1);
                    } else if (Key == "[growtype name]") {
                        DataTable.growtype <- array(6, {});
                        for (local i = 0; i< 5; i++) {
                            local name = Data.Get();
                            DataTable.growtype[i].name <- name;
                        }
                    }
                    //基础属性
                    else if (Key == "[HP MAX]]" || Key == "[MP MAX]]" || Key == "[physical attack]]" || Key == "[physical defense]]" || Key == "[magical attack]]" || Key == "[magical defense]]" || Key == "[inventory limit]]" || Key == "[MP regen speed]]" || Key == "[move speed]]" || Key == "[attack speed]]" || Key == "[cast speed]]" || Key == "[hit recovery]]" || Key == "[jump power]]" || Key == "[weight]]" || Key == "[jump speed]]") {
                        local RealKey = Key.slice(1, Key.len() - 1);
                        DataTable[RealKey] <- Data.Get().tofloat();

                    }
                    //基础Ani
                    else if (Key == "[waiting motion]" || Key == "[move motion]" || Key == "[sit motion]" || Key == "[damage motion 1]" || Key == "[damage motion 2]" || Key == "[down motion]" || Key == "[overturn motion]" || Key == "[jump motion]" || Key == "[jumpattack motion]" || Key == "[rest motion]" || Key == "[throw motion 1-1]" || Key == "[throw motion 1-2]" || Key == "[throw motion 2-1]" || Key == "[throw motion 2-2]" || Key == "[throw motion 3-1]" || Key == "[throw motion 3-2]" || Key == "[throw motion 4-1]" || Key == "[throw motion 4-2]" || Key == "[dash motion]" || Key == "[dashattack motion]" || Key == "[getitem motion]" || Key == "[buff motion]" || Key == "[simple rest motion]" || Key == "[simple move motion]" || Key == "[back motion]") {
                        local RealKey = Key.slice(1, Key.len() - 1);
                        DataTable[RealKey] <- dirpath + Data.Get().tolower();
                    }
                    //普攻Ani
                    else if (Key == "[attack motion]") {
                        DataTable.attack_motion <- [];
                        while (true) {
                            local Ret = Data.Get();
                            if (Ret == "[/attack motion]") break;
                            DataTable.attack_motion.append(dirpath + Ret.tolower());
                        }
                    }
                    //进阶Ani
                    else if (Key == "[etc motion]") {
                        DataTable.etc_motion <- [];
                        while (true) {
                            local Ret = Data.Get();
                            if (Ret == "[/etc motion]") break;
                            DataTable.etc_motion.append(dirpath + Ret.tolower());
                        }
                    }
                    //基础Atk
                    else if (Key == "[jumpattack info]" || Key == "[dashattack info]") {
                        local RealKey = Key.slice(1, Key.len() - 1);
                        DataTable[RealKey] <- (dirpath + Data.Get().tolower());
                    }
                    //普攻Atk
                    else if (Key == "[attack info]") {
                        DataTable.attack_info <- [];
                        while (true) {
                            local Ret = Data.Get();
                            if (Ret == "[/attack info]") break;
                            DataTable.attack_info.append(dirpath + Ret.tolower());
                        }
                    }
                    //进阶Atk
                    else if (Key == "[etc attack info]") {
                        DataTable.etc_attack_info <- [];
                        while (true) {
                            local Ret = Data.Get();
                            if (Ret == "[/etc attack info]") break;
                            DataTable.etc_attack_info.append(dirpath + Ret.tolower());
                        }
                    }
                }
                // print(DataTable);
                if (_DEBUG_) print("初始化角色" + DataTable.job);
            });
            CharacterInfoList.push(Info);
        }
    }

    function InitEquipmentList() {
        EquipmentList = ScriptData.GetFileData("equipment/equipment.lst", function(DataTable, Data) {
            while (!Data.Eof()) {
                local Key = Data.Get();
                DataTable.rawset(Key, Data.Get());
            }
            if (_DEBUG_) print("加载装备List完成, 共" + DataTable.len() + "个");
        });
    }

    constructor() {
        //初始化地图列表
        InitMapList();
        //初始化角色
        InitCharacter();
        //初始化装备列表
        InitEquipmentList();



        getroottable().AssetManager <- this;
    }


    //Public::

    //获取装备信息
    function GetEquipment(Idx) {
        //如果没有这件装备则返回
        if (!(EquipmentList.rawin(Idx))) return;
        local Path = EquipmentList[Idx];
        local m_data = ScriptData.GetFileData("equipment/" + Path, function(DataTable, Data) {
            DataTable.DirPath <- DataTable.filepath.slice(0, DataTable.filepath.lastfind("/") + 1);
            while (!Data.Eof()) {
                local Pack = Data.Get();
                //名称 grade 套装Id
                if (Pack == "[name]" || Pack == "[part set index]" || Pack == "[grade]") {
                    local RealKey = Pack.slice(1, -1);
                    DataTable[RealKey] <- Data.Get();
                }
                //适用角色
                else if (Pack == "[usable job]") {
                    DataTable.usable_job <- [];
                    while (true) {
                        local Ret = Data.Get();
                        if (Ret == "[/usable job]") break;
                        DataTable.usable_job.append(Ret.slice(1, -1).tolower());
                    }
                }
                //图标
                else if (Pack == "[icon]") {
                    DataTable.icon <- {};
                    local Ret = Data.Get();
                    DataTable.icon.path <- Ret.tolower();
                    Ret = Data.Get();
                    DataTable.icon.index <- Ret.tointeger();
                }
                //装备类型
                else if (Pack == "[equipment type]") {
                    DataTable.type <- {};
                    local Ret = Data.Get();
                    DataTable.type.path <- Ret.tolower().slice(1, -1);
                    Ret = Data.Get();
                    DataTable.type.index <- Ret.tointeger();
                }
                //Ani
                else if (Pack == "[animation job]") {
                    local Job = Data.Get().slice(1, -1);
                    DataTable["Ani_" + Job] <- {};
                    Data.Get();

                    local Index1 = Data.Get();
                    local Index2 = Data.Get();
                    DataTable["Ani_" + Job].variation <- [Index1, Index2];
                    DataTable["Ani_" + Job].layer_variation <- [];

                    while (true) {
                        local Ret = Data.Get();
                        if (Ret == "[animation job]" || (endswith(Ret, "]") && Ret != "[equipment ani script]" && Ret != "[layer variation]")) {
                            Data.Pos--;
                            break;
                        } else if (Ret == "[layer variation]") {
                            local InfoBuf = {};
                            InfoBuf.Zorder <- Data.Get();
                            InfoBuf.Path <- Data.Get();
                            DataTable["Ani_" + Job].layer_variation.append(InfoBuf);
                        }
                    }
                }
            }
        });
        return m_data;
    }
}