53 lines
1.4 KiB
C++
53 lines
1.4 KiB
C++
#ifndef STATE_HPP
|
|
#define STATE_HPP
|
|
|
|
#include <functional>
|
|
#include <vector>
|
|
#include <string>
|
|
|
|
template <typename Type>
|
|
struct Transition {
|
|
const std::string target_state;
|
|
const std::function<bool (const Type&)> condition;
|
|
};
|
|
|
|
template <typename OwnerType>
|
|
class State {
|
|
public:
|
|
virtual ~State() = default;
|
|
|
|
virtual void OnEnter(OwnerType &owner) = 0;
|
|
virtual void OnUpdate(OwnerType &owner) = 0;
|
|
virtual void OnExit(OwnerType &owner) = 0;
|
|
|
|
virtual void AddTransition(const std::string &to,
|
|
std::function<bool (const OwnerType&)> condition) = 0;
|
|
virtual std::vector<Transition<OwnerType>> GetTransitions() = 0;
|
|
|
|
virtual std::string Id() const = 0;
|
|
};
|
|
|
|
template <typename OwnerType>
|
|
class AState : public State<OwnerType> {
|
|
public:
|
|
AState(const std::string &id) : entity_id(id) {};
|
|
|
|
void AddTransition(const std::string &to,
|
|
std::function<bool (const OwnerType&)> condition) override {
|
|
transitions.push_back({ to, condition });
|
|
}
|
|
std::vector<Transition<OwnerType>> GetTransitions() override {
|
|
return transitions;
|
|
}
|
|
|
|
std::string Id() const override {
|
|
return entity_id;
|
|
}
|
|
|
|
private:
|
|
std::vector<Transition<OwnerType>> transitions;
|
|
std::string entity_id;
|
|
};
|
|
|
|
#endif // STATE_HPP
|