MasterActionComp.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 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 _putdown = 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. for (float i = _miniY - 2f; i <= 2f; i += 0.05f) // 多次碰撞寻找自己的cock
  44. {
  45. var position = transform.position;
  46. position.y = _miniY; // 创建射线基础位置,y为脚底
  47. var ray = new Ray(position, transform.forward); // 创建前进射线
  48. if (Physics.Raycast(ray, out var hitInfo))
  49. {
  50. // 发生碰撞,处理碰撞事件
  51. Debug.Log("Hit object: " + hitInfo.collider.gameObject.name);
  52. _collisionX = hitInfo.point.x;
  53. if (hitInfo.collider.CompareTag("cock"))
  54. {
  55. var comp = hitInfo.collider.gameObject.GetComponent<CockActionComp>();
  56. if (comp.playerId != playerId)
  57. {
  58. Debug.Log("不是自己的cock");
  59. }
  60. else
  61. {
  62. _cock = hitInfo.collider.gameObject;
  63. break;
  64. }
  65. }
  66. else
  67. {
  68. Debug.Log("碰撞单位不是cock");
  69. }
  70. }
  71. else
  72. {
  73. Debug.LogWarning("无碰撞");
  74. }
  75. }
  76. }
  77. private void FixedUpdate()
  78. {
  79. var stateInfo = _animator.GetCurrentAnimatorStateInfo(0);
  80. if (stateInfo.IsTag("rwalk"))
  81. {
  82. if ((transform.forward.x > 0 && transform.position.x <= _initialX) ||
  83. (transform.forward.x < 0 && transform.position.x >= _initialX))
  84. {
  85. _masterController.Idle();
  86. GameCore.Instance.ReportMasterBack(transform.forward.x > 0);
  87. }
  88. else
  89. {
  90. transform.Translate(_rThrust * Time.deltaTime);
  91. }
  92. }
  93. else
  94. {
  95. transform.Translate(_thrust * Time.deltaTime);
  96. }
  97. // 锁定y轴旋转
  98. var eulerAngles = transform.eulerAngles;
  99. eulerAngles.y = _initialAnglesY;
  100. transform.eulerAngles = eulerAngles;
  101. // 抓鸡的时候操作一下
  102. if (transform.forward.x > 0)
  103. {
  104. if (_collisionX != 0 && transform.position.x >= _collisionX)
  105. {
  106. _thrust = Vector3.zero;
  107. _masterController.Lift();
  108. }
  109. }
  110. else
  111. {
  112. if (_collisionX != 0 && transform.position.x <= _collisionX)
  113. {
  114. _thrust = Vector3.zero;
  115. _masterController.Lift();
  116. }
  117. }
  118. }
  119. private void OnLowest() // 抱起动画最低点回调
  120. {
  121. if (!_putdown)
  122. {
  123. _liftPos = _cock.transform.position;
  124. _cock.transform.parent = transform;
  125. var comp = _cock.GetComponent<CockActionComp>();
  126. switch (comp.cockId)
  127. {
  128. case 1:
  129. _cock.transform.localPosition = new Vector3(0, 0.9f, 0.3f);
  130. break;
  131. case 2:
  132. _cock.transform.localPosition = new Vector3(0, 0.9f, 0.4f);
  133. break;
  134. default:
  135. _cock.transform.localPosition = new Vector3(0, 0.9f, 0.3f);
  136. break;
  137. }
  138. StartCoroutine(PutDown());
  139. }
  140. else
  141. {
  142. _cock.transform.parent = GameCore.Instance.parent.transform;
  143. _cock.transform.position = _liftPos;
  144. _putdown = false;
  145. }
  146. }
  147. private IEnumerator PutDown()
  148. {
  149. yield return new WaitForSeconds(5f); // 5秒后放下来
  150. _putdown = true;
  151. _masterController.PutDown();
  152. }
  153. }
  154. }