93 lines
1.8 KiB
C#
93 lines
1.8 KiB
C#
using Sandbox;
|
|
using Sandbox.UI;
|
|
|
|
namespace Sasalka;
|
|
|
|
public class InventoryItem : Component
|
|
{
|
|
[Property] public BaseItemDefinition Definition { get; set; }
|
|
[Sync, Property] public int Count { get; set; } = 1;
|
|
[Sync] public bool Equipped { get; set; } = false;
|
|
|
|
protected override void OnStart()
|
|
{
|
|
if ( GameObject.Components.TryGet<PickupItem>( out var item ) )
|
|
{
|
|
string itemName = Definition?.Name;
|
|
|
|
if ( string.IsNullOrEmpty( itemName ) )
|
|
{
|
|
item.Label = "Unknown Item";
|
|
}
|
|
else
|
|
{
|
|
item.Label = Count > 1
|
|
? $"{itemName} x{Count}"
|
|
: itemName;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool CanStackWith( InventoryItem other )
|
|
{
|
|
if ( other == null || Definition == null || other.Definition == null )
|
|
return false;
|
|
|
|
return Definition == other.Definition && Definition.MaxCount > 1;
|
|
}
|
|
|
|
public bool CanAddCount( int amount )
|
|
{
|
|
if ( Definition == null )
|
|
return false;
|
|
|
|
return Count + amount <= Definition.MaxCount;
|
|
}
|
|
|
|
public bool TryAddCount( int amount )
|
|
{
|
|
if ( !CanAddCount( amount ) )
|
|
return false;
|
|
|
|
Count += amount;
|
|
return true;
|
|
}
|
|
|
|
public bool TryRemoveCount( int amount )
|
|
{
|
|
if ( Count < amount )
|
|
return false;
|
|
|
|
Count -= amount;
|
|
return true;
|
|
}
|
|
|
|
public InventoryItem Clone()
|
|
{
|
|
var clone = new InventoryItem { Definition = Definition, Count = Count, Equipped = false };
|
|
|
|
return clone;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Виртуальный метод для экипировки предмета
|
|
/// </summary>
|
|
public virtual void OnEquipped()
|
|
{
|
|
Equipped = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Виртуальный метод для снятия предмета
|
|
/// </summary>
|
|
public virtual void OnUnEquipped()
|
|
{
|
|
Equipped = false;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{Definition?.Name ?? "Unknown"} x{Count}";
|
|
}
|
|
}
|