From 766cdff5ffa72b65d7f106658d1603f47739b2ba Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Fri, 27 Oct 2023 11:05:14 +0800 Subject: + init --- GameCode/BlockTrigger.cs | 140 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 GameCode/BlockTrigger.cs (limited to 'GameCode/BlockTrigger.cs') diff --git a/GameCode/BlockTrigger.cs b/GameCode/BlockTrigger.cs new file mode 100644 index 0000000..604c466 --- /dev/null +++ b/GameCode/BlockTrigger.cs @@ -0,0 +1,140 @@ +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(); + } +} -- cgit v1.1-26-g67d0