public TrafficLightYellowState(TrafficLight trafficLight, Light yellowLight) { _trafficLight = trafficLight; _yellowLight = yellowLight; _trafficLight.OnLightStateChanged += (previousLight) => _previousLight = previousLight; }
void Start() { YellowState = new TrafficLightYellowState(this, _yellowLight); RedState = new TrafficLightRedState(this, _redLight); GreenState = new TrafficLightGreenState(this, _greenLight); _currentState = GreenState; _currentState.Enter(); }
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!"); }
/// <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; }
public void Update() { _state = _state.Change(); }
public TrafficLight() { _state = new RedLight(); }
public TrafficLightV2(ITrafficLightState trafficLightState) { State = trafficLightState; }
internal async Task MoveNext(ITrafficLightState state = null) { this.State = state ?? new GreenState(); await this.State.Handle(this); }
public TrafficLight(ITrafficLightState ws) { State = ws; }