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


- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class LostCurrencyController : MonoBehaviour
- {
- public int currency;
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if(collision.GetComponent
() != null) - {
- PlayerManager.instance.currency += currency;
- Destroy(this.gameObject);
- }
- }
- }
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- using UnityEngine.SceneManagement;//关于场景的操作
-
- public class GameManager : MonoBehaviour, ISaveManager
- {
- public static GameManager instance;
-
- private Transform player;
-
- [SerializeField] private Checkpoint[] checkpoints;
- [SerializeField] private string closestCheckpointId;
-
- [Header("Lost currency")]
- [SerializeField] private GameObject lostCurrencyPerfab;
- public int lostCurrencyAmount;
- [SerializeField] private float lostCurrencyX;
- [SerializeField] private float lostCurrencyY;
- private void Awake()
- {
- if (instance != null)
- {
- Destroy(instance.gameObject);
- }
- else
- instance = this;
-
- checkpoints = FindObjectsOfType
(); - player = PlayerManager.instance.player.transform;
-
- }
-
- private void Start()
- {
- checkpoints = FindObjectsOfType
(); -
- }
- public void RestratScene()//场景重开函数
- {
- SaveManager.instance.SaveGame();
- Scene scene = SceneManager.GetActiveScene();//获得初始场景
- SceneManager.LoadScene(scene.name);//获取的场景必须通过字符串载入
- }
-
- public void LoadData(GameData _data)
- {
-
-
-
- foreach (KeyValuePair<string, bool> pair in _data.checkpoints)
- {
-
- foreach (Checkpoint checkpoint in checkpoints)
- {
-
- if (checkpoint.id == pair.Key && pair.Value == true)
- {
- checkpoint.ActivateCheckpoint();
-
- }
- }
- }
- closestCheckpointId = _data.closestCheckpointId;
-
- PlacePlayerAtClosestCheckpoint();
- LoadLostCurrency(_data);
- }
-
- private void LoadLostCurrency(GameData _data)//产生可以捡到的钱尸体函数
- {
-
- lostCurrencyAmount = _data.lostCurrencyAmount;
- lostCurrencyX = _data.lostCurrencyX;
- lostCurrencyY = _data.lostCurrencyY;
-
- if(lostCurrencyAmount > 0)
- {
-
- GameObject newLostCurrency = Instantiate(lostCurrencyPerfab, new Vector3(lostCurrencyX, lostCurrencyY), Quaternion.identity);
- newLostCurrency.GetComponent
().currency = lostCurrencyAmount; - }
-
- lostCurrencyAmount = 0;
- }
-
-
- private void PlacePlayerAtClosestCheckpoint()//传送至最近检查点函数
- {
-
- foreach (Checkpoint checkpoint in checkpoints)
- {
-
- if (closestCheckpointId == checkpoint.id && checkpoint.activationStatus)
- {
-
- PlayerManager.instance.player.transform.position = checkpoint.transform.position;
- }
- }
- }
-
- public void SaveData(ref GameData _data)
- {
- _data.lostCurrencyAmount = lostCurrencyAmount;//此地方的钱是player死后stats赋值的
- _data.lostCurrencyX = player.position.x;
- _data.lostCurrencyY = player.position.y;
-
- if(FindClosestCheckpoint()!=null)
- _data.closestCheckpointId = FindClosestCheckpoint().id;//Save后调用
- _data.checkpoints.Clear();
- foreach (Checkpoint checkpoint in checkpoints)
- {
- _data.checkpoints.Add(checkpoint.id, checkpoint.activationStatus);
- }
-
- }
-
- private Checkpoint FindClosestCheckpoint()//寻找最近检查点的函数
- {
- float closetDistance = Mathf.Infinity;
- Checkpoint closestCheckpoint = null;
-
- foreach (var checkpoint in checkpoints)//遍历检查点比较距离寻找最近的检查点
- {
- float distanceToCheckpoint = Vector2.Distance(PlayerManager.instance.player.transform.position, checkpoint.transform.position);
- if (distanceToCheckpoint < closetDistance && checkpoint.activationStatus == true)
- {
- closetDistance = distanceToCheckpoint;
- closestCheckpoint = checkpoint;
- }
- }
-
- return closestCheckpoint;
-
- }
- }
- 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 SerializableDictionary<string, bool> checkpoints;
- public string closestCheckpointId;
-
- public float lostCurrencyX;
- public float lostCurrencyY;
- public int lostCurrencyAmount;
-
- public GameData()
- {
- this.lostCurrencyX = 0;
- this.lostCurrencyY = 0;
- this.lostCurrencyAmount = 0;
-
- this.currency = 0;
- skillTree = new SerializableDictionary<string, bool>();
- inventory = new SerializableDictionary<string, int>();
- equipmentId = new List<string>();
-
- closestCheckpointId = string.Empty;
- checkpoints = new SerializableDictionary<string, bool>();
- }
- }
- using System.Collections;
- using UnityEngine;
-
- public class UI : MonoBehaviour
- {
- [Header("End screen")]
- [SerializeField] private UI_FadeScreen fadeScreen;
- [SerializeField] private GameObject endText;
- [SerializeField] private GameObject restartButton;
- [Space]
-
- [SerializeField] private GameObject characterUI;
- [SerializeField] private GameObject skillTreeUI;
- [SerializeField] private GameObject craftUI;
- [SerializeField] private GameObject optionsUI;
- [SerializeField] private GameObject inGameUI;
-
- public UI_itemTooltip itemToolTip;
- public UI_statToolTip statToopTip;
- public Ui_SkillToolTip skillToolTip;
-
- public UI_CraftWindow craftWindow;
- public void Awake()
- {
- SwitchTo(skillTreeUI);//修复可能出现skill没法加载成功的bug
-
- }
- public void Start()
- {
- SwitchTo(inGameUI);
- itemToolTip.gameObject.SetActive(false);
- statToopTip.gameObject.SetActive(false);
- }
-
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.C))
- {
- SwitchWithKeyTo(characterUI);
- }
-
- if (Input.GetKeyDown(KeyCode.B))
- {
- SwitchWithKeyTo(craftUI);
- }
-
- if (Input.GetKeyDown(KeyCode.K))
- {
- SwitchWithKeyTo(skillTreeUI);
- }
-
- if (Input.GetKeyDown(KeyCode.O))
- {
- SwitchWithKeyTo(optionsUI);
- }
- }
-
- public void SwitchTo(GameObject _menu)//切换窗口函数
- {
-
- for (int i = 0; i < transform.childCount; i++)
- {
- bool fadeScreen = transform.GetChild(i).GetComponent
() != null;//保证存在淡入淡出效果的函数时才会为真,才会使darkScreen保持存在 -
- if (!fadeScreen)
- transform.GetChild(i).gameObject.SetActive(false);
- }
-
- if (_menu != null)
- {
- _menu.SetActive(true);
- }
- }
-
- public void SwitchWithKeyTo(GameObject _menu)//键盘切换窗口函数
- {
- if (_menu != null && _menu.activeSelf)//通过判断是否传入mune和mune是否激活来决定使设置为可视或不可使
- {
- _menu.SetActive(false);
- CheckForInGameUI();
- return;
- }
- SwitchTo(_menu);
- }
-
- private void CheckForInGameUI()//当其他UI不在时自动切换值InGameUI函数
- {
- for (int i = 0; i < transform.childCount; i++)
- {
- if (transform.GetChild(i).gameObject.activeSelf && transform.GetChild(i).GetComponent
() == null) //修复InGameUI在fadeScreen打开后,没法存在的问题 - return;
- }
-
- SwitchTo(inGameUI);
- }
-
- public void SwitchOnEndScreen()//死亡综合效果函数
- {
- SwitchTo(null);
- fadeScreen.FadeOut();
- StartCoroutine(EndScreenCorutine());
- }
-
- IEnumerator EndScreenCorutine()//死亡显示文本函数
- {
- yield return new WaitForSeconds(1);
- endText.SetActive(true);
- yield return new WaitForSeconds(1.5f);
- restartButton.SetActive(true);
- }
-
- public void RestartGameButton()//场景重开函数
- {
- GameManager.instance.RestratScene();//调用GameManager的重开函数
- }
- }
-
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
-
- public class UI_InGame : MonoBehaviour
- {
- [SerializeField] private PlayerStats playerStats;
- [SerializeField] Slider slider;
-
- [SerializeField] private Image dashImage;
- [SerializeField] private Image parryImage;
- [SerializeField] private Image crystalImage;
- [SerializeField] private Image swordImage;
- [SerializeField] private Image blackholeImage;
- [SerializeField] private Image flaskholeImage;
-
- [Header("Souls info")]
- [SerializeField] private TextMeshProUGUI currentSouls;
- [SerializeField] private float soulsAmount;//灵魂数量
- [SerializeField] private float increaseRate = 100;//增长速率
-
-
- private SkillManager skills;
- void Start()
- {
- if(playerStats != null)
- {
- playerStats.onHealthChanged += UpdateHealthUI;
- }
-
- skills = SkillManager.instance;
-
-
- }
-
- // Update is called once per frame
- void Update()
- {
- UpdateSoulsUI();//货币随着时间变化的特效函数
-
- if (Input.GetKeyDown(KeyCode.LeftShift) && skills.dash.dashUnlocked)//使用技能后图标变黑
- {
- SetCoolDownOf(dashImage);
- }
- if (Input.GetKeyDown(KeyCode.Q) && skills.parry.parryUnlocked)
- {
- SetCoolDownOf(parryImage);
- }
- if (Input.GetKeyDown(KeyCode.F) && skills.crystal.crystalUnlocked)
- {
- SetCoolDownOf(crystalImage);
- }
- if (Input.GetKeyDown(KeyCode.Mouse1) && skills.sword.swordUnlocked)
- {
- SetCoolDownOf(swordImage);
- }
- if (Input.GetKeyDown(KeyCode.R) && skills.blackhole.blackholeUnlocked)
- {
- SetCoolDownOf(blackholeImage);
- }
- if (Input.GetKeyDown(KeyCode.Alpha1) && Inventory.instance.GetEquipment(EquipmentType.Flask) != null)
- {
- SetCoolDownOf(flaskholeImage);
- }
-
- CheckCooldown(dashImage, skills.dash.cooldown);
- CheckCooldown(parryImage, skills.parry.cooldown);
- CheckCooldown(crystalImage, skills.crystal.cooldown);
- CheckCooldown(swordImage, skills.sword.cooldown);
- CheckCooldown(blackholeImage, skills.blackhole.cooldown);
- CheckCooldown(flaskholeImage, Inventory.instance.flaskCooldown);
- }
-
- private void UpdateSoulsUI()//货币随着时间变化的特效函数
- {
- //逻辑是判断目前的钱少于实际的钱,则以速率*时间的方式增加直到相同或者大于后等于实际的钱
- if (soulsAmount < PlayerManager.instance.GetCurrency())
- {
- soulsAmount += Time.deltaTime * increaseRate;
- }
- else
- soulsAmount = PlayerManager.instance.GetCurrency();
-
- currentSouls.text = ((int)soulsAmount).ToString();
- }
-
- private void UpdateHealthUI()//更新血量条函数,此函数由Event触发
- {
- slider.maxValue = playerStats.GetMaxHealthValue();
- slider.value = playerStats.currentHealth;
- }
-
- private void SetCoolDownOf(Image _image)//使用技能后使图标变黑的函数
- {
- if (_image.fillAmount <= 0)
- _image.fillAmount = 1;
- }
-
- private void CheckCooldown(Image _image,float _cooldown)//使图标根据cd逐渐变白的函数
- {
- if(_image.fillAmount > 0)
- {
- _image.fillAmount -= 1 / _cooldown * Time.deltaTime;
- }
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class PlayerStats : CharacterStats
- {
- private Player player;
-
-
-
-
- protected override void Start()
- {
- player = GetComponent
(); - base.Start();
- }
- public override void DoDamage(CharacterStats _targetStats)
- {
- base.DoDamage(_targetStats);
-
- }
- public override void TakeDamage(int _damage)
- {
- base.TakeDamage(_damage);
- }
- protected override void Die()
- {
- base.Die();
- player.Die();
- GameManager.instance.lostCurrencyAmount = PlayerManager.instance.currency;
- PlayerManager.instance.currency = 0;
-
- GetComponent
()?.GenerateDrop(); - }
- protected override void DecreaseHealthBy(int _damage)
- {
- base.DecreaseHealthBy(_damage);
- ItemData_Equipment currentArmor = Inventory.instance.GetEquipment(EquipmentType.Armor);
-
- if(currentArmor != null)
- {
- currentArmor.Effect(player.transform);
- }
- }
-
- public override void OnEvasion()
- {
- player.skill.dogge.CreateMirageOnDoDogge();
- }
-
- public void CloneDoDamage(CharacterStats _targetStats,float _multiplier)
- {
- if (TargetCanAvoidAttack(_targetStats))设置闪避
- {
- return;
- }
-
- int totleDamage = damage.GetValue() + strength.GetValue();
-
- if(_multiplier > 0)
- {
- totleDamage = Mathf.RoundToInt(totleDamage * _multiplier);
- }
-
- //爆伤设置
- if (CanCrit())
- {
- totleDamage = CalculateCriticalDamage(totleDamage);
- }
-
- totleDamage = CheckTargetArmor(_targetStats, totleDamage);//设置防御
-
- _targetStats.TakeDamage(totleDamage);
-
- DoMagicaDamage(_targetStats); // 可以去了也可以不去
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.WSA;
-
- public class EnemyStats : CharacterStats
- {
- private Enemy enemy;
- private ItemDrop myDropSystem;
- public Stat soulsDropAmount;//金币掉落
-
- [Header("Level details")]
- [SerializeField] private int leval = 1;
-
- [Range(0f, 1f)]//一个使数值设置成为一定范围的设置
- [SerializeField] private float percantageModifier = .4f;//设置等级和成长比例
-
- public override void DoDamage(CharacterStats _targetStats)
- {
- base.DoDamage(_targetStats);
- }
-
- protected override void Die()
- {
- base.Die();
- enemy.Die();
-
- PlayerManager.instance.currency += soulsDropAmount.GetValue();
- myDropSystem.GenerateDrop();
- }
-
- protected override void Start()
- {
- soulsDropAmount.SetDefaultValue(100);
- //改变伤害和生命值
- //解决初始血量在升级后不满
- ApplyLevelModifier();
-
- enemy = GetComponent
(); - base.Start();
-
-
- myDropSystem = GetComponent
(); -
- }
-
- private void ApplyLevelModifier()
- {
- Modify(strength);
- Modify(agility);
- Modify(intelligence);
- Modify(vitality);
-
- Modify(damage);
- Modify(critChance);
- Modify(critPower);
-
- Modify(Health);
- Modify(armor);
- Modify(evasion);
- Modify(magicResistance);
-
- Modify(fireDamage);
- Modify(iceDamage);
- Modify(lightingDamage);
-
- Modify(soulsDropAmount);
- }
-
- //专门对某个数值进行提升的函数
- private void Modify(Stat _stat)
- {
- for(int i =1;i
- {
- float modifier = _stat.GetValue() * percantageModifier;
-
- _stat.AddModifier(Mathf.RoundToInt(modifier));
- }
- }
-
- public override void TakeDamage(int _damage)
- {
- base.TakeDamage(_damage);
-
- }
- }