CockActionComp.cs 4.8 KB

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