MasterActionComp.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 bool _startCheckPos = false; // 过了碰撞点再开始检查是否复位
  27. private void Start()
  28. {
  29. _animator = GetComponent<Animator>();
  30. _initialAnglesY = transform.eulerAngles.y;
  31. _initialX = transform.position.x;
  32. _masterController = new AnimatorMasterController(_animator);
  33. _masterController.OnWalk += OnWalk;
  34. _masterController.Idle();
  35. }
  36. private void OnWalk()
  37. {
  38. _thrust = new Vector3(0, 0, MoveSpeed);
  39. Debug.Log("start walk");
  40. StartCoroutine(CockRayCast());
  41. }
  42. private IEnumerator CockRayCast() // 判断碰撞点,现在不用射线了
  43. {
  44. yield return new WaitForSeconds(1f);
  45. var actionComp = GameCore.Instance.GetCockActionCompByPlayerId(playerId);
  46. var cockObj = actionComp.gameObject;
  47. _cock = cockObj;
  48. if (playerId == GameCore.Instance.localPlayer.playerId)
  49. _collisionX = _cock.transform.position.x - 1.5f;
  50. else
  51. {
  52. _collisionX = _cock.transform.position.x + 1.5f;
  53. }
  54. }
  55. private void FixedUpdate()
  56. {
  57. var stateInfo = _animator.GetCurrentAnimatorStateInfo(0);
  58. if (stateInfo.IsTag("rwalk"))
  59. {
  60. if ((transform.forward.x > 0 && transform.position.x <= _initialX) ||
  61. (transform.forward.x < 0 && transform.position.x >= _initialX))
  62. {
  63. if (_startCheckPos)
  64. {
  65. _thrust = Vector3.zero;
  66. _masterController.Idle();
  67. _startCheckPos = false;
  68. GameCore.Instance.ReportMasterBack(transform.forward.x > 0);
  69. }
  70. }
  71. else
  72. {
  73. transform.Translate(_rThrust * Time.deltaTime);
  74. }
  75. }
  76. else if (stateInfo.IsTag("walk"))
  77. {
  78. Debug.Log("move speed is " + _thrust);
  79. transform.Translate(_thrust * Time.deltaTime);
  80. }
  81. // 锁定y轴旋转
  82. var eulerAngles = transform.eulerAngles;
  83. eulerAngles.y = _initialAnglesY;
  84. transform.eulerAngles = eulerAngles;
  85. // 抓鸡的时候操作一下
  86. if (transform.forward.x > 0)
  87. {
  88. if (_collisionX != 0 && transform.position.x >= _collisionX)
  89. {
  90. _thrust = Vector3.zero;
  91. _startCheckPos = true;
  92. _masterController.Lift();
  93. }
  94. }
  95. else
  96. {
  97. if (_collisionX != 0 && transform.position.x <= _collisionX)
  98. {
  99. _thrust = Vector3.zero;
  100. _startCheckPos = true;
  101. _masterController.Lift();
  102. }
  103. }
  104. }
  105. private void OnLowest() // 抱起动画最低点回调
  106. {
  107. if (!_isPutDown)
  108. {
  109. _liftPos = _cock.transform.position;
  110. _cock.transform.parent = transform;
  111. var comp = GameCore.Instance.GetCockActionCompByPlayerId(playerId);
  112. comp.CloseGravity = true;
  113. switch (comp.cockId)
  114. {
  115. case 1:
  116. comp.stayPos = new Vector3(0, 0.9f, 0.3f);
  117. break;
  118. case 2:
  119. comp.stayPos = new Vector3(0, 0.9f, 0.4f);
  120. break;
  121. default:
  122. comp.stayPos = new Vector3(0, 0.9f, 0.3f);
  123. break;
  124. }
  125. StartCoroutine(PutDown());
  126. }
  127. else
  128. {
  129. var comp = GameCore.Instance.GetCockActionCompByPlayerId(playerId);
  130. comp.stayPos = Vector3.zero;
  131. _cock.transform.parent = GameCore.Instance.parent.transform;
  132. _cock.transform.position = _liftPos;
  133. comp.OpenGravity = true;
  134. _isPutDown = false;
  135. }
  136. }
  137. private IEnumerator PutDown()
  138. {
  139. yield return new WaitForSeconds(5f); // 5秒后放下来
  140. _isPutDown = true;
  141. _masterController.PutDown();
  142. }
  143. }
  144. }