Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
ThurderStrike_Controller.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThurderStrike_Controller : MonoBehaviour
{
protected virtual void OnTriggerEnter2D(Collider2D collision)
{
if(collision.GetComponent<Enemy>()!= null)
{
PlayerStats playerStats = PlayerManager.instance.player.GetComponent<PlayerStats>();
EnemyStats enemyTarget = collision.GetComponent<EnemyStats>();
playerStats.DoDamage(enemyTarget);
}
}
}
ItemEffect.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName ="New Item Data",menuName = "Data/Item effect")]
public class ItemEffect : ScriptableObject
{
public virtual void ExecuteEffect(Transform _enemyPosition)
{
Debug.Log("Effect executed");
}
}
ThurderStrike_Effect.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Thurder strike effect", menuName = "Data/Item effect/Thurder strike")]
public class ThurderStrike_Effect : ItemEffect
{
[SerializeField] private GameObject thurderStrikePrefab;
public override void ExecuteEffect(Transform _enemyPosition)
{
GameObject newThurderStrike = Instantiate(thurderStrikePrefab,_enemyPosition.position,Quaternion.identity);
Destroy(newThurderStrike, 1);
}
}
ItemData_Equipment.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum EquipmentType
{
Weapon,
Armor,
Amulet,
Flask
}
[CreateAssetMenu(fileName = "New Item Data", menuName = "Data/Equipment")]
public class ItemData_Equipment : ItemData
{
public EquipmentType equipmentType;
public ItemEffect[] itemEffects;
[Header("Major stats")]
public int strength; // 力量 增伤1点 爆伤增加 1% 物抗
public int agility;// 敏捷 闪避 1% 闪避几率增加 1%
public int intelligence;// 1 点 魔法伤害 1点魔抗
public int vitality;//加血的
[Header("Offensive stats")]
public int damage;
public int critChance; // 暴击率
public int critPower; //150% 爆伤
[Header("Defensive stats")]
public int maxHealth;
public int armor;
public int evasion;//闪避值
public int magicResistance;
[Header("Magic stats")]
public int fireDamage;
public int iceDamage;
public int lightingDamage;
public void AddModifiers()
{
PlayerStats playerStats = PlayerManager.instance.player.GetComponent<PlayerStats>();
playerStats.strength.AddModifier(strength);
playerStats.agility.AddModifier(agility);
playerStats.intelligence.AddModifier(intelligence);
playerStats.vitality.AddModifier(vitality);
playerStats.damage.AddModifier(damage);
playerStats.critChance.AddModifier(critChance);
playerStats.critPower.AddModifier(critPower);
playerStats.maxHealth.AddModifier(maxHealth);
playerStats.armor.AddModifier(armor);
playerStats.evasion.AddModifier(evasion);
playerStats.magicResistance.AddModifier(magicResistance);
playerStats.fireDamage.AddModifier(fireDamage);
playerStats.iceDamage.AddModifier(iceDamage);
playerStats.lightingDamage.AddModifier(lightingDamage);
}
public void RemoveModifiers()
{
PlayerStats playerStats = PlayerManager.instance.player.GetComponent<PlayerStats>();
playerStats.strength.RemoveModifier(strength);
playerStats.agility.RemoveModifier(agility);
playerStats.intelligence.RemoveModifier(intelligence);
playerStats.vitality.RemoveModifier(vitality);
playerStats.damage.RemoveModifier(damage);
playerStats.critChance.RemoveModifier(critChance);
playerStats.critPower.RemoveModifier(critPower);
playerStats.maxHealth.RemoveModifier(maxHealth);
playerStats.armor.RemoveModifier(armor);
playerStats.evasion.RemoveModifier(evasion);
playerStats.magicResistance.RemoveModifier(magicResistance);
playerStats.fireDamage.RemoveModifier(fireDamage);
playerStats.iceDamage.RemoveModifier(iceDamage);
playerStats.lightingDamage.RemoveModifier(lightingDamage);
}
public void Effect(Transform _enemyPosition)//循环调用Effect类里的Effect的函数
{
foreach(var item in itemEffects)
{
item.ExecuteEffect(_enemyPosition);
}
}
}
PlayerAnimationTrigger.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerAnimationTriggers : MonoBehaviour
{
private Player player => GetComponentInParent<Player>();//获得夫组件上的实际存在的Player组件
private void AnimationTrigger()
{
player.AnimationTrigger();
}
private void AttackTrigger()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(player.attackCheck.position, player.attackCheckRadius);//创建一个碰撞器组,保存所有圈所碰到的碰撞器
//https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Physics2D.OverlapCircleAll.html
foreach(var hit in colliders)//https://blog.csdn.net/m0_52358030/article/details/121722077
{
if(hit.GetComponent<Enemy>()!=null)
{
EnemyStats _target = hit.GetComponent<EnemyStats>();
player.stats.DoDamage(_target);//提供一个可以从player调用敌人受伤函数的入口,通过传入受伤敌人的组件
if(Inventory.instance.GetEquipment(EquipmentType.Weapon) != null)
Inventory.instance.GetEquipment(EquipmentType.Weapon).Effect(_target.transform);
// hit.GetComponent<Enemy>().DamageImpact();
}
}
}
private void ThrowSword()
{
SkillManager.instance.sword.CreateSword();
}
}