Example #1
0
    // ----------------------------私有方法---------------------------------

    /// <summary>
    /// 按照ID加载文件, 并且将加载文件缓存
    /// </summary>
    /// <param name="mapId">被加载地图DI</param>
    /// <param name="mapCenter">地图中心点</param>
    /// <param name="unitWidth">单位宽度</param>
    /// <returns>被加载地图内容, 如果不存在返回null</returns>
    private MapBase GetMapInfo(int mapId, Vector3 mapCenter, int unitWidth)
    {
        MapBase result = null;

        if (!isLoaded)
        {
            // 加载文件
            mapDataDic = Utils.DepartFileData(Utils.LoadFileRotate(MapDataFilePath));

            if (mapDataDic == null)
            {
                Debug.LogError("加载失败");
            }
            else
            {
                isLoaded = true;
                mapWHDic = Utils.GetDateWH(mapDataDic);
            }
        }
        if (mapId > 0)
        {
            // 加载地图数据
            var whVector = mapWHDic[Utils.GetMapFileNameById(mapId, 1)];
            result = new MapBase((int)whVector.x, (int)whVector.y, mapCenter, unitWidth);
            for (var level = 1; level <= LoadMapLevelCount; level++)
            {
                // 从缓存中查找, 如果缓存中不存在, 则从文件中加载
                var key = Utils.GetMapFileNameById(mapId, level);
                if (mapDataDic != null && mapDataDic.ContainsKey(key))
                {
                    var mapData = mapDataDic[key];

                    // 添加地图单元数组
                    var mapCellArray = GetCells(mapData, (UnitType)level, result);
                    result.AddMapCellArray(mapCellArray, mapData, level);
                }
                else
                {
                    Debug.LogError("地图不存在 ID:" + mapId);
                }
            }
            // 添加对应的怪层

            //var monsterMapData = Utils.GetEmptyIntArray(result.MapHeight, result.MapWidth);
            //var monsterMapCellArray = GetCells(monsterMapData, UnitType.Tower);
            //result.AddMapCellArray(monsterMapCellArray, monsterMapData, MapPlayerLayer);

            // 添加对应的Tower层
            var towerMapData      = Utils.GetEmptyIntArray(result.MapHeight, result.MapWidth);
            var towerMapCellArray = GetCells(towerMapData, UnitType.Tower);
            result.AddMapCellArray(towerMapCellArray, towerMapData, MapPlayerLayer);
        }

        return(result);
    }