ccl4/blueberryPeak/Assets/Scripts/Player/PlayerInteraction.cs
2025-06-19 01:07:40 +02:00

201 lines
6.3 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerInteraction : MonoBehaviour
{
public float interactRange = 3f;
public Dialog[] dialog;
[SerializeField] private InputActionReference InteractAction;
[SerializeField] private InputActionReference DialogAction;
[SerializeField] private GameObject dialogManagerObject;
private List<GameObject> interactableObjects = new();
private int blueberryCount;
private int dialogIndex;
private DialogManager dialogManager;
[SerializeField] private List<InventoryItem> inventory = new();
private GameObject speaker;
private bool talking;
private void Start()
{
InteractAction.action.Enable();
InteractAction.action.performed += OnInteract;
DialogAction.action.Enable();
DialogAction.action.performed += onDialogContinue;
dialogManager = dialogManagerObject.GetComponent<DialogManager>();
}
private void OnTriggerEnter(Collider other)
{
print("outside");
if (other.gameObject.tag == "Interactable")
{
print("inside");
interactableObjects.Add(other.gameObject);
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Interactable") interactableObjects.Remove(other.gameObject);
}
private void OnInteract(InputAction.CallbackContext context)
{
if (context.performed && !talking)
{
speaker = interactableObjects.Count > 0 ? ColliderInteractable() : PhysicsRaycastInteractable();
dialog = speaker.GetComponent<QuestGiver>().Talk();
if (dialog == null || dialog.Length == 0) return;
// immedeately start the dialog
dialogManager.ShowDialog("<b>" + dialog[dialogIndex].speaker + ": ", dialog[dialogIndex].text,
gameObject.GetComponentInChildren<Animator>(), speaker.GetComponentInChildren<Animator>());
talking = true;
// skip the 0th dialog entry since it has already been shown
if (dialogIndex >= dialog.Length)
{
// reset the dialog index if we reach the end of the dialog
dialogIndex = 0;
// reset the dialog to be null so it cannot be scrolled through when not in an interaction
dialog = null;
talking = false;
speaker = null;
dialogManager.HideDialog();
}
dialogIndex++;
}
}
private GameObject PhysicsRaycastInteractable()
{
var ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, interactRange))
if (hit.collider.CompareTag("Interactable"))
//Debug.Log("Interacted with: " + hit.collider.name);
// You can add more actions here
return hit.collider.gameObject;
return null;
}
private GameObject ColliderInteractable()
{
var closestDistance = float.MaxValue;
var pos = -1;
for (var i = 0; i < interactableObjects.Count; i++)
if ((interactableObjects[i].transform.position - transform.position).magnitude < closestDistance)
{
closestDistance = (interactableObjects[i].transform.position - transform.position).magnitude;
pos = i;
}
return interactableObjects[pos];
}
private void onDialogContinue(InputAction.CallbackContext context)
{
if (context.performed && dialog != null && dialog.Length > 0)
{
print("Dialog continue");
// scroll the dialog only if there is text to be shown
if (dialogIndex >= dialog.Length)
{
// reset the dialog index if we reach the end of the dialog
dialogIndex = 0;
// reset the dialog to be null so it cannot be scrolled through when not in an interaction
dialog = null;
print("Dialog ended");
print(" " + talking);
talking = false;
print(" " + talking);
speaker = null;
dialogManager.HideDialog();
}
else
{
dialogManager.ShowDialog("<b>" + dialog[dialogIndex].speaker + "</b>: ",
dialog[dialogIndex].text, gameObject?.GetComponentInChildren<Animator>(),
speaker?.GetComponentInChildren<Animator>());
dialogIndex++;
}
}
}
public Dialog[] GetDialog()
{
return dialog;
}
public void SetDialog(Dialog[] savedDialog)
{
dialog = savedDialog;
}
public int GetBlueberryCount()
{
return blueberryCount;
}
public void SetBlueberryCount(int count)
{
blueberryCount = count;
}
public void CollectBlueberry(int newBlueberries)
{
blueberryCount += newBlueberries;
Debug.Log("Collected " + blueberryCount + " blueberries. Total: " + blueberryCount);
}
public void AddToInventory(InventoryItem reward)
{
InventoryItem item = inventory.Find(e => e.itemName == reward.itemName);
if (item == null)
inventory.Add(Instantiate(reward));
else
item.quantity += reward.quantity;
Debug.Log("Added " + reward.name + " to inventory. Total items: " + inventory.Count);
}
public void RemoveFromInventory(InventoryItem reward)
{
Debug.Log("Attempting to remove " + reward.name + " from inventory.");
InventoryItem item = inventory.Find(e => e.itemName == reward.itemName);
if (item != null)
{
inventory.Remove(item);
Debug.Log("Removed " + item.itemName + " from inventory. Total items: " + inventory.Count);
}
else
{
Debug.LogWarning("Item not found in inventory: " + reward.name);
}
}
public int GetItemQuantity(string itemName)
{
var item = inventory.Find(i => i.itemName == itemName);
return item != null ? item.quantity : 0;
}
public List<InventoryItem> GetInventoryItems()
{
return inventory;
}
public void SetInventoryItems(List<InventoryItem> items)
{
inventory = items;
}
}