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

Unity实战案例全解析 类宝可梦回合制的初级案例 源码分析(加了注释和流程图)

这是一个老教程了,但是对于没有写过回合制的初级程序同学来讲是比较适合的,也可以直接看源码,半小时内可以解决战斗

当然,我也没写过回合制系统所以就到处找,思路明白了就能自己修改了

视频教程 - 油管链接 

Turn-Based Combat in Unity (youtube.com)

GitHub - 完整工程案例 

wBrackeys/Turn-based-combat: Project files for our tutorial on how to create a turn-based battle system. (github.com)

实现效果 

流程图 

Start进入PlayerAttack,之后便是几个枚举状态之间的循环了

主要代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;// 定义战斗状态的枚举
public enum BattleState { START, PLAYERTURN, ENEMYTURN, WON, LOST }public class BattleSystem : MonoBehaviour
{// 玩家和敌人的预制体public GameObject playerPrefab;public GameObject enemyPrefab;// 玩家和敌人的战斗位置public Transform playerBattleStation;public Transform enemyBattleStation;// 玩家和敌人的单位Unit playerUnit;Unit enemyUnit;// 对话文本public Text dialogueText;// 玩家和敌人的HUDpublic BattleHUD playerHUD;public BattleHUD enemyHUD;// 当前战斗状态public BattleState state;// Start is called before the first frame updatevoid Start(){// 初始化战斗状态为STARTstate = BattleState.START;// 启动设置战斗的协程StartCoroutine(SetupBattle());}// 设置战斗的协程IEnumerator SetupBattle(){// 实例化玩家和敌人的预制体GameObject playerGO = Instantiate(playerPrefab, playerBattleStation);playerUnit = playerGO.GetComponent<Unit>();GameObject enemyGO = Instantiate(enemyPrefab, enemyBattleStation);enemyUnit = enemyGO.GetComponent<Unit>();// 更新对话文本dialogueText.text = "A wild " + enemyUnit.unitName + " approaches...";// 设置玩家和敌人的HUDplayerHUD.SetHUD(playerUnit);enemyHUD.SetHUD(enemyUnit);// 等待2秒yield return new WaitForSeconds(2f);// 切换到玩家回合state = BattleState.PLAYERTURN;PlayerTurn();}// 玩家攻击的协程IEnumerator PlayerAttack(){// 玩家攻击敌人并检查敌人是否死亡bool isDead = enemyUnit.TakeDamage(playerUnit.damage);// 更新敌人的HP和对话文本enemyHUD.SetHP(enemyUnit.currentHP);dialogueText.text = "The attack is successful!";// 等待2秒yield return new WaitForSeconds(2f);// 如果敌人死亡,结束战斗;否则,切换到敌人回合if(isDead){state = BattleState.WON;EndBattle();} else{state = BattleState.ENEMYTURN;StartCoroutine(EnemyTurn());}}// 敌人回合的协程IEnumerator EnemyTurn(){// 更新对话文本dialogueText.text = enemyUnit.unitName + " attacks!";// 等待1秒yield return new WaitForSeconds(1f);// 敌人攻击玩家并检查玩家是否死亡bool isDead = playerUnit.TakeDamage(enemyUnit.damage);// 更新玩家的HPplayerHUD.SetHP(playerUnit.currentHP);// 等待1秒yield return new WaitForSeconds(1f);// 如果玩家死亡,结束战斗;否则,切换到玩家回合if(isDead){state = BattleState.LOST;EndBattle();} else{state = BattleState.PLAYERTURN;PlayerTurn();}}// 结束战斗的方法void EndBattle(){if(state == BattleState.WON){dialogueText.text = "You won the battle!";} else if (state == BattleState.LOST){dialogueText.text = "You were defeated.";}}// 玩家回合的方法void PlayerTurn(){dialogueText.text = "Choose an action:";}// 玩家治疗的协程IEnumerator PlayerHeal(){// 玩家恢复HPplayerUnit.Heal(5);// 更新玩家的HP和对话文本playerHUD.SetHP(playerUnit.currentHP);dialogueText.text = "You feel renewed strength!";// 等待2秒yield return new WaitForSeconds(2f);// 切换到敌人回合state = BattleState.ENEMYTURN;StartCoroutine(EnemyTurn());}// 攻击按钮的事件处理方法public void OnAttackButton(){if (state != BattleState.PLAYERTURN)return;StartCoroutine(PlayerAttack());}// 治疗按钮的事件处理方法public void OnHealButton(){if (state != BattleState.PLAYERTURN)return;StartCoroutine(PlayerHeal());}
}

次要代码

战斗单位脚本,主要是属性

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Unit : MonoBehaviour
{public string unitName;public int unitLevel;public int damage;public int maxHP;public int currentHP;public bool TakeDamage(int dmg){currentHP -= dmg;if (currentHP <= 0)return true;elsereturn false;}public void Heal(int amount){currentHP += amount;if (currentHP > maxHP)currentHP = maxHP;}}

 更新UI代码
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class BattleHUD : MonoBehaviour
{public Text nameText;public Text levelText;public Slider hpSlider;public void SetHUD(Unit unit){nameText.text = unit.unitName;levelText.text = "Lvl " + unit.unitLevel;hpSlider.maxValue = unit.maxHP;hpSlider.value = unit.currentHP;}public void SetHP(int hp){hpSlider.value = hp;}}

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

相关文章:

  • AI绘图大模型 Stable Diffusion 使用详解
  • es索引库操作和使用RestHignLevelClient客户端操作es
  • 安卓数据共享
  • Gin框架操作指南02:JSON渲染
  • 【随手记】MySQL单表访问方法
  • 机器学习:情感分析的原理、应用场景及优缺点介绍
  • 基于SSM的医院药品管理系统
  • 特征融合篇 | YOLOv10 引入动态上采样模块 | 超过了其他上采样器
  • 【Linux系列】写入文本到文件
  • 【踩坑随笔】Tensorflow-GPU训练踩坑
  • 【云岚到家】-day07-4-实战项目-优惠券活动-项目准备
  • axios的使用
  • Ubuntu 使用命令克隆和恢复SD卡
  • Java 小游戏《超级马里奥》
  • go语言defer详解
  • 【C语言】循环中断break
  • centos ping能通但是wget超时-解决
  • SDIO - DWC MSHC 电压切换和频率切换
  • EI-CLIP 深度理解 PPT
  • leetcode力扣刷题系列——【最小元素和最大元素的最小平均值】
  • 【线性回归分析】:基于实验数据的模型构建与可视化
  • CountUp.js 实现数字增长动画 Vue
  • 设计模式大全
  • redis IO多路复用机制
  • Oracle漏洞修复 19.3 补丁包 升级为19.22
  • Q2=10 and Q2=1--PLB(Fig.4)
  • sd卡挂载返回FR_NOT_READY等错误
  • 推荐一款超级实用的浏览器扩展程序!实时翻译网页,支持多种语言(带私活源码)
  • manjaro kde 24 应该如何设置才能上网(2024-10-13亲测)
  • 2024软件测试面试大全(答案+文档)