Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GridInfo : MonoBehaviour{
- public List<BuildableObject> objects;
- public BuildableObject core;
- public Queue<BuildableObject> invalidCubes = new Queue<BuildableObject>();
- public void Update()
- {
- ConnectionPointCheck();
- }
- void OnGizmosDraw()
- {
- Gizmos.color = Color.red;
- foreach (BuildableObject item in invalidCubes)
- {
- Gizmos.DrawWireCube(item.transform.position, Vector3.one * item.GetComponentInChildren<Collider>().bounds.max.magnitude);
- }
- Gizmos.color = Color.blue;
- foreach (BuildableObject item in objects)
- {
- Gizmos.DrawWireCube(item.transform.position, Vector3.one * item.GetComponentInChildren<Collider>().bounds.max.magnitude);
- }
- }
- public BuildableObject ValidConnection(ConnectionPoint point)
- {
- for(int i = 0; i<objects.Count; i++)
- {
- for (int p = 0; p < objects[i].connectionPoints.Length; p++)
- {
- if(Vector3.Distance(point.GetGlobalPosition(), objects[i].connectionPoints[p].GetGlobalPosition()) < 0.05f)
- {
- if(objects[i].connectionPoints[p] != point)
- return objects[i];
- }
- }
- }
- return null;
- }
- public void AddObject(BuildableObject _object)
- {
- objects.Add(_object);
- //ConnectionPointCheck();
- }
- public void RemoveObject(BuildableObject _object)
- {
- objects.Remove(_object);
- }
- public void ConnectionPointCheck()
- {
- Queue<BuildableObject> invalidObjects = new Queue<BuildableObject>(objects);
- HashSet<BuildableObject> uncheckedObjects = new HashSet<BuildableObject>();
- Queue<ConnectionPoint> possibleConnections;
- invalidCubes = new Queue<BuildableObject>();
- while(invalidObjects.Count > 0)
- {
- BuildableObject currentObject = invalidObjects.Dequeue();
- bool coreFound = false;
- //int remainingConnections = 0;
- if(currentObject == core)
- {
- //remainingConnections = 0;
- possibleConnections = null;
- coreFound = true;
- possibleConnections = new Queue<ConnectionPoint>();
- }
- else
- {
- uncheckedObjects = new HashSet<BuildableObject>(objects);
- uncheckedObjects.Remove(currentObject);
- FindUsedConnections(currentObject, out possibleConnections);
- }
- while(possibleConnections.Count != 0)
- {
- ConnectionPoint selectedPoint = possibleConnections.Dequeue();
- //remainingConnections--;
- BuildableObject foundConnection = ValidConnection(selectedPoint);
- if(foundConnection != null && coreFound == false)
- {
- for (int uncheckIndex = 0; uncheckIndex < uncheckedObjects.Count; uncheckIndex++)
- {
- if(uncheckedObjects.Contains(foundConnection))
- {
- if(foundConnection != core)
- {
- for(int index=0; index<foundConnection.connectionPoints.Length; index++)
- {
- if(ValidConnection(foundConnection.connectionPoints[index]) != null)
- {
- possibleConnections.Enqueue(foundConnection.connectionPoints[index]);
- //remainingConnections++;
- }
- }
- uncheckedObjects.Remove(foundConnection);
- }
- else
- {
- coreFound = true;
- possibleConnections = null;
- //remainingConnections = 0;
- }
- }
- }
- }
- }
- if(!coreFound)
- {
- invalidCubes.Enqueue(currentObject);
- }
- }
- }
- public void FindUsedConnections(BuildableObject _object, out Queue<ConnectionPoint> _possibleConnections)
- {
- _possibleConnections = new Queue<ConnectionPoint>();
- for (int c = 0; c < _object.connectionPoints.Length; c++)
- {
- if(ValidConnection(_object.connectionPoints[c]) != null)
- {
- _possibleConnections.Enqueue(_object.connectionPoints[c]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement