public TrafficSystemNode GetNextNodeHighway()
    {
        for (int cIndex = 0; cIndex < m_connectedNodes.Count; cIndex++)
        {
            TrafficSystemNode node = m_connectedNodes[cIndex];

            if (node && node.IsHighway() && node.m_lane > m_lane)
            {
                return(node);
            }
        }

        return(null);
    }
    public void AddChangeLaneNode(TrafficSystemNode a_node)
    {
        bool foundNode = false;

        for (int cIndex = 0; cIndex < m_connectedChangeLaneNodes.Count; cIndex++)
        {
            if (m_connectedChangeLaneNodes[cIndex] == a_node)
            {
                foundNode = true;
            }
        }

        if (!foundNode && a_node)
        {
            m_connectedChangeLaneNodes.Add(a_node);
        }
    }
    private void ConnectLanes(GameObject currentLane, GameObject previousLane)
    {
        try
        {
            TrafficSystemNode nextNode = new TrafficSystemNode();
            int selected;

            //Removing the min amount of nodes as the last items in the lane don't have anything to connect with because of this limit
            for (int i = 0; i < (currentLane.transform.childCount - this.minNodesLaneChange); i++)
            {
                //TODO: can create a loop here to add multiple lanes the car can move to
                //      Will need to check if it is already connected
                selected = random.Next((i + minNodesLaneChange), (i + maxNodesLaneChange));

                //to insure that the selected node is not outside the number of nodes available.
                if (selected > (previousLane.transform.childCount - 1))
                {
                    selected = (previousLane.transform.childCount - 2);
                }
                nextNode = previousLane.transform.GetChild(selected).GetComponent <TrafficSystemNode>();
                currentLane.transform.GetChild(i).GetComponent <TrafficSystemNode>().m_connectedNodes.Add(nextNode);
            }


            //----------------------------------
            for (int i = 0; i < (previousLane.transform.childCount - this.minNodesLaneChange); i++)
            {
                selected = random.Next((i + minNodesLaneChange), (i + maxNodesLaneChange));

                //to insure that the selected node is not outside the number of nodes available.
                if (selected > (currentLane.transform.childCount - 1))
                {
                    selected = (currentLane.transform.childCount - 2);
                }
                nextNode = currentLane.transform.GetChild(selected).GetComponent <TrafficSystemNode>();
                previousLane.transform.GetChild(i).GetComponent <TrafficSystemNode>().m_connectedNodes.Add(nextNode);
            }
        }
        catch (System.Exception ex)
        {
            Debug.Log(ex.Message);
        }
    }
    private void ConnectNodes(GameObject folder)
    {
        try
        {
            for (int i = 0; i < folder.transform.childCount; i++)
            {
                //Cannot connect the last node to anything
                //Though it would be nice to connect it to the next road.  TODO: Someday, hopefully
                if (i < (folder.transform.childCount - 1))
                {
                    TrafficSystemNode nextNode = folder.transform.GetChild(i + 1).GetComponent <TrafficSystemNode>();

                    folder.transform.GetChild(i).GetComponent <TrafficSystemNode>().m_connectedNodes.Clear();
                    folder.transform.GetChild(i).GetComponent <TrafficSystemNode>().m_connectedNodes.Add(nextNode);
                }
            }
        }
        catch
        {   }
    }
//	public bool HasSplineToFollow( TrafficSystemVehicle a_obj )
//	{
//		if(Parent)
//		{
//			Vector3[] path = Parent.GetPath( m_lane, m_driveSide, a_obj );
//
//			if(path != null && path.Length > 1)
//			{
//				return true;
//			}
//		}
//
//		return false;
//	}
//
//	public Vector3[] GetPath( TrafficSystemVehicle a_obj )
//	{
//		if(Parent)
//		{
//			return Parent.GetPath( m_lane, m_driveSide, a_obj );
//		}
//
//		return null;
//	}

    public TrafficSystemNode GetRandomChangeLangeNode()
    {
        if (m_connectedChangeLaneNodes.Count <= 0)
        {
            return(null);
        }

        List <int> nodeIndex = new List <int>(m_connectedChangeLaneNodes.Count);

        for (int nIndex = 0; nIndex < m_connectedChangeLaneNodes.Count; nIndex++)
        {
            nodeIndex.Insert(nIndex, nIndex);
        }

        TrafficSystemNode sameLaneNode = null;
        int count = 0;

        while (count < m_connectedChangeLaneNodes.Count)
        {
            int nIndex = Random.Range(0, nodeIndex.Count);
            int lIndex = nodeIndex[nIndex];

            TrafficSystemNode node = m_connectedChangeLaneNodes[lIndex];

            if (node)
            {
                sameLaneNode = node;
                count        = m_connectedChangeLaneNodes.Count;
            }

            nodeIndex.Remove(nIndex);
            count++;
        }

        return(sameLaneNode);
    }
    public void AddConnectedNode( TrafficSystemNode a_node )
    {
        bool foundNode = false;
        for(int cIndex = 0; cIndex < m_connectedNodes.Count; cIndex++)
        {
            if(m_connectedNodes[cIndex] == a_node)
                foundNode = true;
        }

        if(!foundNode && a_node)
            m_connectedNodes.Add(a_node);
    }
 void Awake()
 {
     TrafficSystemNode = (TrafficSystemNode)target;
     m_previousDriveSide = TrafficSystemNode.m_driveSide;
 }
Esempio n. 8
0
    void ChangeNodeMaterial( TrafficSystemNode a_node, TrafficSystem.DriveSide a_side )
    {
        a_node.m_driveSide = a_side;

        if(a_node.m_driveSide == TrafficSystem.DriveSide.LEFT)
        {
            if(a_node.m_isPrimary)
            {
                Material material = AssetDatabase.LoadAssetAtPath(TrafficSystemEditor.PrimaryNodeLeftSideMaterial, typeof(Material)) as Material;
                a_node.GetComponent<Renderer>().material = material;
            }
            else
            {
                Material material = AssetDatabase.LoadAssetAtPath(TrafficSystemEditor.SecondaryNodeLeftSideMaterial, typeof(Material)) as Material;
                a_node.GetComponent<Renderer>().material = material;
            }
        }
        else
        {
            if(a_node.m_isPrimary)
            {
                Material material = AssetDatabase.LoadAssetAtPath(TrafficSystemEditor.PrimaryNodeRightSideMaterial, typeof(Material)) as Material;
                a_node.GetComponent<Renderer>().material = material;
            }
            else
            {
                Material material = AssetDatabase.LoadAssetAtPath(TrafficSystemEditor.SecondaryNodeRightSideMaterial, typeof(Material)) as Material;
                a_node.GetComponent<Renderer>().material = material;
            }
        }
    }
    void OnDrawGizmos()
    {
                #if !UNITY_EDITOR
        return;
                #endif

        if (TrafficSystem.Instance && !TrafficSystem.Instance.m_showGizmos)
        {
            return;
        }

        if (m_connectedNodes.Count > 0 && m_linkedIndicator)
        {
            m_linkedIndicator.SetActive(true);
        }
        else if (m_linkedIndicator)
        {
            m_linkedIndicator.SetActive(false);
        }

        float scaleFactorCube   = 0.15f;
        float scaleFactorSphere = 0.225f;
        if (!m_connectedLocalNode)
        {
            for (int nIndex = 0; nIndex < m_connectedNodes.Count; nIndex++)
            {
                TrafficSystemNode connectedNode = m_connectedNodes[nIndex];
                if (connectedNode)
                {
                    Vector3 offset = new Vector3(0.0f, 0.1f, 0.0f);
                    Gizmos.color = Color.white;
                    Gizmos.DrawLine(transform.position + offset, connectedNode.transform.position + offset);

                    Vector3 dir = transform.position - connectedNode.transform.position;
//					Gizmos.color = Color.white;
//					Gizmos.DrawCube( (transform.position - (dir.normalized * ((dir.magnitude / 2) + scaleFactorSphere))) + offset, new Vector3(scaleFactorCube * 1.4f, scaleFactorCube * 1.4f, scaleFactorCube * 1.4f) );
                    Gizmos.color = Color.yellow;
                    Gizmos.DrawCube((transform.position - (dir.normalized * ((dir.magnitude / 2) + scaleFactorSphere))) + offset, new Vector3(scaleFactorCube, scaleFactorCube, scaleFactorCube));
                    Gizmos.color = Color.white;
                    Gizmos.DrawSphere((transform.position - (dir.normalized * (dir.magnitude / 2))) + offset, scaleFactorSphere);
                }
            }
        }

        if (m_connectedLocalNode)
        {
            Vector3 offset = new Vector3(0.0f, 0.1f, 0.0f);
            Gizmos.color = Color.white;
            Gizmos.DrawLine(transform.position + offset, m_connectedLocalNode.transform.position + offset);

            Vector3 dir = transform.position - m_connectedLocalNode.transform.position;
//			Gizmos.color = Color.white;
//			Gizmos.DrawCube( (transform.position - (dir.normalized * ((dir.magnitude / 2) + scaleFactorSphere))) + offset, new Vector3(scaleFactorCube * 1.4f, scaleFactorCube * 1.4f, scaleFactorCube * 1.4f) );
            Gizmos.color = Color.yellow;
            Gizmos.DrawCube((transform.position - (dir.normalized * ((dir.magnitude / 2) + scaleFactorSphere))) + offset, new Vector3(scaleFactorCube, scaleFactorCube, scaleFactorCube));
            Gizmos.color = Color.white;
            Gizmos.DrawSphere((transform.position - (dir.normalized * (dir.magnitude / 2))) + offset, scaleFactorSphere);
        }

        for (int cIndex = 0; cIndex < m_connectedChangeLaneNodes.Count; cIndex++)
        {
            if (m_connectedChangeLaneNodes[cIndex])           // && m_connectedChangeLaneNodes[cIndex].m_connectedNodes.Count > 0)
            {
                Vector3 offset = new Vector3(0.0f, 0.1f, 0.0f);
                Gizmos.color = Color.white;
                Gizmos.DrawLine(transform.position + offset, m_connectedChangeLaneNodes[cIndex].transform.position + offset);

                Vector3 dir = transform.position - m_connectedChangeLaneNodes[cIndex].transform.position;
//				Gizmos.color = Color.white;
//				Gizmos.DrawCube( (transform.position - (dir.normalized * ((dir.magnitude / 2) + scaleFactorSphere))) + offset, new Vector3(scaleFactorCube * 1.4f, scaleFactorCube * 1.4f, scaleFactorCube * 1.4f) );
                Gizmos.color = Color.yellow;
                Gizmos.DrawCube((transform.position - (dir.normalized * ((dir.magnitude / 2) + scaleFactorSphere))) + offset, new Vector3(scaleFactorCube, scaleFactorCube, scaleFactorCube));
                Gizmos.color = Color.white;
                Gizmos.DrawSphere((transform.position - (dir.normalized * (dir.magnitude / 2))) + offset, scaleFactorSphere);
            }
        }
    }
Esempio n. 10
0
    protected void SetNextNode( TrafficSystemNode a_node )
    {
        m_nodeHighwaySearch            = null;
        TrafficSystemNode previousNode = m_nextNode;
        m_nextNode                     = a_node;

        //print ("m_nodeToPathToRoute.Count ( " + m_nodeToPathToRoute.Count + " ) > 0 && m_nodeToPathToRoute.Count ( " + m_nodeToPathToRoute.Count + " ) < m_nodeToPathToRouteIndex ( " + m_nodeToPathToRouteIndex + " )");
        if(m_nodeToPathToRoute.Count > 0 && m_nodeToPathToRouteIndex < m_nodeToPathToRoute.Count)
        {
            m_nextNode = m_nodeToPathToRoute[m_nodeToPathToRouteIndex];
            m_nodeToPathToRouteIndex++;
        }

        if(!m_nextNode)
        {
            Debug.LogWarning("Vehicle node invalid, check all nodes on this road piece have correct links as this is probably the issue!: \"" + previousNode.Parent.name + "\" -> Previous Node was: \"" + previousNode.name + "\"\nTIP: In \"Editor Mode\" try clicking on the road piece then clicking off it as this should remove any null links.");

            if(m_killOnEmptyPath)
                Debug.Log("Vehicle set to kill on empty path so we will destroy vehicle " + gameObject.name);
        }
        else
        {
            float newSpeed = m_nextNode.GetSpeedLimit() + m_speedVariation;

            if(m_changeMaxVelocityOnSpeedLimitInc)
                m_velocityMaxOriginal = newSpeed;

            if(newSpeed <= m_velocityMaxOriginal)
                m_velocityMax = newSpeed;
        }
    }
    public virtual void Update()
    {
        //		CanSeeVehicleViaSphere();

        if(!TrafficLight)
            m_trafficLightCoolDown += Time.deltaTime;

        StopMoving = false;

        if(WaitingForTraffic)
            StopMoving = true;

        if(CrashDetected)
        {
            m_timetoWaitAfterCrashTimer += Time.deltaTime;
            CrashOverCheck();
        }

        if(IsStuck())
        {
            m_stuckDestroyTimer += Time.deltaTime;
            if(m_stuckDestroyTimer >= m_stuckDestroyTimerMax)
            {
                if (Debug.isDebugBuild)
                    Debug.LogWarning("Vehicle Destroyed - m_stuckDestroyTimerMax - Vehicle had unusual behaviour");

                Kill();
            }
        }
        else if(IsStopped() && m_enableWaitingDestroyTimer)
        {
            m_waitingDestroyTimer += Time.deltaTime;
            if(m_waitingDestroyTimer >= m_waitingDestroyTimerMax)
            {
                if (Debug.isDebugBuild)
                    Debug.LogWarning("Vehicle Destroyed - m_waitingDestroyTimerMax - (m_enableWaitingDestroyTimer = true) - Vehicle was waiting too long in the one position");

                Kill();
            }
        }
        else
        {
            m_waitingDestroyTimer = 0.0f;
            m_stuckDestroyTimer   = 0.0f;
        }

        if(!m_useRoadSpeedLimits && m_velocityMaxOriginal != m_velocityMax)
        {
            m_returnToOriginalVelocityTimer += Time.deltaTime;
            if(m_returnToOriginalVelocityTimer >= m_returnToOriginalVelocityTimerMax)
            {
                m_velocityMax = m_velocityMaxOriginal;
                m_returnToOriginalVelocityTimer = 0.0f;
            }
        }

        if(VehicleHit) // if we are close to a car previously, wait before checking
        {
            m_waitTimer += Time.deltaTime;
            if(m_waitTimer < m_waitTime)
                return;
        }

        if(m_nextNode)
        {
            bool isOnHighway = false;
            if(m_nextNode.IsHighway() && m_nextNode.IsHighwayChangeLaneAccepted() && m_vehicleCheckOvertakeOnHighway && m_nodeHighwaySearch != m_nextNode)
                isOnHighway = true;

            if(isOnHighway)
            {
                RaycastHit hitInfo;
                VehicleHit  = null;
                if(Physics.SphereCast( (transform.position + m_vehicleCheckOffset), m_vehicleCheckRadiusOvertakeHighwayOnly, transform.forward, out hitInfo, m_vehicleCheckDistOvertakeHighwayOnly ))
                {
                    TrafficSystemVehicle vehicle = hitInfo.transform.GetComponent<TrafficSystemVehicle>();
                    if(vehicle)
                    {
                        if(vehicle != this && vehicle.VehicleHit != this && vehicle != m_previousVehicleToTryOvertaking)
                        {
                            float randChanceToOvertake = 0.0f;
                            randChanceToOvertake = Random.Range(0.0f, 1.0f);

                            m_previousVehicleToTryOvertaking = vehicle;

                            if(randChanceToOvertake <= m_randomChanceToOvertake)
                            {
                                TrafficSystemNode node = m_nextNode.GetNextNodeHighway();
                                if(node)
                                {
                                    SetNextNode( node );
                                    m_nodeHighwaySearch = m_nextNode;
                                }
                            }
                        }
                    }
                }

                RaycastHit[] hitInfoAll = Physics.SphereCastAll( (transform.position + m_vehicleCheckOffset), m_vehicleCheckRadius, transform.forward, m_vehicleCheckDist );
                VehicleHit  = null;
                for(int hIndex = 0; hIndex < hitInfoAll.Length; hIndex++)
                {
                    TrafficSystemVehicle vehicle = hitInfoAll[hIndex].transform.GetComponent<TrafficSystemVehicle>();
                    if(vehicle)
                    {
                        if(vehicle != this && vehicle.VehicleHit != this)
                        {
                            StopMoving = true;
                            VehicleHit = vehicle;
                            m_velocityMax = VehicleHit.m_velocityMax;
                            m_waitTimer = 0.0f;
                            m_waitTime = Random.Range(m_waitTimeMin, m_waitTimeMax);
                        }
                    }
                }
            }
            else
            {
                RaycastHit[] hitInfoAll = Physics.SphereCastAll( (transform.position + m_vehicleCheckOffset), m_vehicleCheckRadius, transform.forward, m_vehicleCheckDist );
                VehicleHit  = null;
                for(int hIndex = 0; hIndex < hitInfoAll.Length; hIndex++)
                {
                    TrafficSystemVehicle vehicle = hitInfoAll[hIndex].transform.GetComponent<TrafficSystemVehicle>();
                    if(vehicle)
                    {
                        if(vehicle != this && vehicle.VehicleHit != this)
                        {
                            StopMoving = true;
                            VehicleHit = vehicle;
                            m_velocityMax = VehicleHit.m_velocityMax;
                            m_waitTimer = 0.0f;
                            m_waitTime = Random.Range(m_waitTimeMin, m_waitTimeMax);
                        }
                    }
                }
            }

            if(!VehicleHit && m_enableVehicleCheckMid)
            {
                RaycastHit[] hitInfo = Physics.SphereCastAll( (transform.position + m_vehicleCheckOffset), (m_vehicleCheckRadius * m_vehicleCheckRadiusMid ), transform.forward, m_vehicleCheckDist - m_vehicleCheckDistMid );

                VehicleHit  = null;
                for(int hIndex = 0; hIndex < hitInfo.Length; hIndex++)
                {
                    TrafficSystemVehicle vehicle = hitInfo[hIndex].transform.GetComponent<TrafficSystemVehicle>();
                    if(vehicle)
                    {
                        if(vehicle != this && vehicle.VehicleHit != this )
                        {
                            StopMoving = true;
                            VehicleHit = vehicle;
                            m_velocityMax = VehicleHit.m_velocityMax;
                            m_waitTimer = 0.0f;
                            m_waitTime = Random.Range(m_waitTimeMin, m_waitTimeMax);
                        }
                    }
                }
            }
        }

        if(!StopMoving && TrafficLight && !CrashDetected) // determine if there is there is room to move throught the intersection
        {
            if(TrafficLight.m_status == TrafficSystemTrafficLight.Status.RED)
            {
                //print ("TrafficLight = RED");
                StopMoving = true;
            }
            else if(CanSeeVehicleViaSphere() && !TrafficLight.IgnoreCanFitAcrossIntersectionCheck())
            {
                //print ("CanSeeVehicleViaSphere: = TRUE");
                StopMoving = true;
            }
            else if(!CanFitAcrossIntersection() && !TrafficLight.IgnoreCanFitAcrossIntersectionCheck())
            {
                //print ("CanFitAcrossIntersection: = FALSE");
                StopMoving = true;
            }
            else if(TrafficLight.m_status == TrafficSystemTrafficLight.Status.YELLOW)
            {
                m_trafficLightYellowEnterDurationTimer += Time.deltaTime;
                if(m_trafficLightYellowEnterDurationTimer <= m_trafficLightYellowEnterDuration)
                {
                    DriveThroughLights();
                }
                else
                    //print ("TrafficLight = RED OR YELLOW");
                    StopMoving = true;
            }
            else if(TrafficLight.m_status == TrafficSystemTrafficLight.Status.GREEN)
            {
                DriveThroughLights();
            }

        //			print ("TrafficLight: " + TrafficLight);
        }

        if(!StopMoving && m_nextNode && !CrashDetected)// && !m_pathingStarted)
        {
            VehicleInSights = CanSeeVehicleViaRay();
            float ratio = 1.0f;
            if(VehicleInSights)
            {
                if(VehicleInSights.VehicleInSights && VehicleInSights.VehicleInSights == this)
                {
                    // do nothing as these two cars are facing eachother for unknown reasons so none of them should slow down with this ray detection.
                }
                else
                {
                    float velocityRatio = 1.0f;

        //					if(m_velocity > 0.0f)
        //						velocityRatio = VehicleInSights.m_velocity / m_velocity;

                    Vector3 dirOfVehicleInSight = transform.position - VehicleInSights.transform.position;
                    ratio = Mathf.Clamp((dirOfVehicleInSight.magnitude / m_vehicleCheckDistRay), 0.0f, 1.0f);

                    if(ratio < m_vehicleCheckDistRayMimicSpeedThreshold)
                        m_velocityMax = VehicleInSights.m_velocityMax;
                }
            }

            if(Accelerate)                                                              // are we accelerating?
                m_velocity = m_velocity + ((m_accelerationRate * ratio) * Time.deltaTime);        // add to the current velocity according while accelerating
        //			else
        //				m_velocity = m_velocity - (m_decelerationRate * Time.deltaTime);        // subtract from the current velocity while decelerating

            float velTmp = m_velocityMax * ratio;

            if(velTmp > m_velocity)
                m_velocity = m_velocity - ((m_accelerationRate * m_decelerationPercentage) * Time.deltaTime);
            else
                m_velocity = velTmp;

            if(VehicleHit)
                m_velocity = VehicleHit.m_velocity;                                     // ensure the velocity is the same as the car in front
            else
                m_velocity = Mathf.Clamp(m_velocity, m_velocityMin, m_velocityMax);     // ensure the velocity never goes out of the min/max boundaries

            Vector3 dir = m_nextNode.transform.position;
            dir.x      += m_lanePosVariation;
            dir.z      += m_lanePosVariation;
            dir         = dir - (transform.position + m_offsetPosVal);           // find the direction to the next node

            Vector3 speed = dir.normalized * m_velocity;                                // work out how fast we should travel in the desired directoin

            for(int rIndex = 0; rIndex < m_lightsRear.Length; rIndex++)
            {
                if(m_lightsRear[rIndex].gameObject.activeSelf)
                    m_lightsRear[rIndex].gameObject.SetActive(false);
            }

            Vector3 wheelAxis = Vector3.zero;

            if(m_wheelAxis == AxisType.X)
                wheelAxis = new Vector3(1.0f, 0.0f, 0.0f);
            else if(m_wheelAxis == AxisType.Y)
                wheelAxis = new Vector3(0.0f, 1.0f, 0.0f);
            else if(m_wheelAxis == AxisType.Z)
                wheelAxis = new Vector3(0.0f, 0.0f, 1.0f);

            for(int wIndex = 0; wIndex < m_wheelsFront.Length; wIndex++)
                m_wheelsFront[wIndex].transform.Rotate(wheelAxis, m_velocity * m_wheelRotMultiplier);

            for(int wIndex = 0; wIndex < m_wheelsRear.Length; wIndex++)
                m_wheelsRear[wIndex].transform.Rotate(wheelAxis, m_velocity * m_wheelRotMultiplier);

            if(m_rigidbody)                                                             // if we have a rigidbody, use the following code to move us
            {
                m_rigidbody.velocity = speed;                                           // set our rigidbody to this speed to move us by the determined speed
                transform.forward    = Vector3.Lerp( transform.forward, dir.normalized, m_rotationSpeed * Time.deltaTime );    // rotate our forward directoin over time to face the node we are moving towards
            }
            else                                                                        // no rigidbody then use the following code to move us
            {
                if(m_collider || collider)                                              // it generally is a bad idea to move something with a collider, so we should tell someone about it if this is happening. See Unity Docs for more info: http://docs.unity3d.com/ScriptReference/Collider.html
                    Debug.LogWarning("Traffic System Warning -> VehicleBase has a collider. You should think about moving the object with a rigidbody instead.");

                transform.position += speed * Time.deltaTime;                           // move us by the determined speed
                transform.forward   = Vector3.Lerp( transform.forward, dir.normalized, m_rotationSpeed * Time.deltaTime );     // rotate our forward directoin over time to face the node we are moving towards
            }

            if(dir.magnitude < m_nextNodeThreshold)                                     // if we are close enough to the node we were travelling towards then check for the next one
            {
        //				if(m_nextNode.HasSplineToFollow( this ))
        //				{
        //					if(!m_traversePath)
        //					{
        //						m_traversePath = true;
        //						ProcessSpline();
        //					}
        //				}
        //				else

        //				if(!IsTrafficJamOnUpcomingNodes())
        //				{
        //					TrafficSystemNode preNextNode = m_nextNode;
        //
        //					List<TrafficSystemNode> blockedNodes = new List<TrafficSystemNode>();
        //					for(int nIndex = 0; nIndex < m_nextNode.m_connectedNodes.Count; nIndex++)
        //					{
        //						TrafficSystemNode nodeToCheck = m_nextNode.m_connectedNodes[nIndex];
        //						if(VehicleExistsOnNode(nodeToCheck))
        //							blockedNodes.Add(nodeToCheck);
        //					}
        //
                SetNextNode( m_nextNode.GetNextNode( this, true/*, blockedNodes*/ ) );                            // gets the next required node to go to and start the process all over again
                //}
            }
        }
        else if(CrashDetected)
        {
            // don't stop, but don't do anything
        }
        else if(StopMoving)
        {
            Stop();
        }

        if(m_killOnEmptyPath && !m_nextNode)
        {
            if (Debug.isDebugBuild)
                Debug.LogWarning("Vehicle Destroyed - No path node");

            Kill();
        }
    }
Esempio n. 12
0
    void FindAllNodesRecursive(TrafficSystemPiece a_trafficSystemPiece, GameObject a_obj)
    {
        if (a_obj)
        {
            if (a_obj.GetComponent <TrafficSystemNode>())
            {
                TrafficSystemNode node = a_obj.GetComponent <TrafficSystemNode>();
                if (node)
                {
                    if (node.m_driveSide == TrafficSystem.DriveSide.LEFT)
                    {
                        if (node.m_roadType == TrafficSystem.RoadType.CHANGE_LANE)
                        {
                            a_trafficSystemPiece.m_leftLaneChangeNodes.Add(node);
                        }
                        else if (node.m_isPrimary)
                        {
                            a_trafficSystemPiece.m_primaryLeftLaneNodes.Add(node);
                        }
                        else
                        {
                            //						bool wasInserted = false;
                            //						for(int sIndex = 0; sIndex < a_trafficSystemPiece.m_secondaryLeftLaneNodes.Count; sIndex++)
                            //						{
                            //							if(a_trafficSystemPiece.m_secondaryLeftLaneNodes[sIndex].m_priority > node.m_priority)
                            //							{
                            //								a_trafficSystemPiece.m_secondaryLeftLaneNodes.Insert(sIndex, node);
                            //								wasInserted = true;
                            //								break;
                            //							}
                            //						}
                            //
                            //						if(!wasInserted)
                            a_trafficSystemPiece.m_secondaryLeftLaneNodes.Add(node);
                        }
                    }
                    else
                    {
                        if (node.m_roadType == TrafficSystem.RoadType.CHANGE_LANE)
                        {
                            a_trafficSystemPiece.m_rightLaneChangeNodes.Add(node);
                        }
                        else if (node.m_isPrimary)
                        {
                            a_trafficSystemPiece.m_primaryRightLaneNodes.Add(node);
                        }
                        else
                        {
                            //						bool wasInserted = false;
                            //						for(int sIndex = 0; sIndex < a_trafficSystemPiece.m_secondaryRightLaneNodes.Count; sIndex++)
                            //						{
                            //							if(a_trafficSystemPiece.m_secondaryRightLaneNodes[sIndex].m_priority > node.m_priority)
                            //							{
                            //								a_trafficSystemPiece.m_secondaryRightLaneNodes.Insert(sIndex, node);
                            //								wasInserted = true;
                            //								break;
                            //							}
                            //						}
                            //
                            //						if(!wasInserted)
                            a_trafficSystemPiece.m_secondaryRightLaneNodes.Add(node);
                        }
                    }
                }
            }

            for (int cIndex = 0; cIndex < a_obj.transform.childCount; cIndex++)
            {
                Transform child = a_obj.transform.GetChild(cIndex);
                if (child.gameObject)
                {
                    FindAllNodesRecursive(a_trafficSystemPiece, child.gameObject);
                }
            }
        }
    }
Esempio n. 13
0
 public void RemoveConnectedNode(TrafficSystemNode a_node)
 {
     m_connectedNodes.Remove(a_node);
 }
Esempio n. 14
0
    TrafficSystemNode FindChildNode( TrafficSystemNode a_node )
    {
        if(a_node.m_connectedLocalNode)
            a_node = FindChildNode( a_node.m_connectedLocalNode );

        return a_node;
    }
Esempio n. 15
0
    void OnGUI()
    {
        if (!TrafficSystem.Instance)
        {
            return;
        }

        if (!TrafficSystem.Instance.VehicleHasFocus(this))
        {
            return;
        }

        if (!m_nextNode)
        {
            return;
        }

        Vector2 buttonSize     = new Vector2(100.0f, 30.0f);
        float   buttonSpacerX  = 20.0f;
        float   buttonSpacerY  = 40.0f;
        float   initScreenPosY = 10.0f;

        if (m_nextNode.IsDirectionChange())
        {
            int buttonCount = 3;

            if (m_nextNode.m_directionListing == TrafficSystemNode.DirectionListing.LEFT_RIGHT_STRAIGHT_UTURN)
            {
                buttonCount = 4;
            }

            float initScreenPosX = (Screen.width / 2) - ((buttonSize.x + buttonSpacerX) * (buttonCount / 2.0f));

            int btnIndex = 0;
            if (m_nextNode.IsDirectionLeft())
            {
                if (GUI.Button(new Rect(initScreenPosX + ((buttonSize.x + buttonSpacerX) * btnIndex), initScreenPosY, buttonSize.x, buttonSize.y), "LEFT"))
                {
                    TrafficSystemNode node = null;
                    if (m_nextNode.IsRoundabout())
                    {
                        for (int cIndex = 0; cIndex < m_nextNode.m_connectedChangeLaneNodes.Count; cIndex++)
                        {
                            if (!node && m_nextNode.m_connectedChangeLaneNodes[cIndex].m_roundaboutExit == TrafficSystemNode.RoundaboutExit.EXIT_3)
                            {
                                node = m_nextNode.m_connectedChangeLaneNodes[cIndex];
                                break;
                            }
                        }
                    }
                    else
                    {
                        for (int cIndex = 0; cIndex < m_nextNode.m_connectedChangeLaneNodes.Count; cIndex++)
                        {
                            if (!node && m_nextNode.m_connectedChangeLaneNodes[cIndex].m_driveSide == TrafficSystem.DriveSide.LEFT)
                            {
                                node = m_nextNode.m_connectedChangeLaneNodes[cIndex];
                                break;
                            }
                        }
                    }

                    if (node)
                    {
                        SetNextNode(node);
                    }

                    WaitForUserInput = false;
                }
            }
            btnIndex++;

            if (m_nextNode.IsDirectionStraight())
            {
                if (GUI.Button(new Rect(initScreenPosX + ((buttonSize.x + buttonSpacerX) * btnIndex), initScreenPosY, buttonSize.x, buttonSize.y), "STRAIGHT"))
                {
                    TrafficSystemNode node = null;
                    if (m_nextNode.IsRoundabout())
                    {
                        for (int cIndex = 0; cIndex < m_nextNode.m_connectedChangeLaneNodes.Count; cIndex++)
                        {
                            if (!node && m_nextNode.m_connectedChangeLaneNodes[cIndex].m_roundaboutExit == TrafficSystemNode.RoundaboutExit.EXIT_2)
                            {
                                node = m_nextNode.m_connectedChangeLaneNodes[cIndex];
                                break;
                            }
                        }
                    }
                    else
                    {
                        for (int cIndex = 0; cIndex < m_nextNode.m_connectedNodes.Count; cIndex++)
                        {
                            if (!node && m_nextNode.m_driveSide == m_nextNode.m_connectedNodes[cIndex].m_driveSide)
                            {
                                node = m_nextNode.m_connectedNodes[cIndex];
                                break;
                            }
                        }
                    }

                    if (node)
                    {
                        SetNextNode(node);
                    }

                    WaitForUserInput = false;
                }
            }
            btnIndex++;

            if (m_nextNode.IsDirectionRight())
            {
                if (GUI.Button(new Rect(initScreenPosX + ((buttonSize.x + buttonSpacerX) * btnIndex), initScreenPosY, buttonSize.x, buttonSize.y), "RIGHT"))
                {
                    TrafficSystemNode node = null;
                    if (m_nextNode.IsRoundabout())
                    {
                        for (int cIndex = 0; cIndex < m_nextNode.m_connectedChangeLaneNodes.Count; cIndex++)
                        {
                            if (!node && m_nextNode.m_connectedChangeLaneNodes[cIndex].m_roundaboutExit == TrafficSystemNode.RoundaboutExit.EXIT_1)
                            {
                                node = m_nextNode.m_connectedChangeLaneNodes[cIndex];
                                break;
                            }
                        }
                    }
                    else
                    {
                        for (int cIndex = 0; cIndex < m_nextNode.m_connectedChangeLaneNodes.Count; cIndex++)
                        {
                            if (!node && m_nextNode.m_connectedChangeLaneNodes[cIndex].m_driveSide == TrafficSystem.DriveSide.RIGHT)
                            {
                                node = m_nextNode.m_connectedChangeLaneNodes[cIndex];
                                break;
                            }
                        }
                    }

                    if (node)
                    {
                        SetNextNode(node);
                    }

                    WaitForUserInput = false;
                }
            }
            btnIndex++;

            if (m_nextNode.IsDirectionUTurn())
            {
                if (GUI.Button(new Rect(initScreenPosX + ((buttonSize.x + buttonSpacerX) * btnIndex), initScreenPosY, buttonSize.x, buttonSize.y), "U-TURN"))
                {
                    TrafficSystemNode node = null;
                    if (m_nextNode.IsRoundabout())
                    {
                        for (int cIndex = 0; cIndex < m_nextNode.m_connectedChangeLaneNodes.Count; cIndex++)
                        {
                            if (!node && m_nextNode.m_connectedChangeLaneNodes[cIndex].m_roundaboutExit == TrafficSystemNode.RoundaboutExit.EXIT_4)
                            {
                                node = m_nextNode.m_connectedChangeLaneNodes[cIndex];
                                break;
                            }
                        }
                    }

                    if (node)
                    {
                        SetNextNode(node);
                    }

                    WaitForUserInput = false;
                }
            }
            btnIndex++;
        }
        else if (!ChangeLaneActivated && m_nextNode.m_roadType == TrafficSystem.RoadType.LANES_MULTI && m_nextNode.m_connectedNodes.Count > 1)
        {
            int   buttonCount    = 2;
            float initScreenPosX = (Screen.width / 2) - ((buttonSize.x + buttonSpacerX) * (buttonCount / 2.0f));

            bool isOnHighwayAndCanChangeLanes = false;
            if (m_nextNode.IsHighway() && m_nextNode.IsHighwayChangeLaneAccepted() && m_vehicleCheckOvertakeOnHighway)
            {
                isOnHighwayAndCanChangeLanes = true;
            }

            if (!isOnHighwayAndCanChangeLanes)
            {
                return;
            }

            TrafficSystemNode rightNode = null;
            TrafficSystemNode leftNode  = null;
            for (int cIndex = 0; cIndex < m_nextNode.m_connectedNodes.Count; cIndex++)
            {
                if (!rightNode && (m_nextNode.m_lane - 1) == m_nextNode.m_connectedNodes[cIndex].m_lane)
                {
                    rightNode = m_nextNode.m_connectedNodes[cIndex];
                }
                if (!leftNode && (m_nextNode.m_lane + 1) == m_nextNode.m_connectedNodes[cIndex].m_lane)
                {
                    leftNode = m_nextNode.m_connectedNodes[cIndex];
                }
            }

            int btnIndex = 0;
            if (leftNode)
            {
                if (GUI.Button(new Rect(initScreenPosX + ((buttonSize.x + buttonSpacerX) * btnIndex), initScreenPosY, buttonSize.x, buttonSize.y), "LEFT"))
                {
                    SetNextNode(leftNode);
                }
            }
            btnIndex++;

            if (rightNode)
            {
                if (GUI.Button(new Rect(initScreenPosX + ((buttonSize.x + buttonSpacerX) * btnIndex), initScreenPosY, buttonSize.x, buttonSize.y), "RIGHT"))
                {
                    SetNextNode(rightNode);
                }
            }
            btnIndex++;
        }
    }
Esempio n. 16
0
 public void RemoveChangeLangeNode( TrafficSystemNode a_node )
 {
     m_connectedChangeLaneNodes.Remove(a_node);
 }
Esempio n. 17
0
    public void SpawnRandomVehicle(TrafficSystemVehicle a_vehiclePrefab)
    {
        if (!TrafficSystem.Instance)
        {
            return;
        }

        TrafficSystemNode node = null;

        int randomLane = Random.Range(0, 4);

        switch (randomLane)
        {
        case 0:
        {
            for (int nIndex = 0; nIndex < m_primaryLeftLaneNodes.Count; nIndex++)
            {
                if (m_primaryLeftLaneNodes[nIndex])
                {
                    node = m_primaryLeftLaneNodes[nIndex];
                    break;
                }
            }

            if (!node)
            {
                for (int nIndex = 0; nIndex < m_primaryRightLaneNodes.Count; nIndex++)
                {
                    if (m_primaryRightLaneNodes[nIndex])
                    {
                        node = m_primaryRightLaneNodes[nIndex];
                        break;
                    }
                }
            }

            if (!node)
            {
                for (int nIndex = 0; nIndex < m_secondaryLeftLaneNodes.Count; nIndex++)
                {
                    if (m_secondaryLeftLaneNodes[nIndex])
                    {
                        node = m_secondaryLeftLaneNodes[nIndex];
                        break;
                    }
                }
            }

            if (!node)
            {
                for (int nIndex = 0; nIndex < m_secondaryRightLaneNodes.Count; nIndex++)
                {
                    if (m_secondaryRightLaneNodes[nIndex])
                    {
                        node = m_secondaryRightLaneNodes[nIndex];
                        break;
                    }
                }
            }
        }
        break;

        case 1:
        {
            for (int nIndex = 0; nIndex < m_primaryRightLaneNodes.Count; nIndex++)
            {
                if (m_primaryRightLaneNodes[nIndex])
                {
                    node = m_primaryRightLaneNodes[nIndex];
                    break;
                }
            }

            if (!node)
            {
                for (int nIndex = 0; nIndex < m_primaryLeftLaneNodes.Count; nIndex++)
                {
                    if (m_primaryLeftLaneNodes[nIndex])
                    {
                        node = m_primaryLeftLaneNodes[nIndex];
                        break;
                    }
                }
            }

            if (!node)
            {
                for (int nIndex = 0; nIndex < m_secondaryLeftLaneNodes.Count; nIndex++)
                {
                    if (m_secondaryLeftLaneNodes[nIndex])
                    {
                        node = m_secondaryLeftLaneNodes[nIndex];
                        break;
                    }
                }
            }

            if (!node)
            {
                for (int nIndex = 0; nIndex < m_secondaryRightLaneNodes.Count; nIndex++)
                {
                    if (m_secondaryRightLaneNodes[nIndex])
                    {
                        node = m_secondaryRightLaneNodes[nIndex];
                        break;
                    }
                }
            }
        }
        break;

        case 2:
        {
            for (int nIndex = 0; nIndex < m_primaryLeftLaneNodes.Count; nIndex++)
            {
                if (m_primaryLeftLaneNodes[nIndex])
                {
                    node = m_primaryLeftLaneNodes[nIndex];
                    break;
                }
            }

            if (!node)
            {
                for (int nIndex = 0; nIndex < m_secondaryLeftLaneNodes.Count; nIndex++)
                {
                    if (m_primaryRightLaneNodes[nIndex])
                    {
                        node = m_primaryRightLaneNodes[nIndex];
                        break;
                    }
                }
            }

            if (!node)
            {
                for (int nIndex = 0; nIndex < m_secondaryRightLaneNodes.Count; nIndex++)
                {
                    if (m_secondaryRightLaneNodes[nIndex])
                    {
                        node = m_secondaryRightLaneNodes[nIndex];
                        break;
                    }
                }
            }

            if (!node)
            {
                for (int nIndex = 0; nIndex < m_secondaryLeftLaneNodes.Count; nIndex++)
                {
                    if (m_secondaryLeftLaneNodes[nIndex])
                    {
                        node = m_secondaryLeftLaneNodes[nIndex];
                        break;
                    }
                }
            }
        }
        break;

        case 3:
        {
            for (int nIndex = 0; nIndex < m_primaryRightLaneNodes.Count; nIndex++)
            {
                if (m_primaryRightLaneNodes[nIndex])
                {
                    node = m_primaryRightLaneNodes[nIndex];
                    break;
                }
            }

            if (!node)
            {
                for (int nIndex = 0; nIndex < m_primaryLeftLaneNodes.Count; nIndex++)
                {
                    if (m_primaryLeftLaneNodes[nIndex])
                    {
                        node = m_primaryLeftLaneNodes[nIndex];
                        break;
                    }
                }
            }

            if (!node)
            {
                for (int nIndex = 0; nIndex < m_secondaryRightLaneNodes.Count; nIndex++)
                {
                    if (m_secondaryRightLaneNodes[nIndex])
                    {
                        node = m_secondaryRightLaneNodes[nIndex];
                        break;
                    }
                }
            }

            if (!node)
            {
                for (int nIndex = 0; nIndex < m_secondaryLeftLaneNodes.Count; nIndex++)
                {
                    if (m_secondaryLeftLaneNodes[nIndex])
                    {
                        node = m_secondaryLeftLaneNodes[nIndex];
                        break;
                    }
                }
            }
        }
        break;
        }

        if (node)
        {
            Vector3 pos = node.transform.position;
            pos -= a_vehiclePrefab.m_offsetPosVal;

            TrafficSystemVehicle vehicle = Instantiate(a_vehiclePrefab, pos, node.transform.rotation) as TrafficSystemVehicle;
            vehicle.m_nextNode    = node;
            vehicle.m_velocityMax = Random.Range(TrafficSystem.Instance.m_randVehicleVelocityMin, TrafficSystem.Instance.m_randVehicleVelocityMax);

            TrafficSystemNode nextNode = node.GetNextNode(vehicle, false);
            if (nextNode)
            {
                vehicle.transform.forward = nextNode.transform.position - vehicle.transform.position;
            }

//			TrafficSystem.Instance.RegisterVehicle( vehicle );
        }
    }
Esempio n. 18
0
 public void RemoveConnectedNode( TrafficSystemNode a_node )
 {
     m_connectedNodes.Remove(a_node);
 }
Esempio n. 19
0
    public void CalculatePathToTake( TrafficSystemNode a_nodeToPathTo )
    {
        if(!m_nextNode)
            return; // we are not connected to the road network

        if(m_calculatingPathToTake) // we are already calculating a path to find
            return;

        m_nodeToPathTo = a_nodeToPathTo;

        if(m_nodeToPathTo)
        {
            m_calculatingPathToTake = true;
            StartCoroutine( ProcessCalculatePathToTake() );
        }
    }
Esempio n. 20
0
    public void PerformCleanUp()
    {
        List <TrafficSystemNode> cleanList = new List <TrafficSystemNode>();

        for (int lIndex = 0; lIndex < m_primaryLeftLaneNodes.Count; lIndex++)
        {
            for (int cIndex = 0; cIndex < m_primaryLeftLaneNodes[lIndex].m_connectedNodes.Count; cIndex++)
            {
                TrafficSystemNode node = m_primaryLeftLaneNodes[lIndex].m_connectedNodes[cIndex];

                if (node)
                {
                    cleanList.Add(node);
                }
            }

            m_primaryLeftLaneNodes[lIndex].m_connectedNodes.Clear();

            for (int cIndex = 0; cIndex < cleanList.Count; cIndex++)
            {
                TrafficSystemNode node = cleanList[cIndex];

                if (node)
                {
                    bool found = false;
                    for (int nIndex = 0; nIndex < m_primaryLeftLaneNodes[lIndex].m_connectedNodes.Count; nIndex++)
                    {
                        TrafficSystemNode checkNode = m_primaryLeftLaneNodes[lIndex].m_connectedNodes[nIndex];

                        if (node == checkNode)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found && node)
                    {
                        m_primaryLeftLaneNodes[lIndex].m_connectedNodes.Add(node);
                    }
                }
            }

            cleanList.Clear();
        }

        cleanList.Clear();

        for (int lIndex = 0; lIndex < m_primaryLeftLaneNodes.Count; lIndex++)
        {
            for (int cIndex = 0; cIndex < m_primaryLeftLaneNodes[lIndex].m_connectedChangeLaneNodes.Count; cIndex++)
            {
                TrafficSystemNode node = m_primaryLeftLaneNodes[lIndex].m_connectedChangeLaneNodes[cIndex];

                if (node)
                {
                    cleanList.Add(node);
                }
            }

            m_primaryLeftLaneNodes[lIndex].m_connectedChangeLaneNodes.Clear();

            for (int cIndex = 0; cIndex < cleanList.Count; cIndex++)
            {
                TrafficSystemNode node = cleanList[cIndex];

                if (node)
                {
                    bool found = false;
                    for (int nIndex = 0; nIndex < m_primaryLeftLaneNodes[lIndex].m_connectedChangeLaneNodes.Count; nIndex++)
                    {
                        TrafficSystemNode checkNode = m_primaryLeftLaneNodes[lIndex].m_connectedChangeLaneNodes[nIndex];

                        if (node == checkNode)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found && node)
                    {
                        m_primaryLeftLaneNodes[lIndex].m_connectedChangeLaneNodes.Add(node);
                    }
                }
            }

            cleanList.Clear();
        }

        cleanList.Clear();

        for (int lIndex = 0; lIndex < m_primaryRightLaneNodes.Count; lIndex++)
        {
            for (int cIndex = 0; cIndex < m_primaryRightLaneNodes[lIndex].m_connectedNodes.Count; cIndex++)
            {
                TrafficSystemNode node = m_primaryRightLaneNodes[lIndex].m_connectedNodes[cIndex];

                if (node)
                {
                    cleanList.Add(node);
                }
            }

            m_primaryRightLaneNodes[lIndex].m_connectedNodes.Clear();

            for (int cIndex = 0; cIndex < cleanList.Count; cIndex++)
            {
                TrafficSystemNode node = cleanList[cIndex];

                if (node)
                {
                    bool found = false;
                    for (int nIndex = 0; nIndex < m_primaryRightLaneNodes[lIndex].m_connectedNodes.Count; nIndex++)
                    {
                        TrafficSystemNode checkNode = m_primaryRightLaneNodes[lIndex].m_connectedNodes[nIndex];

                        if (node == checkNode)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found && node)
                    {
                        m_primaryRightLaneNodes[lIndex].m_connectedNodes.Add(node);
                    }
                }
            }

            cleanList.Clear();
        }

        cleanList.Clear();

        for (int lIndex = 0; lIndex < m_primaryRightLaneNodes.Count; lIndex++)
        {
            for (int cIndex = 0; cIndex < m_primaryRightLaneNodes[lIndex].m_connectedChangeLaneNodes.Count; cIndex++)
            {
                TrafficSystemNode node = m_primaryRightLaneNodes[lIndex].m_connectedChangeLaneNodes[cIndex];

                if (node)
                {
                    cleanList.Add(node);
                }
            }

            m_primaryRightLaneNodes[lIndex].m_connectedChangeLaneNodes.Clear();

            for (int cIndex = 0; cIndex < cleanList.Count; cIndex++)
            {
                TrafficSystemNode node = cleanList[cIndex];

                if (node)
                {
                    bool found = false;
                    for (int nIndex = 0; nIndex < m_primaryRightLaneNodes[lIndex].m_connectedChangeLaneNodes.Count; nIndex++)
                    {
                        TrafficSystemNode checkNode = m_primaryRightLaneNodes[lIndex].m_connectedChangeLaneNodes[nIndex];

                        if (node == checkNode)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found && node)
                    {
                        m_primaryRightLaneNodes[lIndex].m_connectedChangeLaneNodes.Add(node);
                    }
                }
            }

            cleanList.Clear();
        }

        cleanList.Clear();

        for (int lIndex = 0; lIndex < m_secondaryLeftLaneNodes.Count; lIndex++)
        {
            for (int cIndex = 0; cIndex < m_secondaryLeftLaneNodes[lIndex].m_connectedNodes.Count; cIndex++)
            {
                TrafficSystemNode node = m_secondaryLeftLaneNodes[lIndex].m_connectedNodes[cIndex];

                if (node)
                {
                    cleanList.Add(node);
                }
            }

            m_secondaryLeftLaneNodes[lIndex].m_connectedNodes.Clear();

            for (int cIndex = 0; cIndex < cleanList.Count; cIndex++)
            {
                TrafficSystemNode node = cleanList[cIndex];

                if (node)
                {
                    bool found = false;
                    for (int nIndex = 0; nIndex < m_secondaryLeftLaneNodes[lIndex].m_connectedNodes.Count; nIndex++)
                    {
                        TrafficSystemNode checkNode = m_secondaryLeftLaneNodes[lIndex].m_connectedNodes[nIndex];

                        if (node == checkNode)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found && node)
                    {
                        m_secondaryLeftLaneNodes[lIndex].m_connectedNodes.Add(node);
                    }
                }
            }

            cleanList.Clear();
        }

        cleanList.Clear();

        for (int lIndex = 0; lIndex < m_secondaryLeftLaneNodes.Count; lIndex++)
        {
            for (int cIndex = 0; cIndex < m_secondaryLeftLaneNodes[lIndex].m_connectedChangeLaneNodes.Count; cIndex++)
            {
                TrafficSystemNode node = m_secondaryLeftLaneNodes[lIndex].m_connectedChangeLaneNodes[cIndex];

                if (node)
                {
                    cleanList.Add(node);
                }
            }

            m_secondaryLeftLaneNodes[lIndex].m_connectedChangeLaneNodes.Clear();

            for (int cIndex = 0; cIndex < cleanList.Count; cIndex++)
            {
                TrafficSystemNode node = cleanList[cIndex];

                if (node)
                {
                    bool found = false;
                    for (int nIndex = 0; nIndex < m_secondaryLeftLaneNodes[lIndex].m_connectedChangeLaneNodes.Count; nIndex++)
                    {
                        TrafficSystemNode checkNode = m_secondaryLeftLaneNodes[lIndex].m_connectedChangeLaneNodes[nIndex];

                        if (node == checkNode)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found && node)
                    {
                        m_secondaryLeftLaneNodes[lIndex].m_connectedChangeLaneNodes.Add(node);
                    }
                }
            }

            cleanList.Clear();
        }

        cleanList.Clear();

        for (int lIndex = 0; lIndex < m_secondaryRightLaneNodes.Count; lIndex++)
        {
            for (int cIndex = 0; cIndex < m_secondaryRightLaneNodes[lIndex].m_connectedNodes.Count; cIndex++)
            {
                TrafficSystemNode node = m_secondaryRightLaneNodes[lIndex].m_connectedNodes[cIndex];

                if (node)
                {
                    cleanList.Add(node);
                }
            }

            m_secondaryRightLaneNodes[lIndex].m_connectedNodes.Clear();

            for (int cIndex = 0; cIndex < cleanList.Count; cIndex++)
            {
                TrafficSystemNode node = cleanList[cIndex];

                if (node)
                {
                    bool found = false;
                    for (int nIndex = 0; nIndex < m_secondaryRightLaneNodes[lIndex].m_connectedNodes.Count; nIndex++)
                    {
                        TrafficSystemNode checkNode = m_secondaryRightLaneNodes[lIndex].m_connectedNodes[nIndex];

                        if (node == checkNode)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found && node)
                    {
                        m_secondaryRightLaneNodes[lIndex].m_connectedNodes.Add(node);
                    }
                }
            }

            cleanList.Clear();
        }

        cleanList.Clear();

        for (int lIndex = 0; lIndex < m_secondaryRightLaneNodes.Count; lIndex++)
        {
            for (int cIndex = 0; cIndex < m_secondaryRightLaneNodes[lIndex].m_connectedChangeLaneNodes.Count; cIndex++)
            {
                TrafficSystemNode node = m_secondaryRightLaneNodes[lIndex].m_connectedChangeLaneNodes[cIndex];

                if (node)
                {
                    cleanList.Add(node);
                }
            }

            m_secondaryRightLaneNodes[lIndex].m_connectedChangeLaneNodes.Clear();

            for (int cIndex = 0; cIndex < cleanList.Count; cIndex++)
            {
                TrafficSystemNode node = cleanList[cIndex];

                if (node)
                {
                    bool found = false;
                    for (int nIndex = 0; nIndex < m_secondaryRightLaneNodes[lIndex].m_connectedChangeLaneNodes.Count; nIndex++)
                    {
                        TrafficSystemNode checkNode = m_secondaryRightLaneNodes[lIndex].m_connectedChangeLaneNodes[nIndex];

                        if (node == checkNode)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found && node)
                    {
                        m_secondaryRightLaneNodes[lIndex].m_connectedChangeLaneNodes.Add(node);
                    }
                }
            }

            cleanList.Clear();
        }

        cleanList.Clear();

        for (int lIndex = 0; lIndex < m_leftLaneChangeNodes.Count; lIndex++)
        {
            for (int cIndex = 0; cIndex < m_leftLaneChangeNodes[lIndex].m_connectedNodes.Count; cIndex++)
            {
                TrafficSystemNode node = m_leftLaneChangeNodes[lIndex].m_connectedNodes[cIndex];

                if (node)
                {
                    cleanList.Add(node);
                }
            }

            m_leftLaneChangeNodes[lIndex].m_connectedNodes.Clear();

            for (int cIndex = 0; cIndex < cleanList.Count; cIndex++)
            {
                TrafficSystemNode node = cleanList[cIndex];

                if (node)
                {
                    bool found = false;
                    for (int nIndex = 0; nIndex < m_leftLaneChangeNodes[lIndex].m_connectedNodes.Count; nIndex++)
                    {
                        TrafficSystemNode checkNode = m_leftLaneChangeNodes[lIndex].m_connectedNodes[nIndex];

                        if (node == checkNode)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found && node)
                    {
                        m_leftLaneChangeNodes[lIndex].m_connectedNodes.Add(node);
                    }
                }
            }

            cleanList.Clear();
        }

        cleanList.Clear();

        for (int lIndex = 0; lIndex < m_leftLaneChangeNodes.Count; lIndex++)
        {
            for (int cIndex = 0; cIndex < m_leftLaneChangeNodes[lIndex].m_connectedChangeLaneNodes.Count; cIndex++)
            {
                TrafficSystemNode node = m_leftLaneChangeNodes[lIndex].m_connectedChangeLaneNodes[cIndex];

                if (node)
                {
                    cleanList.Add(node);
                }
            }

            m_leftLaneChangeNodes[lIndex].m_connectedChangeLaneNodes.Clear();

            for (int cIndex = 0; cIndex < cleanList.Count; cIndex++)
            {
                TrafficSystemNode node = cleanList[cIndex];

                if (node)
                {
                    bool found = false;
                    for (int nIndex = 0; nIndex < m_leftLaneChangeNodes[lIndex].m_connectedChangeLaneNodes.Count; nIndex++)
                    {
                        TrafficSystemNode checkNode = m_leftLaneChangeNodes[lIndex].m_connectedChangeLaneNodes[nIndex];

                        if (node == checkNode)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found && node)
                    {
                        m_leftLaneChangeNodes[lIndex].m_connectedChangeLaneNodes.Add(node);
                    }
                }
            }

            cleanList.Clear();
        }

        cleanList.Clear();

        for (int lIndex = 0; lIndex < m_rightLaneChangeNodes.Count; lIndex++)
        {
            for (int cIndex = 0; cIndex < m_rightLaneChangeNodes[lIndex].m_connectedNodes.Count; cIndex++)
            {
                TrafficSystemNode node = m_rightLaneChangeNodes[lIndex].m_connectedNodes[cIndex];

                if (node)
                {
                    cleanList.Add(node);
                }
            }

            m_rightLaneChangeNodes[lIndex].m_connectedNodes.Clear();

            for (int cIndex = 0; cIndex < cleanList.Count; cIndex++)
            {
                TrafficSystemNode node = cleanList[cIndex];

                if (node)
                {
                    bool found = false;
                    for (int nIndex = 0; nIndex < m_rightLaneChangeNodes[lIndex].m_connectedNodes.Count; nIndex++)
                    {
                        TrafficSystemNode checkNode = m_rightLaneChangeNodes[lIndex].m_connectedNodes[nIndex];

                        if (node == checkNode)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found && node)
                    {
                        m_rightLaneChangeNodes[lIndex].m_connectedNodes.Add(node);
                    }
                }
            }

            cleanList.Clear();
        }

        cleanList.Clear();

        for (int lIndex = 0; lIndex < m_rightLaneChangeNodes.Count; lIndex++)
        {
            for (int cIndex = 0; cIndex < m_rightLaneChangeNodes[lIndex].m_connectedChangeLaneNodes.Count; cIndex++)
            {
                TrafficSystemNode node = m_rightLaneChangeNodes[lIndex].m_connectedChangeLaneNodes[cIndex];

                if (node)
                {
                    cleanList.Add(node);
                }
            }

            m_rightLaneChangeNodes[lIndex].m_connectedChangeLaneNodes.Clear();

            for (int cIndex = 0; cIndex < cleanList.Count; cIndex++)
            {
                TrafficSystemNode node = cleanList[cIndex];

                if (node)
                {
                    bool found = false;
                    for (int nIndex = 0; nIndex < m_rightLaneChangeNodes[lIndex].m_connectedChangeLaneNodes.Count; nIndex++)
                    {
                        TrafficSystemNode checkNode = m_rightLaneChangeNodes[lIndex].m_connectedChangeLaneNodes[nIndex];

                        if (node == checkNode)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found && node)
                    {
                        m_rightLaneChangeNodes[lIndex].m_connectedChangeLaneNodes.Add(node);
                    }
                }
            }

            cleanList.Clear();
        }

        cleanList.Clear();
    }
Esempio n. 21
0
 public void RemoveChangeLangeNode(TrafficSystemNode a_node)
 {
     m_connectedChangeLaneNodes.Remove(a_node);
 }
    protected void SetNextNode( TrafficSystemNode a_node )
    {
        m_nodeHighwaySearch            = null;
        TrafficSystemNode previousNode = m_nextNode;
        m_nextNode                     = a_node;

        if(!m_nextNode)
        {
            Debug.LogWarning("Vehicle node invalid, check all nodes on this road piece have correct links as this is probably the issue!: \"" + previousNode.Parent.name + "\" -> Previous Node was: \"" + previousNode.name + "\"\nTIP: In \"Editor Mode\" try clicking on the road piece then clicking off it as this should remove any null links.");

            if(m_killOnEmptyPath)
                Debug.Log("Vehicle set to kill on empty path so we will destroy vehicle " + gameObject.name);
        }
        else
        {
            m_velocityMaxOriginal = m_nextNode.GetSpeedLimit() + m_speedVariation;
            m_velocityMax         = m_velocityMaxOriginal;
        }
    }
Esempio n. 23
0
    public TrafficSystemNode GetNextNode(TrafficSystemVehicle a_vehicle, bool a_checkLocalConnectedNode = true, List <TrafficSystemNode> a_blockedNodes = null)
    {
        if (m_connectedNodes.Count <= 0 && !m_connectedLocalNode && m_connectedChangeLaneNodes.Count <= 0)
        {
            return(null);
        }

        if (a_checkLocalConnectedNode && m_connectedLocalNode)
        {
            return(m_connectedLocalNode);
        }

        float randomChanceUseOfframp  = Random.Range(0.0f, 1.0f);
        float randomChanceOfDirChange = Random.Range(0.0f, 1.0f);

        if (m_connectedChangeLaneNodes.Count > 0)
        {
            if (randomChanceOfDirChange <= a_vehicle.m_chanceOfDirChange)
            {
                TrafficSystemNode node = GetRandomChangeLangeNode();

                if (node)
                {
                    return(node);
                }
            }
            else if (m_connectedNodes.Count <= 0 && !m_connectedLocalNode)
            {
                TrafficSystemNode node = GetRandomChangeLangeNode();

                if (node)
                {
                    return(node);
                }
            }
        }

        TrafficSystemNode offrampNode = null;

        for (int nIndex = 0; nIndex < m_connectedNodes.Count; nIndex++)
        {
            TrafficSystemNode node = m_connectedNodes[nIndex];
            if (node && node.m_roadType == TrafficSystem.RoadType.OFFRAMP)
            {
                offrampNode = node;
                break;
            }
        }

        if (a_vehicle.m_randomLaneChange > 0.0f && m_connectedNodes.Count > 1)
        {
            float randomChance = Random.Range(0.0f, 1.0f);

            if (randomChance <= a_vehicle.m_randomLaneChange)
            {
                List <int> nodeIndex = new List <int>(m_connectedNodes.Count);
                for (int nIndex = 0; nIndex < m_connectedNodes.Count; nIndex++)
                {
                    nodeIndex.Insert(nIndex, nIndex);
                }

                TrafficSystemNode sameLaneNode = null;
                int count = 0;
                while (count < m_connectedNodes.Count)
                {
                    int nIndex = Random.Range(0, nodeIndex.Count);
                    int lIndex = nodeIndex[nIndex];

                    TrafficSystemNode node = m_connectedNodes[lIndex];

                    if (node)
                    {
                        bool blockNode = false;
                        if (a_blockedNodes != null)
                        {
                            for (int cIndex = 0; cIndex < a_blockedNodes.Count; cIndex++)
                            {
                                if (node == a_blockedNodes[cIndex])
                                {
                                    blockNode = true;
                                    break;
                                }
                            }
                        }

                        if (!blockNode)
                        {
                            if (node.m_lane != m_lane || (node.m_lane == m_lane && node.m_roadType == TrafficSystem.RoadType.OFFRAMP))
                            {
                                return(node);
                            }
                            else
                            {
                                sameLaneNode = node;
                            }
                        }
                    }

                    nodeIndex.Remove(nIndex);
                    count++;
                }

                if (sameLaneNode)
                {
                    return(sameLaneNode);
                }
            }
            else
            {
                for (int nIndex = 0; nIndex < m_connectedNodes.Count; nIndex++)
                {
                    TrafficSystemNode node = m_connectedNodes[nIndex];

                    if (node && node.m_lane == m_lane)
                    {
                        bool blockNode = false;
                        if (a_blockedNodes != null)
                        {
                            for (int cIndex = 0; cIndex < a_blockedNodes.Count; cIndex++)
                            {
                                if (node == a_blockedNodes[cIndex])
                                {
                                    blockNode = true;
                                    break;
                                }
                            }
                        }

                        if (!blockNode)
                        {
                            if (randomChanceUseOfframp <= a_vehicle.m_chanceOfUsingOfframp)
                            {
                                if (offrampNode)
                                {
                                    return(offrampNode);
                                }
                            }
                            else if (node.IsNormalRoad())
                            {
                                return(node);
                            }
                        }
                    }
                }
            }
        }

        TrafficSystemNode nextNodeInOurLane    = null;
        TrafficSystemNode nextNodeNotInOurLane = null;

        for (int nIndex = 0; nIndex < m_connectedNodes.Count; nIndex++)
        {
            TrafficSystemNode node = m_connectedNodes[nIndex];

            if (node)
            {
                bool blockNode = false;
                if (a_blockedNodes != null && m_connectedNodes.Count > 1)
                {
                    for (int cIndex = 0; cIndex < a_blockedNodes.Count; cIndex++)
                    {
                        if (node == a_blockedNodes[cIndex])
                        {
                            blockNode = true;
                            break;
                        }
                    }
                }

                if (!blockNode)
                {
                    if (node.m_lane == m_lane)
                    {
                        if (randomChanceUseOfframp <= a_vehicle.m_chanceOfUsingOfframp)
                        {
                            if (offrampNode)
                            {
                                return(offrampNode);
                            }
                            else if (node.IsNormalRoad())
                            {
                                return(node);
                            }
                            else
                            {
                                nextNodeInOurLane = node;
                            }
                        }
                        else if (node.IsNormalRoad())
                        {
                            return(node);
                        }
                        else
                        {
                            nextNodeNotInOurLane = node;
                        }
                    }
                    else
                    {
                        nextNodeNotInOurLane = node;
                    }
                }
            }
        }

        if (nextNodeInOurLane)
        {
            return(nextNodeInOurLane);
        }
        else
        {
            return(nextNodeNotInOurLane);
        }
    }
    public TrafficSystemVehicle VehicleExistsOnNode( TrafficSystemNode a_node )
    {
        if(!a_node)
            return null;

        RaycastHit[] hitInfo;
        hitInfo = Physics.SphereCastAll( a_node.transform.position, 2.0f, transform.forward, 0.0f );

        for(int hIndex = 0; hIndex < hitInfo.Length; hIndex++)
        {
            TrafficSystemVehicle vehicle = hitInfo[hIndex].transform.GetComponent<TrafficSystemVehicle>();
            if(vehicle && vehicle != this && vehicle.m_velocity <= 0.0f)
                return vehicle;
        }

        return null;
    }
Esempio n. 25
0
 void Awake()
 {
     TrafficSystemNode   = (TrafficSystemNode)target;
     m_previousDriveSide = TrafficSystemNode.m_driveSide;
 }