
1.编写交互操作逻辑,我会用脚本给玩家物体添加一个子物体,子物体有一个触发器,当玩家按下交互键,通过回调看是否有Catch到的可交互物体,若有,我会触发可交互物体的OnCall,若没有,则不做反应。
2.编写可交互物体基类,可交互物体继承自Event,Start时检查碰撞盒状态。当检查到玩家进入触发器,把自己交给玩家的Catch。当检查到玩家离开触发器,看目前的Catch是否和自己一样,若一样则清除Catch,若不一样说明已经Catch了其他东西,不做反应。
3.编写绳结类,继承自可交互物体。当交互,给中介者发送信息,让中介者更改记录的当前记录的绳结是哪一个
(1.内含一个来自中介者的枚举类型的变量,记录自己属于哪一种绳结
(2.当OnCall,把自己的类型发给中介者
4.编写绳结中介者,负责玩家、皇帝和绳结的信息交流。
(1.有一个枚举类型,内含三种绳结种类
(2.有一个枚举类型的变量,记录当前记录的是哪一个绳结
(3.当绳结发来信息,更新记录的当前绳结
5.添加新的按键监听,并增加PlayerControl的虚函数
6.编写黄帝类
(1.继承自可交互物体
(2.内含一个来自中介者的枚举类型,记录皇帝对哪一种绳结提出要求
(3.当OnCall,检查中介者中记录的绳结和要求的是不是同一种,若是则触发后续流程,若不是,则触发摇头等动作,目前不做反应
(4.内含函数与后期外界对接,用来指定皇帝需要的绳结类型
*记得给Player类的Interact函数加上Base
我是每天上班提醒小助手,今天你上班了吗?😺
93 lines
3.2 KiB
C#
93 lines
3.2 KiB
C#
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;
|
||
protected PlayerInteract playerInteract;
|
||
|
||
protected virtual 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();
|
||
//为左切换操作订阅方法
|
||
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;//设置碰撞盒位置
|
||
}
|
||
|
||
/// <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(){
|
||
//如果有交互事件,则调用事件
|
||
playerInteract.OnCall();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 按下十字键左或者Q触发
|
||
/// </summary>
|
||
protected virtual void OnLeftChange(){}
|
||
/// <summary>
|
||
/// 按下十字键右或者E触发
|
||
/// </summary>
|
||
protected virtual void OnRightChange(){}
|
||
}
|