upd
This commit is contained in:
39
Code/Inventory/Usable/AmmoUseableBase.cs
Normal file
39
Code/Inventory/Usable/AmmoUseableBase.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
namespace Sasalka;
|
||||
|
||||
public abstract class AmmoUseableBase : UseableBase
|
||||
{
|
||||
protected InventoryItem AmmoItem => FindAmmoItem();
|
||||
|
||||
private InventoryItem FindAmmoItem()
|
||||
{
|
||||
// var ammoDefinition = new InventoryItemDefinition();
|
||||
|
||||
return Dedugan.Local.Inventory.Items.FirstOrDefault( i => i.Definition.Name == "Pistol Ammo" );
|
||||
}
|
||||
|
||||
public override bool CanUse()
|
||||
{
|
||||
var ammo = AmmoItem;
|
||||
return base.CanUse() && ammo != null && ammo.Count > 0;
|
||||
}
|
||||
|
||||
public override void Use()
|
||||
{
|
||||
if ( !CanUse() )
|
||||
return;
|
||||
|
||||
OnUse();
|
||||
|
||||
var ammo = AmmoItem;
|
||||
if ( ammo != null )
|
||||
{
|
||||
ammo.Count--;
|
||||
Log.Info( $"[AmmoUseableBase] Ammo left: {ammo.Count}" );
|
||||
|
||||
if ( ammo.Count <= 0 )
|
||||
{
|
||||
Dedugan.Local.Inventory.RemoveItem( ammo );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
Code/Inventory/Usable/IUseContext.cs
Normal file
6
Code/Inventory/Usable/IUseContext.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Sasalka;
|
||||
|
||||
public interface IUseContext
|
||||
{
|
||||
public IEnumerable<IUseable> GetUsables();
|
||||
}
|
||||
8
Code/Inventory/Usable/IUseable.cs
Normal file
8
Code/Inventory/Usable/IUseable.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Sasalka;
|
||||
|
||||
public interface IUseable
|
||||
{
|
||||
public void Use();
|
||||
bool CanUse();
|
||||
float Cooldown { get; }
|
||||
}
|
||||
15
Code/Inventory/Usable/UseSystem.cs
Normal file
15
Code/Inventory/Usable/UseSystem.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace Sasalka;
|
||||
|
||||
public static class UseSystem
|
||||
{
|
||||
public static void TryUse( IUseContext context )
|
||||
{
|
||||
foreach ( var useable in context.GetUsables() )
|
||||
{
|
||||
if ( useable.CanUse() )
|
||||
{
|
||||
useable.Use();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
31
Code/Inventory/Usable/UseableBase.cs
Normal file
31
Code/Inventory/Usable/UseableBase.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Sandbox;
|
||||
|
||||
namespace Sasalka;
|
||||
|
||||
public abstract class UseableBase : Component, IUseable
|
||||
{
|
||||
[Property] public float Cooldown { get; set; } = 0.5f;
|
||||
|
||||
private TimeSince _timeSinceUsed;
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
_timeSinceUsed = Cooldown;
|
||||
}
|
||||
|
||||
public virtual bool CanUse()
|
||||
{
|
||||
return _timeSinceUsed >= Cooldown && !Inventar.IsInventoryOpen;
|
||||
}
|
||||
|
||||
public virtual void Use()
|
||||
{
|
||||
if ( !CanUse() )
|
||||
return;
|
||||
|
||||
OnUse();
|
||||
_timeSinceUsed = 0;
|
||||
}
|
||||
|
||||
protected abstract void OnUse();
|
||||
}
|
||||
Reference in New Issue
Block a user