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

embedded/2024/9/24 10:22:31/

   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);}
}


http://www.ppmy.cn/embedded/12988.html

相关文章

kettle从入门到精通 第五十三课 ETL之kettle MQTT/RabbitMQ producer 实战

1、MQTT介绍 MQTT (Message Queuing Telemetry Transport) 是一种轻量级的消息传输协议&#xff0c;设计用于连接低带宽、高延迟或不可靠网络的设备。 MQTT 是基于发布/订阅模式&#xff08;Publish/Subscribe&#xff09;的协议&#xff0c;其中设备可以发布消息到一个主题&…

web server apache tomcat11-07-Realm Configuration

前言 整理这个官方翻译的系列&#xff0c;原因是网上大部分的 tomcat 版本比较旧&#xff0c;此版本为 v11 最新的版本。 开源项目 从零手写实现 tomcat minicat 别称【嗅虎】心有猛虎&#xff0c;轻嗅蔷薇。 系列文章 web server apache tomcat11-01-官方文档入门介绍 web…

【Python学习】面向对象

面向对象 1.初识对象1.1 生活中数据的组织1. 2 程序中数据的组织1.3 使用对象组织数据1.4进行对比 2. 成员方法2.1 类的定义和使用2.2 成员变量和成员方法2.2.1 成员变量2.2.2 成员方法的定义语法2.2.3 self 的作用 3. 类和对象3.1 面向过程3.2 面向对象 4.构造方法4.1 属性成员…

Qt 实战(1)Qt 概述

一、Qt概述 1、什么是Qt&#xff1f; Qt&#xff08;官方发音 [kju:t]&#xff0c;音同 cute&#xff09;是一个跨平台的 C 开发库&#xff0c;主要用来开发图形用户界面&#xff08;Graphical User Interface&#xff0c;GUI&#xff09;程序&#xff0c;也可以开发不带界面的…

恶补《操作系统》2_1——王道学习笔记

2操作系统-进程 2.1_1 进程的定义、组成、组织方式、特征 组成&#xff1a;PCB&#xff08;进程存在唯一的标志&#xff09;&#xff0c;程序段&#xff0c;数据段 组织方式&#xff1a;链接方式&#xff0c;指针指向不同的队列&#xff1b;索引方式&#xff0c;索引表 特征…

HTML+JS俄罗斯方块小游戏

俄罗斯方块 效果图部分代码领取源码下期内容预报 效果图 部分代码 <!DOCTYPE html> <html><head><meta charset"utf-8" /><title>俄罗斯方块</title><style type"text/css">.MainFrame{border: 1px solid burl…

java+idea+mysql采用医疗AI自然语言处理技术的3D智能导诊导系统源码

javaideamysql采用医疗AI自然语言处理技术的3D智能导诊导系统源码 随着人工智能技术的快速发展&#xff0c;语音识别与自然语言理解技术的成熟应用&#xff0c;基于人工智能的智能导诊导医逐渐出现在患者的生活视角中&#xff0c;智能导诊系统应用到医院就医场景中&#xff0c…

聊聊路径规划算法(二)——图搜索法

图搜索法通过利用已有的环境地图和版图中的障碍物等数据信息建立&#xff0c;由起点至结束点的可行路线。一般分为深度最优和广度最优二种走向。深度优先算法优先拓展搜索深度较大的节点&#xff0c;因此能够更迅速的获得下一个可行路径&#xff0c;不过深度优先算法获取的第一…