ccl4/blueberryPeak/Assets/Scripts/Quests/QuestGiver.cs

107 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
public class QuestGiver : MonoBehaviour
{
[SerializeField] private TextAsset questFile;
[SerializeField] private InventoryItem reward;
[SerializeField] private InventoryItem requiredItem;
[SerializeField] private DeleteTreeReward rewardFunction;
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 CheckInventory(List<InventoryItem> inventory)
{
// Check if the required item is in the player's inventory
if (inventory.Contains(requiredItem))
{
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)
// If the quest is completed, return the repetition dialog
return quest.repetition;
if (!isActive)
{
// Initialize the quest
isActive = true;
return quest.initial;
}
if (isActive && isCompleted)
{
isActive = false; // reset the quest
isCompleted = true;
// take the Requireditem from the player inventory
// If the quest is active and completed, return the success dialog
return quest.success;
}
// If the quest is active but not completed, return the reminder dialog
CheckInventory(GameObject.FindGameObjectsWithTag("Player")[0].GetComponent<PlayerInteraction>()
.GetInventoryItems());
return quest.reminder;
}
public Dialog[] FinishQuest()
{
// If the quest is active and completed, return the success dialog
isActive = false;
isCompleted = true;
GameObject.Find("Player").GetComponent<PlayerInteraction>().RemoveFromInventory(requiredItem);
reward.giveReward(GameObject.Find("Player")); // Give the reward to the player
rewardFunction.RewardPlayer();
if (rewardFunction != null) rewardFunction.RewardPlayer();
return quest.success;
}
}