CockActionComp.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // 水平与垂直方向运动相关
  23. public int playerId;
  24. public int cockId;
  25. public bool rightForward = false;
  26. // miss文本相关参数
  27. private GameObject _missPrefab;
  28. private bool _createMiss = false;
  29. public ICockController CockController
  30. {
  31. get
  32. {
  33. if (_cockController != null) return _cockController;
  34. _cockController = new AnimatorCockController(animator);
  35. _cockController.OnRun += OnRun;
  36. _cockController.OnIdle += OnIdle;
  37. _cockController.OnAttack += OnAttack;
  38. _cockController.OnJumpAttack += OnJumpAttack;
  39. return _cockController;
  40. }
  41. }
  42. public Animator animator;
  43. private void OnCollisionEnter(Collision collision)
  44. {
  45. if (!collision.gameObject.CompareTag(CockTag) || !_firstTrigger) return;
  46. _firstTrigger = false;
  47. _cockController.IdleInBattle();
  48. GameCore.Instance.GetCurState().BattleStart();
  49. }
  50. private void Awake()
  51. {
  52. _cockMoveComp = GetComponent<CockMoveComp>();
  53. }
  54. private void Start()
  55. {
  56. _missPrefab = Resources.Load<GameObject>("Prefab/prefab_miss");
  57. StartCoroutine(CallEverySeconds());
  58. }
  59. private IEnumerator CallEverySeconds()
  60. {
  61. while (true)
  62. {
  63. curAction?.Invoke();
  64. curAction = null;
  65. yield return _waitEverySecond;
  66. }
  67. }
  68. private void Update()
  69. {
  70. if (!_createMiss) return;
  71. _createMiss = false;
  72. CreateMissText();
  73. }
  74. private void FixedUpdate()
  75. {
  76. // transform.Translate(_thrust * Time.deltaTime);
  77. // 移动游戏对象
  78. // if (GameCore.Instance.inBattleState)
  79. // transform.position += _curData.GetJumpVector();
  80. // if (GameCore.Instance.inBattleState)
  81. // {
  82. // if (rightForward)
  83. // {
  84. // transform.position += _curData.GetMoveVector();
  85. // }
  86. // else
  87. // {
  88. // transform.position -= _curData.GetMoveVector();
  89. // }
  90. // }
  91. // if (transform.position.y < _miniY)
  92. // {
  93. // var position = transform.position;
  94. // position = new Vector3(position.x, _miniY, position.z);
  95. // transform.position = position;
  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. }