using System.Collections; using System.Linq; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core; namespace KairoEngine.Core.GameActions { [System.Serializable, HideReferenceObjectPicker] public class OnEventGameActionTrigger : GameActionTriggerBase { public override string name { get { return $"On Event \"{eventName}\""; } } public override GameActionTriggersController controller { get => _controller; set { _controller = value; typeName = "OnEventGameActionTrigger"; } } public override string GetTypeName() => "OnEventGameActionTrigger"; public override string GetTriggerName() => "On Event"; [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Core/Editor/Icons/OnEventGameActionTriggerIcon.png")] public string eventName = "event"; [IconFoldoutGroup("@name")] public float delay = 0; [IconFoldoutGroup("@name")] public bool oneShot = false; [IconFoldoutGroup("@name")] public bool debugEvents = false; private float _counter = 0; private bool _triggered = false; public override void OnEnable() { GenericEvents.StartListening(eventName, ReceiveEvent); if(debugEvents) Debug.Log($"Started listening to \'{eventName}\' events"); _counter = 0f; _triggered = false; } public override void Update() { if(_triggered) { _counter += Time.deltaTime; if(_counter > delay) TriggerActions(); } } public override void OnDisable() { GenericEvents.StopListening(eventName, TriggerActions); if(debugEvents) Debug.Log($"Stopped listening to \'{eventName}\' events"); } private void ReceiveEvent() { if(debugEvents) Debug.Log($"Event \'{eventName}\' triggered"); _triggered = true; if(delay == 0) TriggerActions(); } private void TriggerActions() { controller.TriggerActions(); if(oneShot) { GenericEvents.StopListening(eventName, ReceiveEvent); if(debugEvents) Debug.Log($"Stopped listening to \'{eventName}\' events"); } else { _counter = 0f; _triggered = false; } } public static OnEventGameActionTrigger JSONToOnEventGameActionTrigger(string data) { return JsonUtility.FromJson(data); } private OnEventGameActionTrigger Duplicate() { OnEventGameActionTrigger trigger = new OnEventGameActionTrigger(); trigger.controller = controller; return trigger; } } }