using System; using System.Collections; using Game; using UnityEngine; namespace Comp { public class MasterActionComp : MonoBehaviour { private Animator _animator; private IMasterController _masterController; public IMasterController MasterController => _masterController; // 水平运动相关 private Vector3 _thrust; private readonly Vector3 _rThrust = new Vector3(0, 0, -MoveSpeed); private const float MoveSpeed = 1f; private float _collisionX; private float _initialAnglesY; // 初始的Y角度 private float _initialX; // 锁定物体相关参数 private float _miniY = -0.6f; // 射线最低Y坐标 // 抱鸡参数 private GameObject _cock; private Vector3 _liftPos; private bool _isPutDown = false; public int playerId; private bool _startCheckPos = false; // 过了碰撞点再开始检查是否复位 private void Start() { _animator = GetComponent(); _initialAnglesY = transform.eulerAngles.y; _initialX = transform.position.x; _masterController = new AnimatorMasterController(_animator); _masterController.OnWalk += OnWalk; _masterController.Idle(); } private void OnWalk() { _thrust = new Vector3(0, 0, MoveSpeed); Debug.Log("start walk"); StartCoroutine(CockRayCast()); } private IEnumerator CockRayCast() // 判断碰撞点,现在不用射线了 { yield return new WaitForSeconds(1f); var actionComp = GameCore.Instance.GetCockActionCompByPlayerId(playerId); var cockObj = actionComp.gameObject; _cock = cockObj; if (playerId == GameCore.Instance.localPlayer.playerId) _collisionX = _cock.transform.position.x - 1.5f; else { _collisionX = _cock.transform.position.x + 1.5f; } } private void FixedUpdate() { var stateInfo = _animator.GetCurrentAnimatorStateInfo(0); if (stateInfo.IsTag("rwalk")) { if ((transform.forward.x > 0 && transform.position.x <= _initialX) || (transform.forward.x < 0 && transform.position.x >= _initialX)) { if (_startCheckPos) { _thrust = Vector3.zero; _masterController.Idle(); _startCheckPos = false; GameCore.Instance.ReportMasterBack(transform.forward.x > 0); } } else { transform.Translate(_rThrust * Time.deltaTime); } } else if (stateInfo.IsTag("walk")) { Debug.Log("move speed is " + _thrust); transform.Translate(_thrust * Time.deltaTime); } // 锁定y轴旋转 var eulerAngles = transform.eulerAngles; eulerAngles.y = _initialAnglesY; transform.eulerAngles = eulerAngles; // 抓鸡的时候操作一下 if (transform.forward.x > 0) { if (_collisionX != 0 && transform.position.x >= _collisionX) { _thrust = Vector3.zero; _startCheckPos = true; _masterController.Lift(); } } else { if (_collisionX != 0 && transform.position.x <= _collisionX) { _thrust = Vector3.zero; _startCheckPos = true; _masterController.Lift(); } } } private void OnLowest() // 抱起动画最低点回调 { if (!_isPutDown) { _liftPos = _cock.transform.position; _cock.transform.parent = transform; var comp = GameCore.Instance.GetCockActionCompByPlayerId(playerId); comp.CloseGravity = true; switch (comp.cockId) { case 1: comp.stayPos = new Vector3(0, 0.9f, 0.3f); break; case 2: comp.stayPos = new Vector3(0, 0.9f, 0.4f); break; default: comp.stayPos = new Vector3(0, 0.9f, 0.3f); break; } StartCoroutine(PutDown()); } else { var comp = GameCore.Instance.GetCockActionCompByPlayerId(playerId); comp.stayPos = Vector3.zero; _cock.transform.parent = GameCore.Instance.parent.transform; _cock.transform.position = _liftPos; comp.OpenGravity = true; _isPutDown = false; } } private IEnumerator PutDown() { yield return new WaitForSeconds(5f); // 5秒后放下来 _isPutDown = true; _masterController.PutDown(); } } }