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