Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
PlayerAnimationTrigger.cs
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)
{
EnemyStat _target = hit.GetComponent<EnemyStat>();
player.stats.DoDamage(_target);//提供一个可以从player调用敌人受伤函数的入口,通过传入受伤敌人的组件
// hit.GetComponent<Enemy>().DamageEffect();
}
}
}
private void ThrowSword()
{
SkillManager.instance.sword.CreateSword();
}
}
Enemy_SkeletonAnimationTrigger.cs
cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_SkeletonAnimationTriggers : MonoBehaviour
{
private Enemy_Skeleton enemy => GetComponentInParent<Enemy_Skeleton>();//拿到enemy实体
private void AnimationTrigger()
{
enemy.AnimationFinishTrigger();//调用实体上的函数,使triggerCalled为true;
}
private void AttackTrigger()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(enemy.attackCheck.position, enemy.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<Player>() != null)
{
PlayerStat target = hit.GetComponent<PlayerStat>();
enemy.stats.DoDamage(target);
//hit.GetComponent<Player>().DamageEffect();
}
}
}
private void OpenCounterWindow() => enemy.OpenCounterAttackWindow();
private void CloseCounter() => enemy.CloseCounterAttackWindow();
}
CharacterStats.cs
cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterStats : MonoBehaviour
{
public Stat strength;
public Stat damage;
public Stat maxHealth;
[SerializeField]private int currentHealth;
protected virtual void Start()
{
currentHealth = maxHealth.GetValue();
}
public virtual void DoDamage(CharacterStats _targetStats)
{
int totleDamage = damage.GetValue() + strength.GetValue();
_targetStats.TakeDamage(totleDamage);
}
protected virtual void TakeDamage(int _damage)
{
currentHealth -= _damage;
Debug.Log(_damage);
if (currentHealth < 0)
Die();
}
protected virtual void Die()
{
}
}
PlayerStat
cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerStat : CharacterStats
{
private Player player;
public override void DoDamage(CharacterStats _targetStats)
{
base.DoDamage(_targetStats);
}
protected override void Die()
{
base.Die();
}
protected override void Start()
{
player = GetComponent<Player>();
base.Start();
}
protected override void TakeDamage(int _damage)
{
base.TakeDamage(_damage);
player.DamageEffect();
}
}
EnemyStat
cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyStat : CharacterStats
{
private Enemy enemy;
public override void DoDamage(CharacterStats _targetStats)
{
base.DoDamage(_targetStats);
}
protected override void Die()
{
base.Die();
}
protected override void Start()
{
enemy = GetComponent<Enemy>();
base.Start();
}
protected override void TakeDamage(int _damage)
{
base.TakeDamage(_damage);
enemy.DamageEffect();
}
}