CockActionComp.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. Debug.Log("关闭重力" + _rigidbody.useGravity + "===" + playerId);
  87. if (stayPos != Vector3.zero)
  88. transform.localPosition = stayPos;
  89. }
  90. if (OpenGravity)
  91. {
  92. CloseGravity = false;
  93. stayPos = Vector3.zero;
  94. OpenGravity = false;
  95. _rigidbody.useGravity = true;
  96. _rigidbody.velocity = Vector3.zero;
  97. Debug.Log("开启重力" + _rigidbody.useGravity + "===" + playerId);
  98. }
  99. }
  100. private void OnRun()
  101. {
  102. _cockMoveComp.MoveAndNeverStop();
  103. }
  104. private void OnIdle()
  105. {
  106. _cockMoveComp.Stop();
  107. }
  108. private void OnAttack()
  109. {
  110. SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId);
  111. _cockMoveComp.Move();
  112. }
  113. private void OnJumpAttack()
  114. {
  115. SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId);
  116. _cockMoveComp.Move();
  117. _cockMoveComp.Jump(_highJump);
  118. }
  119. public void OnAttackEnd()
  120. {
  121. }
  122. public void OnJumpEnd()
  123. {
  124. }
  125. public void SetHighJump(bool high)
  126. {
  127. _highJump = high;
  128. }
  129. public void CreateMiss()
  130. {
  131. _createMiss = true;
  132. }
  133. // ReSharper disable Unity.PerformanceAnalysis
  134. private void CreateMissText()
  135. {
  136. var obj = Instantiate(_missPrefab);
  137. obj.GetComponent<MissText>().SetTarget(transform);
  138. }
  139. }
  140. }