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

Unity类银河恶魔城学习记录3-1 EnemyStateMachine源代码 P47

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
Enemy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Enemy : MonoBehaviour
{#region 组件public Animator anim { get; private set; }public Rigidbody2D rb { get; private set; }#endregion#region 类public EnemyStateMachine stateMachine;public EnemyIdleState idleState;#endregionprivate void Awake(){stateMachine = new EnemyStateMachine();idleState = new EnemyIdleState(this, stateMachine, "Idle");anim = GetComponentInChildren<Animator>();rb = GetComponent<Rigidbody2D>();}void Start(){stateMachine.Initialize(idleState);}void Update(){stateMachine.currentState.Update();}
}

EnemyState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class EnemyState
{protected Enemy enemy;protected EnemyStateMachine stateMachine;protected bool triggerCalled;private string animBoolName;protected float stateTimer;public EnemyState(Enemy _enemy, EnemyStateMachine _stateMachine, string _animBoolName){this.enemy = _enemy;this.stateMachine = _stateMachine;this.animBoolName = _animBoolName;}public virtual void Enter(){triggerCalled = false;Debug.Log("I enter" + animBoolName);enemy.anim.SetBool(animBoolName, true);}public virtual void Update(){stateTimer -= Time.deltaTime;Debug.Log("I'm in " + animBoolName);}public virtual void Exit(){enemy.anim.SetBool(animBoolName, false);}}

EnemyStateMachine.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class EnemyStateMachine
{public EnemyState currentState { get; private set; }//记得加private set,不然很可能会被外部改了public void Initialize(EnemyState _startState){currentState = _startState;currentState.Enter();}public void ChangeState(EnemyState _newState){currentState.Exit();currentState = _newState;currentState.Enter();}
}

EnemyIdleState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class EnemyIdleState : EnemyState
{public EnemyIdleState(Enemy _enemy, EnemyStateMachine _stateMachine, string _animBoolName) : base(_enemy, _stateMachine, _animBoolName){}public override void Enter(){base.Enter();}public override void Exit(){base.Exit();}public override void Update(){base.Update();}
}

http://www.lryc.cn/news/294899.html

相关文章:

  • 使用webstorm调试vue 2 项目
  • 深度学习缝模块怎么描述创新点?(附写作模板+涨点论文)
  • html,css,js速成
  • 《Docker极简教程》--Docker基础--基础知识(一)
  • Web html和css
  • Three.js学习6:透视相机和正交相机
  • ❤ React18 环境搭建项目与运行(地址已经放Gitee开源)
  • 2024 RTE行业(实时互动行业)人才发展学习总结
  • 92.网游逆向分析与插件开发-游戏窗口化助手-显示游戏数据到小助手UI
  • Stable Diffusion 模型下载:majicMIX fantasy 麦橘幻想
  • docker compose安装minio
  • 二、SSM 整合配置实战
  • 『运维备忘录』之 Yum 命令详解
  • CSS中可继承与不可继承属性有哪些
  • Zephyr NRF7002 实现AppleJuice
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • Webpack插件浅析
  • 【Java 数据结构】反射
  • LangChain结合通义千问的自建知识库
  • 【证书管理】实验报告
  • App Store外区账号分享
  • 判断字符串是否包含正则表达式默认的特殊字符c++
  • 【蓝桥杯选拔赛真题64】python数字塔 第十五届青少年组蓝桥杯python 选拔赛比赛真题解析
  • javaEE - 23( 21000 字 Servlet 入门 -1 )
  • 【sentinel流量卫兵搭建与微服务整合】
  • Linux环境下配置mysql主从复制
  • 生物素-PEG4-酪胺,Biotin-PEG4-TSA,应用于酶联免疫吸附实验
  • Android:文件读写
  • 2024/2/5
  • 政安晨:示例演绎Python的函数与获取帮助的方法