using System.Collections.Generic; using Gameplay; using TGS; using UnityEngine; public partial class Map { private TerrainGridSystem _terrainGridSystem; private MapData _mapData; private int _showInfoMask; private bool _isLegionMap; private Dictionary _terrainTypeIndexDic = new Dictionary(); private Dictionary _legionTerrainTypeIndexDic = new Dictionary(); public enum InfoMask { ShowCells = 1 << 0, ShowTerritories = 1 << 1, ShowAll = ShowCells | ShowTerritories } private Map() { } public MapData MapData => _mapData; public TerrainGridSystem TerrainGridSystem => _terrainGridSystem; public Dictionary TerrainTypeIndexDic => _terrainTypeIndexDic; public Dictionary LegionTerrainTypeIndexDic => _legionTerrainTypeIndexDic; public static Map Create(TerrainGridSystem tgs, MapData data, bool isLegionMap = false) { var map = new Map { _terrainGridSystem = tgs, _mapData = data, _isLegionMap = isLegionMap, }; map.ResetByData(); return map; } public void Destroy() { //Object.DestroyImmediate(GameObject); } public MapBlockData GetBlockData(int cellIndex) { return _mapData.GetBlockData(cellIndex); } public void SetTerrainType(int tgsCellIndex, int terrainTypeIndex, bool isLegionMap = false) { var blockIndex = TGSCellIndex2BlockIndex(tgsCellIndex); _mapData.SetBlockTerrainType(blockIndex, terrainTypeIndex, isLegionMap); } public Vector3 BlockPosition2WorldPosition(Vector2Int blockPosition) { return _terrainGridSystem.CellGetPosition(BlockPosition2TGSCellIndex(blockPosition)); } public int BlockPosition2TGSCellIndex(Vector2Int blockPosition) { return BlockIndex2TGSCellIndex(_mapData.Position2BlockIndex(blockPosition)); } public Vector3 TGSCellIndex2WorldPosition(int cellIndex) { return _terrainGridSystem.CellGetPosition(cellIndex); } public bool WorldPosition2BlockPosition(Vector3 worldPosition, out Vector2Int position) { if (WorldPosition2BlockIndex(worldPosition, out var index)) { position = _mapData.BlockIndex2Position(index); return true; } position = Vector2Int.zero; return false; } public Vector2Int TGSCellIndex2BlockPosition(int cellIndex) { return _mapData.BlockIndex2Position(TGSCellIndex2BlockIndex(cellIndex)); } public Cell GetCellByWorldPos(Vector3 worldPos) { return _terrainGridSystem.CellGetAtPosition(worldPos, true); } public bool WorldPosition2TGSCellIndex(Vector3 worldPosition, out int index) { var cell = _terrainGridSystem.CellGetAtPosition(worldPosition, true); if (cell == null) { index = -1; return false; } index = cell.index; return true; } public bool WorldPosition2BlockIndex(Vector3 worldPosition, out int index) { if (WorldPosition2TGSCellIndex(worldPosition, out var tgsCellIndex)) { index = TGSCellIndex2BlockIndex(tgsCellIndex); return true; } index = -1; return false; } public int BlockIndex2TGSCellIndex(int blockIndex) { return blockIndex; } public int TGSCellIndex2BlockIndex(int tgsCellIndex) { return tgsCellIndex; } public void SetRegionColor(int regionId, bool show, Color color) { if (regionId >= 0) { var region = _mapData.GetRegion(regionId); SetRegionColor(region, show, color); } } public void SetRegionColor(MapData.Region region, bool show, Color color) { SetBlocksColor(region.positions, show, color); } public void SetRegionColor(int regionId, bool show, string RGBAColor) { if (regionId >= 0) { var region = _mapData.GetRegion(regionId); SetBlocksColor(region.positions, show, MapUtils.GetRGBAColor(RGBAColor)); } } public void ShowInfo(int mask) { _showInfoMask = mask; UpdateShowInfo(); } public bool CheckIsShow(InfoMask mask) { return (_showInfoMask & (int)mask) != 0; } public void ShowInfo(InfoMask mask, bool show) { DebugUtil.Log("Map ShowInfo: " + show); if (show) { _showInfoMask = _showInfoMask | (int)mask; } else { _showInfoMask = _showInfoMask & ~(int)mask; } UpdateShowInfo(); } public void SetBlockColor(int cellIndex, bool show, Color color) { if (show) { _terrainGridSystem.CellSetColor(cellIndex, color); } else { _terrainGridSystem.CellSetColor(cellIndex, Color.clear); } } public void SetBlocksColor(List cellIndexs, bool show, Color color) { for (var i = 0; i < cellIndexs.Count; i++) { var cellIndex = cellIndexs[i]; if (show) { _terrainGridSystem.CellSetColor(cellIndex, color); } else { _terrainGridSystem.CellSetColor(cellIndex, Color.clear); } } } private void UpdateShowInfo() { _terrainGridSystem.showCells = (_showInfoMask & (int)InfoMask.ShowCells) != 0; _terrainGridSystem.colorizeTerritories = (_showInfoMask & (int)InfoMask.ShowTerritories) != 0; } private int Region2TGSTerritoryIndex(int regionId) { return regionId; } private void ResetByData() { //GameObject.name = _mapData.name; _terrainGridSystem.transform.position = _mapData.position; _terrainGridSystem.columnCount = _mapData.Width; _terrainGridSystem.rowCount = _mapData.Height; _terrainGridSystem.cellBorderThickness = 0.1f; _terrainGridSystem.TerritoryDestroyAll(); _terrainTypeIndexDic.Clear(); _legionTerrainTypeIndexDic.Clear(); _mapData.showBuffPoints.Clear(); if (!_isLegionMap) { for (var i = 0; i < _mapData.BlocksData.Count; i++) { var blockData = _mapData.BlocksData[i]; if (!blockData.IsDefaultTerrainType()) { UpdateTGSByData(_terrainGridSystem, i, blockData); } _runTimeTerrainProperty.Add(new RuntimeBlockProperty(blockData.GetTerrainTypeProperty())); } } else { for (var i = 0; i < _mapData.BlocksData.Count; i++) { var blockData = _mapData.BlocksData[i]; if (!blockData.IsDefaultLegionTerrainType()) { UpdateTGSByLegionData(_terrainGridSystem, i, blockData); } // TODO 不确定是否需要该属性 // _runTimeTerrainProperty.Add(new RuntimeBlockProperty(blockData.GetTerrainTypeProperty())); } } ShowInfo(0); } private void UpdateTGSByData(TerrainGridSystem tgs, int blockIndex, MapBlockData mapBlockData) { var isError = false; if (mapBlockData.IsDefaultTerrainType()) { isError = true; DebugUtil.LogG($"UpdateTGSByData error, blockIndex: {blockIndex} is default terrain type"); } var cellIndex = BlockIndex2TGSCellIndex(blockIndex); Dictionary terrainTypeProperties = TerrainTypeConfig.DicTerrainTypePropertie; if (terrainTypeProperties == null || !terrainTypeProperties.ContainsKey(mapBlockData.TerrainTypeIndex)) { isError = true; DebugUtil.LogError(" UpdateTGSByData error, blockIndex: {0} terrainTypeIndex: {1}", blockIndex, mapBlockData.TerrainTypeIndex); } var prop = isError ? TerrainTypeConfig.DefaultTerrainTypeProperty : terrainTypeProperties[mapBlockData.TerrainTypeIndex]; // Key: mapBlockData.TerrainTypeIndex Value: tgs.territories.index if (!_terrainTypeIndexDic.ContainsKey(mapBlockData.TerrainTypeIndex)) { var territory = tgs.TerritoryCreate(cellIndex); _terrainTypeIndexDic.Add(mapBlockData.TerrainTypeIndex, _terrainTypeIndexDic.Count); territory.fillColor = prop.Color; } if (!string.IsNullOrEmpty(prop.buffObjectPath)) { _mapData.showBuffPoints.Add(new MapData.ShowBuffData { cellIndex = cellIndex, path = prop.buffObjectPath, }); } tgs.CellSetTerritory(cellIndex, _terrainTypeIndexDic[mapBlockData.TerrainTypeIndex]); // tgs.CellSetCanCross(cellIndex, prop.selectable); // tgs.CellSetCrossLevel(cellIndex, prop.crossLevel); } private void UpdateTGSByLegionData(TerrainGridSystem tgs, int blockIndex, MapBlockData mapBlockData) { var isError = false; if (mapBlockData.IsDefaultLegionTerrainType()) { isError = true; DebugUtil.LogG($"UpdateTGSByLegionData error, blockIndex: {blockIndex} is default terrain type"); } var cellIndex = BlockIndex2TGSCellIndex(blockIndex); var terrainTypeProperties = LegionTerrainTypeConfig.DicTerrainTypePropertie; if (terrainTypeProperties == null || !terrainTypeProperties.ContainsKey(mapBlockData.LegionTypeIndex)) { isError = true; DebugUtil.LogError("UpdateTGSByLegionData error, blockIndex: {0} terrainTypeIndex: {1}", blockIndex, mapBlockData.LegionTypeIndex); } var prop = isError ? LegionTerrainTypeConfig.DefaultTerrainTypeProperty : terrainTypeProperties[mapBlockData.LegionTypeIndex]; // Key: mapBlockData.TerrainTypeIndex Value: tgs.territories.index if (!_legionTerrainTypeIndexDic.ContainsKey(mapBlockData.LegionTypeIndex)) { var territory = tgs.TerritoryCreate(cellIndex); _legionTerrainTypeIndexDic.Add(mapBlockData.LegionTypeIndex, _legionTerrainTypeIndexDic.Count); territory.fillColor = prop.Color; } if (!string.IsNullOrEmpty(prop.buffObjectPath)) { _mapData.showBuffPoints.Add(new MapData.ShowBuffData { cellIndex = cellIndex, path = prop.buffObjectPath, }); } tgs.CellSetTerritory(cellIndex, _legionTerrainTypeIndexDic[mapBlockData.LegionTypeIndex]); } }