/// <summary> /// 转换地图数据为地图单位 /// </summary> /// <param name="mapData">地图数据</param> /// <param name="layer">层级</param> /// <param name="mapBase">地图基类</param> /// <returns></returns> private static MapCellBase[,] GetCells(int[][] mapData, UnitType layer, MapBase mapBase = null) { var height = mapData.Length; var width = mapData[0].Length; var mapCellDataArray = new MapCellBase[height, width]; // 遍历内容 for (var i = 0; i < height; i++) { for (var j = 0; j < width; j++) { if (mapData[i][j] > 0) { // 加载模型 var mapCell = UnitFictory.Single.CreateUnit <MapCellBase>(layer, mapData[i][j]); mapCell.X = j; mapCell.Y = i; if (mapBase != null) { mapCell.Draw(mapBase.Leftdown, mapBase.UnitWidth); } //根据数据加载 mapCellDataArray[i, j] = mapCell; } } } return(mapCellDataArray); }
/// <summary> /// 添加地图单元 /// </summary> /// <param name="mapCell"></param> /// <param name="layer"></param> public void AddMapCell([NotNull] MapCellBase mapCell, int layer) { if (!mapDataGroupDic.ContainsKey(layer)) { mapDataGroupDic.Add(layer, new Dictionary <int, List <MapCellBase> >()); } var groupDic = mapDataGroupDic[layer]; // 遍历数据, 添加进分组字典 if (!groupDic.ContainsKey(mapCell.DataId)) { groupDic.Add(mapCell.DataId, new List <MapCellBase>() { mapCell }); } else { groupDic[mapCell.DataId].Add(mapCell); } mapCellArrayDic[layer][mapCell.Y, mapCell.X] = mapCell; mapArrayDic[layer][mapCell.Y][mapCell.X] = mapCell.DataId; NeedDraw = true; }
/// <summary> /// 销毁单元 /// </summary> /// <param name="mapCell">地图单元</param> public void DestoryMapCell(MapCellBase mapCell) { if (mapCell != null) { GameObject.Destroy(mapCell.GameObj); } }
/// <summary> /// 创建单位 /// </summary> /// <param name="unitType">单位类型</param> /// <param name="dataId">数据ID</param> /// <returns>地图单元类</returns> public T CreateUnit <T>(UnitType unitType, int dataId) where T : MapCellBase { // 地图单元与障碍物使用相同单元替换Image的方式进行服用, 来解决创建单位过多问题 MapCellBase result = null; switch (unitType) { // ---------------------------加载数据进行替换模式---------------------------- case UnitType.MapCell: // 地图单元 { result = new MapCell(null, dataId, MapManager.MapBaseLayer); } break; case UnitType.Obstacle: // 障碍物 { var go = GetGameObject(MapCellTableName, dataId, MapDrawer.Single.ItemParentList[MapManager.MapObstacleLayer]); result = new Obstacle(go, dataId, MapManager.MapObstacleLayer); go.name = result.MapCellId.ToString(); go.SetActive(true); } break; // ---------------------------------加载预设模式--------------------------------- case UnitType.FightUnit: // 战斗单位 { var go = GetGameObject(MapCellTableName, dataId, MapDrawer.Single.ItemParentList[MapManager.MapPlayerLayer]); result = new FightUnit(go, dataId, MapManager.MapPlayerLayer); go.name = result.MapCellId.ToString(); go.SetActive(true); } break; case UnitType.NPC: // NPC { var go = GetGameObject(MapCellTableName, dataId, MapDrawer.Single.ItemParentList[MapManager.MapNpcLayer]); // TODO 区分出兵点入兵点 switch (dataId) { case 301: // 出兵点 result = new OutMonsterPoint(go, dataId, MapManager.MapObstacleLayer); break; case 302: // 入兵点 result = new InMonsterPoint(go, dataId, MapManager.MapObstacleLayer); break; case 401: // 塔基 result = new TowerPoint(go, dataId, MapManager.MapObstacleLayer); break; default: result = new Npc(go, dataId, MapManager.MapNpcLayer); break; } go.name = result.MapCellId.ToString(); go.SetActive(true); } break; case UnitType.Tower: { // 创建塔 var go = GetGameObject(MapCellTableName, dataId, MapDrawer.Single.ItemParentList[MapManager.MapNpcLayer]); result = new Tower(go, dataId, MapManager.MapPlayerLayer); // 设置数据 go.name = result.MapCellId.ToString(); go.SetActive(true); } break; case UnitType.TowerCell: { // 创建塔单元 var go = GetGameObject(MapCellTableName, dataId, MapDrawer.Single.ItemParentList[MapManager.MapPlayerLayer]); result = new TheFiveCellBase(go, dataId, MapManager.MapPlayerLayer); // 设置数据 go.name = result.MapCellId.ToString(); go.SetActive(true); } break; } return((T)result); }