Exemple #1
0
    public List <IAction> UpdateState()
    {
        TriggeredTransition = null;
        foreach (ITransition t in CurrentState.GetTransition())
        {
            if (t.IsTriggered())
            {
                TriggeredTransition = t;
                Debug.Log("triggered");
                break;
            }
        }

        if (TriggeredTransition != null)
        {
            TargetState = TriggeredTransition.GetTargetState();

            ActionList.AddRange(CurrentState.GetExitActionList());
            ActionList.AddRange(CurrentState.GetActionList());
            ActionList.AddRange(TargetState.GetEntryActionList());

            //Adding transition to the tracker
            StateTracker.AddTransition((State)CurrentState, (State)TargetState, actor);
            CurrentState = TargetState;

            return(ActionList);
        }

        return(CurrentState.GetActionList());
    }
    private void Update()
    {
        CheckPlayerState();

        stateName  = currentState.ToString();
        totalTime += Time.deltaTime;
        if (totalTime >= 0.5f)
        {
            // check and apply transitions, return a list of actions
            ITransition triggered = null;
            // store first transition that triggers
            if (currentState != null)
            {
                foreach (ITransition t in currentState.GetTransitions())
                {
                    if (t.IsTriggered())
                    {
                        triggered = t;
                        break;
                    }
                }

                // check if we have a transition to fire
                if (triggered != null)
                {
                    targetState = triggered.GetTargetState();
                    actions     = currentState.GetExitActions();

                    // actions += triggered.getActions()
                    IAction[] newActions = new IAction[actions.Length + triggered.GetActions().Length];
                    actions.CopyTo(newActions, 0);
                    triggered.GetActions().CopyTo(newActions, actions.Length);
                    actions    = newActions;
                    newActions = null;

                    // actions += targetState.getEntryActions()
                    newActions = new IAction[actions.Length + targetState.GetEntryActions().Length];
                    actions.CopyTo(newActions, 0);
                    targetState.GetEntryActions().CopyTo(newActions, actions.Length);

                    currentState = targetState;
                    currentState.GetActions();
                }
                else
                {
                    currentState.GetActions();
                }
            }
            totalTime = 0.0f;
        }
    }