132 lines
4.0 KiB
C#
132 lines
4.0 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class ItemUseSystem : MonoBehaviour
|
|
{
|
|
public Inventory inventory;
|
|
public CraftingSystem craftingSystem;
|
|
public PlayerMovement playerMovement;
|
|
public Transform playerTransform;
|
|
public Material torchLightMaterial;
|
|
public Text statusText;
|
|
public int selectedSlotIndex;
|
|
public int selectableSlotCount = 10;
|
|
|
|
public event Action OnSelectionChanged;
|
|
|
|
public void SelectSlot(int slotIndex)
|
|
{
|
|
selectedSlotIndex = Mathf.Clamp(slotIndex, 0, Mathf.Max(0, selectableSlotCount - 1));
|
|
OnSelectionChanged?.Invoke();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
for (int i = 0; i < selectableSlotCount && i < 10; i++)
|
|
{
|
|
KeyCode keyCode = i == 9 ? KeyCode.Alpha0 : (KeyCode)((int)KeyCode.Alpha1 + i);
|
|
if (Input.GetKeyDown(keyCode))
|
|
{
|
|
SelectSlot(i);
|
|
}
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.F))
|
|
{
|
|
UseSelectedItem();
|
|
}
|
|
}
|
|
|
|
public void UseSelectedItem()
|
|
{
|
|
if (inventory == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
InventorySlot slot = inventory.GetSlot(selectedSlotIndex);
|
|
if (slot == null || slot.item == null)
|
|
{
|
|
SetMessage("В выбранном слоте нет предмета.");
|
|
return;
|
|
}
|
|
|
|
string itemId = slot.item.itemId;
|
|
if (itemId == "bandage")
|
|
{
|
|
inventory.RemoveFromSlot(selectedSlotIndex, 1);
|
|
SetMessage("Использован бинт: здоровье восстановлено.");
|
|
}
|
|
else if (itemId == "torch")
|
|
{
|
|
inventory.RemoveFromSlot(selectedSlotIndex, 1);
|
|
PlaceTorch();
|
|
SetMessage("Факел установлен рядом с игроком.");
|
|
}
|
|
else if (itemId == "magic_amulet")
|
|
{
|
|
if (playerMovement != null)
|
|
{
|
|
playerMovement.ApplySpeedBuff(1.6f, 12f);
|
|
}
|
|
|
|
SetMessage("Амулет активирован: скорость увеличена на 12 секунд.");
|
|
}
|
|
else if (itemId == "axe")
|
|
{
|
|
SetMessage("Топор готов к использованию. Им можно рубить древесину в расширенной версии.");
|
|
}
|
|
else if (itemId == "pickaxe")
|
|
{
|
|
SetMessage("Кирка готова к использованию. Им можно добывать камень и руду в расширенной версии.");
|
|
}
|
|
else
|
|
{
|
|
SetMessage("Этот предмет нельзя использовать напрямую.");
|
|
}
|
|
|
|
OnSelectionChanged?.Invoke();
|
|
}
|
|
|
|
private void PlaceTorch()
|
|
{
|
|
if (playerTransform == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameObject torch = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
|
|
torch.name = "Placed Torch";
|
|
torch.transform.position = playerTransform.position + playerTransform.forward * 0.9f + Vector3.up * 0.45f;
|
|
torch.transform.localScale = new Vector3(0.12f, 0.45f, 0.12f);
|
|
if (torchLightMaterial != null)
|
|
{
|
|
torch.GetComponent<Renderer>().material = torchLightMaterial;
|
|
}
|
|
|
|
GameObject lightObject = new GameObject("Torch Light");
|
|
lightObject.transform.SetParent(torch.transform, false);
|
|
lightObject.transform.localPosition = new Vector3(0f, 0.75f, 0f);
|
|
Light light = lightObject.AddComponent<Light>();
|
|
light.type = LightType.Point;
|
|
light.range = 5f;
|
|
light.intensity = 2.2f;
|
|
light.color = new Color(1f, 0.65f, 0.28f);
|
|
}
|
|
|
|
private void SetMessage(string message)
|
|
{
|
|
if (craftingSystem != null)
|
|
{
|
|
craftingSystem.lastMessage = message;
|
|
craftingSystem.Refresh();
|
|
}
|
|
|
|
if (statusText != null)
|
|
{
|
|
statusText.text = message;
|
|
}
|
|
}
|
|
}
|