Unity类银河恶魔城学习记录11-15 p117 Ice and Fire item Effect源代码

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考

此代码仅为较上一P有所改变的代码

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

IceAndFire_Controller.cs
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IceAndFire_Controller : ThurderStrike_Controller
{
    protected override void OnTriggerEnter2D(Collider2D collision)
    {
        base.OnTriggerEnter2D(collision);
    }
}
ThurderStrike_Controller.cs
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.DoMagicaDamage(enemyTarget);
        }
    }
}
IceAndFire_Effect.cs
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "Ice and Fire effect", menuName = "Data/Item effect/Ice and Fire")]
public class IceAndFire_Effect : ItemEffect
{
    [SerializeField] private GameObject iceAndFirePrufab;
    [SerializeField] private float xVelocity;
    public override void ExecuteEffect(Transform _respawPosition)
    {
        Player player = PlayerManager.instance.player;

        bool thirdAttack = player.GetComponent<Player>().primaryAttack.comboCounter == 2;//设置第三下平a产生火球

        if (thirdAttack)//设置第三下平a产生火球
        {
            GameObject newIceAndFire = Instantiate(iceAndFirePrufab, _respawPosition.position, player.transform.rotation);//使火球旋转方向与player对齐
            newIceAndFire.GetComponent<Rigidbody2D>().velocity = new Vector2(xVelocity*player.facingDir,0);//获得速度并使其朝向与player一致

            Destroy(newIceAndFire, 10);// 自动销毁10秒
        }

        
    }
}
ThurderStrike_Effect.cs
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);
    }
}
PlayerPrimaryAttack.cs
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerPrimaryAttackState : PlayerState
{
    //p38 2.从ground进入

    public int comboCounter { get; private set; }

    private float lastTimeAttacked;//距离上一次攻击的时间
    private float comboWindow = 2;//可以间隔的时间
    public PlayerPrimaryAttackState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
    {
    }

    public override void Enter()
    {
        base.Enter();

        xInput = 0;//修复攻击乱转的问题

        if(comboCounter >2||Time.time>comboWindow+lastTimeAttacked)//当计数器超过2和间隔时间大于window时,进入第一个攻击动作
        {
            comboCounter = 0;
        }
        

        player.anim.SetInteger("ComboCounter", comboCounter);//设置animtor里的comboCounter

        #region 选择攻击方向
        float attackDir = player.facingDir;

        if(xInput != 0)
        {
            attackDir = xInput;
        }
        #endregion
                                                                   //使其能改变攻击方向
        player.SetVelocity(player.attackMovement[comboCounter].x * attackDir, player.attackMovement[comboCounter].y);//给角色初速度,让角色在攻击触发时移动一点

        stateTimer = .1f;
    }

    public override void Exit()
    {
        base.Exit();
        player.StartCoroutine("BusyFor", .15f);
        comboCounter++;
        lastTimeAttacked = Time.time;
    }

    public override void Update()
    {
        base.Update();
        if(stateTimer<0)
        {
            player.SetZeroVelocity();
        }//1.修改移动时攻击时后可以移动的BUG
        //2.但给了点时间模拟惯性可以动一点
        if (triggerCalled)
        {
            stateMachine.ChangeState(player.idleState);
        }
    }
}
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)
            {
                EnemyStats _target = hit.GetComponent<EnemyStats>();

                if(_target != null)//修复无敌人时触发DoDamge出错的bug
                    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();
    }
}
相关推荐
zfoo-framework4 分钟前
kotlin技术栈各种 学习文档
学习
是隼人17 分钟前
buuctf-pwn wdb_2018_3rd_soEasy(ret2shellcode)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
mldong8 小时前
C# 开发者也有自己的轻量工作流引擎了:NuGet 装包,5 分钟跑通一条审批流
后端·c#·.net
mykj15519 小时前
英语单词APP解锁学习新方式,智能高效背单词
学习·英语单词app
知识分享小能手9 小时前
深度学习学习教程,从入门到精通,深度学习中的正则化 — 完整知识点与代码示例(7)
人工智能·深度学习·学习
~kiss~10 小时前
Agent 的 未来吗?String: An Agentic OS Where Every App Is a Markdown File
学习
HugoStudio_SWAN11 小时前
洛谷 B4450 / B3867 / B3923 智慧购物、储蓄与做题——从程序到生活
c++·学习·程序人生·算法·生活
samble12 小时前
从零搭建工业控制系统(二十二):线程安全与并发控制
c#·wpf·并发·mvvm·线程安全·工业控制
UIU11413 小时前
scanf与cout的误区:探究其内部的机制
c++·学习·c#·scanf
wgslucky13 小时前
Godot GDScript初学者遇到的问题记录
游戏引擎·godot