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;
}

View File

@@ -39,6 +39,12 @@ public sealed partial class Dedugan : Component, IUseContext, Component.INetwork
private Vector3 _wishDirection;
// Кэш для оптимизации
private bool _isOwner;
private float _lastBodyYaw;
private float _lastCameraYaw;
private bool _bodyRotationDirty = true;
protected override void OnStart()
{
InventoryStart();
@@ -66,9 +72,12 @@ public sealed partial class Dedugan : Component, IUseContext, Component.INetwork
protected override void OnUpdate()
{
_isOwner = Network.IsOwner;
UpdateCustomAnimations();
InventoryUpdate();
if ( Network.IsOwner )
if ( _isOwner )
{
EyeAngles += Input.AnalogLook;
EyeAngles = EyeAngles.WithPitch( MathX.Clamp( EyeAngles.pitch, -89f, 89f ) );
@@ -122,6 +131,21 @@ public sealed partial class Dedugan : Component, IUseContext, Component.INetwork
/// </summary>
private void UpdateBodyRotation()
{
var currentBodyYaw = Renderer.LocalRotation.Yaw();
var currentCameraYaw = EyeAngles.yaw;
// Проверяем, изменились ли углы
if ( Math.Abs( currentBodyYaw - _lastBodyYaw ) < 0.1f &&
Math.Abs( currentCameraYaw - _lastCameraYaw ) < 0.1f &&
!_bodyRotationDirty )
{
return; // Пропускаем обновление, если углы не изменились
}
_lastBodyYaw = currentBodyYaw;
_lastCameraYaw = currentCameraYaw;
_bodyRotationDirty = false;
var targetRotation = Rotation.LookAt( Rotation.FromYaw( EyeAngles.yaw ).Forward, -_directionToAxis );
var currentForward = Renderer.LocalRotation.Forward;
float angleDiff = currentForward.Angle( targetRotation.Forward );
@@ -162,6 +186,7 @@ public sealed partial class Dedugan : Component, IUseContext, Component.INetwork
{
Renderer.LocalRotation = Rotation.Slerp( Renderer.LocalRotation, Rotation.FromYaw( EyeAngles.yaw ),
Time.Delta * 3f );
_bodyRotationDirty = true; // Помечаем, что тело повернулось
}
}
}

View File

@@ -35,6 +35,10 @@ public class BaseWeapon : InventoryItem, IUseable
private WeaponItemDefinition _weaponDefinition;
private bool _isInitialized;
private InventoryItem _inventoryItem;
// Оптимизация обновлений
private TimeSince _lastEffectsUpdate = 0f;
private const float EFFECTS_UPDATE_INTERVAL = 0.1f; // Обновляем эффекты каждые 100мс
// IUseable реализация - Cooldown вычисляется на основе FireRate
public float Cooldown
@@ -95,8 +99,12 @@ public class BaseWeapon : InventoryItem, IUseable
FinishReload();
}
// Обновляем эффекты
UpdateEffects();
// Обновляем эффекты с интервалом для оптимизации
if ( _lastEffectsUpdate >= EFFECTS_UPDATE_INTERVAL )
{
UpdateEffects();
_lastEffectsUpdate = 0f;
}
}
/// <summary>
@@ -132,11 +140,16 @@ public class BaseWeapon : InventoryItem, IUseable
{
base.OnEquipped();
// Получаем Rigidbody в момент экипировки
_rigidbody = GameObject.Components.Get<Rigidbody>();
// Кэшируем Rigidbody при экипировке
if ( _rigidbody == null )
_rigidbody = GameObject.Components.Get<Rigidbody>();
if ( _rigidbody != null )
_rigidbody.Enabled = false;
if ( _pickupItem == null )
_pickupItem = GameObject.Components.Get<PickupItem>();
if ( _pickupItem != null )
_pickupItem.Enabled = false;
}
@@ -148,8 +161,7 @@ public class BaseWeapon : InventoryItem, IUseable
{
base.OnUnEquipped();
// Получаем Rigidbody в момент снятия
_rigidbody = GameObject.Components.Get<Rigidbody>();
// Используем кэшированный Rigidbody
if ( _rigidbody != null )
_rigidbody.Enabled = true;