Exemple #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]);
            }
        }
    }
Exemple #2
0
        public void b000_BuildTreeTest()
        {
            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

            {
                int[] correctResult = new int[] {
                    2, 1, 0, 3
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(2));
                Assert.AreEqual(4, tree.GetSize());

                int cursor = 0;
                tree.DepthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }

            {
                int[] correctResult = new int[] {
                    4, 5, 6, 7, 8, 9
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(4));

                int cursor = 0;
                tree.DepthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }

            {
                int[] correctResult = new int[] {
                    4, 5, 6, 7, 8, 9
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(4));

                int cursor = 0;
                tree.BreadthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }

            {
                int[] correctResult = new int[] {
                    5, 4, 6, 7, 8, 9
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(5));

                int cursor = 0;
                tree.DepthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }

            {
                int[] correctResult = new int[] {
                    5, 4, 6, 7, 8, 9
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(5));

                int cursor = 0;
                tree.BreadthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }

            {
                int[] correctResult = new int[] {
                    6, 4, 5, 7, 8, 9
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(6));

                int cursor = 0;
                tree.DepthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }
            {
                int[] correctResult = new int[] {
                    6, 4, 7, 8, 9, 5
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(6));

                int cursor = 0;
                tree.BreadthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }

            {
                int[] correctResult = new int[] {
                    7, 6, 4, 5, 8, 9
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(7));

                int cursor = 0;
                tree.DepthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }

            {
                int[] correctResult = new int[] {
                    7, 6, 4, 8, 9, 5
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(7));

                int cursor = 0;
                tree.BreadthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }

            {
                int[] correctResult = new int[] {
                    8, 6, 4, 5, 7, 9
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(8));

                int cursor = 0;
                tree.DepthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }

            {
                int[] correctResult = new int[] {
                    8, 6, 4, 7, 9, 5
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(8));

                int cursor = 0;
                tree.BreadthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }

            {
                int[] correctResult = new int[] {
                    9, 6, 4, 5, 7, 8
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(9));

                int cursor = 0;
                tree.DepthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }

            {
                int[] correctResult = new int[] {
                    9, 6, 4, 7, 8, 5
                };

                TreePath tree = graphPath.BuildTree(graphPath.GetNodeById(9));
                Assert.AreEqual(6, tree.GetSize());

                int cursor = 0;
                tree.BreadthFirstTraversal((TreePathNode node) => {
                    Assert.AreEqual(correctResult[cursor++], node._id);
                });
            }
        }