Refactor LimboHSM

This commit is contained in:
Serhii Snitsaruk 2024-07-21 13:45:36 +02:00
parent f7eb374c1e
commit cf99a27692
No known key found for this signature in database
GPG Key ID: A965EF8799FFEC2D
2 changed files with 13 additions and 12 deletions

View File

@ -119,7 +119,7 @@ void LimboHSM::remove_transition(LimboState *p_from_state, const StringName &p_e
transitions.erase(key); transitions.erase(key);
} }
void LimboHSM::get_transition(LimboState *p_from_state, const StringName &p_event, Transition &r_transition) const { void LimboHSM::_get_transition(LimboState *p_from_state, const StringName &p_event, Transition &r_transition) const {
ERR_FAIL_COND_MSG(p_from_state != nullptr && p_from_state->get_parent() != this, "LimboHSM: Unable to get a transition from a state that is not an immediate child of this HSM."); ERR_FAIL_COND_MSG(p_from_state != nullptr && p_from_state->get_parent() != this, "LimboHSM: Unable to get a transition from a state that is not an immediate child of this HSM.");
ERR_FAIL_COND_MSG(p_event == StringName(), "LimboHSM: Unable to get a transition with an empty event string."); ERR_FAIL_COND_MSG(p_event == StringName(), "LimboHSM: Unable to get a transition with an empty event string.");
@ -163,13 +163,13 @@ bool LimboHSM::_dispatch(const StringName &p_event, const Variant &p_cargo) {
LimboState *to_state = nullptr; LimboState *to_state = nullptr;
Transition transition; Transition transition;
get_transition(active_state, p_event, transition); _get_transition(active_state, p_event, transition);
if (transition.is_valid()) { if (transition.is_valid()) {
to_state = Object::cast_to<LimboState>(ObjectDB::get_instance(transition.to_state)); to_state = Object::cast_to<LimboState>(ObjectDB::get_instance(transition.to_state));
} }
if (to_state == nullptr) { if (to_state == nullptr) {
// Get ANYSTATE transition. // Get ANYSTATE transition.
get_transition(nullptr, p_event, transition); _get_transition(nullptr, p_event, transition);
if (transition.is_valid()) { if (transition.is_valid()) {
to_state = Object::cast_to<LimboState>(ObjectDB::get_instance(transition.to_state)); to_state = Object::cast_to<LimboState>(ObjectDB::get_instance(transition.to_state));
if (to_state == active_state) { if (to_state == active_state) {

View File

@ -16,14 +16,6 @@
#define TransitionKey Pair<uint64_t, StringName> #define TransitionKey Pair<uint64_t, StringName>
struct TransitionKeyHasher {
static uint32_t hash(const TransitionKey &P) {
uint64_t h1 = HashMapHasherDefault::hash(P.first);
uint64_t h2 = HashMapHasherDefault::hash(P.second);
return hash_one_uint64((h1 << 32) | h2);
}
};
class LimboHSM : public LimboState { class LimboHSM : public LimboState {
GDCLASS(LimboHSM, LimboState); GDCLASS(LimboHSM, LimboState);
@ -35,6 +27,14 @@ public:
}; };
private: private:
struct TransitionKeyHasher {
static uint32_t hash(const TransitionKey &P) {
uint64_t h1 = HashMapHasherDefault::hash(P.first);
uint64_t h2 = HashMapHasherDefault::hash(P.second);
return hash_one_uint64((h1 << 32) | h2);
}
};
struct Transition { struct Transition {
ObjectID from_state; ObjectID from_state;
ObjectID to_state; ObjectID to_state;
@ -58,6 +58,8 @@ private:
HashMap<TransitionKey, Transition, TransitionKeyHasher> transitions; HashMap<TransitionKey, Transition, TransitionKeyHasher> transitions;
void _get_transition(LimboState *p_from_state, const StringName &p_event, Transition &r_transition) const;
protected: protected:
static void _bind_methods(); static void _bind_methods();
@ -92,7 +94,6 @@ public:
void add_transition(LimboState *p_from_state, LimboState *p_to_state, const StringName &p_event); void add_transition(LimboState *p_from_state, LimboState *p_to_state, const StringName &p_event);
void remove_transition(LimboState *p_from_state, const StringName &p_event); void remove_transition(LimboState *p_from_state, const StringName &p_event);
bool has_transition(LimboState *p_from_state, const StringName &p_event) const { return transitions.has(Transition::make_key(p_from_state, p_event)); } bool has_transition(LimboState *p_from_state, const StringName &p_event) const { return transitions.has(Transition::make_key(p_from_state, p_event)); }
void get_transition(LimboState *p_from_state, const StringName &p_event, Transition &r_transition) const;
LimboState *anystate() const { return nullptr; } LimboState *anystate() const { return nullptr; }