first commit
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class PlayerInteractor : MonoBehaviour
|
||||
{
|
||||
public Inventory inventory;
|
||||
public CraftingSystem craftingSystem;
|
||||
public float interactRadius = 1.8f;
|
||||
public Text interactionText;
|
||||
|
||||
private ResourcePickup nearestPickup;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
FindNearestPickup();
|
||||
|
||||
if (interactionText != null)
|
||||
{
|
||||
interactionText.text = nearestPickup != null
|
||||
? "E - подобрать: " + nearestPickup.GetPickupText()
|
||||
: "Подойдите к ресурсу и нажмите E";
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.E) && nearestPickup != null)
|
||||
{
|
||||
if (nearestPickup.TryCollect(inventory))
|
||||
{
|
||||
SetMessage("Подобрано: " + nearestPickup.GetPickupText());
|
||||
}
|
||||
else
|
||||
{
|
||||
SetMessage("Инвентарь заполнен.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void FindNearestPickup()
|
||||
{
|
||||
nearestPickup = null;
|
||||
Collider[] hits = Physics.OverlapSphere(transform.position, interactRadius);
|
||||
float bestDistance = float.MaxValue;
|
||||
|
||||
for (int i = 0; i < hits.Length; i++)
|
||||
{
|
||||
ResourcePickup pickup = hits[i].GetComponentInParent<ResourcePickup>();
|
||||
if (pickup == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
float distance = Vector3.Distance(transform.position, pickup.transform.position);
|
||||
if (distance < bestDistance)
|
||||
{
|
||||
bestDistance = distance;
|
||||
nearestPickup = pickup;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SetMessage(string message)
|
||||
{
|
||||
if (craftingSystem != null)
|
||||
{
|
||||
craftingSystem.lastMessage = message;
|
||||
craftingSystem.Refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user