CockActionComp.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections;
  3. using Game;
  4. using Sound;
  5. using UnityEngine;
  6. namespace Comp
  7. {
  8. public class CockActionComp : MonoBehaviour
  9. {
  10. private const string CockTag = "cock";
  11. private ICockController _cockController;
  12. // 跑步跟切换待机状态相关参数
  13. private bool _firstTrigger = true; // 第一次碰撞切换为待机
  14. private Vector3 _thrust;
  15. private const float MoveSpeed = 1f;
  16. // 锁定物体相关参数
  17. private float _miniY; // 最低Y坐标
  18. // 执行动作相关
  19. public Action curAction;
  20. private readonly WaitForSeconds _waitEverySecond = new WaitForSeconds(1f);
  21. // 水平与垂直方向运动相关
  22. private CockJumpData _lowData, _highData, _curData;
  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)
  34. {
  35. _cockController = new AnimatorCockController(animator);
  36. _cockController.OnRun += OnRun;
  37. _cockController.OnIdle += OnIdle;
  38. _cockController.OnAttack += OnAttack;
  39. _cockController.OnJumpAttack += OnJumpAttack;
  40. return _cockController;
  41. }
  42. return _cockController;
  43. }
  44. }
  45. public Animator animator;
  46. private void OnCollisionEnter(Collision collision)
  47. {
  48. if (collision.gameObject.CompareTag(CockTag) && _firstTrigger)
  49. {
  50. _firstTrigger = false;
  51. _cockController.IdleInBattle();
  52. GameCore.Instance.GetCurState().BattleStart();
  53. }
  54. }
  55. private void Start()
  56. {
  57. _missPrefab = Resources.Load<GameObject>("Prefab/prefab_miss");
  58. _miniY = transform.position.y;
  59. // 跳跃相关
  60. _lowData = new CockJumpData(0.5f, 0.45f, false);
  61. _highData = new CockJumpData(1.0f, 0.3f, true);
  62. _curData = _highData;
  63. StartCoroutine(CallEverySeconds());
  64. }
  65. private IEnumerator CallEverySeconds()
  66. {
  67. while (true)
  68. {
  69. curAction?.Invoke();
  70. curAction = null;
  71. yield return _waitEverySecond;
  72. }
  73. }
  74. private void Update()
  75. {
  76. if (_createMiss)
  77. {
  78. _createMiss = false;
  79. CreateMissText();
  80. }
  81. }
  82. private void FixedUpdate()
  83. {
  84. transform.Translate(_thrust * Time.deltaTime);
  85. // 移动游戏对象
  86. // if (GameCore.Instance.inBattleState)
  87. transform.position += _curData.GetJumpVector();
  88. if (GameCore.Instance.inBattleState)
  89. {
  90. if (rightForward)
  91. {
  92. transform.position += _curData.GetMoveVector();
  93. }
  94. else
  95. {
  96. transform.position -= _curData.GetMoveVector();
  97. }
  98. }
  99. if (transform.position.y < _miniY)
  100. {
  101. var position = transform.position;
  102. position = new Vector3(position.x, _miniY, position.z);
  103. transform.position = position;
  104. }
  105. }
  106. private void OnRun()
  107. {
  108. _thrust = new Vector3(0, 0, MoveSpeed);
  109. }
  110. private void OnIdle()
  111. {
  112. _thrust = new Vector3(0, 0, 0);
  113. }
  114. private void OnAttack()
  115. {
  116. _curData.StartMove();
  117. }
  118. private void OnJumpAttack()
  119. {
  120. SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId);
  121. _curData.StartJump();
  122. }
  123. public void OnAttackEnd()
  124. {
  125. _curData.StopMove();
  126. }
  127. public void OnJumpEnd()
  128. {
  129. _curData.StopJump();
  130. }
  131. public void SetHighJump(bool high)
  132. {
  133. _curData = high ? _highData : _lowData;
  134. }
  135. public void CreateMiss()
  136. {
  137. this._createMiss = true;
  138. }
  139. private void CreateMissText()
  140. {
  141. var obj = Instantiate(_missPrefab);
  142. obj.GetComponent<MissText>().SetTarget(transform);
  143. }
  144. }
  145. public class CockJumpData
  146. {
  147. private readonly float _gravity; // 重力
  148. private readonly float _jumpVelocity; // 跳跃速度
  149. private readonly bool _forward; // 是否前进
  150. private Vector3 _jumpDirection = Vector3.zero;
  151. private Vector3 _moveDirection = Vector3.zero;
  152. private bool _move = false; // 是否移动状态
  153. private float _timeToJumpApex;
  154. private float _curJumpTime;
  155. // 跳跃高度 到达跳跃高度的时间
  156. public CockJumpData(float jumpHeight, float timeToJumpApex, bool forward)
  157. {
  158. _forward = forward;
  159. _timeToJumpApex = timeToJumpApex;
  160. // 计算跳跃速度和重力
  161. _gravity = -(2 * jumpHeight) / Mathf.Pow(timeToJumpApex, 2);
  162. _jumpVelocity = Mathf.Abs(_gravity) * timeToJumpApex;
  163. }
  164. public Vector3 GetJumpVector()
  165. {
  166. if (_curJumpTime < 0)
  167. {
  168. return Vector3.zero;
  169. }
  170. // 计算重力
  171. _jumpDirection.y += _gravity * Time.deltaTime;
  172. _curJumpTime -= Time.deltaTime;
  173. // 移动游戏对象
  174. return _jumpDirection * Time.deltaTime;
  175. }
  176. public Vector3 GetMoveVector()
  177. {
  178. if (!_move)
  179. return Vector3.zero;
  180. // 计算位移
  181. _moveDirection.x = (_forward ? 30f : -30f) * Time.deltaTime;
  182. return _moveDirection * Time.deltaTime;
  183. }
  184. public void StartJump()
  185. {
  186. _jumpDirection.y = _jumpVelocity;
  187. _curJumpTime = _timeToJumpApex * 2; // 双倍时间
  188. _move = true;
  189. }
  190. public void StartMove()
  191. {
  192. _move = true;
  193. }
  194. public void StopJump()
  195. {
  196. _move = false;
  197. }
  198. public void StopMove()
  199. {
  200. _move = false;
  201. }
  202. }
  203. }