Unity类银河战士恶魔城学习总结(P137 Merge skill tree with Clone skill把技能树和分身技能相组合)

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

教程源地址:https://www.udemy.com/course/2d-rpg-alexdev/

本章节把多重影分身技能黑洞技能加入了技能树中

神罗天征!!!

多重影分身之术!!!yi so ku ,Naruto

Clone_Skill.cs

添加了技能树上面的按钮

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class Clone_Skill : Skill
{
    [Header("分身")]
    [SerializeField] private float attackMutiplier;//攻击乘数
    [SerializeField] private GameObject clonePrefab;//克隆原型
    [SerializeField] private float cloneDuration;//克隆持续时间
    [Space]

    [Header("分身攻击")]
    [SerializeField] private UI_SkillTreeSlot cloneAttackUnlockButton;
    [SerializeField] private float cloneAttackMultiplier;
    [SerializeField] private bool canAttack;// 判断是否可以攻击

    [Header("侵略性分身")]
    [SerializeField] private UI_SkillTreeSlot aggressiveCloneUnlockButton;
    [SerializeField] private float aggresiveCloneAttackMultiplier;
    public bool canApplyOnHitEffect { get; private set; }//侵略性分身


    [Header("多重分身")]
    [SerializeField] private UI_SkillTreeSlot multipleUnlockButton;//多重分身
    [SerializeField] private float multiCloneAttackMultiplier; 
    [SerializeField] private bool canDuplicateClone;//可以重复clone
    [SerializeField] private float chanceToDuplicate;//重复clone的几率

    [Header("水晶替换分身")]
    [SerializeField] private UI_SkillTreeSlot crystalInsteadUnlockButton;//是否使用水晶代替克隆
    public bool crystalInsteadOfClone;//是否使用水晶代替克隆



    protected override void Start()
    {
        base.Start();


        cloneAttackUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockCloneAttack);
        aggressiveCloneUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockAggressiveClone);
        multipleUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockMultipleClone);
        crystalInsteadUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockCryStalInstead);
    }


    #region Unlock region


    private void UnlockCloneAttack()
    {
        if (cloneAttackUnlockButton.unlocked)
        {
            canAttack = true;
            attackMutiplier = cloneAttackMultiplier;
        }
    }

    private void UnlockAggressiveClone()
    {
        if (aggressiveCloneUnlockButton.unlocked)
        {
            canApplyOnHitEffect = true;
            attackMutiplier = aggresiveCloneAttackMultiplier;
        }
    }

    private void UnlockMultipleClone()
    {
        if (multipleUnlockButton.unlocked)
        {
            canDuplicateClone = true;
            attackMutiplier = multiCloneAttackMultiplier;
        }
    }

    private void UnlockCryStalInstead()
    {
        if (crystalInsteadUnlockButton.unlocked)
        {
            crystalInsteadOfClone = true;
        }
    }

    #endregion



    public void CreateClone(Transform _clonePosition, Vector3 _offset)//传入克隆位置
    {

        if (crystalInsteadOfClone)//克隆转换成水晶
        {
            SkillManager.instance.crystal.CreateCrystal();
            SkillManager.instance.crystal.CurrentCrystalChooseRandomTarget();
            return;
        }


        GameObject newClone = Instantiate(clonePrefab);//创建新的克隆//克隆 original 对象并返回克隆对象。


        newClone.GetComponent<Clone_Skill_Controller>().
            SetupClone(_clonePosition, cloneDuration, canAttack, _offset, FindClosesetEnemy(newClone.transform), canDuplicateClone, chanceToDuplicate, player,attackMutiplier);

    }


   
    public void CreateCloneWithDelay(Transform _enemyTransform)//反击成功成克隆
    {
        StartCoroutine(CloneDelayCorotine(_enemyTransform, new Vector3(2 * player.facingDir, -.1f,0)));
    }

    private IEnumerator CloneDelayCorotine(Transform _transform, Vector3 _offset)//产生克隆之后的延迟
    {
        yield return new WaitForSeconds(.4f);
        CreateClone(_transform, _offset);
    }

}

Clone_Skill_Controller.cs

using UnityEngine;

//P65克隆控制
//2024年11月20日加入到技能树中
public class Clone_Skill_Controller : MonoBehaviour
{
    private Player player;
    private SpriteRenderer sr;
    private Animator anim;
    [SerializeField] private float cloneLosingSpeed;//加速消失时间

    private float cloneTimer;
    private float attackMulitiplier;
    [SerializeField] private Transform attackCheck;
    [SerializeField] private float attackCheckRadius = .8f;
    private Transform closestEnemy;
    private int facingDir=1;
    private bool canDuplicateClone;
    private float chanceToDuplicate;



    private void Awake()
    {
        sr = GetComponent<SpriteRenderer>();
        anim = GetComponent<Animator>();
    }


    private void Update()
    {
        cloneTimer -= Time.deltaTime;

        if (cloneTimer < 0)
        {
            sr.color = new Color(1, 1, 1, sr.color.a - (Time.deltaTime * cloneLosingSpeed));//逐渐消失

            if (sr.color.a < 0)
                Destroy(gameObject);//当透明度小于等于0时销毁克隆体
        }
    }


    public void SetupClone(Transform _newTransform, float _cloneDuration, bool _canAttack, Vector3 _offset, Transform _closetEnemy,bool _canDuplicate,float _chanceToDuplicate,Player _player,float _attackMulitiplier)
    {
        if (_canAttack)
        {
            anim.SetInteger("AttackNumber", Random.Range(1, 3));//随机clone攻击的动画
        }

        attackMulitiplier = _attackMulitiplier;
        player = _player;
        transform.position = _newTransform.position + _offset;//将克隆体的位置设置为传入的位置
        
        cloneTimer = _cloneDuration;//设置克隆体的持续时间

        closestEnemy = _closetEnemy;
        canDuplicateClone = _canDuplicate;
        chanceToDuplicate = _chanceToDuplicate;
        FaceClosestTarget();
    }


    private void AnimationTrigger()
    {
        cloneTimer = -.1f;
    }

    private void AttackTrigger()//攻击碰撞触发函数
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCheck.position, attackCheckRadius);//这行代码在 Unity 中用于检测一个圆形区域内的所有碰撞体,并返回它们的数组。

        foreach (var hit in colliders)//for循环遍历碰撞体数组
        {
            if (hit.GetComponent<Enemy>() != null)
            {
                //player.stats.DoDamage(hit.GetComponent<CharacterStats>());

                PlayerStats playerStats = player.GetComponent<PlayerStats>();//获取玩家的stats
                EnemyStats enemyStats = hit.GetComponent<EnemyStats>(); //获取敌人的stats

                playerStats.CloneDoDamage(enemyStats, attackMulitiplier);//对敌人造成伤害

                if(player.skill.clone.canApplyOnHitEffect)//如果可以应用击中效果
                {
                    ItemData_Equipment weaponData = Inventory.instance.GetEquipment(EquipmentType.Weapon);

                    if (weaponData != null)//应用武器特殊效果
                        weaponData.Effect(hit.transform);
                }

                if (canDuplicateClone)//如果可以多重影分身
                {
                    if (Random.Range(0, 100) <= chanceToDuplicate)
                    {
                        SkillManager.instance.clone.CreateClone(hit.transform,new Vector3(.5f*facingDir,0));//在敌人身上创建克隆
                    }
                }
            }
        }
    }

    private void FaceClosestTarget()//面对最近的目标函数
    {

        if (closestEnemy != null)
        {
            if (transform.position.x > closestEnemy.position.x)
            {
                facingDir = -1;
                transform.Rotate(0, 180, 0);
            }
        }

    }
}

BlackHole_Skill_Controller.cs

using System.Collections.Generic;
using UnityEngine;



//2024年10月22日
public class BlackHole_Skill_Controller : MonoBehaviour
{
    [SerializeField] private GameObject hotkeyPrefab;
    [SerializeField] private List<KeyCode> keyCodeList;

    private float maxSize;
    private float growSpeed;
    private float shrinkSpeed;
    private float blackholeTimer;


    private bool canGrow = true;//换成private进行传递
    private bool canShrink;//= false保险起见我自己添加的
    private bool canCreateHotkeys = true;
    private bool cloneAttackReleased;
    private bool playerCanDisapear = true;//产生黑洞玩家默认消失


    private int amountOfAttacks = 4;
    private float cloneAttackCooldown = .3f;
    private float cloneAttackTimer;


    private List<Transform> targets = new List<Transform>();
    private List<GameObject> createdHotKey = new List<GameObject>();

    public bool playerCanExitState { get; private set; }

    public void SetupBlackhole(float _maxSize, float _growSpeed, float _shrinkSpeed, int _amountOfAttacks, float _cloneAttackCooldown, float _blackholeDuration)
    {
        maxSize = _maxSize;
        growSpeed = _growSpeed;
        shrinkSpeed = _shrinkSpeed;
        amountOfAttacks = _amountOfAttacks;
        cloneAttackCooldown = _cloneAttackCooldown;

        blackholeTimer = _blackholeDuration;

        if (SkillManager.instance.clone.crystalInsteadOfClone)
            playerCanDisapear = false;
    }


    private void Update()
    {
        cloneAttackTimer -= Time.deltaTime;// 修正了这里的时间减少方式
        blackholeTimer -= Time.deltaTime;

        if (blackholeTimer < 0)
        {
            blackholeTimer = Mathf.Infinity;

            if (targets.Count > 0)
                RealseCloneAttack();
            else
                FinishBlackHoleAbility();

        }



        if (Input.GetKeyDown(KeyCode.R))
        {
            RealseCloneAttack();
        }

        CloneAttackLogic();

        if (canGrow && !canShrink)//变大函数
        {
            transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(maxSize, maxSize), growSpeed * Time.deltaTime);//Vector2.Lerp 是 Unity 中的一个插值函数,用于在两个 Vector2 之间进行线性插值。
        }

        if (canShrink)
        {
            transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(-1, 1), shrinkSpeed * Time.deltaTime);

            if (transform.localScale.x < 0)
                Destroy(gameObject);
        }
    }


    private void RealseCloneAttack()
    {
        if (targets.Count <= 0)
            return;

        DestroyHotKeys();
        cloneAttackReleased = true;
        canCreateHotkeys = false;

        if (playerCanDisapear)
        {
            playerCanDisapear = false;
            PlayerManager.instance.player.fx.MakeTransprent(true);
        }
    }

    private void CloneAttackLogic()
    {
        if (cloneAttackTimer < 0 && cloneAttackReleased && amountOfAttacks > 0)//如果时间小于0并且克隆攻击释放并且攻击次数大于0
        {
            cloneAttackTimer = cloneAttackCooldown;

            int randomIndex = Random.Range(0, targets.Count);//随机选择一个敌人

            float xOffset;//随机选择一个偏移量

            if (Random.Range(0, 100) > 50)
                xOffset = 2;
            else
                xOffset = -2;


            if (SkillManager.instance.clone.crystalInsteadOfClone)//如果选择了水晶代替克隆
            {
                SkillManager.instance.crystal.CreateCrystal();
                SkillManager.instance.crystal.CurrentCrystalChooseRandomTarget();//随机选择敌人
            }
            else
            {
                SkillManager.instance.clone.CreateClone(targets[randomIndex], new Vector3(xOffset, 0, 0));
            }

            amountOfAttacks--;

            if (amountOfAttacks <= 0)
            {
                Invoke("FinishBlackHoleAbility", .5f);//延时调用
            }
        }
    }

    private void FinishBlackHoleAbility()
    {
        DestroyHotKeys();
        playerCanExitState = true;
        canShrink = true;
        cloneAttackReleased = false;
    }



    private void OnTriggerEnter2D(Collider2D collision)//碰到敌人把他们加入到targets中
    {
        if (collision.GetComponent<Enemy>() != null)
        {
            collision.GetComponent<Enemy>().FreezeTime(true);

            CreatHotKey(collision);

        }
    }

    private void OnTriggerExit2D(Collider2D collision)//private void ontriggerexit2d(collider2d collision) => collision.getcomponent<enemy>()?.freezetime(false);
    {
        if (collision.GetComponent<Enemy>() != null)
            collision.GetComponent<Enemy>().FreezeTime(false);
    }




    private void CreatHotKey(Collider2D collision)
    {
        if (keyCodeList.Count <= 0)
        {
            Debug.LogWarning("Not enough hot keys");
            return;
        }

        if (!canCreateHotkeys)
            return;

        GameObject newHotKey = Instantiate(hotkeyPrefab, collision.transform.position + new Vector3(0, 2), Quaternion.identity);
        createdHotKey.Add(newHotKey);

        //随机KeyCode传给HotKey,并且传过去一个毁掉一个
        KeyCode choosenKey = keyCodeList[Random.Range(0, keyCodeList.Count)];

        keyCodeList.Remove(choosenKey);


        Blackhole_HotKey_Controller newHotKeyScript = newHotKey.GetComponent<Blackhole_HotKey_Controller>();


        newHotKeyScript.SetupHotKey(choosenKey, collision.transform, this);
    }

    public void AddEnemyToList(Transform _enemyTransform) => targets.Add(_enemyTransform);

    private void DestroyHotKeys()
    {
        if (createdHotKey.Count <= 0)
            return;

        for (int i = 0; i < createdHotKey.Count; i++)
        {
            Destroy(createdHotKey[i]);
        }
    }
}

Blackhole_Skill.cs

using UnityEngine;
using UnityEngine.UI;

public class Blackhole_Skill : Skill
{

    [SerializeField] private UI_SkillTreeSlot blackHoleUnlockButton;
    public bool blackHoleUnlocked { get; private set; }
    [SerializeField] private int amountOfAttacks;
    [SerializeField] private float cloneCooldown;
    [SerializeField] private float blackholeDuration;
    [Space]
    [SerializeField] private GameObject blackHolePrefab;
    [SerializeField] private float maxSize;
    [SerializeField] private float growSpeed;
    [SerializeField] private float shrinkSpeed;

    BlackHole_Skill_Controller currentBlackhole;//当前的黑洞

  


    private void UnlockBlackHole()
    {
        if (blackHoleUnlockButton.unlocked)
            blackHoleUnlocked =true;
    }

    public override bool CanUseSkill()
    {
        return base.CanUseSkill();
    }


    public override void UseSkill()
    {
        base.UseSkill();//调用了基类 Skill中的 UseSkill 方法

        GameObject newBlackHole = Instantiate(blackHolePrefab, player.transform.position, Quaternion.identity); //这行代码使用 Instantiate 方法在玩家当前位置生成一个新的黑洞对象

        currentBlackhole = newBlackHole.GetComponent<BlackHole_Skill_Controller>();

        currentBlackhole.SetupBlackhole(maxSize, growSpeed, shrinkSpeed, amountOfAttacks, cloneCooldown,blackholeDuration);//调用SetupBlackhole,传递一系列参数来配置黑洞的行为,包括最大尺寸、增长速度、缩小速度、攻击次数和克隆冷却时间
    }

    protected override void Start()
    {
        base.Start();

        blackHoleUnlockButton.GetComponent<Button>().onClick.AddListener(UnlockBlackHole);
    }

    protected override void Update()
    {
        base.Update();
    }

    public bool SkillCompeleted()
    {
        if(!currentBlackhole)
        {
            return false;
        }

        if (currentBlackhole.playerCanExitState)
        {
            currentBlackhole = null;
            return true;
        }

        return false;
    }

    public float GetBlackholeRadius()//解决水晶黑洞的攻击范围问题
    {
        return maxSize / 2;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值