CockActionComp.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // 水平与垂直方向运动相关
  24. public int playerId;
  25. public int cockId;
  26. public bool rightForward = false;
  27. // miss文本相关参数
  28. private GameObject _missPrefab;
  29. private bool _createMiss = false;
  30. public bool willBeLift = false;
  31. public ICockController CockController
  32. {
  33. get
  34. {
  35. if (_cockController != null) return _cockController;
  36. _cockController = new AnimatorCockController(animator);
  37. _cockController.OnRun += OnRun;
  38. _cockController.OnIdle += OnIdle;
  39. _cockController.OnAttack += OnAttack;
  40. _cockController.OnJumpAttack += OnJumpAttack;
  41. return _cockController;
  42. }
  43. }
  44. public Animator animator;
  45. private void OnCollisionEnter(Collision collision)
  46. {
  47. if (!collision.gameObject.CompareTag(CockTag) || !_firstTrigger) return;
  48. _firstTrigger = false;
  49. _cockController.IdleInBattle();
  50. GameCore.Instance.GetCurState().BattleStart();
  51. }
  52. private void Awake()
  53. {
  54. _cockMoveComp = GetComponent<CockMoveComp>();
  55. _rigidbody = GetComponent<Rigidbody>();
  56. }
  57. private void Start()
  58. {
  59. _missPrefab = Resources.Load<GameObject>("Prefab/prefab_miss");
  60. StartCoroutine(CallEverySeconds());
  61. }
  62. private IEnumerator CallEverySeconds()
  63. {
  64. while (true)
  65. {
  66. curAction?.Invoke();
  67. curAction = null;
  68. yield return _waitEverySecond;
  69. }
  70. }
  71. private void Update()
  72. {
  73. if (!_createMiss) return;
  74. _createMiss = false;
  75. CreateMissText();
  76. }
  77. private void OnRun()
  78. {
  79. _cockMoveComp.MoveAndNeverStop();
  80. }
  81. private void OnIdle()
  82. {
  83. _cockMoveComp.Stop();
  84. }
  85. private void OnAttack()
  86. {
  87. SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId);
  88. _cockMoveComp.Move();
  89. }
  90. private void OnJumpAttack()
  91. {
  92. SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId);
  93. _cockMoveComp.Move();
  94. _cockMoveComp.Jump(_highJump);
  95. }
  96. public void OnAttackEnd()
  97. {
  98. if (willBeLift)
  99. {
  100. _rigidbody.useGravity = false;
  101. }
  102. }
  103. public void OnJumpEnd()
  104. {
  105. if (willBeLift)
  106. {
  107. _rigidbody.useGravity = false;
  108. }
  109. }
  110. public void SetHighJump(bool high)
  111. {
  112. _highJump = high;
  113. }
  114. public void CreateMiss()
  115. {
  116. _createMiss = true;
  117. }
  118. // ReSharper disable Unity.PerformanceAnalysis
  119. private void CreateMissText()
  120. {
  121. var obj = Instantiate(_missPrefab);
  122. obj.GetComponent<MissText>().SetTarget(transform);
  123. }
  124. }
  125. }