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); // 水平与垂直方向运动相关 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(); } 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 FixedUpdate() { // transform.Translate(_thrust * Time.deltaTime); // 移动游戏对象 // if (GameCore.Instance.inBattleState) // transform.position += _curData.GetJumpVector(); // if (GameCore.Instance.inBattleState) // { // if (rightForward) // { // transform.position += _curData.GetMoveVector(); // } // else // { // transform.position -= _curData.GetMoveVector(); // } // } // if (transform.position.y < _miniY) // { // var position = transform.position; // position = new Vector3(position.x, _miniY, position.z); // transform.position = position; // } } 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); } } }