feat: added the apply state to Action
This commit is contained in:
parent
ad3292aafd
commit
bb4f870dae
|
@ -1,5 +1,8 @@
|
||||||
#include "action.hpp"
|
#include "action.hpp"
|
||||||
|
#include "character_actor.hpp"
|
||||||
|
#include "global_world_state.hpp"
|
||||||
#include "utils/godot_macros.h"
|
#include "utils/godot_macros.h"
|
||||||
|
#include <godot_cpp/classes/global_constants.hpp>
|
||||||
#include <godot_cpp/variant/utility_functions.hpp>
|
#include <godot_cpp/variant/utility_functions.hpp>
|
||||||
|
|
||||||
namespace godot::goap {
|
namespace godot::goap {
|
||||||
|
@ -8,6 +11,7 @@ void Action::_bind_methods() {
|
||||||
GDPROPERTY(context_prerequisites, Variant::DICTIONARY);
|
GDPROPERTY(context_prerequisites, Variant::DICTIONARY);
|
||||||
GDPROPERTY(prerequisites, Variant::DICTIONARY);
|
GDPROPERTY(prerequisites, Variant::DICTIONARY);
|
||||||
GDPROPERTY(effects, Variant::DICTIONARY);
|
GDPROPERTY(effects, Variant::DICTIONARY);
|
||||||
|
GDPROPERTY_HINTED(apply_state, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "StateArgs");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Action::set_context_prerequisites(Dictionary dict) {
|
void Action::set_context_prerequisites(Dictionary dict) {
|
||||||
|
@ -34,6 +38,24 @@ Dictionary Action::get_effects() const {
|
||||||
return Action::property_map_to_dict(this->effects);
|
return Action::property_map_to_dict(this->effects);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Action::set_apply_state(Ref<StateArgs> state) {
|
||||||
|
this->apply_state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ref<StateArgs> Action::get_apply_state() const {
|
||||||
|
return this->apply_state;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Action::get_is_completed(CharacterActor *context) {
|
||||||
|
GlobalWorldState *state = GlobalWorldState::get_singleton();
|
||||||
|
for(WorldProperty const &prop : this->effects) {
|
||||||
|
return (prop.key.begins_with("g_")
|
||||||
|
? state->get_world_property(prop.key)
|
||||||
|
: context->call("get_" + prop.key)) == prop.value;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
Dictionary Action::property_map_to_dict(WorldState const &props) {
|
Dictionary Action::property_map_to_dict(WorldState const &props) {
|
||||||
Dictionary out{};
|
Dictionary out{};
|
||||||
for(KeyValue<StringName, Variant> const &prop : props) {
|
for(KeyValue<StringName, Variant> const &prop : props) {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef GOAP_ACTION_HPP
|
#ifndef GOAP_ACTION_HPP
|
||||||
#define GOAP_ACTION_HPP
|
#define GOAP_ACTION_HPP
|
||||||
|
|
||||||
|
#include "state.hpp"
|
||||||
#include <godot_cpp/classes/object.hpp>
|
#include <godot_cpp/classes/object.hpp>
|
||||||
#include <godot_cpp/classes/ref_counted.hpp>
|
#include <godot_cpp/classes/ref_counted.hpp>
|
||||||
#include <godot_cpp/classes/resource.hpp>
|
#include <godot_cpp/classes/resource.hpp>
|
||||||
|
@ -8,6 +9,8 @@
|
||||||
#include <godot_cpp/templates/pair.hpp>
|
#include <godot_cpp/templates/pair.hpp>
|
||||||
#include <godot_cpp/variant/variant.hpp>
|
#include <godot_cpp/variant/variant.hpp>
|
||||||
|
|
||||||
|
namespace godot { class CharacterActor; }
|
||||||
|
|
||||||
namespace godot::goap {
|
namespace godot::goap {
|
||||||
typedef HashMap<StringName, Variant> WorldState;
|
typedef HashMap<StringName, Variant> WorldState;
|
||||||
typedef KeyValue<StringName, Variant> WorldProperty;
|
typedef KeyValue<StringName, Variant> WorldProperty;
|
||||||
|
@ -22,6 +25,10 @@ public:
|
||||||
Dictionary get_prerequisites() const;
|
Dictionary get_prerequisites() const;
|
||||||
void set_effects(Dictionary dict);
|
void set_effects(Dictionary dict);
|
||||||
Dictionary get_effects() const;
|
Dictionary get_effects() const;
|
||||||
|
void set_apply_state(Ref<StateArgs> args);
|
||||||
|
Ref<StateArgs> get_apply_state() const;
|
||||||
|
|
||||||
|
bool get_is_completed(CharacterActor *context);
|
||||||
|
|
||||||
static Dictionary property_map_to_dict(WorldState const &props);
|
static Dictionary property_map_to_dict(WorldState const &props);
|
||||||
static void dict_to_property_map(Dictionary const &dict, WorldState &out);
|
static void dict_to_property_map(Dictionary const &dict, WorldState &out);
|
||||||
|
@ -29,6 +36,7 @@ public:
|
||||||
WorldState context_prerequisites{};
|
WorldState context_prerequisites{};
|
||||||
WorldState prerequisites{};
|
WorldState prerequisites{};
|
||||||
WorldState effects{};
|
WorldState effects{};
|
||||||
|
Ref<StateArgs> apply_state{};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue