/// <summary>
        /// Add a state to the statemachine.
        /// </summary>
        public ResultCode AddState(BaseState state)
        {
            if (state == null)
            {
                return ResultCode.StateDoesNotExist;
            }

            if (!this.states.ContainsKey(state.Name))
            {
                this.states.Add(state.Name, state);
                state.Statemachine = this;
                return ResultCode.StateAdded;
            }

            return ResultCode.StateAlreadyExists;
        }
示例#2
0
 //if desired you can overwrite it and do some transition conditions
 public virtual bool IsTransitionAllowed(BaseState target)
 {
     return true;
 }
        /// <summary>
        /// Called when state BaseState.End() is ready.
        /// </summary>
        private void StateEndCallBack()
        {
            this.previousState = this.activeState;
            this.activeState = this.nextState;

            if (StateChanged != null)
            {
                StateChanged(this.previousState, this.activeState);
            }

            this.activeState.Start(StateStartCallBack);
        }
        /// <summary>
        /// Switch the state and call BaseState.Start(), BaseState.End() and set the previousState.
        /// </summary>
        protected virtual ResultCode SwitchState(BaseState next)
        {
            if (next == null)
            {
                return ResultCode.StateDoesNotExist;
            }

            if (this.activeState != null)
            {
                this.nextState = next;
                this.isInTransition = true;
                this.activeState.End(StateEndCallBack);

                return ResultCode.StateTransitionActivated;
            }

            this.activeState = next;
            this.activeState.Start(StateStartCallBack);

            return ResultCode.StateTransitionActivated;
        }