MasterActionComp.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections;
  3. using Game;
  4. using UnityEngine;
  5. namespace Comp
  6. {
  7. public class MasterActionComp : MonoBehaviour
  8. {
  9. private Animator _animator;
  10. private IMasterController _masterController;
  11. public IMasterController MasterController => _masterController;
  12. // 水平运动相关
  13. private Vector3 _thrust;
  14. private readonly Vector3 _rThrust = new Vector3(0, 0, -MoveSpeed);
  15. private const float MoveSpeed = 1f;
  16. private float _collisionX;
  17. private float _initialAnglesY; // 初始的Y角度
  18. private float _initialX;
  19. // 锁定物体相关参数
  20. private float _miniY = -0.6f; // 射线最低Y坐标
  21. // 抱鸡参数
  22. private GameObject _cock;
  23. private Vector3 _liftPos;
  24. private bool _isPutDown = false;
  25. public int playerId;
  26. private void Start()
  27. {
  28. _animator = GetComponent<Animator>();
  29. _initialAnglesY = transform.eulerAngles.y;
  30. _initialX = transform.position.x;
  31. _masterController = new AnimatorMasterController(_animator);
  32. _masterController.OnWalk += OnWalk;
  33. _masterController.Idle();
  34. }
  35. private void OnWalk()
  36. {
  37. _thrust = new Vector3(0, 0, MoveSpeed);
  38. StartCoroutine(CockRayCast());
  39. }
  40. private IEnumerator CockRayCast() // 判断碰撞点,现在不用射线了
  41. {
  42. yield return new WaitForSeconds(1f);
  43. var actionComp = GameCore.Instance.GetCockActionCompByPlayerId(playerId);
  44. var cockObj = actionComp.gameObject;
  45. _cock = cockObj;
  46. if (playerId == GameCore.Instance.localPlayer.playerId)
  47. _collisionX = _cock.transform.position.x - 1f;
  48. else
  49. {
  50. _collisionX = _cock.transform.position.x + 1f;
  51. }
  52. actionComp.willBeLift = true;
  53. }
  54. private void FixedUpdate()
  55. {
  56. var stateInfo = _animator.GetCurrentAnimatorStateInfo(0);
  57. if (stateInfo.IsTag("rwalk"))
  58. {
  59. if ((transform.forward.x > 0 && transform.position.x <= _initialX) ||
  60. (transform.forward.x < 0 && transform.position.x >= _initialX))
  61. {
  62. _masterController.Idle();
  63. GameCore.Instance.ReportMasterBack(transform.forward.x > 0);
  64. }
  65. else
  66. {
  67. transform.Translate(_rThrust * Time.deltaTime);
  68. }
  69. }
  70. else
  71. {
  72. transform.Translate(_thrust * Time.deltaTime);
  73. }
  74. // 锁定y轴旋转
  75. var eulerAngles = transform.eulerAngles;
  76. eulerAngles.y = _initialAnglesY;
  77. transform.eulerAngles = eulerAngles;
  78. // 抓鸡的时候操作一下
  79. if (transform.forward.x > 0)
  80. {
  81. if (_collisionX != 0 && transform.position.x >= _collisionX)
  82. {
  83. _thrust = Vector3.zero;
  84. _masterController.Lift();
  85. }
  86. }
  87. else
  88. {
  89. if (_collisionX != 0 && transform.position.x <= _collisionX)
  90. {
  91. _thrust = Vector3.zero;
  92. _masterController.Lift();
  93. }
  94. }
  95. }
  96. private void OnLowest() // 抱起动画最低点回调
  97. {
  98. if (!_isPutDown)
  99. {
  100. _liftPos = _cock.transform.position;
  101. _cock.transform.parent = transform;
  102. var comp = _cock.GetComponent<CockActionComp>();
  103. switch (comp.cockId)
  104. {
  105. case 1:
  106. _cock.transform.localPosition = new Vector3(0, 0.9f, 0.3f);
  107. break;
  108. case 2:
  109. _cock.transform.localPosition = new Vector3(0, 0.9f, 0.4f);
  110. break;
  111. default:
  112. _cock.transform.localPosition = new Vector3(0, 0.9f, 0.3f);
  113. break;
  114. }
  115. StartCoroutine(PutDown());
  116. }
  117. else
  118. {
  119. _cock.transform.parent = GameCore.Instance.parent.transform;
  120. _cock.transform.position = _liftPos;
  121. _cock.GetComponent<Rigidbody>().useGravity = true;
  122. _isPutDown = false;
  123. }
  124. }
  125. private IEnumerator PutDown()
  126. {
  127. yield return new WaitForSeconds(5f); // 5秒后放下来
  128. _isPutDown = true;
  129. _masterController.PutDown();
  130. }
  131. }
  132. }