Exemplo n.º 1
0
    public virtual bool SetState <StateType>() where StateType : StateOld, new()
    {
        bool success;

        foreach (StateOld state in stateList)
        {
            if (state is StateType)
            {
                success = SetState(state);
                break;
            }
        }
        StateOld stateComponent = GetComponent <StateType>();

        if (stateComponent)
        {
            stateComponent.Initialize(this);
            stateList.Add(stateComponent);
            success = SetState(stateComponent);
            return(success);
        }
        StateOld newState = gameObject.AddComponent <StateType>();

        newState.Initialize(this);
        stateList.Add(newState);
        success = SetState(newState);
        return(success);
    }
Exemplo n.º 2
0
    public virtual bool SetOrInitializeState(StateOld state)
    {
        bool success = false;

        if (state && state != currentState)
        {
            StateOld oldState = currentState;
            currentState = state;
            if (oldState)
            {
                oldState.ExitState();
            }
            if (stateList.Contains(currentState))
            {
                currentState.EnterState();
            }
            else
            {
                currentState.Initialize(this);
                stateList.Add(currentState);
            }
            success = true;
        }
        return(success);
    }