/// <summary>
        /// Adds a new transition that can happen from any possible state
        /// </summary>
        /// <param name="transition">The transition instance; The "from" field can be
        /// left empty, as it has no meaning in this context.</param>
        public void AddTransitionFromAny(TransitionBase transition)
        {
            transition.fsm  = this;
            transition.mono = mono;

            transition.Init();

            transitionsFromAny.Add(transition);
        }
 public WrappedTransition Wrap(TransitionBase <TStateId> transition)
 {
     return(new WrappedTransition(
                transition,
                beforeOnEnter,
                afterOnEnter,
                beforeShouldTransition,
                afterShouldTransition
                ));
 }
        /// <summary>
        /// Checks if a transition can take place, and if this is the case, transition to the
        /// "to" state and return true. Otherwise it returns false
        /// </summary>
        /// <param name="transition"></param>
        /// <returns></returns>
        private bool TryTransition(TransitionBase transition)
        {
            if (!transition.ShouldTransition())
            {
                return(false);
            }

            RequestStateChange(transition.to, transition.forceInstantly);

            return(true);
        }
        /// <summary>
        /// Adds a new transition between two states
        /// </summary>
        /// <param name="transition">The transition instance</param>
        public void AddTransition(TransitionBase transition)
        {
            if (!transitions.ContainsKey(transition.from))
            {
                transitions[transition.from] = new List <TransitionBase>();
            }

            transition.fsm  = this;
            transition.mono = mono;

            transitions[transition.from].Add(transition);
        }
Beispiel #5
0
            public void AddTriggerTransition(TEvent trigger, TransitionBase <TStateId> transition)
            {
                triggerToTransitions = triggerToTransitions
                                       ?? new Dictionary <TEvent, List <TransitionBase <TStateId> > >();

                List <TransitionBase <TStateId> > transitionsOfTrigger;

                if (!triggerToTransitions.TryGetValue(trigger, out transitionsOfTrigger))
                {
                    transitionsOfTrigger = new List <TransitionBase <TStateId> >();
                    triggerToTransitions.Add(trigger, transitionsOfTrigger);
                }

                transitionsOfTrigger.Add(transition);
            }
            public WrappedTransition(
                TransitionBase <TStateId> transition,

                Action <TransitionBase <TStateId> > beforeOnEnter = null,
                Action <TransitionBase <TStateId> > afterOnEnter  = null,

                Action <TransitionBase <TStateId> > beforeShouldTransition = null,
                Action <TransitionBase <TStateId> > afterShouldTransition  = null) : base(
                    transition.from, transition.to, forceInstantly: transition.forceInstantly)
            {
                this.transition = transition;

                this.beforeOnEnter = beforeOnEnter;
                this.afterOnEnter  = afterOnEnter;

                this.beforeShouldTransition = beforeShouldTransition;
                this.afterShouldTransition  = afterShouldTransition;
            }
        /// <summary>
        /// Runs one logic step. It does at most one transition itself and
        /// calls the active state's logic function (after the state transition, if
        /// one occurred).
        /// </summary>
        override public void OnLogic()
        {
            if (activeState == null)
            {
                throw new System.Exception("The FSM has not been initialised yet! "
                                           + "Call fsm.SetStartState(...) and fsm.OnEnter() or fsm.Init() to initialise");
            }

            // Try the "global" transitions that can transition from any state
            for (int i = 0; i < transitionsFromAny.Count; i++)
            {
                TransitionBase transition = transitionsFromAny[i];

                // Don't transition to the "to" state, if that state is already the active state
                if (transition.to == activeState.name)
                {
                    continue;
                }

                if (TryTransition(transition))
                {
                    activeState.OnLogic();
                    return;
                }
            }

            // Try the "normal" transitions that transition from one specific state to another
            for (int i = 0; i < activeTransitions.Count; i++)
            {
                TransitionBase transition = activeTransitions[i];

                if (TryTransition(transition))
                {
                    activeState.OnLogic();
                    return;
                }
            }

            activeState.OnLogic();
        }
Beispiel #8
0
 public void AddTransition(TransitionBase <TStateId> t)
 {
     transitions = transitions ?? new List <TransitionBase <TStateId> >();
     transitions.Add(t);
 }