entity_spell_system/inventory/bag.cpp

81 lines
1.9 KiB
C++
Raw Normal View History

#include "bag.h"
Ref<BagSlot> Bag::get_slot(int index) {
ERR_FAIL_INDEX_V(index, _slots.size(), Ref<BagSlot>(NULL));
2019-05-06 21:31:19 +02:00
return (_slots.get(index));
}
Ref<BagSlot> Bag::get_and_remove_slot(int index) {
ERR_FAIL_INDEX_V(index, _slots.size(), Ref<BagSlot>(NULL));
2019-05-06 21:31:19 +02:00
Ref<BagSlot> slot = _slots.get(index);
2019-05-06 21:31:19 +02:00
_slots.set(index, Ref<BagSlot>(memnew(BagSlot())));
return slot;
}
int Bag::get_slot_count() {
return _slots.size();
}
void Bag::set_slot_count(int count) {
ERR_FAIL_COND(_slots.size() > count);
2019-05-06 21:31:19 +02:00
for (int i = 0; i < count; ++i) {
_slots.push_back(Ref<BagSlot>(memnew(BagSlot())));
2019-05-06 21:31:19 +02:00
}
}
2019-05-06 21:31:19 +02:00
bool Bag::try_to_add_item(Ref<ItemInstance> item, int count) {
ERR_FAIL_COND_V(!item.is_valid(), false);
for (int i = 0; i < _slots.size(); ++i) {
Ref<BagSlot> slot = _slots.get(i);
2019-05-06 21:31:19 +02:00
if (!slot->has_item()) {
slot->set_item(item);
slot->set_count(count);
return true;
}
}
return false;
}
2019-05-06 21:31:19 +02:00
bool Bag::add_item_to_slot(Ref<ItemInstance> item, int slot_index, int count) {
ERR_FAIL_COND_V(!item.is_valid(), false);
ERR_FAIL_INDEX_V(slot_index, _slots.size(), false);
2019-05-06 21:31:19 +02:00
Ref<BagSlot> slot = _slots.get(slot_index);
2019-05-06 21:31:19 +02:00
ERR_FAIL_COND_V(!slot.is_valid(), false);
if (slot->has_item()) {
return false;
}
slot->set_item_count(item, count);
return true;
}
Bag::Bag() {
}
Bag::~Bag() {
_slots.clear();
}
void Bag::_bind_methods() {
2019-05-06 21:31:19 +02:00
ClassDB::bind_method(D_METHOD("get_slot", "index"), &Bag::get_slot);
ClassDB::bind_method(D_METHOD("get_and_remove_slot", "index"), &Bag::get_and_remove_slot);
ClassDB::bind_method(D_METHOD("get_slot_count"), &Bag::get_slot_count);
ClassDB::bind_method(D_METHOD("set_slot_count", "count"), &Bag::set_slot_count);
ClassDB::bind_method(D_METHOD("try_to_add_item", "item", "count"), &Bag::try_to_add_item, DEFVAL(1));
ClassDB::bind_method(D_METHOD("add_item_to_slot", "item", "slot_index", "count"), &Bag::add_item_to_slot, DEFVAL(1));
}