feat: defined action with prereqs and effects
This commit is contained in:
parent
3b75b13a14
commit
e0261a033e
53
src/action.cpp
Normal file
53
src/action.cpp
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
#include "action.hpp"
|
||||||
|
#include "utils/godot_macros.h"
|
||||||
|
|
||||||
|
namespace godot::goap {
|
||||||
|
void Action::_bind_methods() {
|
||||||
|
#define CLASSNAME Action
|
||||||
|
GDPROPERTY(context_prerequisites, Variant::DICTIONARY);
|
||||||
|
GDPROPERTY(prerequisites, Variant::DICTIONARY);
|
||||||
|
GDPROPERTY(effects, Variant::DICTIONARY);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Action::set_context_prerequisites(Dictionary dict) {
|
||||||
|
Action::dict_to_property_map(dict, this->context_prerequisites);
|
||||||
|
}
|
||||||
|
|
||||||
|
Dictionary Action::get_context_prerequisites() const {
|
||||||
|
return Action::property_map_to_dict(this->context_prerequisites);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Action::set_prerequisites(Dictionary dict) {
|
||||||
|
Action::dict_to_property_map(dict, this->prerequisites);
|
||||||
|
}
|
||||||
|
|
||||||
|
Dictionary Action::get_prerequisites() const {
|
||||||
|
return Action::property_map_to_dict(this->prerequisites);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Action::set_effects(Dictionary dict) {
|
||||||
|
Action::dict_to_property_map(dict, this->effects);
|
||||||
|
}
|
||||||
|
|
||||||
|
Dictionary Action::get_effects() const {
|
||||||
|
return Action::property_map_to_dict(this->effects);
|
||||||
|
}
|
||||||
|
|
||||||
|
Dictionary Action::property_map_to_dict(WorldState const &props) {
|
||||||
|
Dictionary out{};
|
||||||
|
for(KeyValue<StringName, Variant> const &prop : props) {
|
||||||
|
out[prop.key] = prop.value;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Action::dict_to_property_map(Dictionary const &dict, WorldState &out) {
|
||||||
|
out.clear();
|
||||||
|
Array keys = dict.keys();
|
||||||
|
Array vals = dict.values();
|
||||||
|
for(size_t i{0}; i < keys.size(); ++i) {
|
||||||
|
if(keys[i].get_type() == Variant::STRING_NAME)
|
||||||
|
out.insert(keys[i], vals[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
36
src/action.hpp
Normal file
36
src/action.hpp
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#ifndef GOAP_ACTION_HPP
|
||||||
|
#define GOAP_ACTION_HPP
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <godot_cpp/classes/object.hpp>
|
||||||
|
#include <godot_cpp/classes/ref_counted.hpp>
|
||||||
|
#include <godot_cpp/classes/resource.hpp>
|
||||||
|
#include <godot_cpp/templates/hash_map.hpp>
|
||||||
|
#include <godot_cpp/templates/pair.hpp>
|
||||||
|
#include <godot_cpp/variant/variant.hpp>
|
||||||
|
|
||||||
|
namespace godot::goap {
|
||||||
|
typedef HashMap<StringName, Variant> WorldState;
|
||||||
|
typedef KeyValue<StringName, Variant> WorldProperty;
|
||||||
|
|
||||||
|
class Action : public Resource {
|
||||||
|
GDCLASS(Action, Resource);
|
||||||
|
static void _bind_methods();
|
||||||
|
public:
|
||||||
|
void set_context_prerequisites(Dictionary dict);
|
||||||
|
Dictionary get_context_prerequisites() const;
|
||||||
|
void set_prerequisites(Dictionary dict);
|
||||||
|
Dictionary get_prerequisites() const;
|
||||||
|
void set_effects(Dictionary dict);
|
||||||
|
Dictionary get_effects() const;
|
||||||
|
protected:
|
||||||
|
static Dictionary property_map_to_dict(WorldState const &props);
|
||||||
|
static void dict_to_property_map(Dictionary const &dict, WorldState &out);
|
||||||
|
public:
|
||||||
|
WorldState context_prerequisites{};
|
||||||
|
WorldState prerequisites{};
|
||||||
|
WorldState effects{};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //!GOAP_ACTION_HPP
|
Loading…
Reference in a new issue