AI захватит мир

This commit is contained in:
Oscar
2025-06-26 23:24:52 +03:00
parent 793165bb03
commit 875d594038
28 changed files with 1263 additions and 266 deletions

View File

@@ -2,35 +2,83 @@
public abstract class AmmoUseableBase : UseableBase
{
private WeaponItemDefinition _cachedWeaponDef;
private InventoryItem _cachedAmmoItem;
protected InventoryItem AmmoItem => FindAmmoItem();
private InventoryItem FindAmmoItem()
{
//По типу патрон поиск + енум типа патрон
return Dedugan.Local.Inventory.Items.FirstOrDefault( i => i.Definition.Name == "Pistol Ammo" );
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;
return base.CanUse() && ammo != null && ammo.Count > 0;
var baseCanUse = base.CanUse();
var hasAmmo = ammo != null && ammo.Count > 0;
return baseCanUse && hasAmmo;
}
public override void Use()
{
if ( !CanUse() )
if (!CanUse())
return;
OnUse();
var ammo = AmmoItem;
if ( ammo != null )
if (ammo != null)
{
ammo.Count--;
if ( ammo.Count <= 0 )
// Уменьшаем количество патронов
if (ammo.TryRemoveCount(1))
{
Dedugan.Local.Inventory.RemoveItem( ammo );
// Если патроны закончились, удаляем предмет из инвентаря
if (ammo.Count <= 0)
{
Dedugan.Local.Inventory.RemoveItem(ammo);
_cachedAmmoItem = null; // Очищаем кэш
}
}
}
}

View File

@@ -1,24 +1,54 @@
using Sandbox.Gravity;
using Sandbox.UI;
using Sasalka;
using System;
using System.Linq;
namespace Sandbox.UI;
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 ) )
{
dedugan.Inventory.AddItem( Components.Get<InventoryItem>() );
RpcDestroy();
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 true;
return false;
}
[Rpc.Broadcast]

View File

@@ -4,8 +4,13 @@ public static class UseSystem
{
public static bool TryUse( IUseContext context )
{
foreach ( var useable in context.GetUsables() )
// Получаем все доступные предметы
var usables = context.GetUsables();
// Проверяем каждый предмет на возможность использования
foreach ( var useable in usables )
{
// Раннее прерывание если предмет может быть использован
if ( useable.CanUse() )
{
useable.Use();

View File

@@ -37,7 +37,8 @@ public abstract class UseableBase : Component, IUseable
public virtual void OnEquipped()
{
Log.Info( $"OnEquip {this}" );
Equipped = true;
// Log.Info( $"OnEquip {this}" );
}
public virtual bool CanUse()