コード例 #1
0
        private bool _AddBuff(Buff buff)
        {
            // look up or create a buff list for the target, then add/stack the buff according to its class type.

            // the logic is a bit more complex that it seems necessary because we ensure the buff appears in the
            // active buff list before calling Apply(), if Apply() fails we undo adding it. This allows buffs to
            // recursively add/stack more of their own buff type without worrying about overwriting existing buffs.
            if (_buffs.ContainsKey(buff.Target))
            {
                Type buffType     = buff.GetType();
                Buff existingBuff = _buffs[buff.Target].FirstOrDefault(b => b != null && b.GetType() == buffType);
                if (existingBuff != null)
                {
                    if (existingBuff.Stack(buff))
                    {
                        return(true);
                    }
                    // buff is non-stacking, just add normally
                }

                _buffs[buff.Target].Add(buff);
                if (buff.Apply())
                {
                    return(true);
                }
                else
                {
                    _buffs[buff.Target].Remove(buff);
                    return(false);
                }
            }
            else
            {
                var keyBuffs = new List <Buff>();
                keyBuffs.Add(buff);
                _buffs[buff.Target] = keyBuffs;
                if (buff.Apply())
                {
                    return(true);
                }
                else
                {
                    _buffs.Remove(buff.Target);
                    return(false);
                }
            }
        }