Unity类银河恶魔城学习记录13-4 p145 Save Skill Tree源代码

2024-04-21 02:44

本文主要是介绍Unity类银河恶魔城学习记录13-4 p145 Save Skill Tree源代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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

 GameData.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GameData
{public int currency;public SerializableDictionary<string, bool> skillTree;public SerializableDictionary<string, int> inventory;public List<string> equipmentId;public GameData(){this.currency = 0;skillTree = new SerializableDictionary<string, bool>();inventory = new SerializableDictionary<string, int>();equipmentId = new List<string>();}
}
Skill
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;public class Skill : MonoBehaviour
{public float cooldown;protected float cooldownTimer;protected Player player;//拿到playerprotected virtual void Start(){player = PlayerManager.instance.player;//拿到playerCheckUnlock();}protected virtual void Update(){cooldownTimer -= Time.deltaTime;}protected virtual void CheckUnlock()//再次打开游戏时读取数据文件后使技能可以使用的函数{}public virtual bool CanUseSkill(){if (cooldownTimer < 0){UseSkill();cooldownTimer = cooldown;return true;}else{Debug.Log("Skill is on cooldown");return false;}}public virtual void UseSkill(){// do some skill thing}//整理能返回最近敌人位置的函数protected virtual Transform FindClosestEnemy(Transform _checkTransform){Collider2D[] colliders = Physics2D.OverlapCircleAll(_checkTransform.position, 25);//找到环绕自己的所有碰撞器float closestDistance = Mathf.Infinity;//正无穷大的表示形式(只读)Transform closestEnemy = null;//https://docs.unity3d.com/cn/current/ScriptReference/Mathf.Infinity.htmlforeach (var hit in colliders){if (hit.GetComponent<Enemy>() != null){float distanceToEnemy = Vector2.Distance(_checkTransform.position, hit.transform.position);//拿到与敌人之间的距离if (distanceToEnemy < closestDistance)//比较距离,如果离得更近,保存这个敌人的位置,更改最近距离{closestDistance = distanceToEnemy;closestEnemy = hit.transform;}}}return closestEnemy;}}
UI_SkillTreeSlot
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;public class UI_SkillTreeSlot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler,ISaveManager
{[SerializeField] private int skillCost;[SerializeField] private string skillName;[TextArea][SerializeField] private string skillDescription;[SerializeField] private Color lockedSkillColor;private UI ui;public bool unlocked;//一个表示是否解锁的bool值private Image skillImage;[SerializeField] private UI_SkillTreeSlot[] shouldBeUnlocked;[SerializeField] private UI_SkillTreeSlot[] shouldBeLocked;//应该解锁和不该解锁的Slot组和Imageprivate void OnValidate(){gameObject.name = "SkillTreeSlot_UI - " + skillName;}private void Awake(){GetComponent<Button>().onClick.AddListener(() => UnlockSkillSlot());}private void Start(){skillImage = GetComponent<Image>();skillImage.color = lockedSkillColor;ui = GetComponentInParent<UI>();if (unlocked)skillImage.color = Color.white;}public void UnlockSkillSlot()//一个判断此Skill是否可以解锁的函数{if (PlayerManager.instance.HaveEnoughMoney(skillCost) == false)return;Debug.Log("Slot unlocked");for (int i = 0; i < shouldBeUnlocked.Length; i++){if (shouldBeUnlocked[i].unlocked == false){Debug.Log("Cannot unlock skill");return;}}for (int i = 0; i < shouldBeLocked.Length; i++){if (shouldBeLocked[i].unlocked == true){Debug.Log("Cannot unlock skill");return;}}unlocked = true;skillImage.color = Color.white;}public void OnPointerEnter(PointerEventData eventData){ui.skillToolTip.ShowToolTip(skillDescription, skillName,skillCost.ToString());}public void OnPointerExit(PointerEventData eventData){ui.skillToolTip.HideToolTip();}public void LoadData(GameData _data){if(_data.skillTree.TryGetValue(skillName,out bool value)){unlocked = value;}}public void SaveData(ref GameData _data){if (_data.skillTree.TryGetValue(skillName, out bool value))//这应该是跟clear一样的,但是为什么不直接用clear?//因为clear会调用24次,每一次都把前面保存的删了,最后只剩下一个{_data.skillTree.Remove(skillName);_data.skillTree.Add(skillName, unlocked);}else{_data.skillTree.Add(skillName, unlocked);}//_data.skillTree.Clear();//_data.skillTree.Add(skillName, unlocked);}
}
Dogge_Skill
using UnityEngine;
using UnityEngine.UI;public class Dogge_Skill : Skill
{[Header("Dodge")][SerializeField] private UI_SkillTreeSlot unlockDoggeButton;[SerializeField] private int evasionAmount;public bool doggeUnlocked;[Header("Mirage dodge")][SerializeField] private UI_SkillTreeSlot unlockMirageDoggeButton;public bool dodgemirageUnlocked;protected override void Start(){base.Start();unlockDoggeButton.GetComponent<Button>().onClick.AddListener(UnlockDodge);unlockMirageDoggeButton.GetComponent<Button>().onClick.AddListener(UnlockMirageDogge);}protected override void CheckUnlock(){UnlockDodge();UnlockMirageDogge();}private void UnlockDodge(){if (unlockDoggeButton.unlocked && !doggeUnlocked){player.stats.evasion.AddModifier(evasionAmount);Inventory.instance.UpdateStatsUI();doggeUnlocked = true;}}private void UnlockMirageDogge(){if (unlockMirageDoggeButton.unlocked)dodgemirageUnlocked = true;}public void CreateMirageOnDoDogge(){if (dodgemirageUnlocked)SkillManager.instance.clone.CreateClone(player.transform, Vector3.zero);}
}

这篇关于Unity类银河恶魔城学习记录13-4 p145 Save Skill Tree源代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/921974

相关文章

国内环境搭建私有知识问答库踩坑记录(ollama+deepseek+ragflow)

《国内环境搭建私有知识问答库踩坑记录(ollama+deepseek+ragflow)》本文给大家利用deepseek模型搭建私有知识问答库的详细步骤和遇到的问题及解决办法,感兴趣的朋友一起看看吧... 目录1. 第1步大家在安装完ollama后,需要到系统环境变量中添加两个变量2. 第3步 “在cmd中

Spring Retry 实现乐观锁重试实践记录

《SpringRetry实现乐观锁重试实践记录》本文介绍了在秒杀商品SKU表中使用乐观锁和MybatisPlus配置乐观锁的方法,并分析了测试环境和生产环境的隔离级别对乐观锁的影响,通过简单验证,... 目录一、场景分析 二、简单验证 2.1、可重复读 2.2、读已提交 三、最佳实践 3.1、配置重试模板

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

将sqlserver数据迁移到mysql的详细步骤记录

《将sqlserver数据迁移到mysql的详细步骤记录》:本文主要介绍将SQLServer数据迁移到MySQL的步骤,包括导出数据、转换数据格式和导入数据,通过示例和工具说明,帮助大家顺利完成... 目录前言一、导出SQL Server 数据二、转换数据格式为mysql兼容格式三、导入数据到MySQL数据

关于rpc长连接与短连接的思考记录

《关于rpc长连接与短连接的思考记录》文章总结了RPC项目中长连接和短连接的处理方式,包括RPC和HTTP的长连接与短连接的区别、TCP的保活机制、客户端与服务器的连接模式及其利弊分析,文章强调了在实... 目录rpc项目中的长连接与短连接的思考什么是rpc项目中的长连接和短连接与tcp和http的长连接短

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

Servlet中配置和使用过滤器的步骤记录

《Servlet中配置和使用过滤器的步骤记录》:本文主要介绍在Servlet中配置和使用过滤器的方法,包括创建过滤器类、配置过滤器以及在Web应用中使用过滤器等步骤,文中通过代码介绍的非常详细,需... 目录创建过滤器类配置过滤器使用过滤器总结在Servlet中配置和使用过滤器主要包括创建过滤器类、配置过滤