示例#1
0
        public TrafficLightYellowState(TrafficLight trafficLight, Light yellowLight)
        {
            _trafficLight = trafficLight;
            _yellowLight  = yellowLight;

            _trafficLight.OnLightStateChanged += (previousLight) => _previousLight = previousLight;
        }
示例#2
0
        void Start()
        {
            YellowState = new TrafficLightYellowState(this, _yellowLight);
            RedState    = new TrafficLightRedState(this, _redLight);
            GreenState  = new TrafficLightGreenState(this, _greenLight);

            _currentState = GreenState;
            _currentState.Enter();
        }
示例#3
0
        public void ChangeState(ITrafficLightState state)
        {
            if (OnLightStateChanged != null)
            {
                OnLightStateChanged.Invoke(_currentState);
            }

            _currentState.Exit();
            _currentState = state;
            state.Enter();
        }
 /// <summary>
 /// 設置燈號
 /// </summary>
 /// <param name="state">燈號狀態</param>
 /// <returns>燈號</returns>
 public LightStateEnum SetState(ITrafficLightState state)
 {
     if (!state.CheckContext(this.State))
     {
         return(LightStateEnum.Null);
     }
     else
     {
         this.State = state;
         return(this.State.GetLightState());
     }
 }
     protected void Toggle()
     {
         Lights = Lights switch
         {
             StopState _ => new GetReadyToGoState(),
             GetReadyToGoState _ => new GoState(),
             GoState _ => new GetReadyToStopState(),
             GetReadyToStopState _ => new StopState(),
                       _ => throw new ArgumentOutOfRangeException(nameof(Lights))
         };
     }
 }
 /// <summary>
 /// Ctor for <see cref="YellowState"/> that takes previous <see cref="TrafficLight"/> state
 /// </summary>
 /// <param name="state">previous <see cref="TrafficLight"/> state</param>
 /// <exception cref="ArgumentNullException">Throws when <paramref name="state"/> is equal to null</exception>
 public YellowState(ITrafficLightState state)
 {
     _previousState = state ?? throw new ArgumentNullException($"{nameof(state)} can't be equal to null!");
 }
示例#7
0
 /// <summary>
 /// 檢查當前狀態是否能進入目標狀態
 /// </summary>
 /// <param name="state">當前狀態</param>
 /// <returns>檢查結果boolean</returns>
 public bool CheckContext(ITrafficLightState state)
 {
     return(state.GetLightState() == LightStateEnum.Red);
 }
 /// <summary>
 /// Standart ctor for <see cref="TrafficLight"/>
 /// </summary>
 public TrafficLight()
 {
     State = new GreenState();
 }
 /// <summary>
 /// 切換燈號
 /// </summary>
 /// <returns>回傳切換後的燈號</returns>
 public LightStateEnum Request()
 {
     this.State = this.State.Handle();
     return(this.State.GetLightState());
 }
 /// <summary>
 /// 建構子
 /// </summary>
 /// <param name="state">燈號狀態</param>
 public TrafficLightContext(ITrafficLightState state)
 {
     this.State = state;
 }
示例#11
0
 public void Update()
 {
     _state = _state.Change();
 }
示例#12
0
 public TrafficLight()
 {
     _state = new RedLight();
 }
示例#13
0
 public TrafficLightV2(ITrafficLightState trafficLightState)
 {
     State = trafficLightState;
 }
示例#14
0
 internal async Task MoveNext(ITrafficLightState state = null)
 {
     this.State = state ?? new GreenState();
     await this.State.Handle(this);
 }
示例#15
0
 public TrafficLight(ITrafficLightState ws)
 {
     State = ws;
 }