131 lines
4.2 KiB
C#
131 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
public class PlayerInteraction : MonoBehaviour
|
|
{
|
|
public float interactRange = 3f;
|
|
private int blueberryCount = 0;
|
|
private List<InventoryItem> inventory = new List<InventoryItem>();
|
|
public Dialog[] dialog;
|
|
int dialogIndex = 0;
|
|
|
|
[SerializeField] private InputActionReference InteractAction;
|
|
[SerializeField] private InputActionReference DialogAction;
|
|
[SerializeField] private GameObject dialogManagerObject;
|
|
|
|
private DialogManager dialogManager;
|
|
void Start()
|
|
{
|
|
InteractAction.action.Enable();
|
|
InteractAction.action.performed += OnInteract;
|
|
|
|
DialogAction.action.Enable();
|
|
DialogAction.action.performed += onDialogContinue;
|
|
|
|
dialogManager = dialogManagerObject.GetComponent<DialogManager>();
|
|
print(dialog.Length);
|
|
}
|
|
|
|
void OnInteract(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
Ray 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
|
|
dialog = hit.collider.GetComponent<QuestGiver>().Talk();
|
|
// immedeately start the dialog
|
|
dialogManager.ShowDialog(dialog[dialogIndex].speaker + ": " + dialog[dialogIndex].text);
|
|
// skip the 0th dialog entry since it has already been shown
|
|
dialogIndex++;
|
|
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;
|
|
dialogManager.HideDialog();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void onDialogContinue(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed && dialog != null && dialog.Length > 0)
|
|
{
|
|
// scroll the dialog only if there is text to be shown
|
|
|
|
print(dialog[dialogIndex].text);
|
|
dialogManager.ShowDialog("<b>" + dialog[dialogIndex].speaker + "</b>: " + dialog[dialogIndex].text);
|
|
dialogIndex++;
|
|
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;
|
|
dialogManager.HideDialog();
|
|
}
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
public int GetBlueberryCount()
|
|
{
|
|
return blueberryCount;
|
|
}
|
|
|
|
public void CollectBlueberry(int newBlueberries)
|
|
{
|
|
blueberryCount += newBlueberries;
|
|
Debug.Log("Collected " + blueberryCount + " blueberries. Total: " + blueberryCount);
|
|
}
|
|
|
|
public void AddToInventory(InventoryItem reward)
|
|
{
|
|
inventory.Add(reward);
|
|
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.");
|
|
if (inventory.Contains(reward))
|
|
{
|
|
inventory.Remove(reward);
|
|
Debug.Log("Removed " + reward.name + " from inventory. Total items: " + inventory.Count);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Item not found in inventory: " + reward.name);
|
|
}
|
|
}
|
|
|
|
public int GetItemQuantity(string itemName)
|
|
{
|
|
InventoryItem item = inventory.Find(i => i.itemName == itemName);
|
|
return item != null ? item.quantity : 0;
|
|
}
|
|
|
|
public List<InventoryItem> GetInventoryItems()
|
|
{
|
|
return inventory;
|
|
}
|
|
}
|