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.h221
1 files changed, 163 insertions, 58 deletions
diff --git a/src/libjin/ai/je_state_machine.h b/src/libjin/ai/je_state_machine.h
index 0d19be9..19b884b 100644
--- a/src/libjin/ai/je_state_machine.h
+++ b/src/libjin/ai/je_state_machine.h
@@ -1,5 +1,5 @@
-#ifndef __JE_STATEMACHINE_TREE_H
-#define __JE_STATEMACHINE_TREE_H
+#ifndef __JE_STATE_MACHINE_H
+#define __JE_STATE_MACHINE_H
#include "../core/je_configuration.h"
#if defined(jin_ai)
@@ -7,71 +7,56 @@
#include <map>
#include <vector>
+#include "../common/je_types.h"
#include "../common/je_object.h"
namespace JinEngine
{
namespace AI
{
- // From Unity3D game engine.
-
- class State;
-
- enum ConditionType
- {
-
- };
+ // Grab from Unity engine.
///
- /// Traslation's condition.
+ /// A single layer statemachine.
///
- struct Condition
+ class StateMachine : public Object
{
+ public:
- };
+ union ParameterValue
+ {
+ int _int;
+ float _float;
+ bool _bool;
+ byte _trigger;
+ };
- ///
- /// Translate to another state.
- ///
- struct Transition
- {
- Condition condition; ///< Condition to active transition.
- State* state; ///< State translate to.
- };
+ enum ParameterType
+ {
+ Int, ///< A integer value.
+ FLoat, ///< A float value.
+ Bool, ///< A bool value.
+ Trigger ///< A trigger will be reset to false after activated.
+ };
- ///
- /// 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.
- };
+ ///
+ /// Traslation's condition.
+ ///
+ struct Condition
+ {
+ std::string parameter;
+ ParameterValue value;
+ };
- ///
- /// A single layer statemachine.
- ///
- class StateMachine : public Object
- {
- public:
///
///
///
- typedef void(*SingleStateCallback)(State* state);
+ typedef void(*SingleStateCallback)(const std::string& stateName);
///
///
///
- typedef void(*DoubleStateCallback)(State* stateFrom, State* stateTo);
+ typedef void(*DoubleStateCallback)(const std::string& stateFrom, const std::string& stateTo);
///
/// State machine constructor.
@@ -88,24 +73,139 @@ namespace JinEngine
///
void onUpdate();
+ ///
+ /// Get current state name.
+ ///
+ const std::string& getCurrentStateName();
+
+ ///
+ ///
+ ///
+ void addParameter(ParameterType type, const std::string& name);
+
///
- /// Add new state.
+ /// Add a state.
///
- void addState(const std::string& name, const State& state);
+ void addState(const std::string& name);
///
- /// Get state reference by given state name.
///
- const State* getState(const std::string& name);
+ ///
+ void addTransition(const std::string& stateFrom, const std::string& stateTo, const std::string& name, const ParameterValue& value);
- void addEnterListener();
- void addExitListener();
- void addTranslateListener();
+ ///
+ ///
+ ///
+ void addTransitioni(const std::string& stateFrom, const std::string& stateTo, const std::string& name, int value);
- const std::string& getCurrentStateName();
- const State* getCurrentState();
+ ///
+ ///
+ ///
+ void addTransitionf(const std::string& stateFrom, const std::string& stateTo, const std::string& name, float value);
+
+ ///
+ ///
+ ///
+ void addTransitionb(const std::string& stateFrom, const std::string& stateTo, const std::string& name, bool value);
+
+ ///
+ ///
+ ///
+ void addTransitiont(const std::string& stateFrom, const std::string& stateTo, const std::string& name);
+
+ ///
+ ///
+ ///
+ void addTransitiont(const std::string& stateFrom, const std::string& stateTo, const std::string& name, const ParameterValue& value);
+
+ ///
+ /// Set parameter value.
+ ///
+ void setParameter(const std::string& name, const ParameterValue& value);
+
+ ///
+ /// Set parameter value.
+ ///
+ void setParameteri(const std::string& name, int value);
+
+ ///
+ /// Set parameter value.
+ ///
+ void setParameterf(const std::string& name, float value);
+
+ ///
+ /// Set parameter value.
+ ///
+ void setParameterb(const std::string& name, bool value);
+
+ ///
+ /// Set parameter value.
+ ///
+ void setParametert(const std::string& name);
+
+ ///
+ /// Equivalent to setParameter(triggername);
+ ///
+ void trigger(const std::string& name);
+
+ ///
+ /// Force change to state.
+ ///
+ void forceToState(const std::string& name);
+
+ ///
+ /// Reset state machine.
+ ///
+ void resetState();
+
+ ///
+ ///
+ ///
+ void addEnterListener(SingleStateCallback callback);
+
+ ///
+ ///
+ ///
+ void addExitListener(SingleStateCallback callback);
+
+ ///
+ ///
+ ///
+ void addTranslateListener(DoubleStateCallback callback);
private:
+
+ struct Parameter
+ {
+ ParameterType type;
+ ParameterValue value;
+ };
+
+ ///
+ /// Translate to another state.
+ ///
+ struct Transition
+ {
+ Condition condition; ///< Condition to active transition.
+ std::string& state; ///<
+ };
+
+ ///
+ /// A single state.
+ ///
+ struct State
+ {
+ std::string name; ///< Name of state.
+ std::vector<Transition> transitions; ///< All transitions this state have.
+ };
+
+ ///
+ /// Check if condition is full filled.
+ ///
+ /// @param condition Condition to check.
+ ///
+ bool checkCondition(const Condition& condition);
+
///
/// All state this state machine keeps.
///
@@ -129,12 +229,17 @@ namespace JinEngine
///
/// Current state.
///
- State* mCurrentState;
+ const State* mCurrentState;
+
+ ///
+ /// All parameters.
+ ///
+ std::map<std::string, Parameter> mParameters;
};
- }
-}
+ } // namespace Graphics
+} // namespace JinEngine
#endif // jin_ai