This commit is contained in:
Oscar
2025-06-28 22:26:47 +03:00
parent f7e8f8bf1f
commit 7223dd3b61
50 changed files with 479 additions and 82 deletions

View File

@@ -19,49 +19,40 @@ public enum ItemRarity
Legendary
}
[GameResource("Base Item Definition", "inv", "", Category = "Sasalka", Icon = "inventory_2")]
[GameResource( "Base Item Definition", "inv", "", Category = "Sasalka", Icon = "inventory_2" )]
public class BaseItemDefinition : GameResource
{
[Property, Title("Basic Info")]
public string Name { get; set; } = "Unknown Item";
[Property, Title( "Basic Info" )] public string Name { get; set; } = "Unknown Item";
[Property]
public string Description { get; set; } = "";
[Property] public string Description { get; set; } = "";
[Property, Category("Visual")]
[ResourceType("prefab")]
public GameObject Prefab { get; set; } = GameObject.GetPrefab("prefabs/item_parcel.prefab");
[Property, Category( "Visual" )]
[ResourceType( "prefab" )]
public GameObject Prefab { get; set; } = GameObject.GetPrefab( "prefabs/item_parcel.prefab" );
[Property, Category("Visual")]
public Texture ImageTexture { get; set; }
[Property, Category( "Visual" )] public Texture ImageTexture { get; set; }
[Property, Category("Visual")]
public string ImageUrl { get; set; }
[Property, Category( "Visual" )] public string ImageUrl { get; set; }
[Property, Category("Properties")]
[Range(1, 1000)]
[Property, Category( "Properties" )]
[Range( 1, 1000 )]
public int MaxCount { get; set; } = 1;
[Property, Category("Properties")]
public virtual ItemCategory Category { get; set; } = ItemCategory.Misc;
[Property, Category( "Properties" )] public virtual ItemCategory Category { get; set; } = ItemCategory.Misc;
[Property, Category("Properties")]
public ItemRarity Rarity { get; set; } = ItemRarity.Common;
[Property, Category( "Properties" )] public ItemRarity Rarity { get; set; } = ItemRarity.Common;
[Property, Category("Properties")]
public float Weight { get; set; } = 1.0f;
[Property, Category( "Properties" )] public float Weight { get; set; } = 1.0f;
[Property, Category("Properties")]
public bool IsStackable => MaxCount > 1;
[Property, Category( "Properties" )] public bool IsStackable = false;
[Property, Category("Properties")]
public bool IsEquipable => this is IEquipable;
[Property, Category( "Properties" )] public bool IsEquipable => this is IEquipable;
public virtual Inventar.InventorySlot? GetSlot() => null;
public virtual bool CanUse() => false;
public virtual void OnUse(InventoryItem item) { }
public virtual void OnUse( InventoryItem item ) { }
public string GetRarityColor()
{

View File

@@ -15,8 +15,8 @@ public class ClothingItemDefinition : BaseItemDefinition, IEquipable
[Property, Category( "Clothing Properties" )]
public bool IsVisible { get; set; } = true;
[Property, Category( "Clothing Properties" )]
public string BodyPart { get; set; } = "";
// [Property, Category( "Clothing Properties" )]
// public string BodyPart { get; set; } = "";
public override Inventar.InventorySlot? GetSlot() => Slot;

View File

@@ -38,15 +38,23 @@ public class Inventar : Component
public event Action<InventoryItem> OnItemAdded;
public event Action<InventoryItem> OnItemRemoved;
// Кэш для оптимизации поиска предметов
private Dictionary<BaseItemDefinition, InventoryItem> _itemCache = new();
private bool _cacheDirty = true;
public bool CanAddItem( InventoryItem item )
{
if ( item == null || item.Definition == null )
return false;
// Проверяем, есть ли уже такой предмет в инвентаре
var existingItem = Items.FirstOrDefault( x => x.Definition == item.Definition );
// Обновляем кэш при необходимости
if ( _cacheDirty )
{
UpdateItemCache();
}
if ( existingItem != null )
// Проверяем, есть ли уже такой предмет в инвентаре
if ( _itemCache.TryGetValue( item.Definition, out var existingItem ) )
{
// Если предмет уже есть, проверяем, можно ли добавить к нему количество
return existingItem.Count + item.Count <= item.Definition.MaxCount;
@@ -62,6 +70,22 @@ public class Inventar : Component
return item.Count <= item.Definition.MaxCount;
}
/// <summary>
/// Обновляет кэш предметов
/// </summary>
private void UpdateItemCache()
{
_itemCache.Clear();
foreach ( var item in Items )
{
if ( item.Definition != null )
{
_itemCache[item.Definition] = item;
}
}
_cacheDirty = false;
}
/// <summary>
/// Добавляет предмет в инвентарь, распределяя по существующим и новым стекам. Возвращает остаток, который не удалось добавить (или 0, если всё добавлено).
/// </summary>
@@ -80,6 +104,7 @@ public class Inventar : Component
{
stack.Count += canAdd;
toAdd -= canAdd;
_cacheDirty = true; // Помечаем кэш как устаревший
OnChanged?.Invoke();
OnItemAdded?.Invoke( stack );
}
@@ -94,6 +119,7 @@ public class Inventar : Component
var newStack = new InventoryItem { Definition = item.Definition, Count = stackCount, MagazineAmmo = item.MagazineAmmo };
Items.Add( newStack );
toAdd -= stackCount;
_cacheDirty = true; // Помечаем кэш как устаревший
OnChanged?.Invoke();
OnItemAdded?.Invoke( newStack );
}
@@ -112,6 +138,7 @@ public class Inventar : Component
// Удаляем весь предмет
UnEquipItem( item );
Items.Remove( item );
_cacheDirty = true; // Помечаем кэш как устаревший
OnChanged?.Invoke();
OnItemRemoved?.Invoke( item );
return true;
@@ -120,6 +147,7 @@ public class Inventar : Component
{
// Уменьшаем количество
item.Count -= count;
_cacheDirty = true; // Помечаем кэш как устаревший
OnChanged?.Invoke();
return true;
}