SoulBoom.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SoulBoom : MonoBehaviour
  5. {
  6. [HideInInspector]
  7. public float time; //当前时间
  8. public float boomTime; //爆炸开始炸的时间
  9. [HideInInspector]
  10. public bool isBoom; //是否已经炸过了
  11. public AttackInfo attackInfo; //damage造成伤害、force爆炸力的大小、repel value韧性值减值,其余的参数没用到
  12. public float destroyTime; //消失时间
  13. public List<MoveCharacter> characters = new List<MoveCharacter>(); //被炸到的对象
  14. public bool canHitTower; //也对塔造成伤害
  15. private void Update()
  16. {
  17. time += Time.deltaTime;
  18. if (time >= boomTime)
  19. {
  20. //造成伤害的瞬间
  21. if (time < boomTime + 0.1)
  22. {
  23. GetComponent<Collider>().enabled = true;
  24. }
  25. else
  26. {
  27. if (!isBoom)
  28. {
  29. for (int i = 0; i < characters.Count; i++)
  30. {
  31. if (characters[i].state == CharacterState.Die)
  32. {
  33. continue;
  34. }
  35. PlayerController playerController =
  36. characters[i].GetComponent<PlayerController>();
  37. if (playerController != null && playerController.isBaseBtnOut)
  38. {
  39. continue;
  40. }
  41. Vector3 target;
  42. Vector3 pos1 = characters[i].transform.position;
  43. Vector3 pos2 = transform.position;
  44. target = (pos1 - pos2).normalized;
  45. characters[i].BeHit
  46. (attackInfo.damage, target * attackInfo.force, true, attackInfo.repelValue);
  47. }
  48. isBoom = true;
  49. GetComponent<Collider>().enabled = false;
  50. }
  51. }
  52. }
  53. if (time >= destroyTime)
  54. {
  55. time = 0;
  56. isBoom = false;
  57. characters = new List<MoveCharacter>();
  58. gameObject.SetActive(false);
  59. }
  60. }
  61. private void OnTriggerEnter(Collider other)
  62. {
  63. BeHitTrigger beHitTrigger = other.GetComponent<BeHitTrigger>();
  64. if (beHitTrigger != null)
  65. {
  66. if(other.transform.parent.parent.parent.gameObject.layer == 6
  67. || other.transform.parent.parent.parent.gameObject.layer == 7)
  68. {
  69. MoveCharacter character = other.GetComponentInParent<MoveCharacter>();
  70. if (character.rb == null)
  71. {
  72. character = character.transform.parent.GetComponent<MoveCharacter>();
  73. }
  74. if (!characters.Exists(t => t == character))
  75. {
  76. characters.Add(character);
  77. }
  78. }
  79. Tower tower = other.GetComponentInParent<Tower>();
  80. if (canHitTower && tower != null)
  81. {
  82. tower.BeHit(attackInfo.damage, attackInfo.force* Vector3.zero,attackInfo.changeHurt,attackInfo.repelValue);
  83. }
  84. }
  85. }
  86. private void OnTriggerExit(Collider other)
  87. {
  88. BeHitTrigger beHitTrigger = other.GetComponent<BeHitTrigger>();
  89. if (beHitTrigger != null
  90. && (other.transform.parent.parent.parent.gameObject.layer == 6
  91. || other.transform.parent.parent.parent.gameObject.layer == 7))
  92. {
  93. MoveCharacter character = other.GetComponentInParent<MoveCharacter>();
  94. if (characters.Exists(t => t == character))
  95. {
  96. characters.Remove(character);
  97. }
  98. }
  99. }
  100. }