From ee6838d33bf0efa7a323e05c73c8808541677e99 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 14 Aug 2019 15:52:23 +0200 Subject: [PATCH] The bag is now scriptable. --- config.pyc | Bin 433 -> 433 bytes entity_spell_system | 1 - inventory/bag.cpp | 299 ++++++++------------------------------- inventory/bag.h | 35 ++--- inventory/grid_bag.cpp | 289 +++++++++++++++++++++++++++++++++++++ inventory/grid_bag.h | 61 ++++++++ inventory/normal_bag.cpp | 112 +++++++++++++++ inventory/normal_bag.h | 47 ++++++ 8 files changed, 580 insertions(+), 264 deletions(-) delete mode 120000 entity_spell_system create mode 100644 inventory/grid_bag.cpp create mode 100644 inventory/grid_bag.h create mode 100644 inventory/normal_bag.cpp create mode 100644 inventory/normal_bag.h diff --git a/config.pyc b/config.pyc index 836dec77fa752e16e50f895323e34d7466b21353..d25b4e726598741efc6c3bd1533b634fd09196bf 100644 GIT binary patch delta 16 YcmdnUypfrm`7 item) { - ERR_FAIL_COND_V(!item.is_valid(), true); - - Ref item_template = item->get_item_template(); - - ERR_FAIL_COND_V(!item_template.is_valid(), true); - - int _item_size_x = item_template->get_inventory_size_x(); - int _item_size_y = item_template->get_inventory_size_y(); - - for (int x = 0; x < _size_x - _item_size_x; ++x) { - for (int y = 0; y < _size_y - _item_size_y; ++y) { - if (_space_map[x * _size_x + y] == 0) { - - bool found = true; - for (int xx = x; xx < _item_size_x; ++xx) { - for (int yy = y; yy < _item_size_y; ++yy) { - if (_space_map[xx * _size_x + yy] != 0) { - found = false; - break; - } - } - - if (!found) - break; - } - - if (found) { - item->set_inventory_position_x(x); - item->set_inventory_position_y(y); - - _items.push_back(item); - - int index = _items.size() - 1; - - for (int xx = x; xx < _item_size_x; ++xx) { - for (int yy = y; yy < _item_size_y; ++yy) { - _space_map.set(xx * _size_x + yy, index); - } - } - - - } - } - } - } - - return false; + ERR_FAIL_COND_V(!has_method("_add_item"), false); + + return call("_add_item", item); } -bool Bag::add_item_to_position(const int x, const int y, Ref item) { - ERR_FAIL_COND_V(!item.is_valid(), false); - ERR_FAIL_COND_V(x < 0 || y < 0, false); - ERR_FAIL_COND_V(x > _size_x || y > _size_y, false); - Ref item_template = item->get_item_template(); - - ERR_FAIL_COND_V(!item_template.is_valid(), true); - - int _item_size_x = item_template->get_inventory_size_x(); - int _item_size_y = item_template->get_inventory_size_y(); - - ERR_FAIL_COND_V(x + _item_size_x > _size_x || y + _item_size_y > _size_y, false); - - int sx = x + _item_size_x; - int sy = y + _item_size_y; - - for (int xx = x; xx < sx; ++xx) { - for (int yy = y; yy < sy; ++yy) { - if (_space_map[xx * _size_x + yy] != 0) { - return false; - } - } - } - - item->set_inventory_position_x(x); - item->set_inventory_position_y(y); - - _items.push_back(item); - - int index = _items.size() - 1; - - for (int xx = x; xx < sx; ++xx) { - for (int yy = y; yy < sy; ++yy) { - _space_map.set(xx * _size_x + yy, index); - } - } - - return true; +Ref Bag::get_item(const int index) { + ERR_FAIL_COND_V(!has_method("_get_item"), Ref(NULL)); + + return call("_get_item", index); } -bool Bag::can_add_item_at(const int x, const int y, const Ref item) const { - ERR_FAIL_COND_V(!item.is_valid(), false); - ERR_FAIL_COND_V(x < 0 || y < 0, false); - ERR_FAIL_COND_V(x > _size_x || y > _size_y, false); - - int indx = _space_map[x * _size_x + y]; - - if (indx != 0) { - Ref it = _items[indx]; - - if (it.is_valid()) { - if (it->get_item_template() == item->get_item_template()) { - //todo check stacks - return false; - } - } - } - - Ref item_template = item->get_item_template(); - - ERR_FAIL_COND_V(!item_template.is_valid(), true); - - int _item_size_x = item_template->get_inventory_size_x(); - int _item_size_y = item_template->get_inventory_size_y(); - - ERR_FAIL_COND_V(x + _item_size_x > _size_x || y + _item_size_y > _size_y, false); - - int sx = x + _item_size_x; - int sy = y + _item_size_y; - - for (int xx = x; xx < sx; ++xx) { - for (int yy = y; yy < sy; ++yy) { - if (_space_map[xx * _size_x + yy] != 0) { - return false; - } - } - } - - return true; -} -int Bag::item_count_under_area(const int x, const int y, const int size_x, const int size_y) const { - ERR_FAIL_COND_V(x < 0 || y < 0, false); - ERR_FAIL_COND_V(x > _size_x || y > _size_y, false); - - ERR_FAIL_COND_V(x + size_x > _size_x || y + size_y > _size_y, false); - - int count = 0; - int sx = x + size_x; - int sy = y + size_y; - - for (int xx = x; xx < sx; ++xx) { - for (int yy = y; yy < sy; ++yy) { - if (_space_map[xx * _size_x + yy] != 0) { - - } - } - } - - return count; +Ref Bag::remove_item(const int index) { + ERR_FAIL_COND_V(!has_method("_remove_item"), Ref(NULL)); + + return call("_remove_item", index); } -Ref Bag::get_item(const int index) const { - ERR_FAIL_INDEX_V(index, _items.size(), Ref(NULL)); - - return (_items.get(index)); +void Bag::swap_items(const int item1_index, const int item2_index) { + ERR_FAIL_COND(!has_method("_swap_items")); + + call("_swap_items", item1_index, item2_index); } -Ref Bag::get_and_remove_item(const int index) { - ERR_FAIL_INDEX_V(index, _items.size(), Ref(NULL)); - - Ref item = _items.get(index); - - _items.remove(index); - - //Go over everything, to make sure item site changes won't cause bugs. - for (int x = 0; x < _size_x; ++x) { - for (int y = 0; y < _size_y ; ++y) { - int indx = x * _size_x + y; - - if (_space_map[indx] == index) { - _space_map.set(indx, 0); - } - } - } - - return item; +bool Bag::can_add_item(const Ref item) { + ERR_FAIL_COND_V(!has_method("_can_add_item"), false); + + return call("_can_add_item", item); } -void Bag::remove_item(const int index) { - ERR_FAIL_INDEX(index, _items.size()); - - _items.remove(index); - - //Go over everything, to make sure item site changes won't cause bugs. - for (int x = 0; x < _size_x; ++x) { - for (int y = 0; y < _size_y ; ++y) { - int indx = x * _size_x + y; - - if (_space_map[indx] == index) { - _space_map.set(indx, 0); - } - } - } +int Bag::get_item_count() { + ERR_FAIL_COND_V(!has_method("_get_item_count"), 0); + + return call("_get_item_count"); } -void Bag::basic_add_item(const Ref item) { - _items.push_back(item); -} -void Bag::basic_remove_item(const int index){ - _items.remove(index); + +int Bag::get_size() { + ERR_FAIL_COND_V(!has_method("_get_size"), 0); + + return call("_get_size"); } -int Bag::get_item_count() const { - return _items.size(); +void Bag::set_size(const int size) { + ERR_FAIL_COND(!has_method("_set_size")); + + call("_set_size"); } -int Bag::get_space_map_entry(const int index) const { - return _space_map[index]; -} -void Bag::set_space_map_entry(const int index, const int value) { - _space_map.set(index, value); -} -int Bag::get_size_x() const { - return _size_x; -} -int Bag::get_size_y() const { - return _size_y; -} - -void Bag::set_size(const int x, const int y) { - ERR_FAIL_COND(x == 0 || y == 0); - ERR_FAIL_COND(_size_x != 0 || _size_y != 0); - - _size_x = x; - _size_y = y; - - _space_map.resize(x * y); - - for (int i = 0; i < _space_map.size(); ++i) { - _space_map.set(i, 0); - } +bool Bag::is_full() { + ERR_FAIL_COND_V(!has_method("_is_full"), true); + + return call("_is_full"); } Bag::Bag() { - _allowed_item_types = 0x1FFF; - - _size_x = 0; - _size_y = 0; + _allowed_item_types = 0xFFFFFF; } Bag::~Bag() { - _items.clear(); - _space_map.clear(); } void Bag::_bind_methods() { + ADD_SIGNAL(MethodInfo("changed", PropertyInfo(Variant::OBJECT, "bag", PROPERTY_HINT_RESOURCE_TYPE, "Bag"))); + ClassDB::bind_method(D_METHOD("get_allowed_item_types"), &Bag::get_allowed_item_types); ClassDB::bind_method(D_METHOD("set_allowed_item_types", "count"), &Bag::set_allowed_item_types); ADD_PROPERTY(PropertyInfo(Variant::INT, "allowed_item_types", PROPERTY_HINT_FLAGS, ItemEnums::BINDING_STRING_ITEM_TYPE_FLAGS), "set_allowed_item_types", "get_allowed_item_types"); + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::BOOL, "could_add"), "_add_item", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "ItemInstance"))); + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "ItemInstance"), "_get_item", PropertyInfo(Variant::INT, "index"))); + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "ItemInstance"), "_remove_item", PropertyInfo(Variant::INT, "index"))); + BIND_VMETHOD(MethodInfo("_swap_items", PropertyInfo(Variant::INT, "item1_index"), PropertyInfo(Variant::INT, "item2_index"))); + + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::BOOL, "can"), "_can_add_item", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "ItemInstance"))); + + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::INT, "count"), "_get_item_count")); + + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::INT, "size"), "_get_size")); + BIND_VMETHOD(MethodInfo("_set_size", PropertyInfo(Variant::INT, "size"))); + + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::BOOL, "full"), "_is_full")); + ClassDB::bind_method(D_METHOD("add_item", "item"), &Bag::add_item); - ClassDB::bind_method(D_METHOD("add_item_to_position", "x", "y", "item"), &Bag::add_item_to_position); - - ClassDB::bind_method(D_METHOD("can_add_item_at", "x", "y", "item"), &Bag::can_add_item_at); - ClassDB::bind_method(D_METHOD("item_count_under_area", "x", "y", "item"), &Bag::item_count_under_area); - ClassDB::bind_method(D_METHOD("get_item", "index"), &Bag::get_item); - ClassDB::bind_method(D_METHOD("get_and_remove_item", "index"), &Bag::get_and_remove_item); ClassDB::bind_method(D_METHOD("remove_item", "index"), &Bag::remove_item); - - ClassDB::bind_method(D_METHOD("basic_add_item", "item"), &Bag::basic_add_item); - ClassDB::bind_method(D_METHOD("basic_remove_item", "index"), &Bag::basic_remove_item); - + ClassDB::bind_method(D_METHOD("swap_items", "item1_index", "item2_index"), &Bag::swap_items); + + ClassDB::bind_method(D_METHOD("can_add_item", "item"), &Bag::can_add_item); + ClassDB::bind_method(D_METHOD("get_item_count"), &Bag::get_item_count); - ClassDB::bind_method(D_METHOD("get_space_map_entry", "index"), &Bag::get_space_map_entry); - ClassDB::bind_method(D_METHOD("set_space_map_entry", "index", "value"), &Bag::set_space_map_entry); - - ClassDB::bind_method(D_METHOD("get_size_x"), &Bag::get_size_x); - ClassDB::bind_method(D_METHOD("get_size_y"), &Bag::get_size_y); - - ClassDB::bind_method(D_METHOD("set_size", "x", "y"), &Bag::set_size); - + ClassDB::bind_method(D_METHOD("get_size"), &Bag::get_size); + ClassDB::bind_method(D_METHOD("set_size", "size"), &Bag::set_size); + + ClassDB::bind_method(D_METHOD("is_full"), &Bag::is_full); } diff --git a/inventory/bag.h b/inventory/bag.h index 6398d8d..5f35163 100644 --- a/inventory/bag.h +++ b/inventory/bag.h @@ -17,27 +17,18 @@ public: void set_allowed_item_types(const int value); bool add_item(Ref item); - bool add_item_to_position(const int x, const int y, Ref item); + Ref get_item(const int index); + Ref remove_item(const int index); + void swap_items(const int item1_index, const int item2_index); + + bool can_add_item(Ref item); - bool can_add_item_at(const int x, const int y, const Ref item) const; - int item_count_under_area(const int x, const int y, const int size_x, const int size_y) const; - - Ref get_item(const int index) const; - Ref get_and_remove_item(const int index); - void remove_item(const int index); - - void basic_add_item(const Ref item); - void basic_remove_item(const int index); - - int get_item_count() const; + int get_item_count(); - int get_space_map_entry(const int index) const; - void set_space_map_entry(const int index, const int value); - - int get_size_x() const; - int get_size_y() const; - - void set_size(const int x, const int y); + int get_size(); + void set_size(const int size); + + bool is_full(); //to_dict(); //from_dict(); @@ -50,12 +41,6 @@ protected: private: int _allowed_item_types; - - int _size_x; - int _size_y; - - Vector > _items; - Vector _space_map; }; #endif diff --git a/inventory/grid_bag.cpp b/inventory/grid_bag.cpp new file mode 100644 index 0000000..87ce8e7 --- /dev/null +++ b/inventory/grid_bag.cpp @@ -0,0 +1,289 @@ +#include "grid_bag.h" + +#include "../data/item_template.h" +#include "../data/item_instance.h" + +int GridBag::get_allowed_item_types() const { + return _allowed_item_types; +} + +void GridBag::set_allowed_item_types(const int value) { + _allowed_item_types = value; +} + +bool GridBag::add_item(Ref item) { + ERR_FAIL_COND_V(!item.is_valid(), true); + + Ref item_template = item->get_item_template(); + + ERR_FAIL_COND_V(!item_template.is_valid(), true); + + int _item_size_x = item_template->get_inventory_size_x(); + int _item_size_y = item_template->get_inventory_size_y(); + + for (int x = 0; x < _size_x - _item_size_x; ++x) { + for (int y = 0; y < _size_y - _item_size_y; ++y) { + if (_space_map[x * _size_x + y] == 0) { + + bool found = true; + for (int xx = x; xx < _item_size_x; ++xx) { + for (int yy = y; yy < _item_size_y; ++yy) { + if (_space_map[xx * _size_x + yy] != 0) { + found = false; + break; + } + } + + if (!found) + break; + } + + if (found) { + item->set_inventory_position_x(x); + item->set_inventory_position_y(y); + + _items.push_back(item); + + int index = _items.size() - 1; + + for (int xx = x; xx < _item_size_x; ++xx) { + for (int yy = y; yy < _item_size_y; ++yy) { + _space_map.set(xx * _size_x + yy, index); + } + } + + + } + } + } + } + + return false; +} + +bool GridBag::add_item_to_position(const int x, const int y, Ref item) { + ERR_FAIL_COND_V(!item.is_valid(), false); + ERR_FAIL_COND_V(x < 0 || y < 0, false); + ERR_FAIL_COND_V(x > _size_x || y > _size_y, false); + + Ref item_template = item->get_item_template(); + + ERR_FAIL_COND_V(!item_template.is_valid(), true); + + int _item_size_x = item_template->get_inventory_size_x(); + int _item_size_y = item_template->get_inventory_size_y(); + + ERR_FAIL_COND_V(x + _item_size_x > _size_x || y + _item_size_y > _size_y, false); + + int sx = x + _item_size_x; + int sy = y + _item_size_y; + + for (int xx = x; xx < sx; ++xx) { + for (int yy = y; yy < sy; ++yy) { + if (_space_map[xx * _size_x + yy] != 0) { + return false; + } + } + } + + item->set_inventory_position_x(x); + item->set_inventory_position_y(y); + + _items.push_back(item); + + int index = _items.size() - 1; + + for (int xx = x; xx < sx; ++xx) { + for (int yy = y; yy < sy; ++yy) { + _space_map.set(xx * _size_x + yy, index); + } + } + + return true; +} + +bool GridBag::can_add_item_at(const int x, const int y, const Ref item) const { + ERR_FAIL_COND_V(!item.is_valid(), false); + ERR_FAIL_COND_V(x < 0 || y < 0, false); + ERR_FAIL_COND_V(x > _size_x || y > _size_y, false); + + int indx = _space_map[x * _size_x + y]; + + if (indx != 0) { + Ref it = _items[indx]; + + if (it.is_valid()) { + if (it->get_item_template() == item->get_item_template()) { + //todo check stacks + return false; + } + } + } + + Ref item_template = item->get_item_template(); + + ERR_FAIL_COND_V(!item_template.is_valid(), true); + + int _item_size_x = item_template->get_inventory_size_x(); + int _item_size_y = item_template->get_inventory_size_y(); + + ERR_FAIL_COND_V(x + _item_size_x > _size_x || y + _item_size_y > _size_y, false); + + int sx = x + _item_size_x; + int sy = y + _item_size_y; + + for (int xx = x; xx < sx; ++xx) { + for (int yy = y; yy < sy; ++yy) { + if (_space_map[xx * _size_x + yy] != 0) { + return false; + } + } + } + + return true; +} +int GridBag::item_count_under_area(const int x, const int y, const int size_x, const int size_y) const { + ERR_FAIL_COND_V(x < 0 || y < 0, false); + ERR_FAIL_COND_V(x > _size_x || y > _size_y, false); + + ERR_FAIL_COND_V(x + size_x > _size_x || y + size_y > _size_y, false); + + int count = 0; + int sx = x + size_x; + int sy = y + size_y; + + for (int xx = x; xx < sx; ++xx) { + for (int yy = y; yy < sy; ++yy) { + if (_space_map[xx * _size_x + yy] != 0) { + + } + } + } + + return count; +} + +Ref GridBag::get_item(const int index) const { + ERR_FAIL_INDEX_V(index, _items.size(), Ref(NULL)); + + return (_items.get(index)); +} + +Ref GridBag::get_and_remove_item(const int index) { + ERR_FAIL_INDEX_V(index, _items.size(), Ref(NULL)); + + Ref item = _items.get(index); + + _items.remove(index); + + //Go over everything, to make sure item site changes won't cause bugs. + for (int x = 0; x < _size_x; ++x) { + for (int y = 0; y < _size_y ; ++y) { + int indx = x * _size_x + y; + + if (_space_map[indx] == index) { + _space_map.set(indx, 0); + } + } + } + + return item; +} + +void GridBag::remove_item(const int index) { + ERR_FAIL_INDEX(index, _items.size()); + + _items.remove(index); + + //Go over everything, to make sure item site changes won't cause bugs. + for (int x = 0; x < _size_x; ++x) { + for (int y = 0; y < _size_y ; ++y) { + int indx = x * _size_x + y; + + if (_space_map[indx] == index) { + _space_map.set(indx, 0); + } + } + } +} + +void GridBag::basic_add_item(const Ref item) { + _items.push_back(item); +} +void GridBag::basic_remove_item(const int index){ + _items.remove(index); +} + +int GridBag::get_item_count() const { + return _items.size(); +} + +int GridBag::get_space_map_entry(const int index) const { + return _space_map[index]; +} +void GridBag::set_space_map_entry(const int index, const int value) { + _space_map.set(index, value); +} + +int GridBag::get_size_x() const { + return _size_x; +} +int GridBag::get_size_y() const { + return _size_y; +} + +void GridBag::set_size(const int x, const int y) { + ERR_FAIL_COND(x == 0 || y == 0); + ERR_FAIL_COND(_size_x != 0 || _size_y != 0); + + _size_x = x; + _size_y = y; + + _space_map.resize(x * y); + + for (int i = 0; i < _space_map.size(); ++i) { + _space_map.set(i, 0); + } +} + +GridBag::GridBag() { + _allowed_item_types = 0x1FFF; + + _size_x = 0; + _size_y = 0; +} + +GridBag::~GridBag() { + _items.clear(); + _space_map.clear(); +} + +void GridBag::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_allowed_item_types"), &GridBag::get_allowed_item_types); + ClassDB::bind_method(D_METHOD("set_allowed_item_types", "count"), &GridBag::set_allowed_item_types); + ADD_PROPERTY(PropertyInfo(Variant::INT, "allowed_item_types", PROPERTY_HINT_FLAGS, ItemEnums::BINDING_STRING_ITEM_TYPE_FLAGS), "set_allowed_item_types", "get_allowed_item_types"); + + ClassDB::bind_method(D_METHOD("add_item", "item"), &GridBag::add_item); + ClassDB::bind_method(D_METHOD("add_item_to_position", "x", "y", "item"), &GridBag::add_item_to_position); + + ClassDB::bind_method(D_METHOD("can_add_item_at", "x", "y", "item"), &GridBag::can_add_item_at); + ClassDB::bind_method(D_METHOD("item_count_under_area", "x", "y", "item"), &GridBag::item_count_under_area); + + ClassDB::bind_method(D_METHOD("get_item", "index"), &GridBag::get_item); + ClassDB::bind_method(D_METHOD("get_and_remove_item", "index"), &GridBag::get_and_remove_item); + ClassDB::bind_method(D_METHOD("remove_item", "index"), &GridBag::remove_item); + + ClassDB::bind_method(D_METHOD("basic_add_item", "item"), &GridBag::basic_add_item); + ClassDB::bind_method(D_METHOD("basic_remove_item", "index"), &GridBag::basic_remove_item); + + ClassDB::bind_method(D_METHOD("get_item_count"), &GridBag::get_item_count); + + ClassDB::bind_method(D_METHOD("get_space_map_entry", "index"), &GridBag::get_space_map_entry); + ClassDB::bind_method(D_METHOD("set_space_map_entry", "index", "value"), &GridBag::set_space_map_entry); + + ClassDB::bind_method(D_METHOD("get_size_x"), &GridBag::get_size_x); + ClassDB::bind_method(D_METHOD("get_size_y"), &GridBag::get_size_y); + + ClassDB::bind_method(D_METHOD("set_size", "x", "y"), &GridBag::set_size); + +} diff --git a/inventory/grid_bag.h b/inventory/grid_bag.h new file mode 100644 index 0000000..3984768 --- /dev/null +++ b/inventory/grid_bag.h @@ -0,0 +1,61 @@ +#ifndef GRID_BAG_H +#define GRID_BAG_H + +#include "core/reference.h" +#include "core/vector.h" + +#include "../item_enums.h" + +class itemTemplate; +class ItemInstance; + +class GridBag : public Reference { + GDCLASS(GridBag, Reference); + +public: + int get_allowed_item_types() const; + void set_allowed_item_types(const int value); + + bool add_item(Ref item); + bool add_item_to_position(const int x, const int y, Ref item); + + bool can_add_item_at(const int x, const int y, const Ref item) const; + int item_count_under_area(const int x, const int y, const int size_x, const int size_y) const; + + Ref get_item(const int index) const; + Ref get_and_remove_item(const int index); + void remove_item(const int index); + + void basic_add_item(const Ref item); + void basic_remove_item(const int index); + + int get_item_count() const; + + int get_space_map_entry(const int index) const; + void set_space_map_entry(const int index, const int value); + + int get_size_x() const; + int get_size_y() const; + + void set_size(const int x, const int y); + + //to_dict(); + //from_dict(); + + GridBag(); + ~GridBag(); + +protected: + static void _bind_methods(); + +private: + int _allowed_item_types; + + int _size_x; + int _size_y; + + Vector > _items; + Vector _space_map; +}; + +#endif diff --git a/inventory/normal_bag.cpp b/inventory/normal_bag.cpp new file mode 100644 index 0000000..8511be0 --- /dev/null +++ b/inventory/normal_bag.cpp @@ -0,0 +1,112 @@ +#include "bag.h" + +#include "../data/item_template.h" +#include "../data/item_instance.h" + +int Bag::get_allowed_item_types() const { + return _allowed_item_types; +} + +void Bag::set_allowed_item_types(const int value) { + _allowed_item_types = value; +} + +bool Bag::add_item(Ref item) { + +} + + +Ref Bag::get_item(const int index) const { + ERR_FAIL_INDEX_V(index, _items.size(), Ref(NULL)); + + return (_items.get(index)); +} + +Ref Bag::remove_item(const int index) { + ERR_FAIL_INDEX_V(index, _items.size(), Ref(NULL)); + + Ref item = _items.get(index); + + _items.remove(index); + + //Go over everything, to make sure item site changes won't cause bugs. + for (int x = 0; x < _size_x; ++x) { + for (int y = 0; y < _size_y ; ++y) { + int indx = x * _size_x + y; + + if (_space_map[indx] == index) { + _space_map.set(indx, 0); + } + } + } + + return item; +} + +bool Bag::can_add_item(const Ref item) const { + +} + +int Bag::get_item_count() const { + return _items.size(); +} + + +int Bag::get_size() const { + return _items.size(); +} + +void Bag::set_size(const int x, const int y) { + ERR_FAIL_COND(x == 0 || y == 0); + ERR_FAIL_COND(_size_x != 0 || _size_y != 0); + + _size_x = x; + _size_y = y; + + _space_map.resize(x * y); + + for (int i = 0; i < _space_map.size(); ++i) { + _space_map.set(i, 0); + } +} + + +Bag::Bag() { + _allowed_item_types = 0x1FFF; + + _size = 0; +} + +Bag::~Bag() { + _items.clear(); +} + +void Bag::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_allowed_item_types"), &Bag::get_allowed_item_types); + ClassDB::bind_method(D_METHOD("set_allowed_item_types", "count"), &Bag::set_allowed_item_types); + ADD_PROPERTY(PropertyInfo(Variant::INT, "allowed_item_types", PROPERTY_HINT_FLAGS, ItemEnums::BINDING_STRING_ITEM_TYPE_FLAGS), "set_allowed_item_types", "get_allowed_item_types"); + + ClassDB::bind_method(D_METHOD("add_item", "item"), &Bag::add_item); + ClassDB::bind_method(D_METHOD("add_item_to_position", "x", "y", "item"), &Bag::add_item_to_position); + + ClassDB::bind_method(D_METHOD("can_add_item_at", "x", "y", "item"), &Bag::can_add_item_at); + ClassDB::bind_method(D_METHOD("item_count_under_area", "x", "y", "item"), &Bag::item_count_under_area); + + ClassDB::bind_method(D_METHOD("get_item", "index"), &Bag::get_item); + ClassDB::bind_method(D_METHOD("get_and_remove_item", "index"), &Bag::get_and_remove_item); + ClassDB::bind_method(D_METHOD("remove_item", "index"), &Bag::remove_item); + + ClassDB::bind_method(D_METHOD("basic_add_item", "item"), &Bag::basic_add_item); + ClassDB::bind_method(D_METHOD("basic_remove_item", "index"), &Bag::basic_remove_item); + + ClassDB::bind_method(D_METHOD("get_item_count"), &Bag::get_item_count); + + ClassDB::bind_method(D_METHOD("get_space_map_entry", "index"), &Bag::get_space_map_entry); + ClassDB::bind_method(D_METHOD("set_space_map_entry", "index", "value"), &Bag::set_space_map_entry); + + ClassDB::bind_method(D_METHOD("get_size_x"), &Bag::get_size_x); + ClassDB::bind_method(D_METHOD("get_size_y"), &Bag::get_size_y); + + ClassDB::bind_method(D_METHOD("set_size", "x", "y"), &Bag::set_size); + +} diff --git a/inventory/normal_bag.h b/inventory/normal_bag.h new file mode 100644 index 0000000..b0fd4b1 --- /dev/null +++ b/inventory/normal_bag.h @@ -0,0 +1,47 @@ +#ifndef BAG_H +#define BAG_H + +#include "core/reference.h" +#include "core/vector.h" + +#include "../item_enums.h" + +class itemTemplate; +class ItemInstance; + +class Bag : public Reference { + GDCLASS(Bag, Reference); + +public: + int get_allowed_item_types() const; + void set_allowed_item_types(const int value); + + bool add_item(Ref item); + Ref get_item(const int index) const; + Ref remove_item(const int index); + + bool can_add_item(Ref item); + + int get_item_count() const; + + int get_size() const; + void set_size(const int size); + + bool is_full() const; + + //to_dict(); + //from_dict(); + + Bag(); + ~Bag(); + +protected: + static void _bind_methods(); + +private: + int _allowed_item_types; + int _item_count; + Vector > _items; +}; + +#endif