Unity—技能系统(三)

Unity技能系统(三)

Unity技能系统(一)

Unity技能系统(二)

Demo展示

六.Buff系统

buff分为增益和减益buff,应该区分开来;

本来计划是也用与或非来记录buff的,一个技能可能有多个buff,但是好像用list来存储也是一样的;

一个技能只能有两个buff图标,一个增益buff给自身,一个减益buff给敌人;

一个技能的增益和减益buff可能有多重效果;

比如:技能闪电——导致减速+感电+击退+自身增加狂暴(变态技能);

但是说这么说,写起来比较麻烦,就不那么细分了,一种效果一个图标单独计时;

这里面需求比较复杂,根据需求自行改写吧;

/// <summary>
/// Buff类型,可叠加
/// </summary>
public enum BuffType
{
    None,
    Burn = 2,           //点燃
    Slow = 4,           //减速
    Light = 8,          //感电
    Stun = 16,          //眩晕
    Poison = 32,        //中毒
    BeatBack = 64,      //击退
    BeatUp = 128,       //击飞
    Pull = 256,         //拉拽
    AddDefence = 512,
    RecoverHp = 1024,
}

1.BuffRun

挂载在拥有buff的物体上,计算buff的效果,如减伤,掉血,减速等;

同时负责buff计时,提供buff计时刷新接口供重复buffIcon调用(也可叠加buff层数按需求);

使用了静态方法初始化和静态链表存储了buff特效的信息,用来动态加载buff特效预制体;

public class BuffRun : MonoBehaviour
{
    private float durationTime;
    public BuffType bufftype;
    private float value;            //伤害或者加成

    private float interval;

    private float attackTimer;

    private float curTime;

    private CharacterStatus target;
    
    //添加buff时候初始化buffrun
    public void InitBuff(BuffType buffType,float duration,float value,float interval)
    {
        bufftype = buffType;
        
        if (buffType == BuffType.BeatBack || buffType == BuffType.BeatUp || buffType == BuffType.Pull)
            duration = 2f;
        
        durationTime = duration;
        this.value = value;
        this.interval = interval;
        curTime = 0;
    }
	
    //重置buff时间
    public void Reset()
    {
        attackTimer = 0;
        curTime = 0;
    }

    void Start()
    {
        curTime = 0;
        target = GetComponent<CharacterStatus>();
        StartCoroutine(ExcuteDamage());
    }

    private void Update()
    {
        curTime += Time.deltaTime;
        
        if(curTime > durationTime)
            Destroy(this);
    }
	
    //执行buff效果,支持多段影响
    private IEnumerator ExcuteDamage()
    {
        attackTimer = 0; //已持续攻击的时间

        do
        {
            //对敌人的影响
            TargetImpact();
            
            yield return new WaitForSeconds(interval);
            attackTimer += interval;
            //做伤害数值的计算
        } while (durationTime > attackTimer);
        
        Destroy(this);
    }

    private void TargetImpact()
    {
        //buff特效挂载点,有些buff挂载不在HitFxPos,所以写在上面
        Transform fxPosTf = target.HitFxPos;
		
        //根据不同buff做相应的效果响应
        if (bufftype == BuffType.Burn || bufftype == BuffType.Poison || bufftype == BuffType.Light)
            target.OnDamage(value, gameObject, true);
        else if (bufftype == BuffType.Slow)//减速
            fxPosTf = target.transform;
        else if (bufftype == BuffType.BeatBack)
        {
            Vector3 dir = -target.transform.position + GameObject.FindGameObjectWithTag("Player").transform.position;
            dir.y = 0;
            target.transform.DOMove(target.transform.position - dir.normalized * value,0.5f);
            durationTime = 2f;
        }
        else if (bufftype == BuffType.BeatUp)
        {
            target.transform.DOMove(target.transform.position - Vector3.up * value,0.5f);
            durationTime = 2f;
        }
        else if (bufftype == BuffType.AddDefence)
        {
            fxPosTf = target.transform;
            target.defence += value;
        }
        else if (bufftype == BuffType.RecoverHp)
        {
            target.OnDamage(-value, gameObject, true);
        }

        //挂载buff特效
        if (buffFx.ContainsKey(bufftype))
        {
            GameObject go = Resources.Load<GameObject>($"Skill/{buffFx[bufftype]}");
            GameObject buffGo = GameObjectPool.I.CreateObject(buffFx[bufftype], go, fxPosTf.position, fxPosTf.rotation);
            buffGo.transform.SetParent(fxPosTf);
            GameObjectPool.I.Destory(buffGo, interval);
        }
    }

    //存储buff特效名称和对应buff类型
    private static Dictionary<BuffType, string> buffFx = new Dictionary<BuffType, string>();
	//初始化buff特效信息
    public static void InitAllBuff()
    {
        buffFx.Add(BuffType.Burn,"Skill_32_R_Fly_100");
        buffFx.Add(BuffType.Light,"Skill_75_Cast");
        buffFx.Add(BuffType.Slow,"Skill_21_R_Fly_100");
        buffFx.Add(BuffType.Poison,"Skill_12_R_Fly_100");
        buffFx.Add(BuffType.AddDefence,"FX_CHAR_Aura");
        buffFx.Add(BuffType.RecoverHp,"FX_Heal_Light_Cast");
    }
	
    //获取buff剩余时间接口
    public float GetRemainTime()
    {
        return durationTime - curTime;
    }
    
    //buff结束恢复目标属性
    private void OnDisable()
    {
        if (bufftype == BuffType.Slow)
            ;
        else if (bufftype == BuffType.AddDefence)
            target.defence -= value;
    }
}

2.BuffIcon

buff图标类,显示倒计时数字显示;

这里写的不是很好,应该加载buffrun的同时加载bufficon,bufficon中不需要单独计时;

暂时改不动了=-=;

bufficon中添加buffRun字段,添加bufficon的同时,赋值buffrun;

通过buffrun获取buff类型和剩余倒计时;

这也用静态方法存储了bufficon的信息,用来动态加载,可以通过外部导入数据来存储;

public static Dictionary<BuffType, string> buffIconName = new Dictionary<BuffType, string>();

public static void InitBuffIconName()
{
    buffIconName.Add(BuffType.Burn,"Buff_13");
    buffIconName.Add(BuffType.Slow,"Buff_15");
    buffIconName.Add(BuffType.Stun,"Buff_12");
    buffIconName.Add(BuffType.Poison,"Buff_14");
    buffIconName.Add(BuffType.BeatBack,"Buff_5");
    buffIconName.Add(BuffType.BeatUp,"Buff_4");
    buffIconName.Add(BuffType.Pull,"Buff_6");
    buffIconName.Add(BuffType.AddDefence,"Buff_3");
    buffIconName.Add(BuffType.RecoverHp,"Buff_7");
    buffIconName.Add(BuffType.Light,"Buff_8");
}

这里写的不太行,参考一下吧;

public class BuffIcon : MonoBehaviour
{
    public Text textCD;
    public Image imgIcon;
    
    private float durationTime;
    private float curTime;

    public BuffType buffType;

    public void LoadIcon(BuffType buffType, float duration)
    {
        durationTime = duration;
        this.buffType = buffType;
        Sprite[] temp = Resources.LoadAll<Sprite>("BuffIcon/Buff");
        if (temp != null)
        {
            foreach (var sp in temp)
            {
                if (sp.name == SkillDeployer.buffIconName[buffType])
                {
                    imgIcon.sprite = Instantiate(sp);
                }
            }
        }
    }

    private void OnEnable()
    {
        curTime = 0;
    }

    void Update()
    {
        curTime += Time.deltaTime;
        
        textCD.text = (durationTime - curTime).ToString("F0");

        if (curTime > durationTime)
        {
            gameObject.SetActive(false);
            curTime = 0;
        }
    }

    public void Refresh()
    {
        //Debug.Log("已有buff刷新持续时间");
        curTime = 0;
    }
}

3.坑点

1.敌人uiPortrait的UI尽量不用使用延迟设置Active来控制显隐藏,Active为false时,bufficon将不再运行,等下次再显示uiPortrait时buff图标会显示错误;

2.采用每次造成伤害将当前目标的uiPortrait调整到显示位置,其他所有敌人的uiPortrait调整位置超出显示区域;

因此需要一个单例来存储所有的uiPortrait;就将它写在MonsterMgr中吧,反正也是用来管理敌人的,顺便管理一下敌人头像也没什么毛病;

提供三个方法,添加uiPortrait,删除uiPortrait,隐藏uiPortrait(移除显示区域);

private List<UIPortrait> allEnemyPortraits = new List<UIPortrait>();
public void AddEnemyPortraits(UIPortrait uiPortrait)
{
    allEnemyPortraits.Add(uiPortrait);
}
public void RemoveEnemyPortraits(UIPortrait uiPortrait)
{
    allEnemyPortraits.Remove(uiPortrait);
}
public void HideAllEnemyPortraits()
{
    foreach (var it in allEnemyPortraits)
    {
        it.GetComponent<RectTransform>().anchoredPosition = hidePos;
    }
}

4.扇形倒计时

UIbuff图标放置两层image组件物体,父节点设置透明度;

1636621558(1)

子物体image组件按下图设置,填充模式,填充百分比,以及顺逆时针;

1636621295

代码动态设置fillAmount的百分比;

//技能倒计时举例,写在Update中
float cd = csm.skills[i].coolRemain;
skillImgs[i].fillAmount = 1 - cd / csm.skills[i].skill.coolTime;
skillTexts[i].text = cd.ToString("F0");
if (skillTexts[i].text == "0")
    skillTexts[i].text = "";

效果:

121212312

posted @ 2021-11-11 20:18  小紫苏  阅读(2310)  评论(2编辑  收藏  举报