//리턴값이 널이면 못찾은거 WeightInfo? StartSearch(List<RoomEdge> edgeList, List<RoomEdge> visitedEdgeList, RoomVertex startVertex, RoomVertex endVertex) { WeightInfo weightInfo = new WeightInfo(0, 0); if(startVertex == endVertex) { return weightInfo; } var connectedEdgeList = new List<RoomEdge>(); //연결된 간선을 찾아온다 foreach (var edge in edgeList) { if (edge.A == startVertex || edge.B == startVertex) { connectedEdgeList.Add(edge); } } foreach (var edge in connectedEdgeList) { if(visitedEdgeList.Contains(edge)) { continue; } else { weightInfo.totalDepth = 1; weightInfo.totalWeight = edge.Weight; var anotherVertex = (startVertex == edge.A) ? edge.B : edge.A; visitedEdgeList.Add(edge); if(anotherVertex == endVertex) { return weightInfo; } var foundWeightInfo = StartSearch(edgeList, visitedEdgeList, anotherVertex, endVertex); if (foundWeightInfo != null) { weightInfo.totalDepth += foundWeightInfo.Value.totalDepth; weightInfo.totalWeight += foundWeightInfo.Value.totalWeight; return weightInfo; } } } return null; }
//메인 방들을 연결한 최소 신장 트리를 구하고 보스방과 플레이어방을 선택. List<RoomEdge> GetMST(List<Room> mainRoomList) { List<RoomVertex> roomVertexList = new List<RoomVertex>(); List<RoomEdge> roomEdgeList = new List<RoomEdge>(); //정점을 만듬 for (int i = 0; i < mainRoomList.Count; ++i) { var vertex = new RoomVertex(mainRoomList[i]); roomVertexList.Add(vertex); } //모든 정점을 간선으로 연결 for (int i = 0; i < roomVertexList.Count - 1; ++i) { for (int j = i + 1; j < roomVertexList.Count; ++j) { RoomVertex vertexA = roomVertexList[i]; RoomVertex vertexB = roomVertexList[j]; float weight = (mainRoomList[i].CachedTransform.position - mainRoomList[j].CachedTransform.position).sqrMagnitude; RoomEdge edge = new RoomEdge(vertexA, vertexB, (int)weight); roomEdgeList.Add(edge); } } //그래프로 만든뒤 MST를 구한다. RoomGraph graph = new RoomGraph(roomVertexList, roomEdgeList); var mst = graph.GetMST(); //플레이어 방과 보스방을고름 SelectPlayerRoomAndBossRoom(mst, roomVertexList); return mst; }