126 lines
3.1 KiB
C#
126 lines
3.1 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;
|
|
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);
|
|
print(item.itemName + item.quantity);
|
|
// 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)
|
|
{
|
|
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>();
|
|
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;
|
|
}
|
|
} |