我有以下代码。
public class MpChange : CusEffect
{
bool AlreadyDo = false;
bool AlreadyStop = false;
public override void CondDel(object sender, CondEventArgs e)
{
if (e.CondMet && !AlreadyDo)
{
DoEffect();
AlreadyDo = true;
AlreadyStop = false;
}
else if (!e.CondMet && !AlreadyStop)
{
StopEffect();
AlreadyStop = true;
AlreadyDo = false;
}
}
public override void DoEffect()
{
Debug.Log(WhoEffect.name + "'s Mp changed +5");
}
public override void StopEffect()
{
Debug.Log(WhoEffect.name + "'s Mp changed -5");
}
}
当角色的hp满时,调用这个函数(然后是DoEffect()),如果没有满,则调用StopEffect()。
当角色的hp被改变时,事件被发布并且这个MpChange类是subscribe It。
在这种情况下,这段代码能整洁吗?
我讨厌使用这两个布尔变量(AlreadyDo,AlreadyStop),被混淆了。
一个布尔值就可以:
bool State = false;
public override void CondDel(object sender, CondEventArgs e)
{
if (e.CondMet == State) return; // nothing to do, state's the current one
State = !State; // flip the flag
if (State) { // do or undo a thing
DoEffect();
} else {
StopEffect();
}
}