ESpirits_Assassin.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public enum AssassinState
  5. {
  6. None = -1,
  7. Normal = 0, //正常状态
  8. FindPlayer = 1, //寻找主角位置
  9. ReadyToDash = 2, //准备攻击主角
  10. Dash = 3, //向主角方向冲刺
  11. Down = 4, //落地斩
  12. }
  13. public class ESpirits_Assassin : MonoBehaviour
  14. {
  15. [HideInInspector]
  16. public float time;
  17. public float attackCD;
  18. public float readyCD;
  19. public float hateDistance;
  20. public AssassinState state;
  21. public Enemy enemy;
  22. public Rigidbody rb;
  23. [HideInInspector]
  24. public float distance;
  25. public DashEffect dashEffect;
  26. public float moveSpeed;
  27. public float offset;
  28. [HideInInspector]
  29. public Vector3 targetDir;
  30. public float dashTime;
  31. private void Update()
  32. {
  33. OnState();
  34. }
  35. public void OnState()
  36. {
  37. switch (state)
  38. {
  39. case AssassinState.Normal:
  40. time += Time.deltaTime;
  41. if (time > attackCD)
  42. {
  43. enemy.isSpiritsAttack = true;
  44. time = 0;
  45. state = AssassinState.FindPlayer;
  46. ChosePlayer();
  47. enemy.searchState = SearchState.InSearchScope;
  48. enemy.ChangeState(CharacterState.Run);
  49. }
  50. break;
  51. case AssassinState.FindPlayer:
  52. ChosePlayer();
  53. if (distance <= hateDistance)
  54. {
  55. state = AssassinState.ReadyToDash;
  56. enemy.ChangeState(CharacterState.Rush);
  57. rb.velocity = Vector3.zero;
  58. enemy.ani.Play("hitted", 0, 0);
  59. enemy.aniCollider.Play("Hurt", 0, 0);
  60. }
  61. break;
  62. case AssassinState.ReadyToDash:
  63. time += Time.deltaTime;
  64. if (time >= readyCD)
  65. {
  66. state = AssassinState.Dash;
  67. dashEffect.isDash = true;
  68. dashEffect.isDashAttack = true;
  69. targetDir =
  70. (enemy.targetCharacter.transform.position - transform.position).normalized;
  71. enemy.ani.Play("attack_summon", 0, 0);
  72. time = 0;
  73. }
  74. break;
  75. case AssassinState.Dash:
  76. time += Time.deltaTime;
  77. Dash();
  78. if (time >= dashTime)
  79. {
  80. rb.velocity = Vector3.zero;
  81. time = 0;
  82. if (enemy.foot.TrigGround)
  83. {
  84. dashEffect.isDashAttack = false;
  85. state = AssassinState.Normal;
  86. enemy.isSpiritsAttack = false;
  87. enemy.searchState = SearchState.NoTarget;
  88. enemy.ChangeState(CharacterState.Idle);
  89. }
  90. else
  91. {
  92. state = AssassinState.Down;
  93. }
  94. }
  95. break;
  96. case AssassinState.Down:
  97. break;
  98. }
  99. }
  100. public void ChosePlayer()
  101. {
  102. float distance0 = 1000;
  103. float distance1 = 1000;
  104. if (PlayersInput.instance[0])
  105. {
  106. distance0 = Mathf.Abs(PlayersInput.instance[0].transform.position.x
  107. - transform.position.x);
  108. }
  109. if (PlayersInput.instance[1])
  110. {
  111. distance1 = Mathf.Abs(PlayersInput.instance[1].transform.position.x
  112. - transform.position.x);
  113. }
  114. if (distance0 <= distance1)
  115. {
  116. enemy.targetCharacter = PlayersInput.instance[0];
  117. distance = distance0;
  118. }
  119. else
  120. {
  121. enemy.targetCharacter = PlayersInput.instance[1];
  122. distance = distance1;
  123. }
  124. }
  125. private void Dash()
  126. {
  127. if (targetDir.x < 0)
  128. {
  129. dashEffect.offset = offset;
  130. }
  131. else
  132. {
  133. dashEffect.offset = -offset;
  134. }
  135. rb.velocity = targetDir * moveSpeed;
  136. }
  137. }