IndieGame/client/Assets/Scripts/Map/Logic/GridMapMananger.cs
DOBEST\zhaoyingjie f242607587 初始化工程
2024-10-11 10:12:15 +08:00

82 lines
2.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Ether
{
public class GridMapMananger : SingletonAutoMono<GridMapMananger>
{
private const string mapDataPath = "Config/MapDta";
[Header("地图信息")]
public List<MapData_SO> mapDataList = new List<MapData_SO>();
//场景名字+坐标和对应的瓦片信息
private Dictionary<string, TileDetails> tileDetailsDict = new Dictionary<string, TileDetails>();
public override void Init()
{
mapDataList = Resources.LoadAll<MapData_SO>(mapDataPath).ToList();
foreach (var mapData in mapDataList)
{
InitTileDetailsDict(mapData);
}
}
/// <summary>
/// 根据地图信息生成字典
/// </summary>
/// <param name="mapData">地图信息</param>
private void InitTileDetailsDict(MapData_SO mapData)
{
foreach (TileProperty tileProperty in mapData.tileProperties)
{
TileDetails tileDetails = new TileDetails
{
gridX = tileProperty.tileCoordinate.x,
gridY = tileProperty.tileCoordinate.y
};
//字典的Key
string key = tileDetails.gridX + "x" + tileDetails.gridY + "y" + mapData.sceneName;
if (GetTileDetails(key) != null)
{
tileDetails = GetTileDetails(key);
}
switch (tileProperty.gridType)
{
case GridType.Obstacle:
tileDetails.isObstacle = tileProperty.boolTypeValue;
break;
}
if (GetTileDetails(key) != null)
tileDetailsDict[key] = tileDetails;
else
tileDetailsDict.Add(key, tileDetails);
}
}
/// <summary>
/// 根据key返回瓦片信息
/// </summary>
/// <param name="key">x+y+地图名字</param>
/// <returns></returns>
private TileDetails GetTileDetails(string key)
{
if (tileDetailsDict.ContainsKey(key))
{
return tileDetailsDict[key];
}
return null;
}
}
}