Item* Player::getWeapon(slots_t slot, bool ignoreAmmo) const
{
Item* item = inventory[slot];
if (!item) {
return nullptr;
}
WeaponType_t weaponType = item->getWeaponType();
if (weaponType == WEAPON_NONE || weaponType == WEAPON_SHIELD || weaponType == WEAPON_AMMO) {
return nullptr;
}
if (!ignoreAmmo && weaponType == WEAPON_DISTANCE) {
const ItemType& it = Item::items[item->getID()];
if (it.ammoType != AMMO_NONE) {
// Najpierw sprawdź, czy amunicja jest w slocie amunicji
Item* ammoItem = inventory[CONST_SLOT_AMMO];
if (!ammoItem || ammoItem->getAmmoType() != it.ammoType) {
// Jeśli nie, przeszukaj plecak
Container* backpack = dynamic_cast<Container*>(inventory[CONST_SLOT_BACKPACK]);
if (backpack) {
for (Item* backpackItem : backpack->getItemList()) {
if (backpackItem && backpackItem->getAmmoType() == it.ammoType) {
ammoItem = backpackItem;
break;
}
}
}
}
if (!ammoItem) {
return nullptr;
}
item = ammoItem;
}
}
return item;
}