Guest User

Untitled

a guest
Jul 15th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. public static void RemoveCableFromNetwork(Cable removedCable)
  2. {
  3. if (GameManager.GameState != GameState.None)
  4. {
  5. CableNetwork oldCableNetwork = removedCable.CableNetwork;
  6. Queue<Cable> checkedCable = new Queue<Cable>(); // leave this empty to optimize the search below... if the first cable finds a neighbor, no sense checking.
  7.  
  8. // start with the removed cables neighbors and we'll recurse from there keeping these as hard barriers
  9. foreach(Cable neighbor in removedCable.ConnectedCables())
  10. {
  11. if (!checkedCable.Contains(neighbor))
  12. {
  13. checkedCable.Enqueue(neighbor);
  14. CableNetwork newCableNetwork = new CableNetwork(neighbor);
  15. Queue<Cable> cableQueue = new Queue<Cable>(neighbor);
  16.  
  17. // start with that and add all the neighbors as we walk through.
  18. while(cableQueue.Count > 0)
  19. {
  20. Cable thisCable = cableQueue.Dequeue();
  21. foreach (Cable thatCableNeighbor in thisCable.ConnectedCables())
  22. {
  23. if (thatCableNeighbor != removedCable && !checkedCable.Contains(thatCableNeighbor) && !newCableNetwork.CableList.Contains(thatCableNeighbor))
  24. {
  25. newCableNetwork.AddCable(thatCableNeighbor);
  26. oldCableNetwork.RemoveCable(thatCableNeighbor);
  27. checkedCable.Enqueue(thatCableNeighbor);
  28. cableQueue.Enqueue(thatCableNeighbor);
  29. }
  30. }
  31. foreach (Device device in thisCable.ConnectedDevices())
  32. {
  33. oldCableNetwork.RemoveDevice(thisCable, device);
  34. }
  35. }
  36. }
  37. }
  38. if (CableNetwork.OnNetworkChanged != null)
  39. {
  40. CableNetwork.OnNetworkChanged();
  41. }
  42. // Is this needed?
  43. // foreach (Device device in removedCable.ConnectedDevices())
  44. // {
  45. // oldCableNetwork.RemoveDevice(thisCable, device);
  46. // }
  47. oldCableNetwork.RemoveCable(removedCable);
  48. }
  49. }
Add Comment
Please, Sign In to add comment