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

110 lines
2.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Beyond
{
public class ActionTrigger : QuestTriggerBase
{
public GameObject[] m_objectsToEnable;
public GameObject[] m_objectsToDisable;
public bool m_fireOnStart = false;
public ActionTrigger[] m_triggersToFire;
public float m_delay = -1f;
public ActionResponse m_actionResponse;
public MaterialChanger[] m_materialsToChange;
[Serializable]
public class MaterialChanger
{
public Renderer renderer;
public Material materialToSet;
}
// Start is called before the first frame update
public override void Start()
{
base.Start();
//gameObject.layer = LayerMask.NameToLayer("Triggers");
m_wasFired = false;
if (m_fireOnStart)
{
OnAction();
}
}
IEnumerator FireAction(string actionName)
{
OnActionResponse(m_actionResponse);
if (m_delay > 0)
{
yield return new WaitForSeconds(m_delay);
}
foreach (var g in m_objectsToEnable)
{
if (g != null)
{
g.SetActive(true);
}
else
{
Debug.LogError($"ActionTrigger ({gameObject.name}), object to enable null ");
}
}
foreach (var g in m_objectsToDisable)
{
if (g != null)
{
g.SetActive(false);
}
else
{
Debug.LogError($"ActionTrigger ({gameObject.name}), object to disable null ");
}
}
foreach (var t in m_triggersToFire)
{
if (t != null)
{
t.OnAction(actionName);
}
else
{
Debug.LogError($"ActionTrigger ({gameObject.name}), trigger to fire is null ");
}
}
if (m_materialsToChange != null)
{
foreach (var m in m_materialsToChange)
{
if (m.renderer && m.materialToSet)
m.renderer.material = m.materialToSet;
}
}
yield return null;
}
[Sirenix.OdinInspector.Button]
public virtual void OnAction(string actionName = "")
{
if (!m_wasFired || !m_fireOnce)
StartCoroutine(FireAction(actionName));
m_wasFired = true;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
OnAction();
}
}
}
}