2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* limbo_state.h
|
|
|
|
* =============================================================================
|
2024-03-04 14:53:39 +00:00
|
|
|
* Copyright 2021-2024 Serhii Snitsaruk
|
2023-07-21 09:50:06 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style
|
|
|
|
* license that can be found in the LICENSE file or at
|
|
|
|
* https://opensource.org/licenses/MIT.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
2022-09-28 10:48:45 +00:00
|
|
|
|
|
|
|
#ifndef LIMBO_STATE_H
|
|
|
|
#define LIMBO_STATE_H
|
|
|
|
|
2024-01-07 04:54:17 +00:00
|
|
|
#include "../blackboard/blackboard.h"
|
2024-01-23 19:02:23 +00:00
|
|
|
#include "../blackboard/blackboard_plan.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-03-06 19:17:23 +00:00
|
|
|
#include "../util/limbo_compat.h"
|
2024-01-07 04:54:17 +00:00
|
|
|
#include "../util/limbo_string_names.h"
|
|
|
|
|
|
|
|
#ifdef LIMBOAI_MODULE
|
2022-09-28 10:48:45 +00:00
|
|
|
#include "scene/main/node.h"
|
2024-01-07 04:54:17 +00:00
|
|
|
#endif // LIMBOAI_MODULE
|
|
|
|
|
|
|
|
#ifdef LIMBOAI_GDEXTENSION
|
|
|
|
#include <godot_cpp/templates/hash_map.hpp>
|
|
|
|
#endif // LIMBOAI_GDEXTENSION
|
2022-09-28 10:48:45 +00:00
|
|
|
|
2022-09-29 10:54:07 +00:00
|
|
|
class LimboHSM;
|
2022-09-28 10:48:45 +00:00
|
|
|
|
|
|
|
class LimboState : public Node {
|
|
|
|
GDCLASS(LimboState, Node);
|
|
|
|
|
|
|
|
private:
|
2024-01-23 19:02:23 +00:00
|
|
|
Ref<BlackboardPlan> blackboard_plan;
|
2022-12-16 10:56:41 +00:00
|
|
|
Node *agent;
|
2022-09-28 10:48:45 +00:00
|
|
|
Ref<Blackboard> blackboard;
|
2024-03-04 14:53:39 +00:00
|
|
|
HashMap<StringName, Callable> handlers;
|
2022-12-16 10:56:41 +00:00
|
|
|
Callable guard_callable;
|
2022-09-28 10:48:45 +00:00
|
|
|
|
|
|
|
protected:
|
2022-09-29 10:54:07 +00:00
|
|
|
friend LimboHSM;
|
2022-12-16 10:56:41 +00:00
|
|
|
|
2022-09-29 10:54:07 +00:00
|
|
|
bool active;
|
|
|
|
|
2022-09-28 10:48:45 +00:00
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
void _notification(int p_what);
|
|
|
|
|
2022-12-16 10:56:41 +00:00
|
|
|
virtual void _initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard);
|
2024-03-04 14:53:39 +00:00
|
|
|
virtual bool _dispatch(const StringName &p_event, const Variant &p_cargo = Variant());
|
2022-12-15 07:26:52 +00:00
|
|
|
|
2024-03-06 19:17:23 +00:00
|
|
|
virtual bool _should_use_new_scope() const { return blackboard_plan.is_valid() || is_root(); }
|
2024-03-11 17:58:40 +00:00
|
|
|
virtual void _update_blackboard_plan();
|
2024-03-06 19:17:23 +00:00
|
|
|
|
2022-09-28 10:48:45 +00:00
|
|
|
virtual void _setup();
|
|
|
|
virtual void _enter();
|
|
|
|
virtual void _exit();
|
2023-04-10 08:08:11 +00:00
|
|
|
virtual void _update(double p_delta);
|
2022-09-28 10:48:45 +00:00
|
|
|
|
2024-01-07 04:54:17 +00:00
|
|
|
#ifdef LIMBOAI_MODULE
|
2022-12-15 07:26:52 +00:00
|
|
|
GDVIRTUAL0(_setup);
|
|
|
|
GDVIRTUAL0(_enter);
|
|
|
|
GDVIRTUAL0(_exit);
|
2023-04-10 08:08:11 +00:00
|
|
|
GDVIRTUAL1(_update, double);
|
2024-01-07 04:54:17 +00:00
|
|
|
#endif // LIMBOAI_MODULE
|
2022-12-15 07:26:52 +00:00
|
|
|
|
2022-09-28 10:48:45 +00:00
|
|
|
public:
|
2024-03-11 17:58:40 +00:00
|
|
|
void set_blackboard_plan(const Ref<BlackboardPlan> &p_plan);
|
2024-04-01 13:06:06 +00:00
|
|
|
_FORCE_INLINE_ Ref<BlackboardPlan> get_blackboard_plan() const { return blackboard_plan; }
|
2024-01-23 11:05:54 +00:00
|
|
|
|
2022-10-19 21:39:27 +00:00
|
|
|
Ref<Blackboard> get_blackboard() const { return blackboard; }
|
2022-10-26 21:12:29 +00:00
|
|
|
|
2022-12-16 10:56:41 +00:00
|
|
|
Node *get_agent() const { return agent; }
|
|
|
|
void set_agent(Node *p_agent) { agent = p_agent; }
|
2022-10-19 21:39:27 +00:00
|
|
|
|
2024-03-04 14:53:39 +00:00
|
|
|
LimboState *named(const String &p_name);
|
2023-03-30 21:37:21 +00:00
|
|
|
LimboState *call_on_enter(const Callable &p_callable);
|
|
|
|
LimboState *call_on_exit(const Callable &p_callable);
|
|
|
|
LimboState *call_on_update(const Callable &p_callable);
|
2022-09-28 10:48:45 +00:00
|
|
|
|
2024-03-04 14:53:39 +00:00
|
|
|
void add_event_handler(const StringName &p_event, const Callable &p_handler);
|
|
|
|
bool dispatch(const StringName &p_event, const Variant &p_cargo = Variant());
|
2024-03-01 10:02:32 +00:00
|
|
|
|
2024-03-04 14:53:39 +00:00
|
|
|
_FORCE_INLINE_ StringName event_finished() const { return LW_NAME(EVENT_FINISHED); }
|
2022-09-28 10:48:45 +00:00
|
|
|
LimboState *get_root() const;
|
2024-03-06 19:17:23 +00:00
|
|
|
_FORCE_INLINE_ bool is_root() const { return !(get_parent() && IS_CLASS(get_parent(), LimboState)); }
|
2022-09-28 10:48:45 +00:00
|
|
|
bool is_active() const { return active; }
|
2022-09-29 20:44:51 +00:00
|
|
|
|
2022-12-16 10:56:41 +00:00
|
|
|
void set_guard(const Callable &p_guard_callable);
|
|
|
|
void clear_guard();
|
2022-09-28 10:48:45 +00:00
|
|
|
|
|
|
|
LimboState();
|
|
|
|
};
|
|
|
|
|
2024-01-13 16:10:42 +00:00
|
|
|
#endif // LIMBO_STATE_H
|