From 51bd3733c4a03fc97d5de740bd275c5ea237e348 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 19 Jun 2024 12:50:29 +0200 Subject: [PATCH] feat: split end state callback into finished and failed --- src/unit.cpp | 15 +++++++++++++-- src/unit.hpp | 5 ++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/unit.cpp b/src/unit.cpp index b987728..6df2ca3 100644 --- a/src/unit.cpp +++ b/src/unit.cpp @@ -49,10 +49,15 @@ void Unit::destroy_state() { if(this->state == nullptr || this->state->is_queued_for_deletion() || !this->state->is_inside_tree()) return; this->state->queue_free(); - this->remove_child(this->state); this->state = nullptr; } +void Unit::state_finished() { + if(this->current_plan.is_empty()) + this->emit_signal("goal_finished"); + this->next_action(); +} + void Unit::next_action() { if(this->state != nullptr && !this->state->is_queued_for_deletion()) this->destroy_state(); @@ -66,5 +71,11 @@ void Unit::next_action() { } this->current_plan.remove_at(0); this->add_child(this->state); - this->state->connect("end_state", this->on_end_state); + + this->state->connect("state_finished", this->on_state_finished); + this->state->connect("state_failed", this->on_plan_failed); +} + +void Unit::replan_goal() { + this->plan_for_goal(this->current_goal); } diff --git a/src/unit.hpp b/src/unit.hpp index ac016a3..8227c95 100644 --- a/src/unit.hpp +++ b/src/unit.hpp @@ -26,14 +26,17 @@ public: UnitWorldState *get_world_state() const; private: void destroy_state(); + void state_finished(); void next_action(); + void replan_goal(); private: goap::Plan current_plan{}; goap::State *state{nullptr}; gd::Node3D *eyes{nullptr}; - gd::Callable on_end_state{callable_mp(this, &Unit::next_action)}; + gd::Callable on_state_finished{callable_mp(this, &Unit::state_finished)}; + gd::Callable on_plan_failed{callable_mp(this, &Unit::replan_goal)}; gd::NavigationAgent3D *agent{nullptr}; UnitWorldState *world_state{nullptr};