feat: reloading withdraws ammo from inventory

This commit is contained in:
Sara 2025-08-10 16:37:02 +02:00
parent a2035e0db0
commit ed85b557a7
8 changed files with 92 additions and 17 deletions

View file

@ -4,6 +4,8 @@
void WeaponInventory::_bind_methods() {
BIND_HPROPERTY(Variant::OBJECT, starting_weapon, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
BIND_PROPERTY(Variant::INT, pistol_ammo);
BIND_PROPERTY(Variant::INT, rifle_ammo);
}
void WeaponInventory::on_switch_input() {
@ -65,6 +67,7 @@ void WeaponInventory::pickup_weapon(Ref<PackedScene> weapon_scene) {
}
Node *weapon_as_node{ weapon_scene->instantiate() };
if (WeaponBase * weapon{ cast_to<WeaponBase>(weapon_as_node) }) {
weapon->_set_inventory(this);
this->weapon_parent->add_child(weapon_as_node);
// Where to put the new weapon, consider empty slot 1, then empty slot 2, then replace current.
if (this->weapons[0] == nullptr) {
@ -100,3 +103,49 @@ void WeaponInventory::set_starting_weapon(Ref<PackedScene> scene) {
Ref<PackedScene> WeaponInventory::get_starting_weapon() const {
return this->starting_weapon;
}
void WeaponInventory::deposit_pistol_ammo(int amount) {
this->pistol_ammo += amount;
}
void WeaponInventory::set_pistol_ammo(int amount) {
this->rifle_ammo = abs(amount);
}
int WeaponInventory::get_pistol_ammo() const {
return this->pistol_ammo;
}
int WeaponInventory::withdraw_pistol_ammo(int max_amount) {
if (max_amount < this->pistol_ammo) {
this->pistol_ammo -= max_amount;
return max_amount;
} else {
int const val{ this->pistol_ammo };
this->pistol_ammo = 0;
return val;
}
}
void WeaponInventory::deposit_rifle_ammo(int amount) {
this->rifle_ammo += amount;
}
void WeaponInventory::set_rifle_ammo(int amount) {
this->rifle_ammo = abs(amount);
}
int WeaponInventory::get_rifle_ammo() const {
return this->rifle_ammo;
}
int WeaponInventory::withdraw_rifle_ammo(int max_amount) {
if (max_amount < this->rifle_ammo) {
this->rifle_ammo -= max_amount;
return max_amount;
} else {
int const val{ this->rifle_ammo };
this->rifle_ammo = 0;
return val;
}
}