ccl4/blueberryPeak/Assets/Scripts/Quests/QuestGiver.cs
2025-06-19 22:44:11 +02:00

149 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
public class QuestGiver : MonoBehaviour
{
[SerializeField] private TextAsset questFile;
[SerializeField] private InventoryItem reward;
[SerializeField] private ItemReward requiredItem;
[SerializeField] public DeleteTreeReward rewardFunction;
[SerializeField] public int requiredBlueberries = 0;
public bool isActive;
public bool isCompleted;
public QuestData quest;
// Start is called once before the first execution of Update after the MonoBehaviour is created
private void Awake()
{
if (questFile == null)
{
Debug.LogError("Quest file is not assigned in the inspector.");
return;
}
// Load the quest data from the TextAsset
try
{
quest = JsonUtility.FromJson<QuestData>(questFile.text);
}
catch (Exception e)
{
Debug.LogError("Failed to parse quest data: " + e.Message);
}
}
private void Start()
{
}
// Update is called once per frame
private void Update()
{
}
public QuestData GetCurrentQuest()
{
return quest;
}
public void SetCurrentQuest(QuestData questSave)
{
quest = questSave;
}
public void CheckInventory(List<InventoryItem> inventory)
{
InventoryItem item = inventory.Find(e => e.itemName == requiredItem.itemName);
// Check if the required item is in the player's inventory
if (item != null && item.quantity >= requiredItem.quantity)
{
isCompleted = true; // Mark the quest as completed
FinishQuest();
}
else
{
isCompleted = false; // Mark the quest as not completed
}
}
public Dialog[] Talk()
{
Debug.Log("Talking to quest giver: " + quest.id);
if (!isActive && isCompleted)
{
return quest.repetition;
}
if (!isActive)
{
isActive = true;
return quest.initial;
}
if (isActive)
{
if (!isCompleted)
{
if (requiredBlueberries > 0)
{
// Check if the player has enough blueberries
PlayerInteraction player = GameObject.Find("Fox").GetComponent<PlayerInteraction>();
if (player.GetBlueberryCount() >= requiredBlueberries)
{
isCompleted = true; // Mark the quest as completed
FinishQuest();
return quest.success;
}
}
else
{
CheckInventory(GameObject.FindGameObjectsWithTag("Player")[0].GetComponent<PlayerInteraction>().GetInventoryItems());
}
}
// Re-check after CheckInventory potentially updates the quest
if (isCompleted)
{
isActive = false;
return quest.success;
}
else
{
return quest.reminder;
}
}
return null; // fallback
}
public Dialog[] FinishQuest()
{
// If the quest is active and completed, return the success dialog
PlayerInteraction player = GameObject.Find("Fox").GetComponent<PlayerInteraction>();
if (requiredBlueberries > 0)
{
player.SetBlueberryCount(player.GetBlueberryCount() - requiredBlueberries);
Debug.Log("Removed " + requiredBlueberries + " blueberries from player.");
}
else
{
player.RemoveFromInventory(requiredItem);
}
if (reward)
{
reward.giveReward(GameObject.Find("Fox")); // Give the reward to the player
}
if (rewardFunction != null)
rewardFunction.RewardPlayer();
isActive = false;
isCompleted = true;
return quest.success;
}
}