This commit is contained in:
Oscar
2025-06-28 18:13:47 +03:00
parent 875d594038
commit 23a35fe3cd
23 changed files with 2579 additions and 607 deletions

View File

@@ -1,85 +1,14 @@
namespace Sasalka;
/// <summary>
/// Базовый класс для предметов, которые используют патроны
/// Теперь используется только для совместимости со старой системой
/// </summary>
public abstract class AmmoUseableBase : UseableBase
{
private WeaponItemDefinition _cachedWeaponDef;
private InventoryItem _cachedAmmoItem;
protected InventoryItem AmmoItem => FindAmmoItem();
private InventoryItem FindAmmoItem()
protected override void OnUse()
{
var player = Dedugan.Local;
if (player?.Inventory == null)
{
return null;
}
// Кэшируем WeaponDefinition для избежания повторных вызовов
if (_cachedWeaponDef == null)
{
_cachedWeaponDef = GetWeaponDefinition();
if (_cachedWeaponDef == null)
{
return null;
}
}
// Проверяем кэшированный результат
if (_cachedAmmoItem != null && _cachedAmmoItem.Count > 0)
{
return _cachedAmmoItem;
}
// Ищем патроны
foreach (var item in player.Inventory.Items)
{
if (item.Definition is AmmoItemDefinition ammoDef && ammoDef.IsCompatibleWith(_cachedWeaponDef.AmmoType))
{
_cachedAmmoItem = item;
return item;
}
}
_cachedAmmoItem = null;
return null;
}
protected virtual WeaponItemDefinition GetWeaponDefinition()
{
// Переопределите в наследниках для возврата определения оружия
return null;
}
public override bool CanUse()
{
var ammo = AmmoItem;
var baseCanUse = base.CanUse();
var hasAmmo = ammo != null && ammo.Count > 0;
return baseCanUse && hasAmmo;
}
public override void Use()
{
if (!CanUse())
return;
OnUse();
var ammo = AmmoItem;
if (ammo != null)
{
// Уменьшаем количество патронов
if (ammo.TryRemoveCount(1))
{
// Если патроны закончились, удаляем предмет из инвентаря
if (ammo.Count <= 0)
{
Dedugan.Local.Inventory.RemoveItem(ammo);
_cachedAmmoItem = null; // Очищаем кэш
}
}
}
// Базовая реализация - ничего не делает
// Логика использования патронов теперь в BaseWeapon
}
}

View File

@@ -18,7 +18,9 @@ public sealed class PickupItem : InteractionButton
// Устанавливаем правильную метку для предмета
if ( GameObject.Components.TryGet<InventoryItem>( out var inventoryItem ) )
{
Label = inventoryItem.Definition?.Name ?? "Подобрать";
Label = inventoryItem.Definition?.Name != ""
? $"{inventoryItem.Definition.Name} x{inventoryItem.Count}"
: "Подобрать";
}
}