AttackController.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class AttackController : MonoBehaviour
  5. {
  6. //攻击类型
  7. public enum AttackType
  8. {
  9. Melee = 0, //近战
  10. Shoot = 1, //射击
  11. Dash = 2, //英灵刺客
  12. }
  13. [System.Serializable]
  14. public struct SpineAniKey
  15. {
  16. public string aniName;
  17. public List<AttackKeyType> keys;
  18. }
  19. public enum KeyType
  20. {
  21. AttackStart,
  22. AttackEnd,
  23. }
  24. [System.Serializable]
  25. public struct AttackKeyType
  26. {
  27. public KeyType attackType;
  28. public string startKeyName;
  29. public float startKeyTime;
  30. public KeyType endType;
  31. public string endKeyName;
  32. public float endKeyTime;
  33. }
  34. private Character owner;
  35. [Header("所有攻击帧事件及时间")]
  36. public List<SpineAniKey> attackKeys;
  37. [Header("攻击类型")]
  38. public AttackType attackType;
  39. [Header("攻击参数")]
  40. public bool canHitFly;
  41. public int armorPiercing; //穿甲率
  42. public List<AttackInfo> Attack_summonInfos;
  43. public List<AttackInfo> Attack_marchInfos;
  44. //以下信息不在面板显示
  45. [HideInInspector]
  46. public bool isNonAttack = false; //无普攻
  47. [HideInInspector]
  48. public GameObject addAttackEffect;
  49. [DisplayOnly]
  50. public int[] curDamage1;
  51. [DisplayOnly]
  52. public int[] curDamage2;
  53. [Header("攻击动画")]
  54. public AttackTrigger attackTrigger;
  55. public List<float> keyTimes; //所有的帧事件时间
  56. [DisplayOnly]
  57. public float attackTime; //攻击剩余时间
  58. //以下信息不在面板显示
  59. [HideInInspector]
  60. public float attackKeyCount; //攻击进行时间
  61. [HideInInspector]
  62. public float nextStartKeyTime, nextEndKeyTime;
  63. [HideInInspector]
  64. public int curKeyNum; //当前锁定到第几个帧事件
  65. [HideInInspector]
  66. public bool isAttackTriggerOn = false; //当前Attack Trigger是否开启
  67. [Header("远程单位")]
  68. public GameObject bulletPrefab; //子弹
  69. public List<Transform> shootPos; //子弹发射位置
  70. public bool shootTrack = false; //是否初始时瞄准目标
  71. public bool shootAlwaysTrack = false; //是否始终追踪
  72. [Header("目标")]
  73. public List<TargetType> targetTypes;
  74. [DisplayOnly]
  75. public List<Character> beTargetCharacter = new List<Character>(); //被哪些锁定
  76. public float getDistanceOffset = 0f;
  77. public void Init()
  78. {
  79. owner = GetComponent<Character>();
  80. curDamage1 = new int[Attack_summonInfos.Count];
  81. for (int i = 0; i < Attack_summonInfos.Count; i++)
  82. {
  83. curDamage1[i] = Attack_summonInfos[i].damage;
  84. }
  85. curDamage2 = new int[Attack_marchInfos.Count];
  86. for (int i = 0; i < Attack_marchInfos.Count; i++)
  87. {
  88. curDamage2[i] = Attack_marchInfos[i].damage;
  89. }
  90. }
  91. private void OnEnable()
  92. {
  93. if (attackTrigger != null)
  94. {
  95. attackTrigger.gameObject.SetActive(false);
  96. }
  97. }
  98. public void ChooseAttack()
  99. {
  100. }
  101. public void SetNextKeyTimes()
  102. {
  103. if (curKeyNum < keyTimes.Count)
  104. {
  105. nextStartKeyTime = keyTimes[curKeyNum];
  106. nextEndKeyTime = keyTimes[curKeyNum + 1];
  107. curKeyNum += 2;
  108. }
  109. }
  110. public virtual void Attack_summon()
  111. {
  112. owner.ani.Play("attack_summon", 0, 0);
  113. if (!isNonAttack)
  114. {
  115. if (attackType == AttackType.Shoot)
  116. {
  117. attackTrigger.isShoot = true;
  118. attackTrigger.GetComponent<Collider>().enabled = false;
  119. attackTrigger.type = AttackTrigger.attackTpye.summon;
  120. }
  121. attackTime = owner.totalAttack_summonTime;
  122. attackKeyCount = 0;
  123. keyTimes = new List<float>();
  124. foreach (SpineAniKey sak in attackKeys)
  125. {
  126. if (sak.aniName == "attack_summon")
  127. {
  128. foreach (AttackKeyType akt in sak.keys)
  129. {
  130. keyTimes.Add(akt.startKeyTime);
  131. keyTimes.Add(akt.endKeyTime);
  132. }
  133. }
  134. }
  135. curKeyNum = 0;
  136. SetNextKeyTimes();
  137. switch (attackType)
  138. {
  139. case AttackType.Melee:
  140. attackTrigger.damage = curDamage1[0];
  141. attackTrigger.changeHurt = Attack_summonInfos[0].changeHurt;
  142. attackTrigger.repelValue = Attack_summonInfos[0].repelValue;
  143. Vector3 attackDir = Attack_summonInfos[0].attackDir.normalized;
  144. if (owner.bodyTrans.localScale.x < 0)
  145. {
  146. attackDir.x = -attackDir.x;
  147. }
  148. attackTrigger.force = attackDir * Attack_summonInfos[0].force;
  149. break;
  150. default:
  151. break;
  152. }
  153. owner.ChangeState(CharacterState.Attack);
  154. }
  155. }
  156. public virtual void Attack_march()
  157. {
  158. Vector3 leftDir = owner.GetMoveDir();
  159. if (leftDir.x > 0.3f)
  160. {
  161. if (owner.bodyTrans.localScale.x > 0)
  162. {
  163. owner.Turn();
  164. }
  165. }
  166. else if (leftDir.x < -0.3f)
  167. {
  168. if (owner.bodyTrans.localScale.x < 0)
  169. {
  170. owner.Turn();
  171. }
  172. }
  173. owner.ani.Play("attack_march", 0, 0);
  174. if (attackType == AttackType.Shoot)
  175. {
  176. attackTrigger.isShoot = true;
  177. attackTrigger.GetComponent<Collider>().enabled = false;
  178. attackTrigger.type = AttackTrigger.attackTpye.march;
  179. }
  180. attackTime = owner.totalAttack_marchTime;
  181. attackKeyCount = 0;
  182. keyTimes = new List<float>();
  183. foreach (SpineAniKey sak in attackKeys)
  184. {
  185. if (sak.aniName == "attack_march")
  186. {
  187. foreach (AttackKeyType akt in sak.keys)
  188. {
  189. keyTimes.Add(akt.startKeyTime);
  190. keyTimes.Add(akt.endKeyTime);
  191. }
  192. }
  193. }
  194. curKeyNum = 0;
  195. SetNextKeyTimes();
  196. attackTrigger.damage = curDamage2[0];
  197. attackTrigger.changeHurt = Attack_marchInfos[0].changeHurt;
  198. attackTrigger.repelValue = Attack_marchInfos[0].repelValue;
  199. Vector3 attackDir = Attack_marchInfos[0].attackDir.normalized;
  200. if (owner.bodyTrans.localScale.x < 0)
  201. {
  202. attackDir.x = -attackDir.x;
  203. }
  204. attackTrigger.force = attackDir * Attack_marchInfos[0].force;
  205. owner.ChangeState(CharacterState.Attack);
  206. }
  207. public virtual void AttackShootEvent(int attackId, int shootId)
  208. {
  209. AttackInfo attackInfo;
  210. if (attackId == 1)
  211. {
  212. attackInfo = Attack_summonInfos[shootId];
  213. }
  214. else
  215. {
  216. attackInfo = Attack_marchInfos[shootId];
  217. attackInfo.damage = curDamage2[0];
  218. }
  219. GameObject bulletObj = PoolManager.Instantiate(bulletPrefab);
  220. Bullet bullet = bulletObj.GetComponent<Bullet>();
  221. Vector3 attackDir = attackInfo.attackDir.normalized;
  222. if (owner.bodyTrans.localScale.x < 0)
  223. {
  224. attackDir.x = -attackDir.x;
  225. }
  226. if (attackId == 1 && owner.Attack_summonShootCanTransmit)
  227. {
  228. bullet.canTransmit = true;
  229. }
  230. bullet.BeShoot(owner, shootPos[shootId].position, attackDir, attackInfo.damage,
  231. attackInfo.force, attackInfo.changeHurt, attackInfo.repelValue, shootTrack,
  232. shootAlwaysTrack, owner.attackTarget ? owner.attackTarget : null);
  233. }
  234. }