Advertisement
djgaven588

CoreFinder

Apr 20th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GridInfo : MonoBehaviour{
  6.  
  7.     public List<BuildableObject> objects;
  8.     public BuildableObject core;
  9.     public Queue<BuildableObject> invalidCubes = new Queue<BuildableObject>();
  10.  
  11.     public void Update()
  12.     {
  13.         ConnectionPointCheck();
  14.     }
  15.  
  16.     void OnGizmosDraw()
  17.     {
  18.         Gizmos.color = Color.red;
  19.         foreach (BuildableObject item in invalidCubes)
  20.         {
  21.             Gizmos.DrawWireCube(item.transform.position, Vector3.one * item.GetComponentInChildren<Collider>().bounds.max.magnitude);
  22.         }
  23.         Gizmos.color = Color.blue;
  24.         foreach (BuildableObject item in objects)
  25.         {
  26.             Gizmos.DrawWireCube(item.transform.position, Vector3.one * item.GetComponentInChildren<Collider>().bounds.max.magnitude);
  27.         }
  28.     }
  29.  
  30.     public BuildableObject ValidConnection(ConnectionPoint point)
  31.     {
  32.         for(int i = 0; i<objects.Count; i++)
  33.         {
  34.             for (int p = 0; p < objects[i].connectionPoints.Length; p++)
  35.             {
  36.                 if(Vector3.Distance(point.GetGlobalPosition(), objects[i].connectionPoints[p].GetGlobalPosition()) < 0.05f)
  37.                 {
  38.                     if(objects[i].connectionPoints[p] != point)
  39.                         return objects[i];
  40.                 }
  41.             }
  42.         }
  43.         return null;
  44.     }
  45.  
  46.     public void AddObject(BuildableObject _object)
  47.     {
  48.         objects.Add(_object);
  49.         //ConnectionPointCheck();
  50.     }
  51.  
  52.     public void RemoveObject(BuildableObject _object)
  53.     {
  54.         objects.Remove(_object);
  55.     }
  56.  
  57.     public void ConnectionPointCheck()
  58.     {
  59.         Queue<BuildableObject> invalidObjects = new Queue<BuildableObject>(objects);
  60.         HashSet<BuildableObject> uncheckedObjects = new HashSet<BuildableObject>();
  61.         Queue<ConnectionPoint> possibleConnections;
  62.         invalidCubes = new Queue<BuildableObject>();
  63.         while(invalidObjects.Count > 0)
  64.         {
  65.             BuildableObject currentObject = invalidObjects.Dequeue();
  66.             bool coreFound = false;
  67.             //int remainingConnections = 0;
  68.  
  69.             if(currentObject == core)
  70.             {
  71.                 //remainingConnections = 0;
  72.                 possibleConnections = null;
  73.                 coreFound = true;
  74.                 possibleConnections = new Queue<ConnectionPoint>();
  75.             }
  76.             else
  77.             {
  78.                 uncheckedObjects = new HashSet<BuildableObject>(objects);
  79.                 uncheckedObjects.Remove(currentObject);
  80.  
  81.                 FindUsedConnections(currentObject, out possibleConnections);
  82.             }
  83.  
  84.             while(possibleConnections.Count != 0)
  85.             {
  86.                 ConnectionPoint selectedPoint = possibleConnections.Dequeue();
  87.                 //remainingConnections--;
  88.                 BuildableObject foundConnection = ValidConnection(selectedPoint);
  89.                 if(foundConnection != null && coreFound == false)
  90.                 {
  91.                     for (int uncheckIndex = 0; uncheckIndex < uncheckedObjects.Count; uncheckIndex++)
  92.                     {
  93.                         if(uncheckedObjects.Contains(foundConnection))
  94.                         {
  95.                             if(foundConnection != core)
  96.                             {
  97.                                 for(int index=0; index<foundConnection.connectionPoints.Length; index++)
  98.                                 {
  99.                                     if(ValidConnection(foundConnection.connectionPoints[index]) != null)
  100.                                     {
  101.                                         possibleConnections.Enqueue(foundConnection.connectionPoints[index]);
  102.                                         //remainingConnections++;
  103.                                     }
  104.                                 }
  105.                                 uncheckedObjects.Remove(foundConnection);
  106.                             }
  107.                             else
  108.                             {
  109.                                 coreFound = true;
  110.                                 possibleConnections = null;
  111.                                 //remainingConnections = 0;
  112.                             }
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.             if(!coreFound)
  118.             {
  119.                 invalidCubes.Enqueue(currentObject);
  120.             }
  121.         }
  122.     }
  123.  
  124.     public void FindUsedConnections(BuildableObject _object, out Queue<ConnectionPoint> _possibleConnections)
  125.     {
  126.         _possibleConnections = new Queue<ConnectionPoint>();
  127.         for (int c = 0; c < _object.connectionPoints.Length; c++)
  128.         {
  129.             if(ValidConnection(_object.connectionPoints[c]) != null)
  130.             {
  131.                 _possibleConnections.Enqueue(_object.connectionPoints[c]);
  132.             }
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement