CockActionComp.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. 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. curAction?.Invoke();
  69. curAction = null;
  70. yield return _waitEverySecond;
  71. }
  72. }
  73. private void Update()
  74. {
  75. if (_createMiss)
  76. {
  77. CreateMissText();
  78. _createMiss = false;
  79. }
  80. }
  81. private void FixedUpdate()
  82. {
  83. if (CloseGravity)
  84. {
  85. _rigidbody.useGravity = false;
  86. if (stayPos != Vector3.zero)
  87. transform.localPosition = stayPos;
  88. }
  89. if (OpenGravity)
  90. {
  91. CloseGravity = false;
  92. stayPos = Vector3.zero;
  93. OpenGravity = false;
  94. _rigidbody.useGravity = true;
  95. _rigidbody.velocity = Vector3.zero;
  96. }
  97. }
  98. private void OnRun()
  99. {
  100. _cockMoveComp.MoveAndNeverStop();
  101. }
  102. private void OnIdle()
  103. {
  104. _cockMoveComp.Stop();
  105. }
  106. private void OnAttack()
  107. {
  108. SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId);
  109. _cockMoveComp.Move();
  110. }
  111. private void OnJumpAttack()
  112. {
  113. SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId);
  114. _cockMoveComp.Move();
  115. _cockMoveComp.Jump(_highJump);
  116. }
  117. public void OnAttackEnd()
  118. {
  119. }
  120. public void OnJumpEnd()
  121. {
  122. }
  123. public void SetHighJump(bool high)
  124. {
  125. _highJump = high;
  126. }
  127. public void CreateMiss()
  128. {
  129. _createMiss = true;
  130. }
  131. // ReSharper disable Unity.PerformanceAnalysis
  132. private void CreateMissText()
  133. {
  134. var obj = Instantiate(_missPrefab);
  135. obj.GetComponent<MissText>().SetTarget(transform);
  136. }
  137. }
  138. }