Ejemplo n.º 1
0
    protected virtual void GenerateElectric()
    {
        GraphPath     wireGraph = LevelManager.Instance.GetWireGraph();
        GraphPathNode startNode = wireGraph.GetNodeByEndpoint(transform.position);

        if (startNode != null)
        {
            TreePath       treePath     = wireGraph.BuildTree(startNode);
            TreePathNode   rootNode     = treePath.GetRoot();
            TreePathNode[] childrenNode = rootNode.GetChildren();
            for (int i = 0; i < childrenNode.Length; i++)
            {
                GameObject electricCurrentObj = Instantiate(_electricCurrentPrefab, transform.position, Quaternion.identity) as GameObject;
                electricCurrentObj.name = "*ElectricCurrent-" + startNode._id + "-" + childrenNode[i]._id + "*";
                electricCurrentObj.transform.SetParent(LevelManager.Instance.GetElectricCurrentGameObjectHolder().transform);
                ElectricCurrent electricCurrent = electricCurrentObj.GetComponent <ElectricCurrent>();
                electricCurrent.MoveToNode(childrenNode[i]);
            }
        }
    }
Ejemplo n.º 2
0
    public static GraphPath Build(Wire[] wires)
    {
        GraphPath graphPath = new GraphPath();

        int indexCount = 0;

        for (int i = 0; i < wires.Length; i++)
        {
            Wire    wire = wires[i];
            Vector3 endPoint1, endPoint2;
            wire.GetEndPoints(out endPoint1, out endPoint2);

            if (!graphPath.NodeExist(endPoint1))
            {
                GraphPathNode graphPathNode1 = new GraphPathNode();
                graphPathNode1._id       = indexCount++;
                graphPathNode1._endPoint = endPoint1;

                if (!graphPath.NodeExist(endPoint2))
                {
                    GraphPathNode graphPathNode2 = new GraphPathNode();
                    graphPathNode2._id       = indexCount++;
                    graphPathNode2._endPoint = endPoint2;
                    graphPathNode1._adjacentNodes.Add(graphPathNode2._id);
                    graphPathNode2._adjacentNodes.Add(graphPathNode1._id);

                    graphPath.AddNode(graphPathNode1);
                    graphPath.AddNode(graphPathNode2);
                }
                else
                {
                    // If the node already exist, don't add the node, but update the
                    // adjacent list of the existing node.
                    GraphPathNode existedNode = graphPath.GetNodeByEndpoint(endPoint2);
                    existedNode._adjacentNodes.Add(graphPathNode1._id);
                    graphPathNode1._adjacentNodes.Add(existedNode._id);
                    graphPath.AddNode(graphPathNode1);
                }
            }
            else
            {
                if (!graphPath.NodeExist(endPoint2))
                {
                    // endpoint1 already exist, but endpoint2 doesn't
                    GraphPathNode existedNode    = graphPath.GetNodeByEndpoint(endPoint1);
                    GraphPathNode graphPathNode2 = new GraphPathNode();
                    graphPathNode2._id       = indexCount++;
                    graphPathNode2._endPoint = endPoint2;
                    //graphPathNode2._gameObject = wire.gameObject;
                    graphPathNode2._adjacentNodes.Add(existedNode._id);
                    existedNode._adjacentNodes.Add(graphPathNode2._id);
                    graphPath.AddNode(graphPathNode2);
                }
                else
                {
                    // both endpoint1 and endpoint2 exist, simply connect them
                    GraphPathNode existedNode1 = graphPath.GetNodeByEndpoint(endPoint1);
                    GraphPathNode existedNode2 = graphPath.GetNodeByEndpoint(endPoint2);
                    existedNode1._adjacentNodes.Add(existedNode2._id);
                    existedNode1._adjacentNodes.Add(existedNode1._id);
                }
            }
        }

        return(graphPath);
    }
Ejemplo n.º 3
0
        public void a040_GetNodeByEndpointTest()
        {
            Wire[] wires = PrepareWireArray(8);
            wires[0].transform.position = new Vector3(0.5f, 0, 1);
            wires[0]._direction         = Wire.Direction.XZ_X;
            wires[1].transform.position = new Vector3(1.5f, 0, 1);
            wires[1]._direction         = Wire.Direction.XZ_X;
            wires[2].transform.position = new Vector3(2.5f, 0, 1);
            wires[2]._direction         = Wire.Direction.XZ_X;
            wires[3].transform.position = new Vector3(5.5f, 0, 5);
            wires[3]._direction         = Wire.Direction.XZ_X;
            wires[4].transform.position = new Vector3(5, 0, 5.5f);
            wires[4]._direction         = Wire.Direction.XZ_Z;
            wires[5].transform.position = new Vector3(5, 0, 6.5f);
            wires[5]._direction         = Wire.Direction.XZ_Z;
            wires[6].transform.position = new Vector3(4.5f, 0, 6);
            wires[6]._direction         = Wire.Direction.XZ_X;
            wires[7].transform.position = new Vector3(5.5f, 0, 6);
            wires[7]._direction         = Wire.Direction.XZ_X;

            GraphPath graphPath = GraphPath.Build(wires);

            //          7
            //          |
            //        8-6-9
            //          |
            // 0-1-2-3, 4-5

            Assert.AreEqual(0, graphPath.GetNodeByEndpoint(new Vector3(0, 0, 1))._id);
            Assert.AreEqual(1, graphPath.GetNodeByEndpoint(new Vector3(1, 0, 1))._id);
            Assert.AreEqual(2, graphPath.GetNodeByEndpoint(new Vector3(2, 0, 1))._id);
            Assert.AreEqual(3, graphPath.GetNodeByEndpoint(new Vector3(3, 0, 1))._id);
            Assert.AreEqual(4, graphPath.GetNodeByEndpoint(new Vector3(5, 0, 5))._id);
            Assert.AreEqual(5, graphPath.GetNodeByEndpoint(new Vector3(6, 0, 5))._id);
            Assert.AreEqual(6, graphPath.GetNodeByEndpoint(new Vector3(5, 0, 6))._id);
            Assert.AreEqual(7, graphPath.GetNodeByEndpoint(new Vector3(5, 0, 7))._id);
            Assert.AreEqual(8, graphPath.GetNodeByEndpoint(new Vector3(4, 0, 6))._id);
            Assert.AreEqual(9, graphPath.GetNodeByEndpoint(new Vector3(6, 0, 6))._id);
            Assert.IsNull(graphPath.GetNodeByEndpoint(new Vector3(10, 0, 6)));
            Assert.NotNull(graphPath.GetNodeByEndpoint(new Vector3(5.4f, 0, 7)));
            Assert.IsNull(graphPath.GetNodeByEndpoint(new Vector3(5.6f, 0, 7)));
        }