示例#1
0
        /// <summary>
        /// Change companion state machine state
        /// </summary>
        /// <param name="stateFlag">Flag of allowed state</param>
        private void ChangeState(StateFlag stateFlag)
        {
            if (this.States == null)
            {
                throw new InvalidStateException("State machine is not ready! Call setup first.");
            }

            if (!this.States.TryGetValue(stateFlag, out ICompanionState newState))
            {
                throw new InvalidStateException($"Invalid state {stateFlag.ToString()}. Is state machine correctly set up?");
            }

            if (this.currentState == newState)
            {
                return;
            }

            if (this.currentState != null)
            {
                this.currentState.Exit();
            }

            newState.Entry();
            this.currentState = newState;
            this.Monitor.Log($"{this.Name} changed state: {this.CurrentStateFlag.ToString()} -> {stateFlag.ToString()}");
            this.CurrentStateFlag = stateFlag;
        }