123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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<CockMoveComp>();
- }
- private void Start()
- {
- _missPrefab = Resources.Load<GameObject>("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<MissText>().SetTarget(transform);
- }
- }
- }
|