HitFeedbackSystem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. public class HitFeedbackSystem : MonoBehaviour
  6. {
  7. //组件
  8. private Character character;
  9. private AttributeStatus attributeStatus;
  10. [TabGroup("顿帧")] public int freezeFrame;
  11. [TabGroup("顿帧")] public bool attackFromHasFreezeFrame;
  12. [TabGroup("顿帧")] [DisplayOnly] public bool isFreeze;
  13. [HideInInspector] public bool canFreeze;
  14. [HideInInspector] public bool canBeFreeze = true;
  15. private RigidbodyConstraints origRC;
  16. private Vector3 velocity; //顿帧前速度
  17. public CharacterState curCharacterState; //顿帧前状态
  18. [HideInInspector] public AttackController.AttackMethod attackMethod;
  19. [HideInInspector] public Character attackFromData;
  20. [TabGroup("僵直")] [DisplayOnly] public float hitStunTime;
  21. [HideInInspector] public bool canHitStun;
  22. private void Awake()
  23. {
  24. character = GetComponentInParent<MoveCharacter>();
  25. attributeStatus = GetComponent<AttributeStatus>();
  26. }
  27. void Start()
  28. {
  29. }
  30. private void OnDisable()
  31. {
  32. isFreeze = false;
  33. }
  34. void FixedUpdate()
  35. {
  36. //顿帧
  37. if (canBeFreeze && isFreeze)
  38. {
  39. if (character.rb == null)
  40. {
  41. return;
  42. }
  43. if (freezeFrame <= 0)
  44. {
  45. isFreeze = false;
  46. character.ani.speed = 1;
  47. character.rb.constraints = origRC;
  48. character.rb.velocity = velocity;
  49. character.ChangeState(curCharacterState);
  50. if (attackFromData != null)
  51. {
  52. attributeStatus.AddSpecialState(attackMethod, attackFromData);
  53. attackFromData = null;
  54. }
  55. }
  56. freezeFrame -= 1;
  57. }
  58. }
  59. public void HitStunUpdate()
  60. {
  61. if (hitStunTime <= 0)
  62. {
  63. character.ChangeState(CharacterState.Idle);
  64. }
  65. else
  66. {
  67. character.rb.velocity = Vector3.zero;
  68. hitStunTime -= Time.deltaTime;
  69. }
  70. }
  71. //顿帧
  72. public void FreezeFrame(AttackController.AttackMethod attackMethod, Character attackFrom)
  73. {
  74. if (canFreeze)
  75. {
  76. if (character.rb == null)
  77. {
  78. return;
  79. }
  80. this.attackMethod = attackMethod;
  81. attackFromData = attackFrom;
  82. canFreeze = false;
  83. if (!isFreeze)
  84. {
  85. HitFeedbackSystem hitFeedbackSystem = (attackFrom as MoveCharacter).hitFeedbackSystem;
  86. origRC = character.rb.constraints;
  87. velocity = character.rb.velocity;
  88. curCharacterState = character.state;
  89. character.ani.speed = 0;
  90. character.rb.constraints = RigidbodyConstraints.FreezeAll;
  91. character.ChangeState(CharacterState.FramePause);
  92. isFreeze = true;
  93. if (attackFromHasFreezeFrame && !hitFeedbackSystem.isFreeze)
  94. {
  95. hitFeedbackSystem.freezeFrame = freezeFrame;
  96. hitFeedbackSystem.origRC = attackFrom.rb.constraints;
  97. hitFeedbackSystem.velocity = attackFrom.rb.velocity;
  98. hitFeedbackSystem.curCharacterState = attackFrom.state;
  99. attackFrom.ani.speed = 0;
  100. attackFrom.rb.constraints = RigidbodyConstraints.FreezeAll;
  101. attackFrom.ChangeState(CharacterState.FramePause);
  102. hitFeedbackSystem.isFreeze = true;
  103. }
  104. }
  105. }
  106. else
  107. {
  108. attributeStatus.AddSpecialState(attackMethod,attackFrom);
  109. }
  110. }
  111. //僵直
  112. public void EnterHitStun(Character attackFrom)
  113. {
  114. if (canHitStun)
  115. {
  116. canHitStun = false;
  117. float dir1 = attackFrom.transform.position.x <= character.transform.position.x? 1:-1;
  118. float dir2 = character.bodyTrans.localScale.x >= 0? 1:-1;
  119. if(dir1 == dir2)
  120. {
  121. character.ani.Play("hitted", 0, 0);
  122. }
  123. else
  124. {
  125. character.ani.Play("hitted_back", 0, 0);
  126. }
  127. character.ChangeState(CharacterState.HitStun);
  128. character.ChangeStateText(CharacterState.HitStun);
  129. }
  130. }
  131. }