feat: added the apply state to Action

This commit is contained in:
Sara 2024-03-30 22:59:57 +01:00
parent ad3292aafd
commit bb4f870dae
2 changed files with 30 additions and 0 deletions

View file

@ -1,5 +1,8 @@
#include "action.hpp"
#include "character_actor.hpp"
#include "global_world_state.hpp"
#include "utils/godot_macros.h"
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
namespace godot::goap {
@ -8,6 +11,7 @@ void Action::_bind_methods() {
GDPROPERTY(context_prerequisites, Variant::DICTIONARY);
GDPROPERTY(prerequisites, Variant::DICTIONARY);
GDPROPERTY(effects, Variant::DICTIONARY);
GDPROPERTY_HINTED(apply_state, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "StateArgs");
}
void Action::set_context_prerequisites(Dictionary dict) {
@ -34,6 +38,24 @@ Dictionary Action::get_effects() const {
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 out{};
for(KeyValue<StringName, Variant> const &prop : props) {

View file

@ -1,6 +1,7 @@
#ifndef GOAP_ACTION_HPP
#define GOAP_ACTION_HPP
#include "state.hpp"
#include <godot_cpp/classes/object.hpp>
#include <godot_cpp/classes/ref_counted.hpp>
#include <godot_cpp/classes/resource.hpp>
@ -8,6 +9,8 @@
#include <godot_cpp/templates/pair.hpp>
#include <godot_cpp/variant/variant.hpp>
namespace godot { class CharacterActor; }
namespace godot::goap {
typedef HashMap<StringName, Variant> WorldState;
typedef KeyValue<StringName, Variant> WorldProperty;
@ -22,6 +25,10 @@ public:
Dictionary get_prerequisites() const;
void set_effects(Dictionary dict);
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 void dict_to_property_map(Dictionary const &dict, WorldState &out);
@ -29,6 +36,7 @@ public:
WorldState context_prerequisites{};
WorldState prerequisites{};
WorldState effects{};
Ref<StateArgs> apply_state{};
};
}