UI/Weapon/WeaponAmmoCheck.razor
@using Sandbox
@using Sandbox.UI
@namespace Opium
@inherits PanelComponent
<root>
@if (CurrentWeapon is RangedWeapon range)
{
@{
var ammo = range.CurrentAmmo;
var suffix = ammo == 1 ? "" : "s";
}
<div class="ammo">
@if (ammo == 0)
{
<label>Empty...</label>
}
else
{
<label>@ammo</label>
<label>shot@(suffix) left...</label>
}
</div>
}
@if (CurrentWeapon is MeleeWeapon melee)
{
<div class="ammo">
@{
var durability = melee.Durability;
}
<label>@GetDurabilityMessage(durability)</label>
</div>
}
</root>
@code
{
/// <summary>
/// The player
/// </summary>
public Opium.PlayerController Player => Components.Get<Opium.PlayerController>(FindMode.EverythingInSelfAndParent);
public BaseWeapon Weapon => Player.Inventory.Current;
public TimeSince TimeSinceButtonPressed = 10f;
[Property] public float DisplayTime { get; set; } = 2f;
[Property] public string InputAction { get; set; } = "Reload";
string GetDurabilityMessage( int durability )
{
if ( durability >= 100 )
{
return "Pristine.";
}
else if ( durability > 66f )
{
return "Got a few more hits out of it.";
}
else if ( durability > 33f )
{
return "It's pretty damaged.";
}
else if ( durability > 10f )
{
return "It's almost broken..";
}
return "It's fucked.";
}
private BaseWeapon CurrentWeapon;
protected override void OnUpdate()
{
if ( Input.Pressed( InputAction ) )
{
CurrentWeapon = Weapon;
TimeSinceButtonPressed = 0;
CurrentWeapon?.EventListener?.Invoke("ammocheck");
}
SetClass( "visible", TimeSinceButtonPressed < DisplayTime );
}
protected override int BuildHash()
{
return HashCode.Combine( Time.Delta );
}
}