Files
beyond/Assets/Scripts/Quests/ConditionalGameObjectController.cs
2024-11-20 15:21:28 +01:00

87 lines
2.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Beyond;
using PixelCrushers;
using PixelCrushers.DialogueSystem;
using UnityEngine;
using PixelCrushers.QuestMachine;
using Sirenix.OdinInspector;
using UnityEngine.Serialization;
public class ConditionalGameObjectController : MonoBehaviour, IMessageHandler
{
[Title("Conditions to Enable Game Objects")] [SerializeField]
private Condition m_conditionToEnable;
[Title("Conditions to Disable Game Objects")] [SerializeField]
private Condition m_conditionToDisable;
[InfoBox("When conditions are met gameobjects below will be enable or disable")]
[SerializeField] private List<GameObject> m_gameObjectsToEnable = new List<GameObject>();
[FormerlySerializedAs("m_gameObjectstoDisable")] [SerializeField]
private List<GameObject> m_gameObjectsToDisable = new List<GameObject>();
private void Awake()
{
// MessageSystem.AddListener(this, QuestMachineMessages.QuestStateChangedMessage, string.Empty);
//PixelCrushers.SaveSystem.saveDataApplied += OnSaveDataApplied;
}
void OnSaveDataApplied()
{
PixelCrushers.SaveSystem.saveDataApplied -= OnSaveDataApplied;
ManageObjects();
}
private void OnEnable()
{
MessageSystem.AddListener(this, QuestMachineMessages.QuestStateChangedMessage, string.Empty);
PixelCrushers.SaveSystem.saveDataApplied += OnSaveDataApplied;
ManageObjects();
}
private void OnDestroy()
{
//MessageSystem.RemoveListener(this, QuestMachineMessages.QuestStateChangedMessage, string.Empty);
//MessageSystem.RemoveListener(this);
//PixelCrushers.SaveSystem.saveDataApplied -= OnSaveDataApplied;
}
private void OnDisable()
{
MessageSystem.RemoveListener(this, QuestMachineMessages.QuestStateChangedMessage, string.Empty);
MessageSystem.RemoveListener(this);
PixelCrushers.SaveSystem.saveDataApplied -= OnSaveDataApplied;
// MessageSystem.RemoveListener(this);
//PixelCrushers.SaveSystem.saveDataApplied -= OnSaveDataApplied;
}
public void OnMessage(MessageArgs messageArgs)
{
switch (messageArgs.message)
{
case QuestMachineMessages.QuestStateChangedMessage:
ManageObjects();
break;
case QuestMachineMessages.QuestCounterChangedMessage:
case QuestMachineMessages.RefreshUIsMessage:
case QuestMachineMessages.QuestTrackToggleChangedMessage:
break;
}
}
private void ManageObjects()
{
if (m_conditionToEnable.IsTrue(null))
{
m_gameObjectsToEnable.ForEach(x => x.SetActive(true));
}
if (m_conditionToDisable.IsTrue(null))
{
m_gameObjectsToDisable.ForEach(x => x.SetActive(false));
}
}
}