当前位置: 首页 > article >正文

RPG项目01_技能释放

基于“RPG项目01_新输入输出”,

修改脚本文件夹中的SkillBase脚本:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

//回复技能,魔法技能,物理技能
public enum SkillType { Up, Magic, Physics };
public class SkillBase : MonoBehaviour{
    protected GameObject prefab;
    protected SkillType type;//类型
    protected string path = "Skill/";
    protected string skillName;
    protected int attValue;
    public int mp;
    protected People from;//从人物释放
    protected People tPeople;//敌人目标
    protected Dictionary<SkillType, UnityAction> typeWithSkill =
        new Dictionary<SkillType, UnityAction>();
    protected float time = 0;//计时器
    protected float skillTime;//技能冷却时间
    protected float delayTime;//延迟时间
    protected float offset;//偏移量
    protected Vector3 skillPoint;//攻击点
    public bool IsRelease { get; private set; }//技能是否释放
    protected int layer;//技能对哪个一层起作用
    protected Collider[] cs;//技能碰撞体
    public event UnityAction Handle;//处理方法
    protected void Init(){
        IsRelease = true;
        layer = LayerMask.GetMask("MyPlayer") + LayerMask.GetMask("Enemy");
        prefab = LoadManager.LoadGameObject(path + skillName);
        typeWithSkill.Add(SkillType.Magic, MagicHurt);
        typeWithSkill.Add(SkillType.Physics, PhysicsHurt);
        typeWithSkill.Add(SkillType.Up, HpUp);
    }
    #region 技能
    private void MagicHurt(){
        cs = GetColliders();
        foreach (Collider c in cs){
            if (c.tag != from.tag){
                c.GetComponent<People>().BeMagicHit(attValue, from);
                c.GetComponent<People>().Anim.SetTrigger("Hurt");
            }
        }
    }
    private void PhysicsHurt(){
        cs = GetColliders();
        foreach (Collider c in cs){
            if (c.tag != from.tag){
                c.GetComponent<People>().BePhysicsHit(attValue, from);
                c.GetComponent<People>().Anim.SetTrigger("Hurt");
            }
        }
    }
    private void HpUp(){
        cs = GetColliders();
        foreach (Collider c in cs){
            if (c.tag == from.tag){
                c.GetComponent<People>().AddHp(attValue);
            }
        }
    }
    #endregion
    protected virtual Collider[] GetColliders(){
        return null;
    }
    public float GetFillTime(){
        if (IsRelease){
            return 0;
        }
        time -= Time.deltaTime;
        if (time < 0){
            IsRelease = true;
        }
        return time / skillTime;
    }
    public void SetEventHandle(UnityAction fun){
        Handle += fun;
    }
    public bool MayRelease(){
        return (from.Mp + mp >= 0) && IsRelease;
    }
    //携程函数-技能释放执行
    public virtual IEnumerator SkillPrefab(){
        IsRelease = false;
        time = skillTime;
        yield return new WaitForSeconds(delayTime);//等待延迟时间
        typeWithSkill[type]();
        from.AddMp(mp);
        skillPoint = from.transform.position + from.transform.forward * offset;
        Quaternion qua = from.transform.rotation;//施放角度
        GameObject effect = Instantiate(prefab, skillPoint, qua);
        yield return new WaitForSeconds(3);//等待三秒
        Handle?.Invoke();//时间调用
        Destroy(effect);//特效销毁
    }
}
技能基类创建完后创建技能子类StraightSkill

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StraightSkill : SkillBase
{//直线技能
    float length;
    float width;
    public StraightSkill(People p, string _name, SkillType _type,
        float _length, float _width, int _att, int _mp, float _skillTime, float _delayTime, float _offset){
        from = p;
        length = _length;
        width = _width;
        skillName = _name;
        mp = _mp;
        type = _type;
        attValue = _att;
        skillTime = _skillTime;
        delayTime = _delayTime;
        offset = _offset;
        Init();
    }
    protected override Collider[] GetColliders(){
        skillPoint = from.transform.position + from.transform.forward * offset;
        Vector3 bound = new Vector3(width * width, length);
        return Physics.OverlapBox(skillPoint, bound, Quaternion.LookRotation(from.transform.forward), layer);
    }
}
再创建球型子类:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class SphereSkill : SkillBase
{//球型技能
    float r;
    public SphereSkill(People p, string _name, SkillType _type, float _r,
        int _att, int _mp, float _skillTime, float _delayTime, float _offset){
        from = p;
        r = _r;
        skillName = _name;
        mp = _mp;
        type = _type;
        attValue = _att;
        skillTime = _skillTime;
        delayTime = _delayTime;
        offset = _offset;
        Init();
    }
    protected override Collider[] GetColliders(){
        skillPoint = from.transform.position + from.transform.forward * offset;
        return Physics.OverlapSphere(skillPoint, r, layer);
    }
}

接下来在角色类中修改代码添加技能:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class MyPlayer : People{
    [Header("==============子类变量==============")]
    public Transform toolPanel;//道具面板
    public Transform skillPanel;//技能面板
    //public BagPanel bag;//背包

    CharacterController contro;
    Controls action;

    float rvalue;
    float spdFast = 1;
    bool isHold;//握刀
    GameObject sword;
    GameObject swordBack;
    public Image imageHp;
    public Image imageMp;
    new void Start() {
        base.Start();
        //获取自身角色控制器
        contro = GetComponent<CharacterController>();
        SetInput();
    }
    void SetInput(){
        action = new Controls();
        action.Enable();
        action.MyCtrl.Move.started += Move;
        action.MyCtrl.Move.performed += Move;
        action.MyCtrl.Move.canceled += StopMove;
        action.MyCtrl.Jump.started += Jump;
        action.MyCtrl.Rotate.started += Rotate;
        action.MyCtrl.Rotate.performed += Rotate;
        action.MyCtrl.Fast.started += FastSpeed;
        action.MyCtrl.Fast.performed += FastSpeed;
        action.MyCtrl.Fast.canceled += FastSpeed;
        action.MyCtrl.GetTool.started += ClickNpcAndTool;
        action.MyCtrl.HoldRotate.performed += Hold;
        action.MyCtrl.HoldRotate.canceled += Hold;
        action.MyAtt.Att.started += Attack;
        action.MyAtt.SwordOut.started += SwordOut;
    }
    private void SwordOut(InputAction.CallbackContext context)
    {
        if (GameManager.gameState != GameState.Play)
        {
            return;
        }
        Anim.SetBool("SwordOut", !Anim.GetBool("SwordOut"));
    }
    private void Attack(InputAction.CallbackContext context)
    {
        if (GameManager.gameState != GameState.Play)
        {
            return;
        }
        if (EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight"))
        {
            Anim.SetInteger("Att", 1);
            Anim.SetTrigger("AttTrigger");

        }
        else
        {
            int num = Anim.GetInteger("Att");
            if (num == 6)
            {
                return;
            }
            if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Light_Attk_" + num))
            {
                Anim.SetInteger("Att", num + 1);
            }
        }
    }
    public void PlayerAttack(string hurt)
    {
        Collider[] cs = Physics.OverlapBox(attPoint.position, Vector3.one * 0.5f,
            attPoint.rotation, LayerMask.GetMask("Enemy"));
        if (cs.Length <= 0)
        {
            return;
        }
        int value = (int)(Att * Anim.GetInteger("Att") * 0.5f);
        foreach (Collider c in cs)
        {
            print(value);
        }
    }

    public void PlayerAttackHard(string hurt)
    {
        Collider[] cs = Physics.OverlapBox(attPoint.position, Vector3.one * 0.5f,
            attPoint.rotation, LayerMask.GetMask("Enemy"));
        if (cs.Length <= 0)
        {
            return;
        }
        int value = (int)(Att * Anim.GetInteger("Att") * 0.5f);
        foreach (Collider c in cs)
        {
            print(value);
            print("让敌人播放击倒特效");
        }
    }
    private void Hold(InputAction.CallbackContext context)
    {
        if (GameManager.gameState != GameState.Play)
        {
            return;
        }
        if (context.phase == InputActionPhase.Canceled)
        {
            isHold = false;
        }
        else
        {
            isHold = true;
        }
    }
    private void ClickNpcAndTool(InputAction.CallbackContext context)
    {
        //throw new NotImplementedException();
    }
    private void FastSpeed(InputAction.CallbackContext context)
    {
        if (GameManager.gameState != GameState.Play)
        {
            return;
        }
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Run") ||
            Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_Inplace"))
        {
            if (context.phase == InputActionPhase.Canceled)
            {
                spdFast = 1;
            }
            else
            {
                spdFast = 2;
            }
        }
    }
    private void Rotate(InputAction.CallbackContext context)
    {
        if (GameManager.gameState != GameState.Play)
        {
            return;
        }
        rvalue = context.ReadValue<float>();
    }
    private void Jump(InputAction.CallbackContext obj){
        Anim.SetTrigger("Jump");
    }

    private void StopMove(InputAction.CallbackContext context){
        Anim.SetBool("IsRun", false);
    }
    private void Move(InputAction.CallbackContext context){
        if (GameManager.gameState != GameState.Play) {
            return;
        }
        Anim.SetBool("IsRun", true);
    }
    void Ctrl()
    {
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Run") ||
            Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_Inplace") ||
            Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle")||
            Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight")){
            float f = action.MyCtrl.Move.ReadValue<float>();
            contro.Move(transform.forward * f * Time.deltaTime * spdFast * Spd);
            contro.Move(transform.up * -9.8f * Time.deltaTime);
            if (isHold)
            {
                transform.Rotate(transform.up * rvalue * 0.3f);
            }
        }
    }
    void Update()
    {
        if (GameManager.gameState != GameState.Play)
        {
            return;
        }
        Ctrl();
    }
    #region 技能
    protected override void InitSkill()
    {
        SphereSkill thunderBombCut = new SphereSkill(this, "雷爆斩", SkillType.Magic, 3, Att * 10, -100, 20, 0.3f, 0);
        skills.Add(1, thunderBombCut);

        SphereSkill windCircleCut = new SphereSkill(this, "旋风斩", SkillType.Physics, 3, Att * 8, -50, 20, 0.2f, 0);
        skills.Add(2, windCircleCut);

        StraightSkill thunderLightCut = new StraightSkill(this, "雷光斩", SkillType.Physics, 7, 0.5f, Att * 7, -50, 20, 0.2f, 0);
        skills.Add(3, thunderLightCut);

        SphereSkill oneCut = new SphereSkill(this, "归一斩", SkillType.Physics, 7, Att * 7, -30, 8, 0.2f, 0);
        skills.Add(4, oneCut);

        StraightSkill crossCut = new StraightSkill(this, "十字斩", SkillType.Physics, 25, 0.5f, Att * 3, -40, 20, 0.2f, 0);
        skills.Add(5, crossCut);


        SphereSkill thunderLargeCut = new SphereSkill(this, "轰雷斩", SkillType.Magic, 7, Att * 15, -120, 25, 0.35f, 0);
        skills.Add(6, thunderLargeCut);
    }
    #endregion
}
在新输入系统中添加Skill文件包

在People基类添加基类

修改MyPlayer代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class MyPlayer : People{
    [Header("==============子类变量==============")]
    public Transform toolPanel;//道具面板
    public Transform skillPanel;//技能面板
    //public BagPanel bag;//背包

    CharacterController contro;
    Controls action;

    float rvalue;
    float spdFast = 1;
    bool isHold;//握刀
    GameObject sword;
    GameObject swordBack;
    public Image imageHp;
    public Image imageMp;
    new void Start() {
        base.Start();
        //获取自身角色控制器
        contro = GetComponent<CharacterController>();
        SetInput();
    }
    void SetInput(){
        action = new Controls();
        action.Enable();
        action.MyCtrl.Move.started += Move;
        action.MyCtrl.Move.performed += Move;
        action.MyCtrl.Move.canceled += StopMove;
        action.MyCtrl.Jump.started += Jump;
        action.MyCtrl.Rotate.started += Rotate;
        action.MyCtrl.Rotate.performed += Rotate;
        action.MyCtrl.Fast.started += FastSpeed;
        action.MyCtrl.Fast.performed += FastSpeed;
        action.MyCtrl.Fast.canceled += FastSpeed;
        action.MyCtrl.GetTool.started += ClickNpcAndTool;
        action.MyCtrl.HoldRotate.performed += Hold;
        action.MyCtrl.HoldRotate.canceled += Hold;

        action.MyAtt.Att.started += Attack;
        action.MyAtt.SwordOut.started += SwordOut;

        action.Skill.F1.started += SkillAtt;
        action.Skill.F2.started += SkillAtt;
        action.Skill.F3.started += SkillAtt;
        action.Skill.F4.started += SkillAtt;
        action.Skill.F5.started += SkillAtt;
        action.Skill.F6.started += SkillAtt;
    }

    private void SkillAtt(InputAction.CallbackContext context)
    {
        if (!Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight"))
        {
            return;
        }
        string[] str = context.control.ToString().Split('/');
        int num = int.Parse(str[2][1].ToString());
        SkillBase skill = skills[num];
        if (!skill.MayRelease())
        {
            return;
        }
        Anim.SetTrigger("CSkill" + num);
        ReleaseSkill(skill);
    }

    public void SkillClick(int num)
    {
        if (!Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight"))
        {
            return;
        }
        SkillBase skill = skills[num];
        if (!skill.MayRelease())
        {
            return;
        }
        Anim.SetTrigger("CSkill" + num);
        ReleaseSkill(skill);
    }

    void UpdateSkillTime()
    {
        for (int i = 0; i < skillPanel.childCount; i++)
        {
            if (skills[i + 1].IsRelease)
            {
                continue;
            }
            Image image = skillPanel.GetChild(i).GetChild(0).GetComponent<Image>();
            image.fillAmount = skills[i + 1].GetFillTime();
        }
    }

    private void SwordOut(InputAction.CallbackContext context)
    {
        if (GameManager.gameState != GameState.Play)
        {
            return;
        }
        Anim.SetBool("SwordOut", !Anim.GetBool("SwordOut"));
    }
    private void Attack(InputAction.CallbackContext context)
    {
        if (GameManager.gameState != GameState.Play)
        {
            return;
        }
        if (EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight"))
        {
            Anim.SetInteger("Att", 1);
            Anim.SetTrigger("AttTrigger");

        }
        else
        {
            int num = Anim.GetInteger("Att");
            if (num == 6)
            {
                return;
            }
            if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Light_Attk_" + num))
            {
                Anim.SetInteger("Att", num + 1);
            }
        }
    }
    public void PlayerAttack(string hurt)
    {
        Collider[] cs = Physics.OverlapBox(attPoint.position, Vector3.one * 0.5f,
            attPoint.rotation, LayerMask.GetMask("Enemy"));
        if (cs.Length <= 0)
        {
            return;
        }
        int value = (int)(Att * Anim.GetInteger("Att") * 0.5f);
        foreach (Collider c in cs)
        {
            print(value);
        }
    }

    public void PlayerAttackHard(string hurt)
    {
        Collider[] cs = Physics.OverlapBox(attPoint.position, Vector3.one * 0.5f,
            attPoint.rotation, LayerMask.GetMask("Enemy"));
        if (cs.Length <= 0)
        {
            return;
        }
        int value = (int)(Att * Anim.GetInteger("Att") * 0.5f);
        foreach (Collider c in cs)
        {
            print(value);
            print("让敌人播放击倒特效");
        }
    }
    private void Hold(InputAction.CallbackContext context)
    {
        if (GameManager.gameState != GameState.Play)
        {
            return;
        }
        if (context.phase == InputActionPhase.Canceled)
        {
            isHold = false;
        }
        else
        {
            isHold = true;
        }
    }
    private void ClickNpcAndTool(InputAction.CallbackContext context)
    {
        //throw new NotImplementedException();
    }
    private void FastSpeed(InputAction.CallbackContext context)
    {
        if (GameManager.gameState != GameState.Play)
        {
            return;
        }
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Run") ||
            Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_Inplace"))
        {
            if (context.phase == InputActionPhase.Canceled)
            {
                spdFast = 1;
            }
            else
            {
                spdFast = 2;
            }
        }
    }
    private void Rotate(InputAction.CallbackContext context)
    {
        if (GameManager.gameState != GameState.Play)
        {
            return;
        }
        rvalue = context.ReadValue<float>();
    }
    private void Jump(InputAction.CallbackContext obj){
        Anim.SetTrigger("Jump");
    }

    private void StopMove(InputAction.CallbackContext context){
        Anim.SetBool("IsRun", false);
    }
    private void Move(InputAction.CallbackContext context){
        if (GameManager.gameState != GameState.Play) {
            return;
        }
        Anim.SetBool("IsRun", true);
    }
    void Ctrl()
    {
        if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Run") ||
            Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_Inplace") ||
            Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle")||
            Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight")){
            float f = action.MyCtrl.Move.ReadValue<float>();
            contro.Move(transform.forward * f * Time.deltaTime * spdFast * Spd);
            contro.Move(transform.up * -9.8f * Time.deltaTime);
            if (isHold)
            {
                transform.Rotate(transform.up * rvalue * 0.3f);
            }
        }
    }
    void Update()
    {
        if (GameManager.gameState != GameState.Play)
        {
            return;
        }
        Ctrl();
    }
    #region 技能
    protected override void InitSkill()
    {
        SphereSkill thunderBombCut = new SphereSkill(this, "雷爆斩", SkillType.Magic, 3, Att * 10, -100, 20, 0.3f, 0);
        skills.Add(1, thunderBombCut);

        SphereSkill windCircleCut = new SphereSkill(this, "旋风斩", SkillType.Physics, 3, Att * 8, -50, 20, 0.2f, 0);
        skills.Add(2, windCircleCut);

        StraightSkill thunderLightCut = new StraightSkill(this, "雷光斩", SkillType.Physics, 7, 0.5f, Att * 7, -50, 20, 0.2f, 0);
        skills.Add(3, thunderLightCut);

        SphereSkill oneCut = new SphereSkill(this, "归一斩", SkillType.Physics, 7, Att * 7, -30, 8, 0.2f, 0);
        skills.Add(4, oneCut);

        StraightSkill crossCut = new StraightSkill(this, "十字斩", SkillType.Physics, 25, 0.5f, Att * 3, -40, 20, 0.2f, 0);
        skills.Add(5, crossCut);


        SphereSkill thunderLargeCut = new SphereSkill(this, "轰雷斩", SkillType.Magic, 7, Att * 15, -120, 25, 0.35f, 0);
        skills.Add(6, thunderLargeCut);
    }
    #endregion
}
修改MyPlayer代码:

将技能预制体包导入:

运行即可释放技能:

按E拔刀后按F1/F2/F3/F4/F5/F6键释放技能

同一个技能不能连续释放是因为冷却,

如果不同技能释放一个后另一个释放不了是因为mp不够,

设置mp初始及mp最大容量

即可释放完F1技能后还可以释放F2/F3/F4/F5/F6技能:


http://www.kler.cn/a/157033.html

相关文章:

  • 管家婆财贸ERP BB059.银行流水导入对账
  • 【Playwright + Python】系列(十)利用 Playwright 完美处理 Dialogs 对话框
  • ODOO学习笔记(3):Odoo和Django的区别是什么?
  • uniapp分享功能
  • CAN总线数据帧格式详细介绍
  • day60 图论章节刷题Part10(Floyd 算法、A * 算法)
  • Flask Echarts 实现历史图形查询
  • [Swift]RxSwift常见用法详解
  • 联想LJ2655DN激光打印机清零方法
  • Django二转Day03 04
  • Qt应用开发--国产工业开发板全志T113-i的部署教程
  • 基于Spring、SpringMVC、MyBatis的在线云音乐网站
  • Xilinx FPGA——ISE的UCF时序约束
  • LeetCode刷题---打家劫舍问题
  • LoadBalancer将服务暴露到外部实现负载均衡purelb-layer2模式配置介绍
  • 万宾科技可燃气体监测仪科技作用全览
  • SpringBoot自定义异常处理机制
  • GEE:使用拉普拉斯(Laplacian)算子对遥感图像进行卷积操作
  • 英语语法学习 - 每周更新学英语知识点
  • 小米秒享3--非小米电脑
  • 基于YOLOv8深度学习的安全帽目标检测系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战
  • python之ddddocr快速识别
  • 茄子科技张韶全:跨多云大数据平台DataCake在OceanBase的实践
  • 2024最新版软件测试八股文(文档)
  • AI浪潮下,非科班出身还有机会入行程序开发领域么?
  • jquery学习笔记