using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RopeAttachPoint : MonoBehaviour {
    public bool port = true;
    public List<RopeAttachPoint> next;
    public List<RopeAttachPoint> prev;
    public List<GameObject> GetConnections() {
        return GetEndsRecursive( this, new List<GameObject>(), new List<GameObject>() );
    }
    private List<GameObject> GetEndsRecursive( RopeAttachPoint node, List<GameObject> objs, List<GameObject> mem ) {
        if ( node.port ) {
            objs.Add( node.gameObject );
        }
        for (int i=0;i<node.next.Count;i++ ) {
            RopeAttachPoint rap = node.next[i];
            if ( rap == null ) {
                node.next.Remove( rap );
                i--;
                continue;
            }
            if ( mem.Contains(rap.gameObject) ) {
                continue;
            }
            mem.Add( rap.gameObject );
            GetEndsRecursive( rap, objs, mem );
        }
        for (int i=0;i<node.prev.Count;i++ ) {
            RopeAttachPoint rap = node.prev[i];
            if ( rap == null ) {
                node.prev.Remove( rap );
                i--;
                continue;
            }
            if ( mem.Contains(rap.gameObject) ) {
                continue;
            }
            mem.Add( rap.gameObject );
            GetEndsRecursive( rap, objs, mem );
        }
        return objs;
    }
}
