93 lines
3.4 KiB
C#
93 lines
3.4 KiB
C#
// ConditionalEventTrigger.cs
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using PixelCrushers.DialogueSystem;
|
|
using PixelCrushers;
|
|
|
|
namespace Beyond
|
|
{
|
|
/// <summary>
|
|
/// A generic, saveable trigger that checks a Lua condition on start or load,
|
|
/// and fires a UnityEvent based on the result.
|
|
/// </summary>
|
|
[AddComponentMenu("Pixel Crushers/Dialogue System/Trigger/Conditional Event Trigger")]
|
|
public class ConditionalEventTrigger : Saver
|
|
{
|
|
[Header("Condition")]
|
|
[Tooltip("The Lua condition to check.")]
|
|
public Condition condition = new Condition();
|
|
|
|
[Tooltip("If ticked, the events will only be fired the first time the condition is met. This state is saved and loaded.")]
|
|
public bool fireOnce = true;
|
|
|
|
[Header("Events")]
|
|
[Tooltip("Actions to perform if the condition is true.")]
|
|
public UnityEvent onConditionTrue;
|
|
|
|
[Tooltip("Actions to perform if the condition is false.")]
|
|
public UnityEvent onConditionFalse;
|
|
|
|
// This field is used by the custom editor for the Lua wizard.
|
|
[HideInInspector]
|
|
public DialogueDatabase selectedDatabase = null;
|
|
[Tooltip("How often to check the condition (in seconds). If zero, only checks on start/load.")]
|
|
public float CheckInterval = 1.0f;
|
|
|
|
private bool hasFired = false;
|
|
|
|
/// <summary>
|
|
/// On game start, check the condition. Handles new games or scene loads.
|
|
/// </summary>
|
|
public override void Start()
|
|
{
|
|
base.Start();
|
|
CheckConditionAndExecute();
|
|
if (CheckInterval > 0)
|
|
InvokeRepeating("CheckConditionAndExecute", CheckInterval + (Random.value * CheckInterval), CheckInterval);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called by the Save System when saving a game. Records if this trigger has already fired.
|
|
/// </summary>
|
|
public override string RecordData()
|
|
{
|
|
return SaveSystem.Serialize(hasFired);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called by the Save System when loading a game. Restores the fired state
|
|
/// and then re-evaluates the condition.
|
|
/// </summary>
|
|
public override void ApplyData(string s)
|
|
{
|
|
hasFired = !string.IsNullOrEmpty(s) ? SaveSystem.Deserialize<bool>(s) : false;
|
|
CheckConditionAndExecute();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Evaluates the Lua condition and invokes the appropriate UnityEvent.
|
|
/// </summary>
|
|
public void CheckConditionAndExecute()
|
|
{
|
|
if (fireOnce && hasFired)
|
|
{
|
|
if (DialogueDebug.logInfo) Debug.Log($"Dialogue System: ConditionalEventTrigger '{name}' will not fire again because Fire Once is true and it has already fired.", this);
|
|
return;
|
|
}
|
|
|
|
bool conditionResult = condition.IsTrue(null);
|
|
|
|
if (DialogueDebug.logInfo) Debug.Log($"Dialogue System: ConditionalEventTrigger '{name}' condition is {conditionResult}. Invoking events.", this);
|
|
|
|
if (conditionResult)
|
|
{
|
|
onConditionTrue.Invoke();
|
|
hasFired = true; // Mark as fired if the condition was met
|
|
}
|
|
else
|
|
{
|
|
onConditionFalse.Invoke();
|
|
}
|
|
}
|
|
}
|
|
} |