Files
LowPolyFPS/Assets/Scripts/AmmoPickup.cs

59 lines
1.8 KiB
C#
Raw Normal View History

2026-10-12 21:28:47 +01:00
using UnityEngine;
public class AmmoPickup : PickupItem
{
[Header("Ammo Settings")]
[SerializeField] private int ammoAmount = 30;
[SerializeField] private string targetWeaponName = ""; // Leave blank for currently equipped weapon
protected override bool CanCollect(GameObject player)
{
var weaponsController = player.GetComponent<WeaponsController>();
if (weaponsController == null)
{
weaponsController = player.GetComponentInChildren<WeaponsController>();
}
if (weaponsController == null) return false;
Weapon activeWeapon = weaponsController.ActiveWeapon;
if (activeWeapon == null) return false;
// If target is specified, check if that weapon can receive ammo
if (!string.IsNullOrEmpty(targetWeaponName))
{
return true;
}
// Active weapon check: if infinite reserve and current mag full, skip pickup if desired
if (activeWeapon.InfiniteReserve)
{
return activeWeapon.currentAmmo < activeWeapon.maxAmmo;
}
return activeWeapon.CurrentReserveAmmo < activeWeapon.MaxReserveAmmo || activeWeapon.currentAmmo < activeWeapon.maxAmmo;
}
protected override void ApplyPickupEffect(GameObject player)
{
var weaponsController = player.GetComponent<WeaponsController>();
if (weaponsController == null)
{
weaponsController = player.GetComponentInChildren<WeaponsController>();
}
if (weaponsController != null)
{
if (string.IsNullOrEmpty(targetWeaponName))
{
weaponsController.AddAmmoToActiveWeapon(ammoAmount);
}
else
{
weaponsController.AddAmmoToMatchingWeapon(targetWeaponName, ammoAmount);
}
}
}
}