| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using Base.Common;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using System;
- public class AttackTrigger : MonoBehaviour
- {
- [FoldoutGroup("场景交互-墙")] [LabelText("能破墙")] public bool canBreakWall;
- [FoldoutGroup("场景交互-墙")] public int wallBrealNum = 10;
- [FoldoutGroup("条件")] [LabelText("是否是远程角色")] public bool isShoot;
- [FoldoutGroup("条件")] [LabelText("是否为单体攻击")] public bool isSingleAttack;
- [FoldoutGroup("条件")] [LabelText("持续伤害")] public bool isDoT;
- public Character owner;
- public bool cantSingleAttack; //单体攻击单位是否已经攻击了
- [Header("攻击对象")]
- public List<BeHitTrigger> trigedObjs;
- public List<float> trigedTime;
- [Header("攻击属性")]
- public AttackController.AttackMethod attackMethod;
- public Vector3 force;
- public bool changeHurt;
- public float repelValue;
- public int offsetY = 1;
- public float hitRate = 1;
- [LabelText("伤害间隔时间")]public float interval;
- [Header("是否由士兵起手式创建")]
- public bool isCreatedByDemonicSummon = false;
- private void Awake()
- {
- owner = GetComponentInParent<Character>();
- }
- public virtual void Update()
- {
- DoT();
- }
- protected virtual void OnTriggerEnter(Collider other)
- {
- if (isShoot) return;
- if (!other.TryGetComponent(out BeHitTrigger hitTrigger)) return;
- Character hitOwner = hitTrigger.owner;
- if (hitOwner == null || hitOwner.isDie) return;
- if (isSingleAttack && cantSingleAttack) return;
- AttackController attackController = owner.attackController;
- if (!TryGetTargetTypeFromTag(hitOwner.tag, out TargetType otherTargetType)) return;
- if (!IsValidTargetType(attackController.targetTypes, otherTargetType)) return;
- trigedObjs.Add(hitTrigger);
- if (isDoT)
- {
- trigedTime.Add(0);
- return;
- }
- hitTrigger.BeHit(attackMethod, owner);
- if (hitTrigger.owner.debugAttackFrom)
- {
- hitTrigger.owner.DebugAttackFrom(owner.name, attackMethod.attackInfo.damage);
- }
- if (owner.TryGetComponent(out Demonic demonic))
- {
- hitTrigger.attackerID = demonic.id;
- }
- if (isSingleAttack)
- {
- cantSingleAttack = true;
- }
- }
- private HashSet<TargetType> _cachedTargetTypes;
- private List<TargetType> _lastTargetTypes;
- private bool IsValidTargetType(List<TargetType> targetTypes, TargetType checkType)
- {
- // 如果列表没有变化,使用缓存的HashSet
- if (targetTypes != _lastTargetTypes)
- {
- _cachedTargetTypes = new HashSet<TargetType>(targetTypes);
- _lastTargetTypes = targetTypes;
- }
- return _cachedTargetTypes.Contains(checkType);
- }
- private bool TryGetTargetTypeFromTag(string tag, out TargetType targetType)
- {
- return GameManager.TagToTargetTypeMap.TryGetValue(tag, out targetType);
- }
- private void OnTriggerExit(Collider other)
- {
- if (!isDoT) return;
- if (!other.TryGetComponent(out BeHitTrigger hitTrigger)) return;
- int index = trigedObjs.IndexOf(hitTrigger);
- if (index == -1) return;
- trigedObjs.Remove(hitTrigger);
- trigedTime.RemoveAt(index);
- }
- protected virtual void OnEnable()
- {
- trigedObjs.Clear();
- trigedTime.Clear();
- cantSingleAttack = false;
- if (isShoot)
- {
- CharacterColliders cc = GetComponentInParent<CharacterColliders>();
- switch (attackMethod.attackInfo.attackMethod_Type)
- {
- case AttackMethod_Type.Attack_Summon:
- cc.Attack_summonShootEvent(0);
- break;
- case AttackMethod_Type.Attack_March:
- cc.Attack_marchShootEvent(0);
- break;
- }
- }
- if (isCreatedByDemonicSummon) attackMethod.AddAdditionalEffects();
- }
- protected virtual void OnDisable()
- {
- if (canBreakWall)
- {
- canBreakWall = false;
- }
- if (isCreatedByDemonicSummon)
- {
- attackMethod.ClearAdditionalEffects();
- isCreatedByDemonicSummon = false;
- }
- }
- public void DoT()
- {
- if (!isDoT) return;
- for(int i = 0; i < trigedObjs.Count; i++)
- {
- BeHitTrigger beHitTrigger = trigedObjs[i];
- float time = trigedTime[i];
- if (beHitTrigger.owner.isDie)
- {
- continue;
- }
- time -= Time.deltaTime;
- if(time <= 0)
- {
- trigedTime[i] = interval;
- beHitTrigger.BeHit(attackMethod, owner);
- }
- else
- {
- trigedTime[i] = time;
- }
- }
- }
- }
|