aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/ai/je_state_machine.h
blob: 0d19be94ec26c5f1c8e83d00936e264b1075d4d5 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef __JE_STATEMACHINE_TREE_H
#define __JE_STATEMACHINE_TREE_H

#include "../core/je_configuration.h"
#if defined(jin_ai)

#include <map>
#include <vector>

#include "../common/je_object.h"

namespace JinEngine
{
    namespace AI
    {
        // From Unity3D game engine.

        class State;

        enum ConditionType
        {

        };

        ///
        /// Traslation's condition.
        ///
        struct Condition
        {

        };

        /// 
        /// Translate to another state. 
        /// 
        struct Transition
        {
            Condition condition; ///< Condition to active transition.
            State* state;        ///< State translate to.
        };

        ///
        /// Definition of state.
        ///
        struct StateDef
        {
        };

        ///
        /// A single state.
        ///
        struct State
        {
            std::string name;                     ///< Name of state. 
            std::vector<Transition> translations; ///< All transitions this state have.
            StateDef definition;                  ///< Definition of state.
            void* userdata;                       ///< A state could have a user data.
        };

        ///
        /// A single layer statemachine.
        ///
        class StateMachine : public Object
        {
        public: 
            ///
            ///
            ///
            typedef void(*SingleStateCallback)(State* state);

            ///
            ///
            ///
            typedef void(*DoubleStateCallback)(State* stateFrom, State* stateTo);

            ///
            /// State machine constructor.
            ///
            StateMachine();

            ///
            /// State machine destructor.
            ///
            ~StateMachine();

            ///
            /// Update statemachine.
            ///
            void onUpdate();

            /// 
            /// Add new state.
            /// 
            void addState(const std::string& name, const State& state);

            ///
            /// Get state reference by given state name.
            ///
            const State* getState(const std::string& name);

            void addEnterListener();
            void addExitListener(); 
            void addTranslateListener(); 

            const std::string& getCurrentStateName();
            const State* getCurrentState();

        private:
            ///
            /// All state this state machine keeps.
            ///
            std::map<std::string, State> mStates;

            ///
            ///
            ///
            std::map<std::string, SingleStateCallback> mOnEnterState;

            ///
            ///
            ///
            std::map<std::string, SingleStateCallback> mOnExitState;

            ///
            /// From first to second.
            ///
            std::map<std::pair<std::string, std::string>, DoubleStateCallback> mOnStateTranslate;

            ///
            /// Current state.
            ///
            State* mCurrentState;
            
        };

    }
}

#endif // jin_ai

#endif