added: item pickups with online persistency
This commit is contained in:
49
Assets/Scripts/Player/InteractionHandler.cs
Normal file
49
Assets/Scripts/Player/InteractionHandler.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user