小妖精的完美游戏教室——buff系统

作者:小妖精Balous,未经作者允许,任何个人与单位不得将此源代码用于商业化项目

#region buff

/// <summary>
/// 是否魔法免疫,魔法免疫的生物不会受到除自己以外的生物施放的buff
/// </summary>
public bool isMagicalImmunity
{
set;
get;
}
/// <summary>
/// 添加buff前触发
/// </summary>
public event AddBuff OnAddBuff;
/// <summary>
/// 增益buff集合
/// </summary>
private LinkedList<Buff> buffList;
/// <summary>
/// 减益buff集合
/// </summary>
private LinkedList<Buff> debuffList;
/// <summary>
/// 执行所有buff效果
/// </summary>
private void buffExecute()
{
if (buffList.Count != 0)
{
for (var item = buffList.First; item != null;)
{
//如果buff有效时间结束,移除buff,否则执行buff效果
if (item.Value.activeTime <= 0)
{
item.Value.Exit();
var next = item.Next;
buffList.Remove(item);
item = next;
continue;
}
else
{
item.Value.Execute();
item.Value.activeTime -= Time.deltaTime;
}
item = item.Next;
}
}
if (debuffList.Count != 0)
{
for (var item = debuffList.First; item != null;)
{
//如果buff有效时间结束,移除buff,否则执行buff效果
if (item.Value.activeTime <= 0)
{
item.Value.Exit();
var next = item.Next;
debuffList.Remove(item);
item = next;
continue;
}
else
{
item.Value.Execute();
item.Value.activeTime -= Time.deltaTime;
}
item = item.Next;
}
}
}
/// <summary>
/// 驱散除击退型硬直外的所有buff
/// </summary>
public void dispellAll()
{
dispellBuff();
dispellDebuff();
}
/// <summary>
/// 驱散所有增益buff
/// </summary>
public void dispellBuff()
{
foreach (Buff buff in buffList) buff.Exit();
buffList.Clear();

}
/// <summary>
/// 驱散除击退型硬直外的所有减益buff
/// </summary>
public void dispellDebuff()
{
LinkedList<Buff> KnockBuffs = new LinkedList<Buff>();
foreach (Buff debuff in debuffList)
{
if (debuff.GetType() == typeof(KnockbackByHit))
{
KnockBuffs.AddLast(debuff);
continue;
}
debuff.Exit();
}
debuffList.Clear();
debuffList = KnockBuffs;
}
/// <summary>
/// 添加不能重叠的buff
/// </summary>
/// <param name="newBuff">新buff</param>
public void buffAddSingle(Buff newBuff)
{
//禁止添加击退型硬直buff
if (newBuff.GetType() == typeof(KnockbackByHit)) return;

//触发添加buff事件
if (OnAddBuff != null)
{
BuffAddingEventArgs e = new BuffAddingEventArgs(newBuff);
OnAddBuff(this, e);

//如果添加buff被撤销,不能添加
if (e.Cancel) return;
}

//如果魔法免疫,不能添加其它生物施放的buff
if (isMagicalImmunity && newBuff.source.gameObject != gameObject) return;

//如果是增益buff,添加进buffList
if (newBuff.buffType == BuffType.Buff)
{
//如果新buff已经存在,则刷新buff
if (buffList.Count != 0)
{
for (var buff = buffList.First; buff != buffList.Last.Next; buff = buff.Next)
{
//如果类型跟施法者相同,视为同一个buff
if (buff.Value.GetType() == newBuff.GetType() && buff.Value.source.gameObject == newBuff.source.gameObject)
{
//如果新buff强度更高,则刷新整个buff
if (buff.Value.level <= newBuff.level)
{
buff.Value.Exit();
buff.Value = newBuff;
newBuff.owner = this;
newBuff.Enter();
newBuff.activeTime = newBuff.initialActiveTime;
}
return;
}
}
}
buffList.AddLast(newBuff);
newBuff.owner = this;
newBuff.Enter();
newBuff.activeTime = newBuff.initialActiveTime;
}
//如果是减益buff,添加进debuffList
else
{
if (debuffList.Count != 0)
{
for (var debuff = debuffList.First; debuff != debuffList.Last.Next; debuff = debuff.Next)
{
//如果类型跟施法者相同,视为同一个buff
if (debuff.Value.GetType() == newBuff.GetType() && debuff.Value.source.gameObject == newBuff.source.gameObject)
{
//如果新buff强度更高,则刷新整个buff
if (debuff.Value.level <= newBuff.level)
{
debuff.Value.Exit();
debuff.Value = newBuff;
newBuff.owner = this;
newBuff.Enter();
newBuff.activeTime = newBuff.initialActiveTime;
}
return;
}
}
}
debuffList.AddLast(newBuff);
newBuff.owner = this;
newBuff.Enter();
newBuff.activeTime = newBuff.initialActiveTime;
}
}
/// <summary>
/// 添加可以重叠的buff
/// </summary>
/// <param name="newBuff">新buff</param>
public void buffAddMult(Buff newBuff)
{
//禁止添击退型硬直buff
if (newBuff.GetType() == typeof(KnockbackByHit)) return;

//触发添加buff事件
if (OnAddBuff != null)
{
BuffAddingEventArgs e = new BuffAddingEventArgs(newBuff);
OnAddBuff(this, e);

//如果添加buff被撤销,不能添加
if (e.Cancel) return;
}

//如果魔法免疫,不能添加其它生物施放的buff
if (isMagicalImmunity && newBuff.source.gameObject != gameObject) return;

//如果是增益buff,添加进buffList
if (newBuff.buffType == BuffType.Buff)
{
buffList.AddLast(newBuff);
newBuff.owner = this;
newBuff.Enter();
newBuff.activeTime = newBuff.initialActiveTime;
}
//如果是减益buff,添加进debuffList
else
{
debuffList.AddLast(newBuff);
newBuff.owner = this;
newBuff.Enter();
newBuff.activeTime = newBuff.initialActiveTime;
}
}

#endregion

原文地址:https://www.cnblogs.com/balous/p/7106127.html