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( 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; } /// /// Виртуальный метод для экипировки предмета /// public virtual void OnEquipped() { Equipped = true; } /// /// Виртуальный метод для снятия предмета /// public virtual void OnUnEquipped() { Equipped = false; } public override string ToString() { return $"{Definition?.Name ?? "Unknown"} x{Count}"; } }