Unity实战篇:Buff系统设计(二:具体代码和实战使用)

首先是Buff种类

    public enum BuffKind
    {
        FreezeBuff = 1,
        GodDefendBuff = 2,
        SteelHeartBuff = 3,
    }

BuffBase

public abstract class BuffBase
    {
        /// 
        /// Buff的类型
        /// 
        public BuffKind m_BuffKind;

        /// 
        /// 计时器
        /// 
        public float timer;

        /// 
        /// Buff持续时间,我们约定,Buff时间为0,则为瞬时Buff,只执行OnAdd
        /// 
        public float m_Length;

        /// 
        /// 所归属的实体
        /// 
        public CostumEntityLogic m_CostumEntityLogic;

        public BuffBase(CostumEntityLogic costumEntityLogic, BuffKind buffKind, float length)
        {
            m_CostumEntityLogic = costumEntityLogic;
            m_Length = length;
            m_BuffKind = buffKind;
            timer = 0;
        }

        /// 
        /// 当添加到实体时执行逻辑
        /// 
        public virtual void OnAdd()
        {
        }

        /// 
        /// 跟随实体每帧更新
        /// 
        public virtual void OnUpdate()
        {
        }

        /// 
        /// 当从实体移除时
        /// 
        public virtual void OnRemove()
        {
        }
    }

CostumEntityLogic

    /// 
    /// 为了添加Buff设计的自定义EntityLogic
    /// 
    public class CostumEntityLogic : MonoBehaviour
    {
        public List m_Buffs = new List();

        /// 
        /// 被冰冻
        /// 
        public bool IsFreeze { get; set; }

        /// 
        /// 是否处于无敌状态
        /// 
        public bool IsGodDefend { get; set; }


        /// 
        /// 得到无敌状态
        /// 
        public virtual void GetGodDefend()
        {
        }

        /// 
        /// 失去无敌状态
        /// 
        public virtual void LoseGodDefend()
        {
        }

        /// 
        /// 得到钢铁之心状态
        /// 
        public virtual void GetSteelHeart()
        {
        }
        
        /// 
        /// 失去钢铁之心状态
        /// 
        public virtual void LoseSteelHeart()
        {
        }
        
        public void AddBuff(BuffBase buffNeed2Add)
        {
            m_Buffs.Add(buffNeed2Add);
            buffNeed2Add.OnAdd();
        }

        private void Update()
        {
            ReFreshBuff();
        }

        public void ReFreshBuff()
        {
            for (int i = m_Buffs.Count - 1; i >= 0; i--)
            {
                m_Buffs[i].OnUpdate();
            }
        }

        public void RemoveBuff(BuffBase buffNeed2Remove)
        {
            m_Buffs.Remove(buffNeed2Remove);
            buffNeed2Remove.OnRemove();
        }
    }

这里我们以冰冻Buff为例

     /// 
    /// 冰冻Buff
    /// 
    public class FreezeBuff : BuffBase
    {
        public FreezeBuff(CostumEntityLogic costumEntityLogic, BuffKind buffKind, float length) : base(costumEntityLogic, buffKind,
            length)
        {
        }

        public override void OnAdd()
        {
            base.OnAdd();
            m_CostumEntityLogic.IsFreeze = true;
        }


        public override void OnUpdate()
        {
            base.OnUpdate();
            if (timer >= m_Length)
            {
                m_CostumEntityLogic.IsFreeze = false;
                m_CostumEntityLogic.RemoveBuff(this);
            }

            timer += Time.fixedDeltaTime;
        }

        public override void OnRemove()
        {
            base.OnRemove();
            GC.Collect();
        }
    }

当我们吃到冰冻道具

我们给具体的怪物类加上时长为1.5秒的冰冻Buff,这样他在Buff持续时间内就不会动了

AddBuff(new FreezeBuff(this, BuffKind.FreezeBuff, 1.5f));

Unity实战篇:Buff系统设计(二:具体代码和实战使用)_第1张图片

关于更多本Buff系统,可以去看我在码云的开源项目,重制版坦克大战(因为使用了GF框架,所以大家可以忽略其他部分的代码,只看Buff系统部分的),欢迎大家提建议

https://blog.csdn.net/qq_15020543/article/details/86760793

你可能感兴趣的:(Unity相关,GameFramework,Unity实战汇总)