Exemple #1
0
    /// <summary>
    /// Initialize this instance.
    /// </summary>
    /// <typeparam name="E">The 1st type parameter.</typeparam>
    protected void Initialize <E>()
    {
        if (!typeof(E).IsEnum)
        {
            throw new ArgumentException("Generic reference must be an enumerated type");
        }

        Array values = Enum.GetValues(typeof(E));

        MachineStates = new Dictionary <Enum, StateComponentBase <T> >();

        //Iterate with enums values
        for (int i = 0; i < values.Length; i++)
        {
            Enum e = (Enum)values.GetValue(i);

            Component Comp = GetComponent(values.GetValue(i).ToString());

            MachineStates.Add(e, (StateComponentBase <T>)Comp);
        }

        if (MachineStates.Count > 0)
        {
            CurrentState    = MachineStates.First().Value;
            CurrentStateID  = MachineStates.First().Key;
            PreviousStateID = CurrentStateID;

            EnterState();
        }

        DebugInfo();
    }
Exemple #2
0
    /// <summary>
    /// Changes the state.
    /// </summary>
    /// <param name="state">State.</param>
    public void ChangeState(Enum ToState)
    {
        if (CurrentStateID.Equals(ToState))
        {
            return;
        }

        ExitState(ToState);

        if (MachineStates.ContainsKey(ToState))
        {
            PreviousStateID = CurrentStateID;
            CurrentState    = MachineStates[ToState];
            CurrentStateID  = ToState;
        }
        else
        {
            Debug.LogWarning("Enum key was not founded");
        }

        EnterState();
    }