Unity类银河恶魔城学习记录10--3 p91 Applying stats to combat源代码

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();
    }
}
相关推荐
-一杯为品-17 分钟前
【51单片机】程序实验5&6.独立按键-矩阵按键
c语言·笔记·学习·51单片机·硬件工程
初九之潜龙勿用1 小时前
C#校验画布签名图片是否为空白
开发语言·ui·c#·.net
风尚云网1 小时前
风尚云网前端学习:一个简易前端新手友好的HTML5页面布局与样式设计
前端·css·学习·html·html5·风尚云网
吾与谁归in2 小时前
【C#设计模式(13)——代理模式(Proxy Pattern)】
设计模式·c#·代理模式
吾与谁归in2 小时前
【C#设计模式(14)——责任链模式( Chain-of-responsibility Pattern)】
设计模式·c#·责任链模式
EterNity_TiMe_3 小时前
【论文复现】(CLIP)文本也能和图像配对
python·学习·算法·性能优化·数据分析·clip
sanguine__3 小时前
java学习-集合
学习
lxlyhwl3 小时前
【STK学习】part2-星座-目标可见性与覆盖性分析
学习
nbsaas-boot3 小时前
如何利用ChatGPT加速开发与学习:以BPMN编辑器为例
学习·chatgpt·编辑器
CV学术叫叫兽3 小时前
一站式学习:害虫识别与分类图像分割
学习·分类·数据挖掘