123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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 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 _putdown = false;
- public int playerId;
- private void Start()
- {
- _animator = GetComponent<Animator>();
- _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);
- StartCoroutine(CockRayCast());
- }
- private IEnumerator CockRayCast()
- {
- yield return new WaitForSeconds(1f);
- for (float i = _miniY - 2f; i <= 2f; i += 0.05f) // 多次碰撞寻找自己的cock
- {
- var position = transform.position;
- position.y = _miniY; // 创建射线基础位置,y为脚底
- var ray = new Ray(position, transform.forward); // 创建前进射线
- if (Physics.Raycast(ray, out var hitInfo))
- {
- // 发生碰撞,处理碰撞事件
- Debug.Log("Hit object: " + hitInfo.collider.gameObject.name);
- _collisionX = hitInfo.point.x;
- if (hitInfo.collider.CompareTag("cock"))
- {
- var comp = hitInfo.collider.gameObject.GetComponent<CockActionComp>();
- if (comp.playerId != playerId)
- {
- Debug.Log("不是自己的cock");
- }
- else
- {
- _cock = hitInfo.collider.gameObject;
- break;
- }
- }
- else
- {
- Debug.Log("碰撞单位不是cock");
- }
- }
- else
- {
- Debug.LogWarning("无碰撞");
- }
- }
- }
- 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))
- {
- _masterController.Idle();
- GameCore.Instance.ReportMasterBack(transform.forward.x > 0);
- }
- else
- {
- transform.Translate(_rThrust * Time.deltaTime);
- }
- }
- else
- {
- 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;
- _masterController.Lift();
- }
- }
- else
- {
- if (_collisionX != 0 && transform.position.x <= _collisionX)
- {
- _thrust = Vector3.zero;
- _masterController.Lift();
- }
- }
- }
- private void OnLowest() // 抱起动画最低点回调
- {
- if (!_putdown)
- {
- _liftPos = _cock.transform.position;
- _cock.transform.parent = transform;
- var comp = _cock.GetComponent<CockActionComp>();
- switch (comp.cockId)
- {
- case 1:
- _cock.transform.localPosition = new Vector3(0, 0.9f, 0.3f);
- break;
- case 2:
- _cock.transform.localPosition = new Vector3(0, 0.9f, 0.4f);
- break;
- default:
- _cock.transform.localPosition = new Vector3(0, 0.9f, 0.3f);
- break;
- }
- StartCoroutine(PutDown());
- }
- else
- {
- _cock.transform.parent = GameCore.Instance.parent.transform;
- _cock.transform.position = _liftPos;
- _putdown = false;
- }
- }
- private IEnumerator PutDown()
- {
- yield return new WaitForSeconds(5f); // 5秒后放下来
- _putdown = true;
- _masterController.PutDown();
- }
- }
- }
|