Files
sasalka/Code/Inventory/Usable/PickupItem.cs
Oscar 23a35fe3cd asd
2025-06-28 18:13:47 +03:00

62 lines
1.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Sandbox.Gravity;
using Sandbox.UI;
using Sasalka;
using System;
using System.Linq;
namespace Sasalka;
[Icon( "skip_next" )]
public sealed class PickupItem : InteractionButton
{
[Property] public override string Label { get; set; } = "E";
protected override void OnStart()
{
base.OnStart();
// Устанавливаем правильную метку для предмета
if ( GameObject.Components.TryGet<InventoryItem>( out var inventoryItem ) )
{
Label = inventoryItem.Definition?.Name != ""
? $"{inventoryItem.Definition.Name} x{inventoryItem.Count}"
: "Подобрать";
}
}
public override bool Press( IPressable.Event e )
{
base.Press( e );
if ( e.Source.Components.TryGet<Dedugan>( out var dedugan ) )
{
var inventoryItem = Components.Get<InventoryItem>();
if ( inventoryItem != null && dedugan.Inventory != null )
{
// Пытаемся добавить предмет в инвентарь, остаток остаётся на земле
int left = dedugan.Inventory.AddItem( inventoryItem );
if ( left <= 0 )
{
RpcDestroy();
return true;
}
else
{
inventoryItem.Count = left;
// Оставляем предмет с новым количеством на земле
return true;
}
}
}
return false;
}
[Rpc.Broadcast]
void RpcDestroy()
{
GameObject.Destroy();
}
}