60 lines
877 B
C#
60 lines
877 B
C#
using Sandbox;
|
|
|
|
namespace Sasalka;
|
|
|
|
public abstract class UseableBase : Component, IUseable
|
|
{
|
|
[Property] public float Cooldown { get; set; } = 0.5f;
|
|
|
|
private bool _equipped;
|
|
|
|
[Property]
|
|
public bool Equipped
|
|
{
|
|
get => _equipped;
|
|
set
|
|
{
|
|
if ( _equipped != value )
|
|
{
|
|
_equipped = value;
|
|
if ( _equipped )
|
|
{
|
|
OnEquip?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private TimeSince _timeSinceUsed;
|
|
public Action OnEquip { get; set; }
|
|
|
|
protected override void OnAwake()
|
|
{
|
|
_timeSinceUsed = Cooldown;
|
|
|
|
OnEquip += OnEquipped;
|
|
}
|
|
|
|
public virtual void OnEquipped()
|
|
{
|
|
Equipped = true;
|
|
// Log.Info( $"OnEquip {this}" );
|
|
}
|
|
|
|
public virtual bool CanUse()
|
|
{
|
|
return _timeSinceUsed >= Cooldown && !Inventar.IsInventoryOpen;
|
|
}
|
|
|
|
public virtual void Use()
|
|
{
|
|
if ( !CanUse() )
|
|
return;
|
|
|
|
OnUse();
|
|
_timeSinceUsed = 0;
|
|
}
|
|
|
|
protected abstract void OnUse();
|
|
}
|