2022-03-11 22:42:57 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 玩家的控制器部分,玩家主类必须继承这个类才能对控制做出反应
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class PlayerControl : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private PlayerC playerC;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 此帧输入方向,-1为左,1为右,0此帧不输入
|
|
|
|
|
/// </summary>
|
|
|
|
|
[SerializeField][ReadOnly][Header("此帧输入方向,-1为左,1为右,0表示此帧不输入")]
|
|
|
|
|
protected int inputDir;
|
2022-03-13 21:40:55 +08:00
|
|
|
|
protected PlayerInteract playerInteract;
|
2022-03-11 22:42:57 +08:00
|
|
|
|
|
2022-04-01 23:35:01 +08:00
|
|
|
|
protected Animator animator;
|
|
|
|
|
|
2022-03-12 14:54:57 +08:00
|
|
|
|
protected virtual void Start()
|
2022-03-11 22:42:57 +08:00
|
|
|
|
{
|
|
|
|
|
playerC = new PlayerC();
|
|
|
|
|
//playerC.Enable();
|
|
|
|
|
playerC.Normal.Enable();
|
|
|
|
|
//为事件订阅方法
|
|
|
|
|
//为移动操作订阅方法
|
|
|
|
|
playerC.Normal.Move.performed += ctx => OnMove(ctx);
|
|
|
|
|
playerC.Normal.Move.canceled += ctx => { inputDir = 0; };
|
|
|
|
|
//为攻击操作订阅方法
|
|
|
|
|
playerC.Normal.Atk.performed += ctx => OnAtk();
|
|
|
|
|
//为跳跃操作订阅方法
|
|
|
|
|
playerC.Normal.Jump.performed += ctx => OnJump();
|
|
|
|
|
//为交互操作订阅方法
|
|
|
|
|
playerC.Normal.Interact.performed += ctx => OnInteract();
|
2022-03-13 21:40:55 +08:00
|
|
|
|
//为左切换操作订阅方法
|
|
|
|
|
playerC.Normal.LeftChange.performed += ctx => OnLeftChange();
|
|
|
|
|
//为右切换操作订阅方法
|
|
|
|
|
playerC.Normal.RightChange.performed += ctx => OnRightChange();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//给玩家物体添加子物体:互动侦察器
|
|
|
|
|
GameObject checker = new GameObject("互动侦察器");//新建游戏物体:互动侦察器
|
|
|
|
|
checker.transform.SetParent(transform);//将其设置为玩家的子物体
|
|
|
|
|
playerInteract = checker.AddComponent<PlayerInteract>();//增加玩家互动侦察器组件
|
|
|
|
|
checker.AddComponent<BoxCollider2D>().isTrigger = true;//增加碰撞盒,并将其设置为触发器
|
|
|
|
|
checker.GetComponent<BoxCollider2D>().size = new Vector2(1f, 1f);//设置碰撞盒大小
|
|
|
|
|
checker.transform.localPosition = Vector3.zero;//设置碰撞盒位置
|
2022-03-15 00:38:35 +08:00
|
|
|
|
|
2022-04-01 23:35:01 +08:00
|
|
|
|
//找到动画机
|
|
|
|
|
animator = GetComponentInChildren<Animator>();
|
|
|
|
|
if (animator == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("玩家动画机丢失!");
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 22:42:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当有移动输入的时候触发,因为涉及读值,重些的时候记得必须Base
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void OnMove(InputAction.CallbackContext ctx){
|
|
|
|
|
//根据读值设置记录的输入方向
|
|
|
|
|
if(ctx.ReadValue<float>() > 0){
|
|
|
|
|
inputDir = 1;
|
|
|
|
|
}else if(ctx.ReadValue<float>() < 0){
|
|
|
|
|
inputDir = -1;
|
|
|
|
|
}
|
|
|
|
|
if(ctx.ReadValue<float>().Equals(0f)){
|
|
|
|
|
inputDir = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 按下攻击时触发
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void OnAtk(){}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 按下跳跃时触发
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void OnJump(){}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 按下交互时触发
|
|
|
|
|
/// </summary>
|
2022-03-13 21:40:55 +08:00
|
|
|
|
protected virtual void OnInteract(){
|
|
|
|
|
//如果有交互事件,则调用事件
|
|
|
|
|
playerInteract.OnCall();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 按下十字键左或者Q触发
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void OnLeftChange(){}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 按下十字键右或者E触发
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void OnRightChange(){}
|
2022-03-15 00:38:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 改变玩家的操作地图至参数值
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="mapName">要改到的操作地图的名称</param>
|
|
|
|
|
public void ToMap(string mapName)
|
|
|
|
|
{
|
|
|
|
|
//先关闭所有的
|
|
|
|
|
CloseAllMaps();
|
|
|
|
|
//再打开指定的
|
|
|
|
|
switch(mapName)
|
|
|
|
|
{
|
|
|
|
|
case "Normal":
|
|
|
|
|
playerC.Normal.Enable();
|
|
|
|
|
break;
|
|
|
|
|
case "Null" :
|
|
|
|
|
playerC.Null.Enable();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Debug.LogError("没有叫" + mapName + "的操作地图");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void CloseAllMaps()
|
|
|
|
|
{
|
|
|
|
|
//关闭所有的操作地图
|
|
|
|
|
playerC.Normal.Disable();
|
|
|
|
|
playerC.Null.Disable();
|
|
|
|
|
}
|
2022-04-01 23:35:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 触发玩家用触发器触发的动画
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="behaviorName">触发器名</param>
|
|
|
|
|
public void Behavior(string behaviorName)
|
|
|
|
|
{
|
|
|
|
|
try{
|
|
|
|
|
//触发动画
|
|
|
|
|
animator.SetTrigger(behaviorName);
|
|
|
|
|
}
|
|
|
|
|
catch(System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
e.ToString();
|
|
|
|
|
Debug.LogError("没有找到触发器" + behaviorName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2022-03-11 22:42:57 +08:00
|
|
|
|
}
|