108 lines
3.0 KiB
C#
108 lines
3.0 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class QuestGiver : MonoBehaviour
|
|
{
|
|
|
|
[SerializeField] private TextAsset questFile;
|
|
[SerializeField] private InventoryItem reward;
|
|
[SerializeField] private InventoryItem requiredItem;
|
|
public bool isActive = false;
|
|
public bool isCompleted = false;
|
|
|
|
public QuestData quest;
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
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 (System.Exception e)
|
|
{
|
|
Debug.LogError("Failed to parse quest data: " + e.Message);
|
|
return;
|
|
}
|
|
}
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
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;
|
|
}
|
|
|
|
else
|
|
{
|
|
if (!isActive)
|
|
{
|
|
// Initialize the quest
|
|
isActive = true;
|
|
return quest.initial;
|
|
}
|
|
|
|
else 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;
|
|
}
|
|
|
|
else
|
|
{
|
|
// 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
|
|
return quest.success;
|
|
}
|
|
}
|