feat: started implementing objective flags system

This commit is contained in:
Sara 2025-03-27 00:23:04 +01:00
parent a43ffb6479
commit 085c402202
2 changed files with 118 additions and 0 deletions

74
src/objective_flags.cpp Normal file
View file

@ -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<ObjectiveData> 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<ObjectiveData> 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<ObjectiveData> 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]);
}
}

44
src/objective_flags.hpp Normal file
View file

@ -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 <cmath>
#include <godot_cpp/classes/node.hpp>
#include <godot_cpp/classes/resource.hpp>
#include <godot_cpp/templates/hash_set.hpp>
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<gd::Ref<ObjectiveData>> sub_objectives{};
};
class ObjectiveFlags : public gd::Node {
GDCLASS(ObjectiveFlags, gd::Node);
static void _bind_methods();
public:
void register_objective(gd::Ref<ObjectiveData> objective_name);
gd::Ref<ObjectiveData> get_objective_by_name();
void set_currently_active_objectives(gd::Array array);
gd::Array get_currently_active_objectives() const;
private:
gd::HashSet<gd::Ref<ObjectiveData>> currently_active_objectives{};
};
#endif // !OBJECTIVE_FLAGS_HPP