feat: StateArgs now require a Planner as construction context

This commit is contained in:
Sara 2024-04-02 22:25:26 +02:00
parent 8d3484163c
commit 73ff6ead46
4 changed files with 18 additions and 14 deletions

View file

@ -64,8 +64,9 @@ void Planner::_bind_methods() {
GDPROPERTY_HINTED(goals, Variant::ARRAY, PROPERTY_HINT_ARRAY_TYPE, GDRESOURCETYPE(Goal));
}
void Planner::_ready() {
void Planner::_enter_tree() {
this->global_world_state = GlobalWorldState::get_singleton();
UtilityFunctions::print("global world state cached: ", this->global_world_state);
this->actor = Object::cast_to<CharacterActor>(this->get_parent());
}
@ -196,7 +197,7 @@ State Planner::get_next_state() {
this->plan = this->make_plan();
if(this->plan.is_empty())
return State::new_invalid();
return this->plan.get(0)->apply_state->construct(this->actor);
return this->plan.get(0)->apply_state->construct(this);
}
void Planner::set_actions(Array value) {

View file

@ -44,7 +44,7 @@ class Planner : public Node {
GDCLASS(Planner, Node);
static void _bind_methods();
public:
virtual void _ready() override;
virtual void _enter_tree() override;
Vector<Ref<Action>> make_plan();
Ref<Goal> select_goal();

View file

@ -1,5 +1,6 @@
#include "state.hpp"
#include "character_actor.hpp"
#include "planner.hpp"
#include "utils/godot_macros.h"
namespace godot::goap {
@ -51,7 +52,7 @@ void StateArgs::_bind_methods() {
GDPROPERTY(argument_property, Variant::STRING_NAME);
}
State StateArgs::construct(Node *context) const {
State StateArgs::construct(Planner *context) const {
return { .type = State::STATE_TYPE_MAX };
}
@ -60,21 +61,21 @@ StringName StateArgs::get_argument_property() const { return this->argument_prop
void MoveStateArgs::_bind_methods() {}
State MoveStateArgs::construct(Node *context) const {
Node3D *node = Object::cast_to<Node3D>(context->call("get_" + this->argument_property));
State MoveStateArgs::construct(Planner *context) const {
Node3D *node = Object::cast_to<Node3D>(context->get_world_property(this->argument_property));
return State::new_move_to(node);
}
void AnimateStateArgs::_bind_methods() {}
State AnimateStateArgs::construct(Node *context) const {
return State::new_animate(context->call("get_" + this->argument_property));
State AnimateStateArgs::construct(Planner *context) const {
return State::new_animate(context->get_world_property(this->argument_property));
}
void ActivateStateArgs::_bind_methods() {}
State ActivateStateArgs::construct(Node *context) const {
Node *node = Object::cast_to<Node>(context->call("get_" + this->argument_property));
State ActivateStateArgs::construct(Planner *context) const {
Node *node = Object::cast_to<Node>(context->get_world_property(this->argument_property));
return State::new_activate(node);
}
}

View file

@ -9,6 +9,8 @@
namespace godot { class CharacterActor; }
namespace godot::goap {
class Planner;
struct State {
~State();
static State new_move_to(Node3D *location);
@ -36,7 +38,7 @@ class StateArgs : public Resource {
GDCLASS(StateArgs, Resource);
static void _bind_methods();
public:
virtual State construct(Node *context) const;
virtual State construct(Planner *context) const;
void set_argument_property(StringName name);
StringName get_argument_property() const;
StringName argument_property;
@ -45,21 +47,21 @@ public:
class MoveStateArgs : public StateArgs {
GDCLASS(MoveStateArgs, StateArgs);
static void _bind_methods();
virtual State construct(Node *context) const override;
virtual State construct(Planner *context) const override;
};
class AnimateStateArgs : public StateArgs {
GDCLASS(AnimateStateArgs, StateArgs);
static void _bind_methods();
public:
virtual State construct(Node *context) const override;
virtual State construct(Planner *context) const override;
};
class ActivateStateArgs : public StateArgs {
GDCLASS(ActivateStateArgs, StateArgs);
static void _bind_methods();
public:
virtual State construct(Node *context) const override;
virtual State construct(Planner *context) const override;
};
};