95 lines
2.8 KiB
C#
95 lines
2.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using PixelCrushers.DialogueSystem;
|
|
using UnityEngine.Events;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class QuestActivator : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
public class ActivationCondition
|
|
{
|
|
public string questName;
|
|
public QuestState state;
|
|
public QuestState[] entriesStates;
|
|
|
|
}
|
|
|
|
public ActivationCondition[] m_conditions;
|
|
public bool m_CheckOnStart = true;
|
|
public bool m_debug = false;
|
|
public UnityEvent m_OnTrueEvent;
|
|
public UnityEvent m_OnFalseEvent;
|
|
|
|
//TODO fix this
|
|
public static bool QuestExists(string name)
|
|
{
|
|
var quest = QuestLog.GetAllQuests();
|
|
foreach (var q in quest)
|
|
{
|
|
if (q.Equals(name))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool CheckCondition(ActivationCondition condition)
|
|
{
|
|
/*
|
|
if (!QuestExists(condition.questName))
|
|
{
|
|
Debug.LogError($"QuestActivator: Quest {condition.questName} doesn't exists!");
|
|
return false;
|
|
}
|
|
*/
|
|
if (condition.state != 0 && !QuestLog.IsQuestInStateMask(condition.questName, condition.state))
|
|
return false;
|
|
int count = QuestLog.GetQuestEntryCount(condition.questName);
|
|
count = count > condition.entriesStates.Length ? condition.entriesStates.Length : count;
|
|
for (int i = 0; i < count ; i++)
|
|
{
|
|
if (condition.entriesStates[i] == 0)
|
|
continue;
|
|
QuestState state = QuestLog.GetQuestEntryState(condition.questName, i + 1);
|
|
if ((state & condition.entriesStates[i]) != state)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void CheckCondition()
|
|
{
|
|
foreach (var c in m_conditions)
|
|
{
|
|
if (!CheckCondition(c))
|
|
{
|
|
if (m_debug)
|
|
Debug.Log($"Condition for quest {c.questName} is false.");
|
|
m_OnFalseEvent?.Invoke();
|
|
return;
|
|
}
|
|
}
|
|
if (m_debug)
|
|
{
|
|
foreach (var c in m_conditions)
|
|
{
|
|
Debug.Log($"All conditions for quest {c.questName} are true.");
|
|
}
|
|
}
|
|
if (m_conditions.Length > 0)
|
|
m_OnTrueEvent?.Invoke();
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
if (m_CheckOnStart)
|
|
{
|
|
CheckCondition();
|
|
}
|
|
}
|
|
}
|
|
}
|