using System; using System.Collections; using Game; using Sound; using UnityEngine; 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; public int randomAttack = 0; private readonly WaitForSeconds _waitEverySecond = new WaitForSeconds(1f); private Rigidbody _rigidbody; public bool OpenGravity = false; public bool CloseGravity = false; public Vector3 stayPos = Vector3.zero; // 水平与垂直方向运动相关 public int playerId; public int cockId; public bool rightForward = false; // miss文本相关参数 private GameObject _missPrefab; private bool _createMiss = 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) { if (curAction == CockController.JumpAndAttack) { if (randomAttack == 0) { curAction?.Invoke(); } else { if (randomAttack == 4) { Debug.Log("random attack=====" + randomAttack); } CockController.RandomAttack(randomAttack); randomAttack = 0; } } else { curAction?.Invoke(); } curAction = null; yield return _waitEverySecond; } } private void Update() { if (_createMiss) { CreateMissText(); _createMiss = false; } } private void FixedUpdate() { if (CloseGravity) { _rigidbody.useGravity = false; if (stayPos != Vector3.zero) transform.localPosition = stayPos; } if (OpenGravity) { CloseGravity = false; stayPos = Vector3.zero; OpenGravity = false; _rigidbody.useGravity = true; _rigidbody.velocity = Vector3.zero; } } 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() { } public void OnJumpEnd() { } 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); } } }