Pickups/WeaponPickup.cs
namespace Opium;
public partial class WeaponPickup : BasePickup
{
[Property] public GameObject Prefab { get; set; }
[Property, ReadOnly] public int SavedAmmo { get; set; } = 100;
[Property] public int Durability { get; set; } = 100;
[Property] public string ItemName { get; set; } = "weapon";
[Property] public bool IsRanged { get; set; } = false;
public override bool ShowInteractionUI
{
get
{
// Dunno what happened, but the box doesn't show for ranged weapons.
if ( IsRanged )
{
return true;
}
else if ( Durability <= 0 && !IsRanged )
{
return false;
}
return base.ShowInteractionUI;
}
}
public override bool CanUse( GameObject player )
{
var inventory = player.Components.Get<PlayerInventory>( FindMode.EnabledInSelfAndDescendants );
if ( inventory is null ) return false;
if ( Durability != -1 && Durability <= 0 ) return false;
return inventory.CanInsertWeapon();
}
public override void OnPickup( GameObject player )
{
var inventory = player.Components.Get<PlayerInventory>( FindMode.EnabledInSelfAndDescendants );
if ( inventory is null ) return;
var weapon = inventory.AddWeaponPrefab( Prefab, false );
if ( weapon is RangedWeapon rangedWeapon )
{
rangedWeapon.CurrentAmmo = SavedAmmo.Clamp( 0, rangedWeapon.MaximumAmmo );
}
if ( weapon is MeleeWeapon meleeWeapon )
{
meleeWeapon.Durability = Durability;
}
OnPickupAction?.Invoke();
// Cya
GameObject.Destroy();
}
}