Пример #1
0
    void InitializeCarNodes()
    {
        // Fill with nodes
        // We add one to the width so the road goes also
        // outside of the city on the top/top left borders
        int carWidth = Width + 1;

        CarNodes.Clear();
        for (int x = 0; x < carWidth; x++)
        {
            for (int z = 0; z < carWidth; z++)
            {
                GameObject n = GameObject.Instantiate(Node);
                n.transform.parent   = mCarRoot.transform;
                n.name               = "Node_" + x.ToString() + "_" + z.ToString();
                n.transform.position = new Vector3(x * NodeSeparation, 0.0f, z * NodeSeparation);
                n.GetComponent <Node>().DebugColor = Color.red;
                CarNodes.Add(n);
            }
        }

        // Link the nodes
        for (int z = 0; z < carWidth; z++)
        {
            for (int x = 0; x < carWidth; x++)
            {
                int idx = x * carWidth + z;
                // Top link
                if (z < carWidth - 1)
                {
                    int           curI = x * carWidth + (z + 1);
                    NodeNeighbors nn   = new NodeNeighbors();
                    nn.OtherNode = CarNodes[curI];
                    nn.Distance  = Vector3.Distance(CarNodes[idx].transform.position,
                                                    CarNodes[curI].transform.position);
                    CarNodes[idx].GetComponent <Node>().AddNeighbor(nn);
                }
                // Bottom link
                if (z > 0)
                {
                    int           curI = x * carWidth + (z - 1);
                    NodeNeighbors nn   = new NodeNeighbors();
                    nn.OtherNode = CarNodes[curI];
                    nn.Distance  = Vector3.Distance(CarNodes[idx].transform.position,
                                                    CarNodes[curI].transform.position);
                    CarNodes[idx].GetComponent <Node>().AddNeighbor(nn);
                }
                // Right link
                if (x < carWidth - 1)
                {
                    int           curI = (x + 1) * carWidth + z;
                    NodeNeighbors nn   = new NodeNeighbors();
                    nn.OtherNode = CarNodes[curI];
                    nn.Distance  = Vector3.Distance(CarNodes[idx].transform.position,
                                                    CarNodes[curI].transform.position);
                    CarNodes[idx].GetComponent <Node>().AddNeighbor(nn);
                }
                // Left link
                if (x > 0)
                {
                    int           curI = (x - 1) * carWidth + z;
                    NodeNeighbors nn   = new NodeNeighbors();
                    nn.OtherNode = CarNodes[curI];
                    nn.Distance  = Vector3.Distance(CarNodes[idx].transform.position,
                                                    CarNodes[curI].transform.position);
                    CarNodes[idx].GetComponent <Node>().AddNeighbor(nn);
                }
            }
        }
    }
Пример #2
0
    /// <summary>
    /// We iterate through the grid nodes and generate pedestrian links. For each
    /// iteration we generate bottom row and then we generate the top row
    /// </summary>
    void InitializePedestrianNodes()
    {
        // Fill nodes
        PedestrianNodes.Clear();
        for (int z = 0; z < Width; z++)
        {
            FillPedestrianBottomNodes(z);
            FillPedestrianTopNodes(z);
        }
        for (int z = 0; z < Width; z++)
        {
            AddCrossWalks(z);
        }

        // Link Nodes
        int width2 = Width * 2;

        for (int z = 0; z < width2; z++)
        {
            for (int x = 0; x < width2; x++)
            {
                int idx = x * width2 + z;
                // Top link
                if (z < width2 - 1)
                {
                    int           curI = x * width2 + (z + 1);
                    NodeNeighbors nn   = new NodeNeighbors();
                    nn.OtherNode = PedestrianNodes[curI];
                    nn.Distance  = Vector3.Distance(PedestrianNodes[idx].transform.position,
                                                    PedestrianNodes[curI].transform.position);
                    PedestrianNodes[idx].GetComponent <Node>().AddNeighbor(nn);
                }
                // Bottom link
                if (z > 0)
                {
                    int           curI = x * width2 + (z - 1);
                    NodeNeighbors nn   = new NodeNeighbors();
                    nn.OtherNode = PedestrianNodes[curI];
                    nn.Distance  = Vector3.Distance(PedestrianNodes[idx].transform.position,
                                                    PedestrianNodes[curI].transform.position);
                    PedestrianNodes[idx].GetComponent <Node>().AddNeighbor(nn);
                }
                // Right link
                if (x < width2 - 1)
                {
                    int           curI = (x + 1) * width2 + z;
                    NodeNeighbors nn   = new NodeNeighbors();
                    nn.OtherNode = PedestrianNodes[curI];
                    nn.Distance  = Vector3.Distance(PedestrianNodes[idx].transform.position,
                                                    PedestrianNodes[curI].transform.position);
                    PedestrianNodes[idx].GetComponent <Node>().AddNeighbor(nn);
                }
                // Left link
                if (x > 0)
                {
                    int           curI = (x - 1) * width2 + z;
                    NodeNeighbors nn   = new NodeNeighbors();
                    nn.OtherNode = PedestrianNodes[curI];
                    nn.Distance  = Vector3.Distance(PedestrianNodes[idx].transform.position,
                                                    PedestrianNodes[curI].transform.position);
                    PedestrianNodes[idx].GetComponent <Node>().AddNeighbor(nn);
                }
            }
        }
    }
Пример #3
0
 //Add a node to the list of neighbors
 public void AddNeighbor(NodeNeighbors node)
 {
     mNeighbors.Add(node);
 }