CangJie/Assets/Scripts//PlayerControl.cs
Roman e10d588cf5 任务:编写操控地图
1.添加基本操作:移动、跳跃、交互和攻击的按键监听,编写好了父类,使用时只需要继承PlayerControl,然后重写里面的函数即可,注释和摘要齐全,如无必要请勿修改

我是每天上班提醒小助手,今天你上班了吗?😺
2022-03-11 22:42:57 +08:00

67 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
void Start()
{
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();
}
/// <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>
protected virtual void OnInteract(){}
}