using System; using UnityEngine; using UnityEngine.Events; public class BlockTrigger : MonoBehaviour { public enum BlockTriggerType { Default, None, ShieldCharge, Echo, Empower } public UnityEvent triggerEvent; public UnityEvent triggerEventEarly; public bool delayOtherActions; public UnityEvent triggerFirstBlockThatDelaysOthers; public UnityEvent triggerSuperFirstBlock; public UnityEvent successfulBlockEvent; public UnityEvent blockRechargeEvent; private BlockEffect[] effects; public float cooldown; private float lastTriggerTime = -5f; public BlockTriggerType blackListedType = BlockTriggerType.None; public float cooldownSuccess; private float lastTriggerTimeSuccessful = -5f; private void Start() { effects = GetComponents(); Block componentInParent = GetComponentInParent(); componentInParent.SuperFirstBlockAction = (Action)Delegate.Combine(componentInParent.SuperFirstBlockAction, new Action(DoSuperFirstBlock)); componentInParent.FirstBlockActionThatDelaysOthers = (Action)Delegate.Combine(componentInParent.FirstBlockActionThatDelaysOthers, new Action(DoFirstBlockThatDelaysOthers)); componentInParent.BlockAction = (Action)Delegate.Combine(componentInParent.BlockAction, new Action(DoBlock)); componentInParent.BlockActionEarly = (Action)Delegate.Combine(componentInParent.BlockActionEarly, new Action(DoBlockEarly)); componentInParent.BlockProjectileAction = (Action)Delegate.Combine(componentInParent.BlockProjectileAction, new Action(DoBlockedProjectile)); componentInParent.BlockRechargeAction = (Action)Delegate.Combine(componentInParent.BlockRechargeAction, new Action(DoBlockRecharge)); if (delayOtherActions) { GetComponentInParent().delayOtherActions = true; } } private void OnDestroy() { Block componentInParent = GetComponentInParent(); if ((bool)componentInParent && componentInParent.SuperFirstBlockAction != null) { componentInParent.SuperFirstBlockAction = (Action)Delegate.Remove(componentInParent.SuperFirstBlockAction, new Action(DoSuperFirstBlock)); } if ((bool)componentInParent && componentInParent.FirstBlockActionThatDelaysOthers != null) { componentInParent.FirstBlockActionThatDelaysOthers = (Action)Delegate.Remove(componentInParent.FirstBlockActionThatDelaysOthers, new Action(DoFirstBlockThatDelaysOthers)); } if ((bool)componentInParent && componentInParent.BlockAction != null) { componentInParent.BlockAction = (Action)Delegate.Remove(componentInParent.BlockAction, new Action(DoBlock)); } if ((bool)componentInParent && componentInParent.BlockActionEarly != null) { componentInParent.BlockActionEarly = (Action)Delegate.Remove(componentInParent.BlockActionEarly, new Action(DoBlockEarly)); } if ((bool)componentInParent && componentInParent.BlockProjectileAction != null) { componentInParent.BlockProjectileAction = (Action)Delegate.Remove(componentInParent.BlockProjectileAction, new Action(DoBlockedProjectile)); } if ((bool)componentInParent && componentInParent.BlockRechargeAction != null) { componentInParent.BlockRechargeAction = (Action)Delegate.Remove(componentInParent.BlockRechargeAction, new Action(DoBlockRecharge)); } } public void DoSuperFirstBlock(BlockTriggerType triggerType) { if (triggerType != blackListedType && !(lastTriggerTime + cooldown > Time.time)) { lastTriggerTime = Time.time; triggerSuperFirstBlock.Invoke(); } } public void DoFirstBlockThatDelaysOthers(BlockTriggerType triggerType) { if (triggerType != blackListedType && !(lastTriggerTime + cooldown > Time.time)) { lastTriggerTime = Time.time; triggerFirstBlockThatDelaysOthers.Invoke(); } } public void DoBlockEarly(BlockTriggerType triggerType) { if (triggerType != blackListedType && !(lastTriggerTime + cooldown > Time.time)) { lastTriggerTime = Time.time; triggerEventEarly.Invoke(); } } public void DoBlock(BlockTriggerType triggerType) { if (triggerType != blackListedType && !(lastTriggerTime + cooldown > Time.time)) { lastTriggerTime = Time.time; triggerEvent.Invoke(); } } public void DoBlockedProjectile(GameObject projectile, Vector3 forward, Vector3 hitPos) { if (!(lastTriggerTimeSuccessful + cooldownSuccess > Time.time)) { lastTriggerTimeSuccessful = Time.time; successfulBlockEvent.Invoke(); for (int i = 0; i < effects.Length; i++) { effects[i].DoBlockedProjectile(projectile, forward, hitPos); } } } public void DoBlockRecharge() { blockRechargeEvent.Invoke(); } }