/*
文件名:MapObject.nut
路径:User/Object/Map/MapObject.nut
创建日期:2024-11-22	19:40
文件用途:地图对象
*/
class Map extends Actor {
    //图层对象Map
    LayerObject = null;

    //地图编号
    m_mapId = 0;
    //数据
    m_data = null;

    //地图长度
    m_length = 0;

    //初始化图层对象
    function InitLayer() {
        //图层ObjMap
        LayerObject = {
            contact = Actor(),
            normal = Actor(),
            bottom = Actor(),
            close = Actor(),
            closeback = Actor(),
            middleback = Actor(),
            distantback = Actor(),
            cover = Actor(),
            max = Actor()
        };
        //把所有图层Obj挂上Map的子对象
        foreach(LayerObj in LayerObject) {
            Addchild(LayerObj);
        }

        local FristOrder = 10000;
        //按照层级给Layer设置层级
        LayerObject.distantback.SetZOrder(FristOrder);
        FristOrder += 50000;
        LayerObject.middleback.SetZOrder(FristOrder);
        FristOrder += 50000;
        LayerObject.bottom.SetZOrder(FristOrder);
        FristOrder += 50000;
        LayerObject.closeback.SetZOrder(FristOrder);
        FristOrder += 50000;
        LayerObject.close.SetZOrder(FristOrder);
        FristOrder += 50000;
        LayerObject.normal.SetZOrder(FristOrder);
        FristOrder += 50000;
        LayerObject.cover.SetZOrder(FristOrder);
        FristOrder += 50000;
        LayerObject.contact.SetZOrder(FristOrder);
        FristOrder += 50000;
        LayerObject.max.SetZOrder(FristOrder);
    };

    //初始化数据
    function InitData(path) {
        m_data = ScriptData.GetFileData(path, function(DataTable, Data) {
            while (!Data.Eof()) {
                local str = Data.Get();
                if (str == "[background pos]") {
                    DataTable.background_pos <- (Data.Get());
                } else if (str == "[map name]") {
                    DataTable.name <- Data.Get();
                } else if (str == "[tile]") {
                    DataTable.tile <- [];
                    while (true) {
                        local tiledata = Data.Get();
                        if (tiledata == "[/tile]") break;
                        DataTable.tile.push(tiledata);
                    }
                } else if (str == "[extended tile]") {
                    DataTable.extended_tile <- [];
                    while (true) {
                        local tiledata = Data.Get();
                        if (tiledata == "[/extended tile]") break;
                        DataTable.extended_tile.push(tiledata);
                    }
                } else if (str == "[far sight scroll]") {
                    DataTable.far_sight_scroll <- (Data.Get());
                } else if (str == "[background animation]") {
                    DataTable.background_animation <- [];
                    while (true) {
                        local anidata = Data.Get();
                        if (anidata == "[/background animation]") break;
                        if (anidata == "[ani info]") {
                            local info = {};
                            Data.Get();
                            info["filename"] <- Data.Get();
                            Data.Get();
                            info["layer"] <- Data.Get().slice(1, -1);
                            Data.Get();
                            info["order"] <- Data.Get();
                            DataTable.background_animation.push(info);
                        }
                    }
                } else if (str == "[sound]") {
                    DataTable.sound <- [];
                    while (true) {
                        local sounddata = Data.Get();
                        if (sounddata == "[/sound]") break;
                        DataTable.sound.push(sounddata);
                    }
                } else if (str == "[animation]") {
                    DataTable.animation <- [];
                    while (true) {
                        local anidata = Data.Get();
                        if (anidata == "[/animation]") break;
                        local info = {};
                        info["filename"] <- anidata;
                        info["layer"] <- Data.Get().slice(1, -1);
                        info["xpos"] <- Data.Get();
                        info["ypos"] <- Data.Get();
                        info["zpos"] <- Data.Get();
                        DataTable.animation.push(info);
                    }
                } else if (str == "[NPC]") {
                    DataTable.npc <- [];
                    while (true) {
                        local npcdata = Data.Get();
                        if (npcdata == "[/NPC]") break;
                        local info = {};
                        info["id"] <- npcdata;
                        info["direction"] <- Data.Get();
                        info["xpos"] <- Data.Get();
                        info["ypos"] <- Data.Get();
                        info["zpos"] <- Data.Get();
                        DataTable.npc.push(info);
                    }
                } else if (str == "[town movable area]") {
                    DataTable.town_movable_area <- [];
                    while (true) {
                        local areadata = Data.Get();
                        if (areadata == "[/town movable area]") break;
                        local info = [];
                        info.push(areadata);
                        for (local i = 0; i< 5; i++) {
                            info.push(Data.Get())
                        }
                        DataTable.town_movable_area.push(info);
                    }
                } else if (str == "[virtual movable area]") {
                    DataTable.virtual_movable_area <- [];
                    while (true) {
                        local areadata = Data.Get();
                        if (areadata == "[/virtual movable area]") break;
                        local info = [];
                        info.push(areadata);
                        for (local i = 0; i< 3; i++) {
                            info.push(Data.Get())
                        }
                        DataTable.virtual_movable_area.push(info);
                    }
                }
            }
        });
        m_data.path <- path;
        m_data.dirpath <- path.slice(0, path.lastfind("/") + 1);
    }

    //初始化地板
    function InitTile() {
        //普通地板
        if ("tile" in m_data) {
            foreach(pos, path in m_data.tile) {
                local realpath = m_data.dirpath + path.tolower();
                local TileObj = Tile(realpath);
                TileObj.SetAnchor(0.0, 0.0);
                TileObj.SetPosition(pos * 224, 0);
                LayerObject.bottom.Addchild(TileObj);
                //计算地图长度
                m_length += 224;
            }
        }
        //补充地板
        if ("extended_tile" in m_data) {
            foreach(pos, path in m_data.extended_tile) {
                local realpath = m_data.dirpath + path.tolower();
                local TileObj = Tile(realpath);
                TileObj.SetAnchor(0.0, 0.0);
                TileObj.SetPosition(pos * 224, 560);
                LayerObject.bottom.Addchild(TileObj);
            }
        }
    }

    //初始化背景动画
    function InitBackgroundAnimation() {
        //背景动画
        if ("background_animation" in m_data) {
            foreach(Info in m_data.background_animation) {
                local realpath = m_data.dirpath + Info.filename.tolower();
                //先构造一个天空 然后通过宽度 和地图宽度得知还需要再构造多少个
                local AniList = [];
                local AniObj = Animation(realpath);
                local width = AniObj.GetSize().w;
                AniList.push(AniObj);
                for (local i = 1; i< width; i++) {
                    local AniObj = Animation(realpath);
                    AniList.push(AniObj);
                }
                foreach(pos, ani in AniList) {
                    ani.SetAnchor(0.0, 0.0);
                    ani.SetPosition(pos * AniObj.GetSize().w, m_data.background_pos);
                    LayerObject[Info.layer].Addchild(ani);
                }
            }
        }
    }

    //初始化场景Ani
    function InitAnimation() {
        foreach(pos, info in m_data.animation) {
            local realpath = m_data.dirpath + info.filename.tolower();
            local AniObj = AnimationObject(realpath);
            AniObj.SetAnchor(0.0, 0.0);
            //注意这里默认要增加120的Y轴偏移
            if (info.layer != "normal") AniObj.SetPositionNoLayer(info.xpos, info.ypos + m_data.background_pos + 120, info.zpos);
            else AniObj.SetPosition(info.xpos, info.ypos + m_data.background_pos + 120, info.zpos);
            LayerObject[info.layer].Addchild(AniObj);
        }
    }

    constructor(arg) {
        base.constructor();

        if (typeof arg == "integer") {

        } else if (typeof arg == "string") {
            InitData(arg);
        }

        //初始化图层
        InitLayer();

        //初始化地板
        InitTile();
        //初始化背景动画
        InitBackgroundAnimation();
        //初始化场景Ani
        InitAnimation();
    }
}