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();
    }
}
相关推荐
奶香臭豆腐6 分钟前
C++ —— 模板类具体化
开发语言·c++·学习
波音彬要多做1 小时前
41 stack类与queue类
开发语言·数据结构·c++·学习·算法
ue星空1 小时前
虚幻引擎结构之UWorld
游戏引擎·虚幻
ue星空1 小时前
虚幻引擎结构之ULevel
游戏引擎·虚幻
向宇it1 小时前
【从零开始入门unity游戏开发之——unity篇01】unity6基础入门开篇——游戏引擎是什么、主流的游戏引擎、为什么选择Unity
开发语言·unity·c#·游戏引擎
m0_748256781 小时前
WebGIS实战开源项目:智慧机场三维可视化(学习笔记)
笔记·学习·开源
仰望大佬0072 小时前
Avalonia实例实战五:Carousel自动轮播图
数据库·microsoft·c#
糖朝2 小时前
c#读取json
c#·json
南七澄江3 小时前
各种网站(学习资源及其他)
开发语言·网络·python·深度学习·学习·机器学习·ai
神洛华4 小时前
Y3地图制作1:水果缤纷乐、密室逃脱
编辑器·游戏引擎·游戏程序