Skip to content

Use a Specific Socket for the Main Path

  • Goal: We want to force all main path connections to use a specific doorway socket. All Tiles along the main path will have to connect to one-another (and to branch Tiles that start from them), using a DoorwaySocket that we specify.
  • Usage: Attach the script to any GameObject in the scene and assign an appropriate socket to the MainPathSocket field in the inspector.
MainPathConnectionRule.cs
using DunGen;
using UnityEngine;

public class MainPathConnectionRule : MonoBehaviour
{
    public DoorwaySocket MainPathSocket = null;

    private TileConnectionRule rule;


    private void OnEnable()
    {
        rule = new TileConnectionRule(CanTilesConnect);
        DoorwayPairFinder.CustomConnectionRules.Add(rule);
    }

    private void OnDisable()
    {
        DoorwayPairFinder.CustomConnectionRules.Remove(rule);
        rule = null;
    }

    private TileConnectionRule.ConnectionResult CanTilesConnect(ProposedConnection connection)
    {
        bool isOnMainPath = connection.PreviousTile.Placement.IsOnMainPath;

        // We're only interested in main path connections
        if (isOnMainPath)
        {
            // Only allow main path connections if they use the correct socket on both doorways
            bool allowConnection =  connection.PreviousDoorway.Socket == MainPathSocket &&
                                    connection.NextDoorway.Socket == MainPathSocket;

            if (allowConnection)
                return TileConnectionRule.ConnectionResult.Allow;
            else
                return TileConnectionRule.ConnectionResult.Deny;
        }
        // If we're not on the main path, just ignore it
        else
            return TileConnectionRule.ConnectionResult.Passthrough;
    }
}