룸 노드가 하나의 부모를 갖고 복도와 방이 정상적으로 연결되도록 여러 유효성 검사를 실시하도록 스크립트를 작성한다.
public class RoomNodeGraphSO : ScriptableObject
{
/// <summary>
/// Get room node by room nodeID
/// </summary>
public RoomNodeSO GetRoomNode(string roomNodeID)
{
if (roomNodeDictionary.TryGetValue(roomNodeID, out RoomNodeSO roomNodeSO))
{
return roomNodeSO;
}
return null;
}
}
public class RoomNodeSO : ScriptableObject
{
public bool IsChildRoomValid(string childID)
{
bool isConnectedBossNodeAlready = false;
// Check if there is there already a connected boss room in the node graph
foreach (RoomNodeSO roomNode in roomNodeGraph.roomNodeList)
{
if (roomNode.roomNodeType.isBossRoom && roomNode.parentRoomNodeIDList.Count > 0)
isConnectedBossNodeAlready = true;
}
// If the child node has a type of boss room and there is already a connected boss room node then return false
if (roomNodeGraph.GetRoomNode(childID).roomNodeType.isBossRoom && isConnectedBossNodeAlready)
return false;
// If the child node has a type of none then return false
if (roomNodeGraph.GetRoomNode(childID).roomNodeType.isNone)
return false;
// If the node already has a child with this child ID return false
if (childRoomNodeIDList.Contains(childID))
return false;
// If this node ID and the child ID are the same return false
if (id == childID)
return false;
// If this childID is already in the parentID list return false
if (parentRoomNodeIDList.Contains(childID))
return false;
// If the child node already has a parent return false
if (roomNodeGraph.GetRoomNode(childID).parentRoomNodeIDList.Count > 0)
return false;
// If child is a corridor and this node is a corridor return false
if (roomNodeGraph.GetRoomNode(childID).roomNodeType.isCorridor && roomNodeType.isCorridor)
return false;
// If child is not a corridor and this node is not a corridor return false
if (!roomNodeGraph.GetRoomNode(childID).roomNodeType.isCorridor && !roomNodeType.isCorridor)
return false;
// If adding a corridor check that this node has < the maximum permitted child corridrs
if (roomNodeGraph.GetRoomNode(childID).roomNodeType.isCorridor && childRoomNodeIDList.Count >= Settings.maxChildCorridors)
return false;
// If the child room is an entrance return false - the entrance must always be the top level parent node
if (roomNodeGraph.GetRoomNode(childID).roomNodeType.isEntrance)
return false;
// If adding a room to a corridor check that this corridor node doesn't already have a room added
if (!roomNodeGraph.GetRoomNode(childID).roomNodeType.isCorridor && childRoomNodeIDList.Count > 0)
return false;
return true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Settings
{
#region ROOM SETTINGS
public const int maxChildCorridors = 3; // Max number number of child corridors leading from a room - maximum should be 3 although this is not recommended
// since it can cause the dungeon building to fail since the rooms are more likely to not fit togther
#endregion
}
이제 여러 유효성 검사가 정상적으로 이루어지는지 살펴보자.
입구와 방, 방과 방 사이에는 연결선이 이루어지지 않는다.
복도를 연결하면 정상적으로 선이 연결된다.
하나의 복도에서 세 개 이상의 방으로 이어질 수 없다.
하지만 두 개 이상의 복도로는 연결이 가능하다.
이렇게 여러 유효성 검사가 적용되며 노드 그래프를 생성할 수 있다.