Unity类银河恶魔城学习记录8-4 P80 Blackhole ability state源代码

 Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码

【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili

Entity.cs 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Entity : MonoBehaviour
{[Header("Knockback info")][SerializeField] protected Vector2 knockbackDirection;//被击打后的速度信息[SerializeField] protected float knockbackDuration;//被击打的时间protected bool isKnocked;//此值通过卡住SetVelocity函数的方式用来阻止当一个角色被攻击时,会乱动的情况[Header("Collision Info")]public Transform attackCheck;//transform类,代表的时物体的位置,用来控制攻击检测的位置public float attackCheckRadius;//检测半径[SerializeField] protected Transform groundCheck;//transform类,代表的时物体的位置,后面会来定位子组件的位置    [SerializeField] protected float groundCheckDistance;[SerializeField] protected Transform wallCheck;//transform类,代表的时物体的位置,后面会来定位子组件的位置    [SerializeField] protected float wallCheckDistance;[SerializeField] protected LayerMask whatIsGround;//LayerMask类,与Raycast配合,https://docs.unity3d.com/cn/current/ScriptReference/Physics.Raycast.html#region 定义Unity组件public SpriteRenderer sr { get; private set; }public Animator anim { get; private set; }//这样才能配合着拿到自己身上的animator的控制权public Rigidbody2D rb { get; private set; }//配合拿到身上的Rigidbody2D组件控制权public EntityFX fx { get; private set; }//拿到EntityFX#endregionpublic int facingDir { get; private set; } = 1;protected bool facingRight = true;//判断是否朝右protected virtual void Awake(){anim = GetComponentInChildren<Animator>();//拿到自己子组件身上的animator的控制权sr = GetComponentInChildren<SpriteRenderer>();fx = GetComponent<EntityFX>();拿到的组件上的EntityFX控制权rb = GetComponent<Rigidbody2D>();}protected virtual void Start(){}protected virtual void Update(){}public virtual void Damage(){fx.StartCoroutine("FlashFX");//IEnumertor本质就是将一个函数分块执行,只有满足某些条件才能执行下一段代码,此函数有StartCoroutine调用//https://www.zhihu.com/tardis/bd/art/504607545?source_id=1001StartCoroutine("HitKnockback");//调用被击打后产生后退效果的函数Debug.Log(gameObject.name+"was damaged");}protected virtual IEnumerator HitKnockback(){isKnocked = true;//此值通过卡住SetVelocity函数的方式用来阻止当一个角色被攻击时,会乱动的情况rb.velocity = new Vector2(knockbackDirection.x * -facingDir, knockbackDirection.y);yield return new WaitForSeconds(knockbackDuration);isKnocked = false;}//被击打后产生后退效果的函数#region 速度函数Velocitypublic virtual void SetZeroVelocity(){if(isKnocked){return;}rb.velocity = new Vector2(0, 0);}//设置速度为0函数public virtual void SetVelocity(float _xVelocity, float _yVelocity){if(isKnocked)return;此值通过卡住SetVelocity函数的方式用来阻止当一个角色被攻击时,会乱动的情况rb.velocity = new Vector2(_xVelocity, _yVelocity);//将rb的velocity属性设置为对应的想要的二维向量。因为2D游戏的速度就是二维向量FlipController(_xVelocity);//在其他设置速度的时候调用翻转控制器}//控制速度的函数,此函数在其他State中可能会使用,但仅能通过player.SeVelocity调用#endregion#region 翻转函数Flippublic virtual void Flip(){facingDir = facingDir * -1;facingRight = !facingRight;transform.Rotate(0, 180, 0);//旋转函数,transform不需要额外定义,因为他是自带的}//翻转函数public virtual void FlipController(float _x)//目前设置x,目的时能在空中时也能转身{if (_x > 0 && !facingRight)//当速度大于0且没有朝右时,翻转{Flip();}else if (_x < 0 && facingRight){Flip();}}#endregion#region 碰撞函数Collisionpublic virtual bool IsGroundDetected(){return Physics2D.Raycast(groundCheck.position, Vector2.down, groundCheckDistance, whatIsGround);}//通过RayCast检测是否挨着地面,https://docs.unity3d.com/cn/current/ScriptReference/Physics2D.Raycast.html//xxxxxxxx()   => xxxxxxxx  == xxxxxxxxxx() return xxxxxxxxx;public virtual bool IsWallDetected(){return Physics2D.Raycast(wallCheck.position, Vector2.right * facingDir, wallCheckDistance, whatIsGround);}//通过RayCast检测是否挨着地面,https://docs.unity3d.com/cn/current/ScriptReference/Physics2D.Raycast.html//xxxxxxxx()   => xxxxxxxx  == xxxxxxxxxx() return xxxxxxxxx;protected virtual void OnDrawGizmos(){Gizmos.DrawLine(groundCheck.position, new Vector3(groundCheck.position.x, groundCheck.position.y - groundCheckDistance));//绘制一条从 from(前面的) 开始到 to(后面的) 的线。Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x + wallCheckDistance, wallCheck.position.y));//绘制一条从 from(前面的) 开始到 to(后面的) 的线。Gizmos.DrawWireSphere(attackCheck.position, attackCheckRadius);//https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Gizmos.DrawWireSphere.html//绘制具有中心和半径的线框球体。}//画图函数#endregionpublic void MakeTransprent(bool isClear){if (isClear)sr.color = Color.clear;elsesr.color = Color.white;}
}
Blackhole_Skill_Controller.cs
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;public class Blackhole_Skill_Controller : MonoBehaviour
{[SerializeField] private GameObject hotKeyPrefab;[SerializeField] private List<KeyCode> KeyCodeList;private float maxSize;//最大尺寸private float growSpeed;//变大速度private float shrinkSpeed;//缩小速度private bool canGrow = true;//是否可以变大private bool canShrink;//缩小private bool canCreateHotKeys = true;专门控制后面进入的没法生成热键private bool cloneAttackReleased;private int amountOfAttacks = 4;private float cloneAttackCooldown = .3f;private float cloneAttackTimer;private List<Transform> targets = new List<Transform>();private List<GameObject> createdHotKey = new List<GameObject>();public void SetupBlackhole(float _maxSize,float _growSpeed,float _shrinkSpeed,int _amountOfAttacks,float _cloneAttackCooldown){maxSize = _maxSize;growSpeed = _growSpeed;shrinkSpeed = _shrinkSpeed;amountOfAttacks = _amountOfAttacks;cloneAttackCooldown = _cloneAttackCooldown;}private void Update(){cloneAttackTimer -= Time.deltaTime;if (Input.GetKeyDown(KeyCode.R)){ReleaseCloneAttack();}CloneAttackLogic();if (canGrow && !canShrink){//这是控制物体大小的参数transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(maxSize, maxSize), growSpeed * Time.deltaTime);//类似MoveToward,不过是放大到多少大小 https://docs.unity3d.com/cn/current/ScriptReference/Vector2.Lerp.html}if (canShrink){transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(0, 0), shrinkSpeed * Time.deltaTime);if (transform.localScale.x <= 1f){Destroy(gameObject);}}}private void ReleaseCloneAttack(){cloneAttackReleased = true;canCreateHotKeys = false;DestroyHotKeys();PlayerManager.instance.player.MakeTransprent(true);}private void CloneAttackLogic(){if (cloneAttackTimer < 0 && cloneAttackReleased){cloneAttackTimer = cloneAttackCooldown;int randomIndex = Random.Range(0, targets.Count);//限制攻击次数和设置攻击偏移量float _offset;if (Random.Range(0, 100) > 50)_offset = 1.5f;else_offset = -1.5f;SkillManager.instance.clone.CreateClone(targets[randomIndex], new Vector3(_offset, 0, 0));amountOfAttacks--;if (amountOfAttacks <= 0){Invoke("FinishBlackholeAbility", 0.5f);}}}private void FinishBlackholeAbility(){canShrink = true;cloneAttackReleased = false;PlayerManager.instance.player.ExitBlackholeAbility();}private void OnTriggerEnter2D(Collider2D collision){if(collision.GetComponent<Enemy>()!=null){collision.GetComponent<Enemy>().FreezeTime(true);CreateHotKey(collision);}}private void OnTriggerExit2D(Collider2D collision){if (collision.GetComponent<Enemy>() != null){collision.GetComponent<Enemy>().FreezeTime(false);}}private void CreateHotKey(Collider2D collision){if(KeyCodeList.Count == 0)//当所有的KeyCode都被去除,就不在创建实例{return;}if(!canCreateHotKeys)//这是当角色已经开大了,不在创建实例{return;}//创建实例GameObject newHotKey = Instantiate(hotKeyPrefab, collision.transform.position + new Vector3(0, 2), Quaternion.identity);//将实例添加进列表createdHotKey.Add(newHotKey);//随机KeyCode传给HotKey,并且传过去一个毁掉一个KeyCode choosenKey = KeyCodeList[Random.Range(0, KeyCodeList.Count)];KeyCodeList.Remove(choosenKey);Blackhole_Hotkey_Controller newHotKeyScript = newHotKey.GetComponent<Blackhole_Hotkey_Controller>();newHotKeyScript.SetupHotKey(choosenKey, collision.transform, this);}public void AddEnemyToList(Transform _myEnemy){targets.Add(_myEnemy);}//销毁Hotkeyprivate void DestroyHotKeys(){if(createdHotKey.Count <= 0){return;}for (int i = 0; i < createdHotKey.Count; i++){Destroy(createdHotKey[i]); }}
}
SkillManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SkillManager : MonoBehaviour
{public static SkillManager instance;public Dash_Skill dash { get; private set; }public Clone_Skill clone { get; private set; }public Sword_Skill sword { get; private set; }public Blackhole_Skill blackhole { get; private set; }private void Awake(){if (instance != null){Destroy(instance.gameObject);}elseinstance = this;}private void Start(){dash = GetComponent<Dash_Skill>();clone = GetComponent<Clone_Skill>();sword = GetComponent<Sword_Skill>();blackhole = GetComponent<Blackhole_Skill>();}
}
Player.cs
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;public class Player : Entity
{[Header("Attack Details")]public Vector2[] attackMovement;//每个攻击时获得的速度组public float counterAttackDuration = .2f;public bool isBusy{ get; private set; }//防止在攻击间隔中进入move//[Header("Move Info")]public float moveSpeed;//定义速度,与xInput相乘控制速度的大小public float jumpForce;public float swordReturnImpact;//在player里设置swordReturnImpact作为击退的参数[Header("Dash Info")][SerializeField] private float dashCooldown;private float dashUsageTimer;//为dash设置冷却时间,在一定时间内不能连续使用public float dashSpeed;//冲刺速度public float dashDuration;//持续时间public float dashDir { get; private set; }#region 定义Statespublic PlayerStateMachine stateMachine { get; private set; }public PlayerIdleState idleState { get; private set; }public PlayerMoveState moveState { get; private set; }public PlayerJumpState jumpState { get; private set; }public PlayerAirState airState { get; private set; }public PlayerDashState dashState { get; private set; }public PlayerWallSlideState wallSlide { get; private set; }public PlayerWallJumpState wallJump { get; private set; }public PlayerPrimaryAttackState primaryAttack { get; private set; }public PlayerCounterAttackState counterAttack { get; private set; }public PlayerAimSwordState aimSword { get; private set; }public PlayerCatchSwordState catchSword { get; private set; }public PlayerBlackholeState blackhole { get; private set; }public SkillManager skill { get; private set; }public GameObject sword{ get; private set; }//声明sword#endregionprotected override void Awake(){base.Awake();stateMachine = new PlayerStateMachine();//通过构造函数,在构造时传递信息idleState = new PlayerIdleState(this, stateMachine, "Idle");moveState = new PlayerMoveState(this, stateMachine, "Move");jumpState = new PlayerJumpState(this, stateMachine, "Jump");airState = new PlayerAirState(this, stateMachine, "Jump");dashState = new PlayerDashState(this, stateMachine, "Dash");wallSlide = new PlayerWallSlideState(this, stateMachine, "WallSlide");wallJump = new PlayerWallJumpState(this, stateMachine, "Jump");//wallJump也是Jump动画primaryAttack = new PlayerPrimaryAttackState(this, stateMachine, "Attack");counterAttack = new PlayerCounterAttackState(this, stateMachine, "CounterAttack");aimSword = new PlayerAimSwordState(this,stateMachine, "AimSword");catchSword = new PlayerCatchSwordState(this, stateMachine, "CatchSword");blackhole = new PlayerBlackholeState(this, stateMachine, "Jump");//this 就是 Player这个类本身}//Awake初始化所以State,为所有State传入各自独有的参数,及animBool,以判断是否调用此动画(与animatoin配合完成)protected override void Start(){base.Start();stateMachine.Initialize(idleState);skill = SkillManager.instance;}protected override void Update()//在mano中update会自动刷新但其他没有mano的不会故,需要在这个updata中调用其他脚本中的函数stateMachine.currentState.update以实现 //stateMachine中的update{base.Update();stateMachine.currentState.Update();//反复调用CurrentState的Update函数CheckForDashInput();}public void AssignNewSword(GameObject _newSword)//保持创造的sword实例的函数{sword = _newSword;}public void CatchTheSword()//通过player的CatchTheSword进入,及当剑消失的瞬间进入{stateMachine.ChangeState(catchSword);Destroy(sword);}public void ExitBlackholeAbility(){stateMachine.ChangeState(airState);}public IEnumerator BusyFor(float _seconds)//https://www.zhihu.com/tardis/bd/art/504607545?source_id=1001{isBusy = true;yield return new WaitForSeconds(_seconds);isBusy = false;}//p39 4.防止在攻击间隔中进入move,通过设置busy值,在使用某些状态时,使其为busy为true,抑制其进入其他state//IEnumertor本质就是将一个函数分块执行,只有满足某些条件才能执行下一段代码,此函数有StartCoroutine调用public void AnimationTrigger() => stateMachine.currentState.AnimationFinishTrigger();//从当前状态拿到AnimationTrigger进行调用的函数public void CheckForDashInput(){if (IsWallDetected()){return;}//修复在wallslide可以dash的BUGif (Input.GetKeyDown(KeyCode.LeftShift) && skill.dash.CanUseSkill())//将DashTimer<0 的判断 改成DashSkill里的判断{dashDir = Input.GetAxisRaw("Horizontal");//设置一个值,可以将dash的方向改为你想要的方向而不是你的朝向if (dashDir == 0){dashDir = facingDir;//只有当玩家没有控制方向时才使用默认朝向}stateMachine.ChangeState(dashState);}}//将Dash切换设置成一个函数,使其在所以情况下都能使用}
PlayerGroundState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//GroundState用于保证只有在Idle和Move这两个地面状态下才能调用某些函数,并且稍微减少一点代码量
public class PlayerGroundState : PlayerState
{public PlayerGroundState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName){}public override void Enter(){base.Enter();}public override void Exit(){base.Exit();}public override void Update(){base.Update();if(Input.GetKeyDown(KeyCode.R)){stateMachine.ChangeState(player.blackhole);}if(Input.GetKeyDown(KeyCode.Mouse1)&&HasNoSword())//点击右键进入瞄准状态,当sword存在时,不能进入aim状态{stateMachine.ChangeState(player.aimSword);}if(Input.GetKeyDown(KeyCode.Q))//摁Q进入反击状态{stateMachine.ChangeState(player.counterAttack);}if(Input.GetKeyDown(KeyCode.Mouse0))//p38 2.从ground进入攻击状态{stateMachine.ChangeState(player.primaryAttack);}if(player.IsGroundDetected()==false){stateMachine.ChangeState(player.airState);}// 写这个是为了防止在空中直接切换为moveState了。if (Input.GetKeyDown(KeyCode.Space) && player.IsGroundDetected()){stateMachine.ChangeState(player.jumpState);}//空格切换为跳跃状态}private bool HasNoSword()//用这个函数同时控制了是否能进入aimSword和如果sword存在便使他回归player的功能{if(!player.sword){return true;}player.sword.GetComponent<Sword_Skill_Controller>().ReturnSword();return false;}
}
PlayerBlackholeState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerBlackholeState : PlayerState
{private float flyTime = .4f;//飞行时间private bool skillUsed;//技能是否在被使用private float defaultGravity;public PlayerBlackholeState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName){}public override void AnimationFinishTrigger(){base.AnimationFinishTrigger();}public override void Enter(){base.Enter();skillUsed = false;stateTimer = flyTime;defaultGravity = rb.gravityScale;rb.gravityScale = 0;}public override void Exit(){base.Exit();rb.gravityScale = defaultGravity;player.MakeTransprent(false);}public override void Update(){base.Update();//使角色释放技能后能飞起来if (stateTimer > 0){rb.velocity = new Vector2(0, 15);}if(stateTimer < 0){rb.velocity = new Vector2(0, -.1f);if(!skillUsed){if(player.skill.blackhole.CanUseSkill())//创建实体skillUsed = true;}}}//在controller,Attack结束后退出状态
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/518857.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

广西普通高等教育专升本考试 大纲与说明(电子与信息大类)

&#xff08;2025 年版&#xff09;广西普通高等教育专升本考试&#xff08;以下简称专升本考试&#xff09; &#xff08;电子与信息大类&#xff09; 详细文件看资源绑定 贯彻党的教育方针&#xff0c;落实立德树人根本任务&#xff0c;是普通高校全 日制高职&#xff08;专…

IDEA 配置文件乱码,项目编码设置

见下图 其中第一二项控制全局以及工程的编码格式&#xff0c;下方的则是 properties 配置文件的格式&#xff0c;统一调整为 UTF-8 后不再乱码

CMA认证和CNAS认可哪个更权威?CMA、CNAS软件测试报告用途简析

一、CMA认证是什么?   CMA认证是指中国计量认证&#xff0c;省级以上的计量行政部门根据中国计量法的规定&#xff0c;对申请CMA测试资质的第三方检测机构进行评估&#xff0c;对检测水平和检测可靠性达到国家标准的实验室授予计量认证合格证书(CMA资质)。 二、CNAS认可是什…

2024.3.6每日一题

LeetCode 找出数组中的 K -or 值 题目链接&#xff1a;2917. 找出数组中的 K-or 值 - 力扣&#xff08;LeetCode&#xff09; 题目描述 给你一个下标从 0 开始的整数数组 nums 和一个整数 k 。 nums 中的 K-or 是一个满足以下条件的非负整数&#xff1a; 只有在 nums 中&…

Vue3之集成Highlight.js代码语法亮显示插件

Vue3之集成Highlight.js代码语法亮显示插件 文章目录 Vue3之集成Highlight.js代码语法亮显示插件1.Vue3中集成说明2. 安装highlight.js与highlightjs/vue-plugin3. Vue3集成与使用1. main.ts(或main.js)中导入highlight.js2. 各语言测试代码3. 高亮显示vue文件4. 显示效果 4. H…

Java+SpringBoot+Vue+MySQL:农业管理新篇章

✍✍计算机毕业编程指导师 ⭐⭐个人介绍&#xff1a;自己非常喜欢研究技术问题&#xff01;专业做Java、Python、微信小程序、安卓、大数据、爬虫、Golang、大屏等实战项目。 ⛽⛽实战项目&#xff1a;有源码或者技术上的问题欢迎在评论区一起讨论交流&#xff01; ⚡⚡ Java、…

Windows环境下搭建chatGLM-6B-int4量化版模型(图文详解-成果案例)

目录 一、ChatGLM-6B介绍 二、环境准备 1. 硬件环境 2. TDM-GCC安装 3.git安装 4.Anaconda安装 三、模型安装 1.下载ChatGLM-6b和环境准备 方式一&#xff1a;git命令 方式二&#xff1a;手动下载 2.下载预训练模型 方式一&#xff1a;在Hugging Face HUb下载&…

2024电商新手怎么入行?哪个平台适合自己?

我是电商珠珠 2024年了&#xff0c;电商行业的热度也不断高涨。过年期间&#xff0c;京东在春晚投放了华为大礼包&#xff1b;淘宝也紧跟春晚步伐上架明星同款穿搭&#xff1b;抖音和央视春晚达成合作&#xff0c;在平台内直播&#xff0c;还送相应的福袋&#xff0c;和春晚独…

判断连续数据同意特征的方法:插旗法

bool isMonotonic(int* nums, int numsSize) {int flag 2;for (int i 1; i < numsSize; i) {if (nums[i-1] > nums[i]) {if (flag 0)return false;flag 1;}else if (nums[i-1] < nums[i]) {if (flag 1)return false;flag 0;}}return true; }此代码较为简单&…

剑指offer--c++--n个骰子的点数

目录 题目&#xff1a; 题目分析&#xff1a; 最后编写代码&#xff1a; 输出结果 题目&#xff1a; 把n个骰子扔在地上&#xff0c;所有骰子朝上一面的点数之和为s。输入n&#xff0c;打印出s的所有可能的值出现的概率。 感谢大佬的帮助&#xff1a;https://www.cnblogs.c…

Deeplearning4j【基础 01】初识Java深度学习框架DL4J

初识Java深度学习框架DL4J 1.起因2.简介3.组件3.1 Deeplearning4j/ScalNet3.1.1 Deeplearning4jf&#xff08;Java&#xff09;3.1.2 ScalNet&#xff08;Scala&#xff09; 3.2 ND4J/LibND4J3.3 SameDiff3.4 DataVec3.5 Arbiter3.6 RL4J 4.总结 内容来自网络&#xff0c;基于官…

7.1 支付模块 - 用户选课

支付模块 - 需求分析、添加选课 文章目录 支付模块 - 需求分析、添加选课一、需求分析1.1 选课业务流程1.2 支付业务流程1.3 在线学习业务流程1.4 课程续期业务流程 二、添加选课2.1 执行流程2.2 数据模型2.2.1 选课记录表 choose_course2.2.2 用户课程表 course_tables 2.3 查…