sasalka/Code/Inventory/Usable/PickupItem.cs
2025-06-26 23:24:52 +03:00

60 lines
1.3 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 ?? "Подобрать";
}
}
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();
}
}