void AdvanceState(float lengthToWait, TrafficState currentState)
    {
        timer -= Time.deltaTime;
        if (timer <= 0)
        {
            switch (currentState)
            {
            case TrafficState.Green:
                myState = TrafficState.Yellow;
                this.gameObject.GetComponent <MeshRenderer>().material.color = Color.yellow;
                timer = 4f;
                break;

            case TrafficState.Yellow:
                myState = TrafficState.Red;
                this.gameObject.GetComponent <MeshRenderer>().material.color = Color.red;
                timer = (int)Random.Range(5, 10f);
                break;

            case TrafficState.Red:
                myState = TrafficState.Green;
                this.gameObject.GetComponent <MeshRenderer>().material.color = Color.green;
                timer = (int)Random.Range(5, 10f);
                break;
            }
        }
    }
    private void Awake()
    {
        int Picker = Random.Range(0, 3);

        switch (Picker)
        {
        case 0:
            myState = TrafficState.Green;
            this.gameObject.GetComponent <MeshRenderer>().material.color = Color.green;
            timer = (int)Random.Range(5, 10f);
            break;

        case 1:
            myState = TrafficState.Yellow;
            this.gameObject.GetComponent <MeshRenderer>().material.color = Color.yellow;
            timer = 4f;
            break;

        case 2:
            myState = TrafficState.Red;
            this.gameObject.GetComponent <MeshRenderer>().material.color = Color.red;
            timer = (int)Random.Range(5, 10f);
            break;
        }
    }
Exemple #3
0
        public CarTrafficLight(int id, TrafficState defaultTrafficState = TrafficState.STOP, GpioController gpioController = null, int?redPin = null, int?yellowPin = null, int?greenPin = null)
        {
            bool stop = defaultTrafficState == TrafficLight.TrafficState.STOP;

            RedLight            = new LightUnit("Red", InactiveColor, stop, gpioController, redPin);
            YellowLight         = new LightUnit("Yellow", InactiveColor, false, gpioController, yellowPin);
            GreenLight          = new LightUnit("Green", InactiveColor, !stop, gpioController, greenPin);
            Id                  = id;
            TrafficTrafficState = defaultTrafficState;
        }
 // Use this for initialization
 void Start()
 {
     blinkCount   = 0;
     currentState = TrafficState.CarStopPedWarn;
     carRed       = this.transform.Find("CarRed").gameObject;
     carYellow    = this.transform.Find("CarYellow").gameObject;
     carGreen     = this.transform.Find("CarGreen").gameObject;
     carGreenLeft = this.transform.Find("CarGreenLeft").gameObject;
     pedRed       = this.transform.Find("PedRed").gameObject;
     pedGreen     = this.transform.Find("PedGreen").gameObject;
 }
Exemple #5
0
 void setAllTurnLightsOn(TrafficState givenState)
 {
     for (int i = 0; i < trafficLights.Count; i++)
     {
         trafficLights[i].currentState = givenState;
     }
     for (int i = 0; i < 4; i++)
     {
         currentStates[i] = givenState;
     }
 }
Exemple #6
0
    /// <summary>
    /// Default-Konstruktor
    /// </summary>
    /// <param name="stopTime">Counter für die Rot-Phase</param>
    /// <param name="waitTime">Counter für die Rot-Gelb-Phase</param>
    /// <param name="goTime">Counter für die Grün-Phase</param>
    /// <param name="attentionTime">Counter für die Gelb-Phase</param>
    /// <param name="startState">Anfangszustand der Ampel</param>
    /// <param name="logs">Ausgaben auf der Unity-Konsole?</param>
    public TrafficLight(int stopTime, int waitTime,
                        int goTime, int attentionTime,
                        TrafficState startState,
                        bool logs)
    {
        LogOutput = logs;

        TimeForStop      = stopTime;
        TimeForWait      = waitTime;
        TimeForGo        = goTime;
        TimeForAttention = attentionTime;

        Counter                  = 0;
        CurrentState             = startState;
        CurrentState.DebugOutput = LogOutput;
        CurrentState.OnStateEntered();
    }
Exemple #7
0
    // Update is called once per frame
    void Update()
    {
        durationSpentInCurrentState += Time.deltaTime;
        if (durationSpentInCurrentState >= signalStateDuration)
        {
            durationSpentInCurrentState = 0.0f;
            state = (TrafficState)(((int)state + 1) % (int)TrafficState.NumStates);
            Color c = colors[(int)state];

            light.material.shader = Shader.Find("_Color");
            light.material.SetColor("_Color", c);

            //Find the Specular shader and change its Color to red
            light.material.shader = Shader.Find("Specular");
            light.material.SetColor("_SpecColor", c);
        }
    }
Exemple #8
0
        public async void ToggleState()
        {
            if (TrafficTrafficState == TrafficState.STOP)
            {
                TrafficTrafficState = TrafficLight.TrafficState.READY;

                await Task.Delay(4000);

                TrafficTrafficState = TrafficLight.TrafficState.GO;
                RedLight.ToggleState();
                GreenLight.ToggleState();
                return;
            }

            TrafficTrafficState = TrafficState.READY;
            GreenLight.ToggleState();
            YellowLight.ToggleState();
            await Task.Delay(3000);

            TrafficTrafficState = TrafficState.STOP;
            YellowLight.ToggleState();
            RedLight.ToggleState();
        }
Exemple #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TrafficControl"/> class.
 /// </summary>
 public TrafficControl()
 {
     this.DefaultStyleKey = typeof(TrafficControl);
     this.currentState    = TrafficState.Off;
     this.Loaded         += this.TrafficControlLoaded;
 }
Exemple #10
0
 /// <summary>
 /// The transition to state method.
 /// </summary>
 /// <param name="newState">
 /// The new state.
 /// </param>
 private void TransitionToState(TrafficState newState)
 {
     this.currentState = newState;
     VisualStateManager.GoToState(this, Enum.GetName(typeof(TrafficState), newState), true);
 }
Exemple #11
0
    // Update is called once per frame

    void Update()
    {
        TrafficState state = TrafficState.Red;
        RaycastHit   hit;

        //Debug.DrawRay(agent.transform.position, agent.transform.forward, Color.yellow, 0.0f, false);
        if (Physics.Raycast(agent.transform.position, agent.transform.forward, out hit, 1.0f))//, DefaultRaycastLayers, QueryTriggerInteraction.Collide))
        {
            //Debug.Log(this.name + " detected " + hit.collider.name + " having tag " + hit.collider.tag);
            if (hit.collider.tag == "TrafficSignal")
            {
                lastSeenTrafficSignal = hit.collider.gameObject;
                state = lastSeenTrafficSignal.GetComponent <TrafficSignal>().state;
            }
        }

        if (waitingOnSignal && lastSeenTrafficSignal.GetComponent <TrafficSignal>().state == TrafficState.Red)
        {
            waitingOnSignal = false;
            agent.velocity  = lastAgentVelocity;
            agent.SetPath(lastAgentPath);
        }
        else
        {
            NavMeshHit lookAheadPosition, currentPosition;
            agent.SamplePathPosition(~zebraCrossPathMask, 1.0f, out lookAheadPosition);
            agent.SamplePathPosition(~zebraCrossPathMask, 0.01f, out currentPosition);

            bool zebraCrossAhead = lookAheadPosition.hit;
            bool alreadyCrossing = currentPosition.hit;

            if (state == TrafficState.Green && zebraCrossAhead && !alreadyCrossing)
            {
                lastAgentVelocity = agent.velocity;
                lastAgentPath     = agent.path;
                waitingOnSignal   = true;
                agent.velocity    = Vector3.zero;
                agent.ResetPath();
            }
        }

        Color c = Color.white;

        if (waitingOnSignal && state == TrafficState.Red)
        {
            c = Color.red;
        }
        else if (waitingOnSignal && state == TrafficState.Green)
        {
            c = Color.blue;
        }
        else if (!waitingOnSignal && state == TrafficState.Red)
        {
            c = Color.yellow;
        }
        else if (!waitingOnSignal && state == TrafficState.Green)
        {
            c = Color.green;
        }


        //Color c = waitingOnSignal ? Color.red : Color.green;
        color.material.shader = Shader.Find("_Color");
        color.material.SetColor("_Color", c);

        //Find the Specular shader and change its Color to red
        color.material.shader = Shader.Find("Specular");
        color.material.SetColor("_SpecColor", c);
    }
 public void ChangeState(TrafficState state)
 {
     this.panelRed.BackColor    = Color.FromArgb(state.HasFlag(TrafficState.Stop) ? alphaOn : alphaOff, Color.Red);
     this.panelYellow.BackColor = Color.FromArgb(state.HasFlag(TrafficState.Warn) ? alphaOn : alphaOff, Color.Yellow);
     this.panelGreen.BackColor  = Color.FromArgb(state.HasFlag(TrafficState.Go) ? alphaOn : alphaOff, Color.Green);
 }
 private void SetNextStateTo(TrafficState state)
 {
     NextTrafficState = state;
 }
Exemple #14
0
 public PedestrianLight(int id, TrafficState defaultTrafficState = TrafficLight.TrafficState.STOP,
                        GpioController gpioController            = null, int?redPin = default(int?),
                        int?yellowPin = default(int?), int?greenPin                 = default(int?)) : base(id, defaultTrafficState, gpioController, redPin, yellowPin, greenPin)
 {
 }
 private void SetNextStateTo(TrafficState state)
 {
     NextTrafficState = state;
 }
    // Update is called once per frame
    void Foo()
    {
        agent.transform.localPosition = new Vector3();
        agent.transform.rotation      = new Quaternion();

        TrafficState state = TrafficState.Green;

        Collider[] colliders = Physics.OverlapSphere(agent.transform.position, 20.0f);
        for (int i = 0; i < colliders.Length; i++)
        {
            if (colliders[i].tag == "TrafficSignal")
            {
                state  = colliders[i].gameObject.GetComponent <TrafficSignal>().state;
                signal = colliders[i].gameObject;
                break;
            }
        }

        waitingOnSignal = state == TrafficState.Red;
        if (signal && Vector3.Dot(signal.transform.position - car.transform.position, car.transform.forward) < 0.0f)
        {
            waitingOnSignal = false;
        }

        NavMeshHit currentPosition;

        agent.SamplePathPosition(~zebraCrossPathMask, 0.01f, out currentPosition);
        bool alreadyCrossing = currentPosition.hit;

        if (alreadyCrossing)
        {
            waitingOnSignal = false;
        }


        if (waitingOnSignal || Vector3.Distance(car.transform.position, destination.position) <= agent.stoppingDistance)
        {
            steering  = 0.0f;
            speed     = 0.0f;
            handbrake = 1.0f;
        }
        else
        {
            Vector2 to   = new Vector2(agent.transform.forward.x, agent.transform.forward.z);
            Vector2 from = new Vector2(agent.desiredVelocity.x, agent.desiredVelocity.z);
            from = from.normalized;
            to   = to.normalized;

            turningFactor = Vector2.Dot(from, to);
            turningFactor = Mathf.Sign(turningFactor) * Mathf.Clamp(Mathf.Abs(turningFactor), 0.35f, 1.0f);

            speed = agent.desiredVelocity.magnitude * turningFactor;
            speed = speed / agent.speed;

            steeringAngle = Vector2.SignedAngle(from, to);
            steering      = RemapRange(-carController.m_MaximumSteerAngle, carController.m_MaximumSteerAngle, -1, 1, steeringAngle);
            if (turningFactor < 0.0f)
            {
                steering = steering * -1.0f;
            }
            handbrake = 0.0f;
        }

        car.GetComponent <CarController>().Move(steering, speed, speed, handbrake);

        if (draw)
        {
            DebugDrawPath(agent.path);
            Debug.DrawRay(agent.transform.position, agent.desiredVelocity, Color.cyan, 0.0f, false);
            Debug.DrawRay(agent.transform.position, agent.transform.forward * Vector3.Magnitude(agent.desiredVelocity), Color.yellow, 0.0f, false);
        }
    }
Exemple #17
0
 public void SetState(TrafficState state)
 {
     TrafficLight = state;
     TrafficLight.StateStart(GreenLight, YellowLight, RedLight);
 }