using System; using System.Collections; using Game; using Sound; using UnityEngine; using UnityEngine.UI; namespace Comp { public class CockActionComp : MonoBehaviour { private const string CockTag = "cock"; // 动画控制器 private ICockController _cockController; // 移动组件 private CockMoveComp _cockMoveComp; private bool _highJump; // 是否高跳 // 跑步跟切换待机状态相关参数 private bool _firstTrigger = true; // 第一次碰撞切换为待机 // 执行动作相关 public Action curAction; private readonly WaitForSeconds _waitEverySecond = new WaitForSeconds(1f); private Rigidbody _rigidbody; // 水平与垂直方向运动相关 public int playerId; public int cockId; public bool rightForward = false; // miss文本相关参数 private GameObject _missPrefab; private bool _createMiss = false; public bool willBeLift = false; public ICockController CockController { get { if (_cockController != null) return _cockController; _cockController = new AnimatorCockController(animator); _cockController.OnRun += OnRun; _cockController.OnIdle += OnIdle; _cockController.OnAttack += OnAttack; _cockController.OnJumpAttack += OnJumpAttack; return _cockController; } } public Animator animator; private void OnCollisionEnter(Collision collision) { if (!collision.gameObject.CompareTag(CockTag) || !_firstTrigger) return; _firstTrigger = false; _cockController.IdleInBattle(); GameCore.Instance.GetCurState().BattleStart(); } private void Awake() { _cockMoveComp = GetComponent(); _rigidbody = GetComponent(); } private void Start() { _missPrefab = Resources.Load("Prefab/prefab_miss"); StartCoroutine(CallEverySeconds()); } private IEnumerator CallEverySeconds() { while (true) { curAction?.Invoke(); curAction = null; yield return _waitEverySecond; } } private void Update() { if (!_createMiss) return; _createMiss = false; CreateMissText(); } private void OnRun() { _cockMoveComp.MoveAndNeverStop(); } private void OnIdle() { _cockMoveComp.Stop(); } private void OnAttack() { SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId); _cockMoveComp.Move(); } private void OnJumpAttack() { SoundCore.Instance.PlaySound(SoundType.CockJumpAttack, "player-" + playerId); _cockMoveComp.Move(); _cockMoveComp.Jump(_highJump); } public void OnAttackEnd() { if (willBeLift) { _rigidbody.useGravity = false; } } public void OnJumpEnd() { if (willBeLift) { _rigidbody.useGravity = false; } } public void SetHighJump(bool high) { _highJump = high; } public void CreateMiss() { _createMiss = true; } // ReSharper disable Unity.PerformanceAnalysis private void CreateMissText() { var obj = Instantiate(_missPrefab); obj.GetComponent().SetTarget(transform); } } }