MasterActionComp.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. Debug.Log("lift cock");
  44. var actionComp = GameCore.Instance.GetCockActionCompByPlayerId(playerId);
  45. var cockObj = actionComp.gameObject;
  46. _cock = cockObj;
  47. if (playerId == GameCore.Instance.localPlayer.playerId)
  48. _collisionX = _cock.transform.position.x - 1.5f;
  49. else
  50. {
  51. _collisionX = _cock.transform.position.x + 1.5f;
  52. }
  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 = GameCore.Instance.GetCockActionCompByPlayerId(playerId);
  103. comp.CloseGravity = true;
  104. switch (comp.cockId)
  105. {
  106. case 1:
  107. comp.stayPos = new Vector3(0, 0.9f, 0.3f);
  108. break;
  109. case 2:
  110. comp.stayPos = new Vector3(0, 0.9f, 0.4f);
  111. break;
  112. default:
  113. comp.stayPos = new Vector3(0, 0.9f, 0.3f);
  114. break;
  115. }
  116. StartCoroutine(PutDown());
  117. }
  118. else
  119. {
  120. var comp = GameCore.Instance.GetCockActionCompByPlayerId(playerId);
  121. comp.stayPos = Vector3.zero;
  122. _cock.transform.parent = GameCore.Instance.parent.transform;
  123. _cock.transform.position = _liftPos;
  124. comp.OpenGravity = true;
  125. _isPutDown = false;
  126. }
  127. }
  128. private IEnumerator PutDown()
  129. {
  130. yield return new WaitForSeconds(5f); // 5秒后放下来
  131. _isPutDown = true;
  132. _masterController.PutDown();
  133. }
  134. }
  135. }