2019-05-06 18:07:07 +02:00
|
|
|
#include "bag.h"
|
|
|
|
|
|
|
|
Ref<BagSlot> Bag::get_slot(int index) {
|
2019-05-30 00:26:02 +02:00
|
|
|
ERR_FAIL_INDEX_V(index, _slots.size(), Ref<BagSlot>(NULL));
|
2019-05-06 21:31:19 +02:00
|
|
|
|
2019-05-30 00:26:02 +02:00
|
|
|
return (_slots.get(index));
|
2019-05-06 18:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<BagSlot> Bag::get_and_remove_slot(int index) {
|
2019-05-30 00:26:02 +02:00
|
|
|
ERR_FAIL_INDEX_V(index, _slots.size(), Ref<BagSlot>(NULL));
|
2019-05-06 21:31:19 +02:00
|
|
|
|
2019-05-30 00:26:02 +02:00
|
|
|
Ref<BagSlot> slot = _slots.get(index);
|
2019-05-06 21:31:19 +02:00
|
|
|
|
2019-05-30 00:26:02 +02:00
|
|
|
_slots.set(index, Ref<BagSlot>(memnew(BagSlot())));
|
2019-05-06 18:07:07 +02:00
|
|
|
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Bag::get_slot_count() {
|
2019-05-30 00:26:02 +02:00
|
|
|
return _slots.size();
|
2019-05-06 18:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bag::set_slot_count(int count) {
|
2019-05-30 00:26:02 +02:00
|
|
|
ERR_FAIL_COND(_slots.size() > count);
|
2019-05-06 18:07:07 +02:00
|
|
|
|
2019-05-06 21:31:19 +02:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2019-05-30 00:26:02 +02:00
|
|
|
_slots.push_back(Ref<BagSlot>(memnew(BagSlot())));
|
2019-05-06 21:31:19 +02:00
|
|
|
}
|
2019-05-06 18:07:07 +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);
|
|
|
|
|
2019-05-30 00:26:02 +02:00
|
|
|
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 18:07:07 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2019-05-30 00:26:02 +02:00
|
|
|
ERR_FAIL_INDEX_V(slot_index, _slots.size(), false);
|
2019-05-06 21:31:19 +02:00
|
|
|
|
2019-05-30 00:26:02 +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;
|
|
|
|
}
|
2019-05-06 18:07:07 +02:00
|
|
|
|
|
|
|
Bag::Bag() {
|
|
|
|
}
|
|
|
|
|
|
|
|
Bag::~Bag() {
|
2019-05-30 00:26:02 +02:00
|
|
|
_slots.clear();
|
2019-05-06 18:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2019-05-06 18:07:07 +02:00
|
|
|
}
|