fix чать инвенторя
This commit is contained in:
78
Assets/Inventory/Scripts/WorldItem.cs
Normal file
78
Assets/Inventory/Scripts/WorldItem.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Предмет в мире — подбирается только по нажатию кнопки E (через charecter.cs).
|
||||
/// Авто-подбор при касании намеренно убран.
|
||||
/// </summary>
|
||||
public class WorldItem : MonoBehaviour
|
||||
{
|
||||
public ItemData itemData;
|
||||
public int amount = 1;
|
||||
|
||||
[Header("Pickup")]
|
||||
public float pickupDelay = 0f;
|
||||
public float interactRange = 2.5f;
|
||||
|
||||
private bool _canPickup;
|
||||
|
||||
public void Setup(ItemData data, int count)
|
||||
{
|
||||
itemData = data;
|
||||
amount = count;
|
||||
|
||||
if (data.modelPrefab != null)
|
||||
{
|
||||
var model = Instantiate(data.modelPrefab, transform);
|
||||
model.transform.localPosition = Vector3.zero;
|
||||
}
|
||||
|
||||
if (pickupDelay > 0)
|
||||
Invoke(nameof(EnablePickup), pickupDelay);
|
||||
else
|
||||
_canPickup = true;
|
||||
}
|
||||
|
||||
private void EnablePickup() => _canPickup = true;
|
||||
|
||||
/// <summary>
|
||||
/// Вызывается из charecter.cs при нажатии E рядом с предметом.
|
||||
/// </summary>
|
||||
public bool TryPickupByButton()
|
||||
{
|
||||
if (!_canPickup)
|
||||
{
|
||||
Debug.LogWarning("[WorldItem] Cannot pickup — delay not elapsed yet");
|
||||
return false;
|
||||
}
|
||||
if (itemData == null)
|
||||
{
|
||||
Debug.LogWarning("[WorldItem] itemData is null!");
|
||||
return false;
|
||||
}
|
||||
if (InventoryManager.Instance == null)
|
||||
{
|
||||
Debug.LogWarning("[WorldItem] InventoryManager.Instance is null!");
|
||||
return false;
|
||||
}
|
||||
|
||||
int remaining = InventoryManager.Instance.AddItem(itemData, amount);
|
||||
if (remaining <= 0)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return true;
|
||||
}
|
||||
|
||||
amount = remaining;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Выбросить предмет с физической силой.
|
||||
/// </summary>
|
||||
public void Throw(Vector3 velocity)
|
||||
{
|
||||
var rb = GetComponent<Rigidbody>();
|
||||
if (rb == null) rb = gameObject.AddComponent<Rigidbody>();
|
||||
rb.linearVelocity = velocity;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user