From 085c402202e0074222ac1c96ece753d9d2d09ece Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 27 Mar 2025 00:23:04 +0100 Subject: [PATCH] feat: started implementing objective flags system --- src/objective_flags.cpp | 74 +++++++++++++++++++++++++++++++++++++++++ src/objective_flags.hpp | 44 ++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 src/objective_flags.cpp create mode 100644 src/objective_flags.hpp diff --git a/src/objective_flags.cpp b/src/objective_flags.cpp new file mode 100644 index 0000000..2f971e6 --- /dev/null +++ b/src/objective_flags.cpp @@ -0,0 +1,74 @@ +#include "objective_flags.hpp" +#include "utils/godot_macros.hpp" + +void ObjectiveData::_bind_methods() { +#define CLASSNAME ObjectiveData + GDSIGNAL("progress_changed"); + GDSIGNAL("is_done"); + GDPROPERTY(target_count, gd::Variant::INT); + GDFUNCTION(get_is_completed); +} + +bool ObjectiveData::get_is_completed() const { + for(gd::Ref sub_objective : this->sub_objectives) { + if(!sub_objective->get_is_completed()) { + return false; + } + } + return this->completed_count >= this->target_count; +} + +void ObjectiveData::set_target_count(int value) { + this->target_count = value; +} + +int ObjectiveData::get_target_count() const { + return this->target_count; +} + +void ObjectiveData::set_completed_count(int value) { + this->completed_count = gd::MIN(value, this->target_count); + this->emit_signal("progress_changed"); +} + +int ObjectiveData::get_completed_count() const { + return this->completed_count; +} + +void ObjectiveData::set_sub_objectives_setting(gd::Array array) { + this->sub_objectives.clear(); + for(int i{0}; i < array.size(); ++i) { + this->sub_objectives.push_back(array[i]); + } +} + +gd::Array ObjectiveData::get_sub_objectives_setting() const { + gd::Array array{}; + for(gd::Ref data : this->sub_objectives) { + array.push_back(data); + } + return array; +} + +void ObjectiveData::check_is_done() { + if(this->get_is_completed()) { + this->emit_signal("is_done"); + } +} + +#undef CLASSNAME + +void ObjectiveFlags::_bind_methods() { +#define CLASSNAME ObjectiveFlags + GDSIGNAL("progress_changed", gd::PropertyInfo(gd::Variant::OBJECT, "objective", gd::PROPERTY_HINT_RESOURCE_TYPE, "ObjectiveData")); +} + +void ObjectiveFlags::register_objective(gd::Ref objective_name) { + this->currently_active_objectives.insert(objective_name); +} + +void ObjectiveFlags::set_currently_active_objectives(gd::Array array) { + for(int i{0}; i < array.size(); ++i) { + this->register_objective(array[i]); + } +} diff --git a/src/objective_flags.hpp b/src/objective_flags.hpp new file mode 100644 index 0000000..a2764c9 --- /dev/null +++ b/src/objective_flags.hpp @@ -0,0 +1,44 @@ +#ifndef OBJECTIVE_FLAGS_HPP +#define OBJECTIVE_FLAGS_HPP + +#include "godot_cpp/templates/vector.hpp" +#include "godot_cpp/variant/array.hpp" +#include +#include +#include +#include + +namespace gd = godot; + +class ObjectiveData : public gd::Resource { + GDCLASS(ObjectiveData, gd::Resource); + static void _bind_methods(); +public: + bool get_is_completed() const; + void set_target_count(int value); + int get_target_count() const; + void set_completed_count(int value); + int get_completed_count() const; + void set_sub_objectives_setting(gd::Array array); + gd::Array get_sub_objectives_setting() const; +private: + void check_is_done(); +private: + int target_count{1}; + int completed_count{0}; + gd::Vector> sub_objectives{}; +}; + +class ObjectiveFlags : public gd::Node { + GDCLASS(ObjectiveFlags, gd::Node); + static void _bind_methods(); +public: + void register_objective(gd::Ref objective_name); + gd::Ref get_objective_by_name(); + void set_currently_active_objectives(gd::Array array); + gd::Array get_currently_active_objectives() const; +private: + gd::HashSet> currently_active_objectives{}; +}; + +#endif // !OBJECTIVE_FLAGS_HPP