Esempio n. 1
0
    private void GenerateNode(bool isBranchingPath, bool isRouterNode = false, TargetNode.PacketData inPktData = null)
    {
        Vector3    currentPos = transform.position;
        Vector3    nextPos    = currentPos;
        TargetNode prevNode   = null;

        // Is there a previous node?
        if (spawnedTargetNodeList.Count > 0)
        {
            prevNode = spawnedTargetNodeList[spawnedTargetNodeList.Count - 1];

            // Generate a position in a random direction, distance set by packet ms.
            if (prevNode != null)
            {
                currentPos = prevNode.transform.position;

                float randRangeX = Random.Range(-SpawnDeltaRange.x, SpawnDeltaRange.x);
                float randRangeY = Random.Range(-SpawnDeltaRange.y, SpawnDeltaRange.y);
                float randRangeZ = Random.Range(-SpawnDeltaRange.z, SpawnDeltaRange.z);

                randRangeZ = Mathf.Clamp(randRangeZ, MinZDistance, SpawnDeltaRange.z);

                Vector3 randomDir = new Vector3(randRangeX, randRangeY, Mathf.Abs(randRangeZ));
                randomDir.Normalize();
                nextPos = currentPos + randomDir * FixedDistance;

                //if (prevNode.IsRouterNode)
                //{
                //	if (lastRouterNode != null)
                //	{
                //		nextPos = new Vector3(lastRouterNode.transform.position.x + 100.0f,
                //							  nextPos.y, spawnedTargetNodeList[0].transform.position.z);
                //		//print("Nextpos: " + nextPos);
                //	}

                //	lastRouterNode = prevNode;
                //}

                // Was the last node a branching point? If so, make another node at the inverse X position (relative to the branching point).
                //if (prevNode.IsBranchingPath && !isRouterNode)
                //{
                //	prevNode.GetComponent<Renderer>().material.color = Color.red; // TEMPORARY

                //	Vector3 relativePos = prevNode.transform.InverseTransformPoint(nextPos);

                //	nextPos.x = prevNode.transform.TransformPoint(relativePos).x;

                //	relativePos.x *= -1;
                //	Vector3 altNodePos = prevNode.transform.TransformPoint(relativePos);

                //	TargetNode spawnedAltNode = Instantiate(NodePrefab, altNodePos, Quaternion.identity).GetComponent<TargetNode>();
                //	prevNode.AlternativeNode = spawnedAltNode;
                //	spawnedAltNode.IsTheAlternativeNode = true;
                //}

                //if (prevNode.IsBranchingPath || isRouterNode || prevNode.IsRouterNode)
                //{
                //	isBranchingPath = false;
                //}
            }
        }

        //isBranchingPath = false;


        // Spawn a new node, and set it to be the 'next node' for the previous one, then add it to the list.
        TargetNode spawnedNode = Instantiate(NodePrefab, nextPos, Quaternion.identity).GetComponent <TargetNode>();

        if (spawnedNode != null)
        {
            spawnedNode.Init(isBranchingPath, isRouterNode, inPktData);

            if (prevNode != null)
            {
                //print("LevelGenerator: Set the previous node's next node to the spawned one");
                prevNode.NextNode = spawnedNode;

                // If there are two previous nodes, make sure there's a NextNode on the potential alternative path.
                TargetNode prevPrevNode = null;
                if (spawnedTargetNodeList.Count > 1)
                {
                    prevPrevNode = spawnedTargetNodeList[spawnedTargetNodeList.Count - 2];

                    if (prevPrevNode != null && prevPrevNode.IsBranchingPath && prevPrevNode.AlternativeNode != null)
                    {
                        prevPrevNode.AlternativeNode.NextNode = spawnedNode;

                        float dist1 = Vector3.Distance(prevNode.transform.position, nextPos);
                        float dist2 = Vector3.Distance(prevPrevNode.AlternativeNode.transform.position, nextPos);

                        if (dist1 < dist2)
                        {
                            prevNode.IsShortestPath = true;
                            prevNode.GetComponent <Renderer>().material.color = Color.green;
                        }
                        else
                        {
                            prevPrevNode.AlternativeNode.IsShortestPath           = true;
                            prevPrevNode.GetComponent <Renderer>().material.color = Color.green;
                        }
                    }
                }
            }

            spawnedTargetNodeList.Add(spawnedNode);

            // Broadcast the spawn.
            if (OnSpawnedNode != null)
            {
                OnSpawnedNode(spawnedNode);

                //if (GeneratedSpline == null)
                //{
                //	print("Making new spline");
                //	GeneratedSpline = new Spline();
                //}

                GeneratedSpline.AddNode(spawnedNode);
            }
        }
    }