aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/ai/je_state_machine.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/ai/je_state_machine.h')
-rw-r--r--src/libjin/ai/je_state_machine.h128
1 files changed, 97 insertions, 31 deletions
diff --git a/src/libjin/ai/je_state_machine.h b/src/libjin/ai/je_state_machine.h
index 9d7994a..193ab65 100644
--- a/src/libjin/ai/je_state_machine.h
+++ b/src/libjin/ai/je_state_machine.h
@@ -14,7 +14,6 @@ namespace JinEngine
{
namespace AI
{
- // Grab from Unity engine.
///
/// A single layer statemachine.
@@ -26,17 +25,36 @@ namespace JinEngine
///
///
///
- typedef void(*SingleStateCallback)(const std::string& stateName);
+ enum Mode
+ {
+ Iterative, ///< Process until reach condition failed.(May cause endless loop)
+ Stepwise ///< Process one state each update.
+ };
///
///
///
- typedef void(*DoubleStateCallback)(const std::string& stateFrom, const std::string& stateTo);
+ typedef void(StateChangeCallback)(void* userdata);
+
+ ///
+ ///
+ ///
+ typedef void(StateTranslateCallback)(void* userdata);
+
+ ///
+ ///
+ ///
+ typedef void(SingleStateCallback)(const std::string& stateName, void* userdata);
+
+ ///
+ ///
+ ///
+ typedef void(DoubleStateCallback)(const std::string& stateFrom, const std::string& stateTo, void* userdata);
///
/// State machine constructor.
///
- StateMachine();
+ StateMachine(Mode mode = Mode::Stepwise, void* userdata = nullptr);
///
/// State machine destructor.
@@ -44,9 +62,14 @@ namespace JinEngine
~StateMachine();
///
- /// Update statemachine.
///
- void onUpdate();
+ ///
+ void setMode(Mode mode);
+
+ ///
+ /// Process current state..
+ ///
+ void update();
///
/// Get current state name.
@@ -54,7 +77,7 @@ namespace JinEngine
const std::string& getCurrentState();
///
- ///
+ /// Add a integer parameter.
///
void addParameteri(const std::string& name);
@@ -79,12 +102,12 @@ namespace JinEngine
void addState(const std::string& name);
///
- ///
+ ///
///
void addTransitioni(const std::string& stateFrom, const std::string& stateTo, const std::string& name, int value);
///
- ///
+ ///
///
void addTransitionf(const std::string& stateFrom, const std::string& stateTo, const std::string& name, float value);
@@ -119,51 +142,56 @@ namespace JinEngine
void setParametert(const std::string& name);
///
- /// Equivalent to setParameter(triggername);
+ /// Force change to state.
///
- void trigger(const std::string& name);
+ void forceToState(const std::string& name);
///
- /// Force change to state.
+ ///
///
- void forceToState(const std::string& name);
+ void addEnterListener(const std::string& state, StateChangeCallback* callback);
///
- /// Reset state machine.
///
- void reset();
+ ///
+ void addExitListener(const std::string& state, StateChangeCallback* callback);
///
- ///
///
- void addEnterListener(SingleStateCallback callback);
+ ///
+ void addTranslateListener(const std::string& from, const std::string& to, StateChangeCallback* callback);
///
///
///
- void addExitListener(SingleStateCallback callback);
+ void setEnterListener(SingleStateCallback* callback);
///
///
///
- void addTranslateListener(DoubleStateCallback callback);
+ void setExitListener(SingleStateCallback* callback);
+
+ ///
+ ///
+ ///
+ void setTranslateListener(DoubleStateCallback* callback);
private:
enum ParameterType
{
Int, ///< A integer value.
- FLoat, ///< A float value.
+ Float, ///< A float value.
Bool, ///< A bool value.
Trigger ///< A trigger will be reset to false after activated.
};
union ParameterValue
{
- int _int;
- float _float;
- bool _bool;
- byte _trigger;
+ int _int; ///< 0 by default.
+ float _float; ///< 0 by default.
+ bool _bool; ///< false by default.
+ bool _trigger; ///< false by default.
};
struct Parameter
@@ -187,7 +215,7 @@ namespace JinEngine
struct Transition
{
Condition condition; ///< Condition to active transition.
- std::string state; ///<
+ std::string state; ///< State to translate to.
};
///
@@ -199,12 +227,31 @@ namespace JinEngine
std::vector<Transition> transitions; ///< All transitions this state have.
};
+ const char* parameterTypeString(ParameterType type);
+
///
/// Check if condition is full filled.
///
/// @param condition Condition to check.
///
- bool checkCondition(const Condition& condition);
+ bool processCondition(const Condition& condition);
+
+ typedef void(Processor)(void*);
+
+ ///
+ ///
+ ///
+ void stepwiseProcess();
+
+ ///
+ ///
+ ///
+ void iterativeProcess();
+
+ ///
+ ///
+ ///
+ void invokeCallback(const std::string& from, const std::string& to);
///
/// All state this state machine keeps.
@@ -214,28 +261,47 @@ namespace JinEngine
///
///
///
- std::map<std::string, SingleStateCallback> mOnEnterState;
+ std::map<std::string, StateChangeCallback*> mOnEnterState;
///
///
///
- std::map<std::string, SingleStateCallback> mOnExitState;
+ std::map<std::string, StateChangeCallback*> mOnExitState;
///
/// From first to second.
///
- std::map<std::pair<std::string, std::string>, DoubleStateCallback> mOnStateTranslate;
+ std::map<std::pair<std::string, std::string>, StateTranslateCallback*> mOnStateTranslate;
+
+ ///
+ ///
+ ///
+ SingleStateCallback* mEnterCallback;
+
+ ///
+ ///
+ ///
+ SingleStateCallback* mExitCallback;
+
+ ///
+ ///
+ ///
+ DoubleStateCallback* mTraslateCallback;
///
/// Current state.
///
- const State* mCurrentState;
+ std::string mCurrentState;
///
/// All parameters.
///
std::map<std::string, Parameter> mParameters;
-
+
+ Mode mMode;
+
+ void* const mUserData;
+
};
} // namespace Graphics