『功能项目』战士职业平A怪物掉血【44】

我们打开上一篇43事件中心的项目,

本章要做的事情是给主角增加一个xxxCtrl.cs脚本,再创建一个xxxOpt.cs调用xxxCtrl.cs机制层利用事件中心再写一个主角战士平A对怪物的伤害

首先创建脚本:PlayerCtrl.cs

cs 复制代码
using UnityEngine;
public class PlayerCtrl : MonoBehaviour{
    BossCtrl bossCtrl;
    public void Start() {
        bossCtrl = FindObjectOfType<BossCtrl>();
    }
    public void SetPlayerHpValueChange(float attackValue){
        bossCtrl.currentHp -= (int)((attackValue - 
            bossCtrl.defineValue) * (1 + Random.Range(-0.3f,0.3f)));
    }
}

将xxxCtrl.cs脚本增加到主角资源框架上

再创建脚本:PlayerOpt.cs

cs 复制代码
using UnityEngine;
public class PlayerOpt : MonoBehaviour{
    GameManager gm;
    PlayerRayClickNavigation player;
    #region 事件中心
    BossCtrl bossCtrl;
    PlayerCtrl playerCtrl;
    Vector3 monsterPos;
    Vector3 selfPos;
    #endregion
    public void Start(){
        gm = GameManager.Instance;
        player = gameObject.GetComponent<PlayerRayClickNavigation>();
        #region 事件中心
        bossCtrl = FindObjectOfType<BossCtrl>();
        playerCtrl = gameObject.AddComponent<PlayerCtrl>();
        EventCenter.Instance.AddEventListener<Vector3>("MonsterPositionChanged", OnMonsterPositionChanged);
        monsterPos = GameObject.FindWithTag("Monster").gameObject.transform.position;
        #endregion
    }
    void Update(){
        #region 事件中心
        selfPos = transform.position;
        Player01A(selfPos, monsterPos);
        #endregion
    }
    #region 事件中心
    void Player01A(Vector3 selfPos, Vector3 monsterPos){
        this.selfPos = selfPos;
        this.monsterPos = monsterPos;
        if (Vector3.Distance(selfPos, monsterPos) < 10){
            if (player.changeProfess == 2){
                if (Input.GetKeyDown(KeyCode.A)){
                    playerCtrl.SetPlayerHpValueChange(gm.infoSys.attackValue);
                }
            }
        }
    }
    void OnDestroy(){
        EventCenter.Instance.RemoveEventListener<Vector3>("MonsterPositionChanged", OnMonsterPositionChanged);
    }
    void OnMonsterPositionChanged(Vector3 newPosition){
        monsterPos = newPosition;
        Player01A(transform.position, monsterPos);
    }
    #endregion
}

再将该脚本增加到资源框架上

保存后运行项目

本章利用事件中心实现了主角战士平A(按A键)对怪物血量造成伤害的效果

接下来的文章内容:

1.战士职业的伤害型技能

2.窗口可拖拽脚本

3.点击名称寻找地点功能

4.隐藏怪物的生成

5.怪物I攻击范围内的主动攻击

6.掉落坐骑蛋的获取

7.异步传送转换场景

以及开放回合制、坐骑系统、宠物系统、背包系统、神炼系统、商城系统、Boss的目标跟随任务导航系统以及UI播放3D动画效果等等。

具体项目运行效果请关注water1024的b站视频项目演示《破碎纪元》

【Unity回合2.5D】破碎纪元_单机游戏热门视频 (bilibili.com)https://www.bilibili.com/video/BV1rZY4e9Ebs/?spm_id_from=333.999.0.0&vd_source=547091a95b03acfa8e8a9e46ef499cd6

相关推荐
Yorlen_Zhang1 小时前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
不绝1911 小时前
C#进阶:预处理指令/反射,Gettype,Typeof/关键类
开发语言·c#
大鹏说大话1 小时前
告别 MSBuild 脚本混乱:用 C# 和 Nuke 构建清晰、可维护的现代化构建系统
开发语言·c#
czhc11400756633 小时前
通信 28
c#
bugcome_com6 小时前
C# 程序结构详解:从 Hello World 开始
c#
唐梓航-求职中7 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#
bugcome_com9 小时前
阿里云 OSS C# SDK 使用实践与参数详解
阿里云·c#
懒人咖19 小时前
缺料分析时携带用料清单的二开字段
c#·金蝶云星空
bugcome_com20 小时前
深入了解 C# 编程环境及其开发工具
c#
wfserial1 天前
c#使用微软自带speech选择男声仍然是女声的一种原因
microsoft·c#·speech