Skip to content

Commit

Permalink
Remove categorized_slot
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptsengineer committed Aug 20, 2024
1 parent 8665fc6 commit 9630eb7
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 126 deletions.
28 changes: 0 additions & 28 deletions doc_classes/CategorizedSlot.xml

This file was deleted.

10 changes: 10 additions & 0 deletions doc_classes/Slot.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
<description>
</description>
</method>
<method name="is_accept_any_categories_of_item" qualifiers="const">
<return type="bool" />
<param index="0" name="item" type="ItemDefinition" />
<description>
</description>
</method>
<method name="is_empty" qualifiers="const">
<return type="bool" />
<description>
Expand All @@ -62,8 +68,12 @@
</method>
</methods>
<members>
<member name="accepted_categories" type="ItemCategory[]" setter="set_accepted_categories" getter="get_accepted_categories" default="[]">
</member>
<member name="amount" type="int" setter="set_amount" getter="get_amount" default="0">
</member>
<member name="categorized" type="int" setter="set_categorized" getter="is_categorized" default="0">
</member>
<member name="item" type="Item" setter="set_item" getter="get_item">
</member>
<member name="max_stack" type="int" setter="set_max_stack" getter="get_max_stack" default="-1">
Expand Down
69 changes: 69 additions & 0 deletions src/base/slot.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "slot.h"

#include "item_definition.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/core/class_db.hpp>

Expand All @@ -11,6 +12,10 @@ void Slot::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_amount"), &Slot::get_amount);
ClassDB::bind_method(D_METHOD("set_max_stack", "max_stack"), &Slot::set_max_stack);
ClassDB::bind_method(D_METHOD("get_max_stack"), &Slot::get_max_stack);
ClassDB::bind_method(D_METHOD("set_categorized", "categorized"), &Slot::set_categorized);
ClassDB::bind_method(D_METHOD("is_categorized"), &Slot::is_categorized);
ClassDB::bind_method(D_METHOD("set_accepted_categories", "accepted_categories"), &Slot::set_accepted_categories);
ClassDB::bind_method(D_METHOD("get_accepted_categories"), &Slot::get_accepted_categories);
ClassDB::bind_method(D_METHOD("get_item_id"), &Slot::get_item_id);
ClassDB::bind_method(D_METHOD("add", "item", "amount"), &Slot::add, DEFVAL(1));
ClassDB::bind_method(D_METHOD("remove", "item", "amount"), &Slot::remove, DEFVAL(1));
Expand All @@ -20,12 +25,47 @@ void Slot::_bind_methods() {
ClassDB::bind_method(D_METHOD("has_valid"), &Slot::has_valid);
ClassDB::bind_method(D_METHOD("contains", "item", "amount"), &Slot::contains, DEFVAL(1));
ClassDB::bind_method(D_METHOD("contains_category", "category"), &Slot::contains_category);
ClassDB::bind_method(D_METHOD("is_accept_any_categories_of_item", "item"), &Slot::is_accept_any_categories_of_item);

ADD_SIGNAL(MethodInfo("updated"));

ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "Item"), "set_item", "get_item");
ADD_PROPERTY(PropertyInfo(Variant::INT, "amount"), "set_amount", "get_amount");
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_stack"), "set_max_stack", "get_max_stack");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "categorized"), "set_categorized", "is_categorized");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "accepted_categories", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "ItemCategory")), "set_accepted_categories", "get_accepted_categories");
}

void Slot::_update_categories_code() {
accepted_categories_code = 0;
if (!Engine::get_singleton()->is_editor_hint()) {
for (size_t i = 0; i < accepted_categories.size(); i++) {
Ref<ItemCategory> c = accepted_categories[i];
if (c == nullptr)
continue;
accepted_categories_code |= c->get_code();
}
}
}

bool Slot::_is_accept_any_categories(const TypedArray<ItemCategory> &other_list) const {
for (size_t i = 0; i < other_list.size(); i++) {
Ref<ItemCategory> c = other_list[i];
if (c == nullptr)
continue;
if ((accepted_categories_code & c->get_code()) > 0) {
return true;
}
}
return false;
}

void Slot::_validate_property(PropertyInfo &p_property) const {
if (p_property.name == StringName("accepted_categories")) {
if (!categorized) {
p_property.usage = PROPERTY_USAGE_NO_EDITOR;
}
}
}

Slot::Slot() {
Expand Down Expand Up @@ -58,6 +98,24 @@ int Slot::get_max_stack() const {
return max_stack;
}

void Slot::set_categorized(const bool &new_categorized) {
categorized = new_categorized;
notify_property_list_changed();
}

int Slot::is_categorized() const {
return categorized;
}

void Slot::set_accepted_categories(const TypedArray<ItemCategory> &new_accepted_categories) {
accepted_categories = new_accepted_categories;
_update_categories_code();
}

TypedArray<ItemCategory> Slot::get_accepted_categories() const {
return accepted_categories;
}

bool Slot::is_full() const {
if (has_valid()) {
return amount >= get_max_stack();
Expand Down Expand Up @@ -89,6 +147,10 @@ bool Slot::contains_category(Ref<ItemCategory> category) const {
}
}

bool Slot::is_accept_any_categories_of_item(const Ref<ItemDefinition> &other_item) const {
return accepted_categories_code == 0 || _is_accept_any_categories(other_item->get_categories());
}

int Slot::get_item_id() const {
if (this->item == nullptr || this->item->get_definition() == nullptr) {
return ItemDefinition::NONE;
Expand All @@ -98,6 +160,13 @@ int Slot::get_item_id() const {
}

int Slot::add(const Ref<Item> item, const int &amount) {
if (categorized) {
_update_categories_code();
if (!is_accept_any_categories_of_item(item->get_definition())) {
return amount;
}
}

if (this->item == nullptr) {
return amount;
}
Expand Down
11 changes: 11 additions & 0 deletions src/base/slot.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class Slot : public Resource {
Ref<Item> item;
int amount = 0;
int max_stack = -1;
bool categorized;
TypedArray<ItemCategory> accepted_categories;
int accepted_categories_code = 0;
void _update_categories_code();
bool _is_accept_any_categories(const TypedArray<ItemCategory> &other_list) const;
void _validate_property(PropertyInfo &p_property) const;

protected:
static void _bind_methods();
Expand All @@ -26,6 +32,10 @@ class Slot : public Resource {
int get_amount() const;
void set_max_stack(const int &new_max_stack);
int get_max_stack() const;
void set_categorized(const bool &new_categorized);
int is_categorized() const;
void set_accepted_categories(const TypedArray<ItemCategory> &new_accepted_categories);
TypedArray<ItemCategory> get_accepted_categories() const;
int get_item_id() const;
virtual int add(const Ref<Item> item, const int &amount);
int remove(const Ref<Item> item, const int &amount);
Expand All @@ -35,6 +45,7 @@ class Slot : public Resource {
bool has_valid() const;
bool contains(Ref<Item> item, int amount) const;
bool contains_category(Ref<ItemCategory> category) const;
bool is_accept_any_categories_of_item(const Ref<ItemDefinition> &other_item) const;
};

#endif // SLOT_CLASS_H
62 changes: 0 additions & 62 deletions src/core/categorized_slot.cpp

This file was deleted.

30 changes: 0 additions & 30 deletions src/core/categorized_slot.h

This file was deleted.

6 changes: 2 additions & 4 deletions src/core/inventory_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "inventory_handler.h"
#include "core/categorized_slot.h"
#include "inventory.h"
#include <godot_cpp/classes/engine.hpp>

Expand Down Expand Up @@ -306,9 +305,8 @@ void InventoryHandler::transaction_to_at(const int &slot_index, Inventory *inven
} else {
// Different items in slot and other_slot
// Check if transaction_slot amount is equal of origin_slot amount
CategorizedSlot *c_slot = Object::cast_to<CategorizedSlot>(*slot);
if (c_slot != nullptr) {
if (!c_slot->is_accept_any_categories_of_item(item->get_definition())) {
if (slot->is_categorized()) {
if (!slot->is_accept_any_categories_of_item(item->get_definition())) {
return;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "base/node_inventories.h"
#include "base/recipe.h"
#include "base/slot.h"
#include "core/categorized_slot.h"
#include "core/hotbar.h"
#include "core/inventory.h"
#include "core/inventory_handler.h"
Expand All @@ -37,7 +36,6 @@ void initialize_gdextension_types(ModuleInitializationLevel p_level) {
ClassDB::register_class<NodeInventories>();
ClassDB::register_class<Recipe>();
ClassDB::register_class<Slot>();
ClassDB::register_class<CategorizedSlot>();
ClassDB::register_class<Hotbar>();
ClassDB::register_class<Inventory>();
ClassDB::register_class<InventoryHandler>();
Expand Down

0 comments on commit 9630eb7

Please sign in to comment.