Beispiel #1
0
    private void breadthFirstPath(GameObject beginningObject, GameObject endingObject)
    {
        Queue <GameObject> Q = new Queue <GameObject>();
        List <GameObject>  V = new List <GameObject>();


        Q.Enqueue(beginningObject);
        V.Add(beginningObject);

        while (Q.Count > 0)
        {
            GameObject    expandedNode = Q.Dequeue();
            ShipComponent script       = expandedNode.GetComponent("ShipComponent") as ShipComponent;

            if (expandedNode == endingObject)
            {
                GameObject tempNode = endingObject;

                ShipComponent nodeScript = tempNode.GetComponent("ShipComponent") as ShipComponent;


                //IMPORTANT STUFF HERE
                bool canCreateConnection = false;
                if (beginningObject.name.Contains("powersupply") && !endingObject.name.Contains("passthrough"))
                {
                    PowerSupply powerScript = beginningObject.GetComponent("PowerSupply") as PowerSupply;
                    if (powerScript.addNode(endingObject))
                    {
                        nodeScript.powerSupply  = beginningObject;
                        nodeScript._powerLevel += 100;
                        Debug.Log("LINK CREATED");
                        canCreateConnection = true;
                    }
                    else
                    {
                        Debug.Log("THIS NODE IS OVERCAPACITY, CANNOT CREATE LINK");
                    }
                }
                if (canCreateConnection)
                {
                    //ending
                    while (tempNode != beginningObject)
                    {
                        //create connection
                        ShipComponent nodeScript2 = tempNode.GetComponent("ShipComponent") as ShipComponent;

                        Vector2 difference = new Vector2(tempNode.transform.position.x - nodeScript2.parent.transform.position.x,
                                                         tempNode.transform.position.y - nodeScript2.parent.transform.position.y);


                        Vector2 midpoint = new Vector2((tempNode.transform.position.x + nodeScript2.parent.transform.position.x) / 2,
                                                       (tempNode.transform.position.y + nodeScript2.parent.transform.position.y) / 2);


                        GameObject connection = (GameObject)Instantiate(graphicConnectionPrefab,
                                                                        new Vector3(midpoint.x, midpoint.y, 0),
                                                                        Quaternion.identity);

                        CircuitConnection circuitConnection = connection.GetComponent("CircuitConnection") as CircuitConnection;


                        float angle = Mathf.Atan2(difference.y, difference.x);


                        angle = angle * Mathf.Rad2Deg + 90;

                        connection.transform.position    = new Vector3(midpoint.x, midpoint.y, 0);
                        connection.transform.eulerAngles = new Vector3(0.0f, 0.0f, angle);
                        connection.transform.localScale  = new Vector3(2.0f, 1.0f, 1.0f);

                        circuitConnection.addConnectedPiece(tempNode);
                        circuitConnection.addConnectedPiece(nodeScript2.parent);
                        circuitConnection.timeStamp = timestamp;

                        nodeScript2._numConnections++;
                        ShipComponent anotherTemp = nodeScript2.parent.GetComponent("ShipComponent") as ShipComponent;
                        anotherTemp._numConnections++;

                        circuitConnections.Add(connection);
                        tempNode = nodeScript2.parent;
                    }
                    timestamp++;
                }
            }

            //get neighbors
            List <GameObject> neighbors = getNeighborsOfNode(script.index_Row, script.index_Column);
            //Debug.Log ("Count: " + neighbors.Count + " index_row: " + script.index_Row + ", index_col: " + script.index_Column);
            foreach (GameObject neighbor in neighbors)
            {
                //Debug.Log ("Nothing");
                if (!V.Contains(neighbor) && neighbor != null)
                {
                    ShipComponent neighborScript = neighbor.GetComponent("ShipComponent") as ShipComponent;
                    neighborScript.parent = expandedNode;
                    V.Add(neighbor);
                    Q.Enqueue(neighbor);
                }
            }
        }
    }