CockActionComp.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections;
  3. using Game;
  4. using Sound;
  5. using UnityEngine;
  6. namespace Comp
  7. {
  8. public class CockActionComp : MonoBehaviour
  9. {
  10. private const string CockTag = "cock";
  11. // 动画控制器
  12. private ICockController _cockController;
  13. // 移动组件
  14. private CockMoveComp _cockMoveComp;
  15. private bool _highJump; // 是否高跳
  16. // 跑步跟切换待机状态相关参数
  17. private bool _firstTrigger = true; // 第一次碰撞切换为待机
  18. // 执行动作相关
  19. public Action curAction;
  20. public int randomAttack = 0;
  21. private readonly WaitForSeconds _waitEverySecond = new WaitForSeconds(1f);
  22. private Rigidbody _rigidbody;
  23. public bool OpenGravity = false;
  24. public bool CloseGravity = false;
  25. public Vector3 stayPos = Vector3.zero;
  26. // 水平与垂直方向运动相关
  27. public int playerId;
  28. public int cockId;
  29. public bool rightForward = false;
  30. // miss文本相关参数
  31. private GameObject _missPrefab;
  32. private bool _createMiss = false;
  33. public ICockController CockController
  34. {
  35. get
  36. {
  37. if (_cockController != null) return _cockController;
  38. _cockController = new AnimatorCockController(animator);
  39. _cockController.OnRun += OnRun;
  40. _cockController.OnIdle += OnIdle;
  41. _cockController.OnAttack += OnAttack;
  42. _cockController.OnJumpAttack += OnJumpAttack;
  43. return _cockController;
  44. }
  45. }
  46. public Animator animator;
  47. private void OnCollisionEnter(Collision collision)
  48. {
  49. if (!collision.gameObject.CompareTag(CockTag) || !_firstTrigger) return;
  50. _firstTrigger = false;
  51. _cockController.IdleInBattle();
  52. GameCore.Instance.GetCurState().BattleStart();
  53. }
  54. private void Awake()
  55. {
  56. _cockMoveComp = GetComponent<CockMoveComp>();
  57. _rigidbody = GetComponent<Rigidbody>();
  58. }
  59. private void Start()
  60. {
  61. _missPrefab = Resources.Load<GameObject>("Prefab/prefab_miss");
  62. StartCoroutine(CallEverySeconds());
  63. }
  64. private IEnumerator CallEverySeconds()
  65. {
  66. while (true)
  67. {
  68. if (curAction == CockController.JumpAndAttack)
  69. {
  70. if (randomAttack == 0)
  71. {
  72. curAction?.Invoke();
  73. }
  74. else
  75. {
  76. if (randomAttack == 4)
  77. {
  78. Debug.Log("random attack=====" + randomAttack);
  79. }
  80. CockController.RandomAttack(randomAttack);
  81. randomAttack = 0;
  82. }
  83. }
  84. else
  85. {
  86. curAction?.Invoke();
  87. }
  88. curAction = null;
  89. yield return _waitEverySecond;
  90. }
  91. }
  92. private void Update()
  93. {
  94. if (_createMiss)
  95. {
  96. CreateMissText();
  97. _createMiss = false;
  98. }
  99. }
  100. private void FixedUpdate()
  101. {
  102. if (CloseGravity)
  103. {
  104. _rigidbody.useGravity = false;
  105. if (stayPos != Vector3.zero)
  106. transform.localPosition = stayPos;
  107. }
  108. if (OpenGravity)
  109. {
  110. CloseGravity = false;
  111. stayPos = Vector3.zero;
  112. OpenGravity = false;
  113. _rigidbody.useGravity = true;
  114. _rigidbody.velocity = Vector3.zero;
  115. }
  116. }
  117. private void OnRun()
  118. {
  119. _cockMoveComp.MoveAndNeverStop();
  120. }
  121. private void OnIdle()
  122. {
  123. _cockMoveComp.Stop();
  124. }
  125. private void OnAttack()
  126. {
  127. SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId);
  128. _cockMoveComp.Move();
  129. }
  130. private void OnJumpAttack()
  131. {
  132. SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId);
  133. _cockMoveComp.Move();
  134. _cockMoveComp.Jump(_highJump);
  135. }
  136. public void OnAttackEnd()
  137. {
  138. }
  139. public void OnJumpEnd()
  140. {
  141. }
  142. public void SetHighJump(bool high)
  143. {
  144. _highJump = high;
  145. }
  146. public void CreateMiss()
  147. {
  148. _createMiss = true;
  149. }
  150. // ReSharper disable Unity.PerformanceAnalysis
  151. private void CreateMissText()
  152. {
  153. var obj = Instantiate(_missPrefab);
  154. obj.GetComponent<MissText>().SetTarget(transform);
  155. }
  156. }
  157. }