public override void Start() { base.Start(); combatController.SelectEvent += Ready; distanceCalc = GetComponent <DistanceCalc>(); distanceCompare = GetComponent <DistanceCompare>(); transform.localPosition = new Vector3(0, 0, 0); }
// Update is called once per frame void Update() { if (distanceCompare == null) { distanceCompare = this.transform.parent.GetComponentInChildren <DistanceCompare>(); distanceCompare.InsideRange += InsideRange; distanceCompare.OutOfRange += OutOfRange; return; } if (commandMove == null) { commandMove = this.transform.parent.GetComponentInChildren <CommandMove>(); commandMove.RemoveWaypoint += InsideRangeRC; return; } }
public void CheckIndex() { if (compareIndex == 0) { CubeSpawner[] spawners = compareList[compareIndex].GetComponentsInChildren <CubeSpawner>(); foreach (CubeSpawner spawner in spawners) { spawner.startComparison(); } } else if (compareIndex == 1) { ObjectSizeChanger[] spawners = compareList[compareIndex].GetComponentsInChildren <ObjectSizeChanger>(); foreach (ObjectSizeChanger spawner in spawners) { spawner.ChangeSize(); } } else if (compareIndex == 2) { DistanceCompare compare = compareList[compareIndex].GetComponent <DistanceCompare>(); compare.SetupCompare(); } }
// Calculate shortest path(s) using Dijkstra's algorithm. Note that // the matrix contains edge distance between vertexes. A zero value // indicates no path between the verticies. Since zero could be a // concievable distance, we add 1 to all distances such that we can // use zero to indicate no path between the verticies. This means // the actual length is the distance value minus 1. public static uint[,] ShortestPaths(uint[,] graph, uint start, uint end) { // Do some validation. if (graph.Rank != 2) { throw(new InvalidOperationException("graph matrix must be two " + "dimensional")); } if (graph.GetLength(0) != graph.GetLength(1)) { throw(new InvalidOperationException("graph matrix must be square")); } // Create an array to hold the distance to each vertex while calculating // the shortest path. uint[] distance = new uint[graph.GetLength(0)]; for (uint i = 0; i < (uint)distance.Length; ++i) { distance[i] = uint.MaxValue; } // Set the distance of our start vertex to zero. This will cause it to // get sorted to the front of the queue and thus get processed first. distance[start] = 0; // This queue initially contains all the indexes. uint[] queue = new uint[distance.Length]; for (uint i = 0; i < (uint)queue.Length; ++i) { queue[i] = i; } int head = 0; IComparer distanceCompare = new DistanceCompare(distance); // Go until queue is empty. while (head != queue.Length) { // Sort the queue of indexes by distance. Array.Sort(queue, head, queue.Length - head, distanceCompare); // Get the vertex with the shortest distance. uint vertex = queue[head]; // Remove front of queue. head++; // Check each neighbor of vertex and see if we need to update // the distance to that neighbor. for (uint i = 0; i < (uint)graph.GetLength(0); ++i) { // Is this a neighbor? if (graph[vertex, i] > 1) { // If we found a shorter distance then update the distance. if (distance[i] > distance[vertex] + (graph[vertex, i] - 1)) { distance[i] = distance[vertex] + (graph[vertex, i] - 1); } } } } // If no path found then return null. if (distance[end] == uint.MaxValue) { return(null); } else { // Create a return graph with just lengths for the shortest // paths. Initialize all edges to 0 (meaning no path). uint[,] paths = new uint[graph.GetLength(0), graph.GetLength(1)]; for (uint i = 0; i < (uint)graph.GetLength(0); ++i) { for (uint j = 0; j < graph.GetLength(1); ++j) { paths[i, j] = 0; } } // Flags to tell whether we already handled a vertex when // generating the paths (eg. backtracing). bool[] handledVertex = new bool[graph.GetLength(0)]; for (uint i = 0; i < (uint)handledVertex.Length; ++i) { handledVertex[i] = false; } // Vertexes queue used when determining the paths. uint[] vertexes = new uint[graph.GetLength(0)]; vertexes[0] = end; uint cur = 0; uint avail = 1; // Go from end to start along the shortest paths found to // fill in the paths graph. while (cur != avail) { for (uint i = 0; i < (uint)graph.GetLength(0); ++i) { // Rows in the graph indicate outgoing edges from the vertex. // Columns in the graph indicate incoming edges to the vertex. // I'm checking down column vertexes[cur] to see if there // in an incoming edge from vertex i. if (graph[i, vertexes[cur]] > 1) { // If the edge was part of a shortest path, eg. distance // of current vertex is the sum of the distance of the // incoming vertex plus the edge, then the incoming vertex // was part of a shortest path. if (distance[vertexes[cur]] == (distance[i] + (graph[i, vertexes[cur]] - 1))) { // This incoming vertex was part of a shortest path // so copy over its edge from the original graph. paths[i, vertexes[cur]] = graph[i, vertexes[cur]]; // If we didn't handle this incoming vertex already // add it to the queue. if (handledVertex[i] == false) { vertexes[avail++] = i; handledVertex[i] = true; } } } } // Move to next vertex to process. cur++; } return(paths); } }