Skip to content

Instantly share code, notes, and snippets.

@nschubach
Last active July 16, 2018 03:18
Show Gist options
  • Select an option

  • Save nschubach/ab85026caa58f45f41d8cf6fbc90d217 to your computer and use it in GitHub Desktop.

Select an option

Save nschubach/ab85026caa58f45f41d8cf6fbc90d217 to your computer and use it in GitHub Desktop.
Called With:
List<Cable> list = new List<Cable>(base.ConnectedCables());
if (this.CableNetwork != null)
{
CableNetwork.RemoveCableFromNetwork(this);
}
base.OnDestroy();
foreach (Cable cable in list)
{
CableNetwork.RebuildCableNetwork(cable);
}
public static void RemoveCableFromNetwork(Cable removedCable)
{
if (GameManager.GameState != GameState.None)
{
CableNetwork oldCableNetwork = removedCable.CableNetwork;
HashSet<Cable> checkedCable = new HashSet<Cable>(); // leave this empty to optimize the search below... if the first cable finds a neighbor, no sense checking.
// start with the removed cables neighbors and we'll recurse from there keeping these as hard barriers
foreach(Cable neighbor in removedCable.ConnectedCables())
{
if (!checkedCable.Contains(neighbor))
{
checkedCable.Add(neighbor);
CableNetwork newCableNetwork = new CableNetwork(neighbor);
Queue<Cable> cableQueue = new Queue<Cable>(neighbor);
// start with that and add all the neighbors as we walk through.
while(cableQueue.Count > 0)
{
Cable thisCable = cableQueue.Dequeue();
foreach (Cable thatCableNeighbor in thisCable.ConnectedCables())
{
if (thatCableNeighbor != removedCable && !checkedCable.Contains(thatCableNeighbor) && !newCableNetwork.CableList.Contains(thatCableNeighbor))
{
newCableNetwork.AddCable(thatCableNeighbor);
oldCableNetwork.RemoveCable(thatCableNeighbor);
checkedCable.Add(thatCableNeighbor);
cableQueue.Enqueue(thatCableNeighbor);
}
}
foreach (Device device in thisCable.ConnectedDevices())
{
oldCableNetwork.RemoveDevice(thisCable, device);
}
}
}
}
// Is this needed?
// foreach (Device device in removedCable.ConnectedDevices())
// {
// oldCableNetwork.RemoveDevice(thisCable, device);
// }
oldCableNetwork.RemoveCable(removedCable);
if (CableNetwork.OnNetworkChanged != null)
{
CableNetwork.OnNetworkChanged();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment