Files
project-reset/Assets/Scripts/Units/InteractionHandler.cs

49 lines
1.6 KiB
C#

using Reset.Items;
using UnityEngine;
using UnityEngine.InputSystem;
namespace Reset.Units{
public class InteractionHandler : MonoBehaviour{
private PlayerEnvironmentManager envManager;
private Inventory inventory;
private EnvironmentObserver observer;
void Awake(){
envManager = GetComponent<PlayerEnvironmentManager>();
inventory = GetComponent<Inventory>();
observer = envManager.FindObserverFromString("itemdrop");
observer.active = true;
}
public void InteractWith(GameObject target){
IInteractable interactable = target.GetComponent<IInteractable>();
if (interactable == null) {
Debug.LogError("This item cannot be interacted with, it has no Interactable interface");
return;
}
if (target.GetComponent<ItemDrop>()) {
inventory.AddToInventory(target.GetComponent<ItemDrop>().item);
}
interactable.Interact();
// Don't do any actions that use the same button (hard set to Jump for now)
Debug.Log($"Just collected the item, consuming the input @ {Time.time}.");
GetComponent<PlayerControls>().SendInputBlock("Jump");
}
void OnInteract(){
CheckForInteraction();
}
private void CheckForInteraction(){
if (observer.active && observer.Evaluate(gameObject)) {
InteractWith(observer.hit.transform.gameObject);
}
}
}
}