Files
gold_dolphin/unity/Assets/Script/GameObject/Player/PlayerControler.cs
2026-06-20 19:35:25 +08:00

211 lines
4.7 KiB
C#

using Manager;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerControler : MonoBehaviour
{
[SerializeField] private InputActions inputActions;
[SerializeField] public Animator animator;
[SerializeField] private Rigidbody2D rb;
[SerializeField] private BoxCollider2D bc;
private CameraVR cameraVR;
[Header("正常速度")]
[SerializeField] private float normalSpeed;
Vector2 InputDir;//移动方向
public float dodgeCoolDown;//闪避Cd时间
public float dodgeSpeed;
private float dodge;
private bool isCanDodge;
private bool isWuDi;
public float wuDiTimer;
public CharacterCombat ch;
public CharacterHp info;
private void Awake()
{
inputActions = new InputActions();
cameraVR = GameObject.FindWithTag("Camera").GetComponentInChildren<CameraVR>();
}
private void Start()
{
inputActions.GamePlay.ComBa_01.started += this.Attack_01;
inputActions.GamePlay.ComBa_02.started += this.Attack_02;
inputActions.GamePlay.Dodge.started += this.Dodge;
inputActions.UI.Stop.started += this.OnStop;
inputActions.UI.Bag.started += this.OpenBag;
dodge = dodgeCoolDown;
isCanDodge = true;
ManagersMode.Poll.CreateGameObjectPoll("Timer", 10);
ManagersMode.Poll.CreateGameObjectPoll("BulletPoll", 3);
ManagersMode.Poll.CreateGameObjectPoll("EffectPoll", 2);
info.Init(ch);
}
private void Update()
{
if(ch.currentHp<=0)
{
UIManager.Instance.Show<UIGameOver>();
ch._animator.Play("die");
}
if(!animator.CheckAnimationTag("Roll"))
{
animator.speed = 1;
}
else
{
this.transform.Translate(InputDir*dodgeSpeed*0.01f);//闪避移动
}
CanDodge();
UseItem();
}
private void OnEnable()
{
inputActions.Enable();
}
private void OnDisable()
{
inputActions.Disable();
}
private void FixedUpdate()
{
if(!animator.CheckAnimationTag("Die"))
{
this.MoveController();
}
else
{
this.rb.velocity =Vector2.zero;
}
}
#region
private void MoveController()
{
InputDir = inputActions.GamePlay.Move.ReadValue<Vector2>().normalized;
animator.SetFloat("Horizontal", InputDir.x);
animator.SetFloat("Vertical", InputDir.y);
animator.SetFloat("Speed", InputDir.sqrMagnitude);
this.rb.velocity = InputDir * normalSpeed;
if (InputDir.x < 0)//向左
{
transform.localScale = new Vector3(1, 1, 1);
}
if (InputDir.x > 0)//向左
{
transform.localScale = new Vector3(-1, 1, 1);
}
}
private void OnStop(InputAction.CallbackContext obj)
{
UIManager.Instance.Show<UIGameOver>();
}
#endregion
/// <summary>
/// 角色攻击
/// </summary>
private void Attack_01(InputAction.CallbackContext obj)
{
animator.SetTrigger("Attack_01");
}
private void Attack_02(InputAction.CallbackContext obj)
{
animator.SetTrigger("Attack_02");
}
private bool CanDodge()
{
if(isCanDodge)
{
return true;
}
dodgeCoolDown -= Time.deltaTime;
if(dodgeCoolDown<=0)
{
isCanDodge = true;
dodgeCoolDown = dodge;
return true;
}
return false;
}
/// <summary>
/// 角色闪避
/// </summary>
private void Dodge(InputAction.CallbackContext obj)
{
//||animator.CheckAnimationTag("Hit")
if (!CanDodge())
{
return;
}
StartCoroutine(nameof(SetWuDi));//无敌状态
isCanDodge = false;
animator.speed = 1f;
if (InputDir.x!=0)
{
animator.Play("roll");
}
if (InputDir.y > 0)
{
animator.Play("roll-up");
}
if (InputDir.y < 0)
{
animator.Play("roll-down");
}
}
IEnumerator SetWuDi()
{
isWuDi = true;
yield return new WaitForSeconds(wuDiTimer);
isWuDi = false;
}
public bool GetIsDodge()
{
return isWuDi;
}
private void OpenBag(InputAction.CallbackContext obj)
{
UIManager.Instance.Show<UIBagInfo>();
}
public void UseItem()
{
if(Input.GetKeyDown(KeyCode.Alpha1))
{
PlayerInfo.Instance.BagMgr.UserItem(1, 1);//回血道具
}
}
}