monocleengine-monogame /MonocleEngine/Components/Logic/StateMachine.cs

Language C# Lines 97
MD5 Hash 561b5b18261542e7c4d51229d6b129c5 Estimated Cost $1,880 (why?)
Repository https://bitbucket.org/gamblore/monocleengine-monogame View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Monocle
{
    public class StateMachine<Indexer> : Component where Indexer : struct
    {
        public delegate Indexer StateCallback();

        private Indexer state;
        private Dictionary<Indexer, Action> enterStates;
        private Dictionary<Indexer, StateCallback> updates;
        private Dictionary<Indexer, Action> leaveStates;

        private Indexer switchTo;
        private int switchCounter;

        public StateMachine(Indexer initialState)
            : base(true, false)
        {
            state = initialState;

            enterStates = new Dictionary<Indexer, Action>();
            updates = new Dictionary<Indexer, StateCallback>();
            leaveStates = new Dictionary<Indexer, Action>();
        }

        public StateMachine()
            : this(default(Indexer))
        {

        }

        public Indexer State
        {
            get { return state; }
            set
            {
#if DEBUG
                if (!updates.ContainsKey(value))
                    throw new Exception("StateMachine has entered a state for which callbacks have not been set (you can set them to null for no actions)");
#endif 
                if (!state.Equals(value))
                {
                    switchCounter = 0;
                    if (leaveStates[state] != null)
                        leaveStates[state]();
                    state = value;
                    if (enterStates[state] != null)
                        enterStates[state]();
                }
            }
        }

        public void SetCallbacks(Indexer state, StateCallback onUpdate, Action onEnterState = null, Action onLeaveState = null)
        {
            updates[state] = onUpdate;
            enterStates[state] = onEnterState;
            leaveStates[state] = onLeaveState;
        }

        public override void Update()
        {
            if (switchCounter > 0)
            {
                switchCounter--;
                if (switchCounter == 0)
                    State = switchTo;
            }

            if (updates[state] != null)
                State = updates[state]();
        }

        static public implicit operator Indexer(StateMachine<Indexer> s)
        {
            return s.state;
        }

        /*
         *  Switch to the specified state in the specified amount of frames.
         *  This switch is cancelled if the state is otherwise changed before it is finished.
         */
        public void DelayedStateChange(Indexer changeTo, int frameDelay)
        {
#if DEBUG
            if (frameDelay <= 0)
                throw new Exception("Frame delay must be larger than zero");
#endif
            switchTo = changeTo;
            switchCounter = frameDelay;
        }
    }
}
Back to Top