Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event refactor & multi callback support in recipes #50

Merged
merged 5 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions ModiBuff/ModiBuff.Benchmarks/BenchTempTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class BenchTempTests : ModifierBenches

private IStackEffect[] _stackEffects;

private Unit _eventUnit;
private EmptyUnit _emptyUnit;

private bool _condition, _conditionTwo;
Expand Down Expand Up @@ -111,7 +110,6 @@ public override void GlobalSetup()
new AddDamageEffect(5), new AddDamageEffect(5, true)
};

_eventUnit = new Unit();
_emptyUnit = new EmptyUnit();*/

_condition = false;
Expand Down Expand Up @@ -412,36 +410,6 @@ public void ArrayPool()
ArrayPool<IStackRevertEffect>.Shared.Return(revertEffectsTemp);
}

//[Benchmark]
public void NullCheckAsCast()
{
IUnit inUnit = _eventUnit;
(inUnit as IEventOwner<EffectOnEvent>)?.ResetEventCounters();
}

//[Benchmark]
public void NullCheckIfIsCast()
{
IUnit inUnit = _eventUnit;
if (inUnit is IEventOwner<EffectOnEvent> eventTarget)
eventTarget.ResetEventCounters();
}

//[Benchmark]
public void NullCheckAsCastEmptyUnit()
{
IUnit inUnit = _emptyUnit;
(inUnit as IEventOwner<EffectOnEvent>)?.ResetEventCounters();
}

//[Benchmark]
public void NullCheckIfIsCastEmptyUnit()
{
IUnit inUnit = _emptyUnit;
if (inUnit is IEventOwner<EffectOnEvent> eventTarget)
eventTarget.ResetEventCounters();
}

[Benchmark]
public void IfElse()
{
Expand Down
2 changes: 1 addition & 1 deletion ModiBuff/ModiBuff.Benchmarks/BenchmarkUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class BenchmarkUnit : IUnit, IModifierOwner, IDamagable, IUpdatabl

public ModifierController ModifierController { get; }

private const int MaxRecursionEventCount = 1;
private const int MaxRecursionCallbackCount = 1;

public BenchmarkUnit(float health, UnitType unitType = UnitType.Good)
{
Expand Down
17 changes: 9 additions & 8 deletions ModiBuff/ModiBuff.Tests/ApplierTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ public void NestedStackApplier()
.Effect(new ApplierEffect("ComplexApplier_Disarm"), EffectOn.Stack)
.Stack(WhenStackEffect.EveryXStacks, everyXStacks: 5);
AddRecipe("ComplexApplier_OnHit_Event")
.Effect(new ApplierEffect("ComplexApplier_Rupture", targeting: Targeting.SourceTarget), EffectOn.Event)
.Event(EffectOnEvent.WhenAttacked);
.Effect(new ApplierEffect("ComplexApplier_Rupture", targeting: Targeting.SourceTarget),
EffectOn.CallbackUnit)
.CallbackUnit(CallbackUnitType.WhenAttacked);
Setup();

Unit.AddModifierSelf("ComplexApplier_OnHit_Event");
Expand Down Expand Up @@ -156,21 +157,21 @@ public void AddDamageStacksEventsAppliers()
.Remove(5).Refresh();
AddRecipe("ComplexApplier2_WhenAttacked_Event")
.Effect(new ApplierEffect("ComplexApplier2_AddDamageAdd", targeting: Targeting.SourceTarget),
EffectOn.Event)
.Event(EffectOnEvent.WhenAttacked)
EffectOn.CallbackUnit)
.CallbackUnit(CallbackUnitType.WhenAttacked)
.Remove(5).Refresh();
AddRecipe("ComplexApplier2_OnAttack_Event")
.Effect(new ApplierEffect("ComplexApplier2_WhenAttacked_Event"), EffectOn.Event)
.Event(EffectOnEvent.OnAttack)
.Effect(new ApplierEffect("ComplexApplier2_WhenAttacked_Event"), EffectOn.CallbackUnit)
.CallbackUnit(CallbackUnitType.OnAttack)
.Remove(60).Refresh();
AddRecipe("ComplexApplier2_WhenHealed")
.Effect(new ApplierEffect("ComplexApplier2_OnAttack_Event"), EffectOn.Stack)
.Stack(WhenStackEffect.EveryXStacks, everyXStacks: 5)
.Remove(5).Refresh();
AddRecipe("ComplexApplier2_WhenHealed_Event")
.Effect(new ApplierEffect("ComplexApplier2_WhenHealed", targeting: Targeting.SourceTarget),
EffectOn.Event)
.Event(EffectOnEvent.WhenHealed);
EffectOn.CallbackUnit)
.CallbackUnit(CallbackUnitType.WhenHealed);
Setup();

//Add damage on 4 stacks buff, that you give someone when they heal you 5 times, for 60 seconds.
Expand Down
47 changes: 47 additions & 0 deletions ModiBuff/ModiBuff.Tests/CallbackEffectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,52 @@ public void DamageOnStun_HealOnAnyNotStunStatusEffectRemoved()
Unit.Update(1);
Assert.AreEqual(UnitHealth, Unit.Health);
}

[Test]
public void MultipleEffectCallbacks()
{
AddRecipe("DamageEffectCallbacksRemoveOnDisarm")
.Effect(new DamageEffect(1), EffectOn.CallbackEffect)
.CallbackEffect(CallbackType.StatusEffectAdded, effect => new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Stun))
((ICallbackEffect)effect).CallbackEffect(target, source);
}))
.Effect(new DamageEffect(2), EffectOn.CallbackEffect2)
.CallbackEffect(CallbackType.StatusEffectAdded, effect => new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Freeze))
((ICallbackEffect)effect).CallbackEffect(target, source);
}))
.Effect(new DamageEffect(3), EffectOn.CallbackEffect3)
.CallbackEffect(CallbackType.StatusEffectAdded, effect => new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Root))
((ICallbackEffect)effect).CallbackEffect(target, source);
}))
.Remove(RemoveEffectOn.CallbackEffect4)
.CallbackEffect(CallbackType.StatusEffectAdded, effect => new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Disarm))
effect.Effect(target, source);
}));
Setup();

Unit.AddModifierSelf("DamageEffectCallbacksRemoveOnDisarm");
Unit.StatusEffectController.ChangeStatusEffect(0, 0, StatusEffectType.Stun, 1f, Unit);
Assert.AreEqual(UnitHealth - 1, Unit.Health);
Unit.StatusEffectController.ChangeStatusEffect(1, 0, StatusEffectType.Freeze, 1f, Unit);
Assert.AreEqual(UnitHealth - 1 - 2, Unit.Health);
Unit.StatusEffectController.ChangeStatusEffect(2, 0, StatusEffectType.Root, 1f, Unit);
Assert.AreEqual(UnitHealth - 1 - 2 - 3, Unit.Health);
Unit.StatusEffectController.ChangeStatusEffect(3, 0, StatusEffectType.Disarm, 1f, Unit);
Unit.Update(0);
Assert.AreEqual(UnitHealth - 1 - 2 - 3, Unit.Health);
Assert.False(Unit.ContainsModifier("DamageEffectCallbacksRemoveOnDisarm"));
}
}
}
39 changes: 39 additions & 0 deletions ModiBuff/ModiBuff.Tests/CallbackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,44 @@ public void Init_RegisterTimerCallback_TogglableState()
Enemy.Update(Unit.CallbackTimerCooldown);
Assert.AreEqual(EnemyDamage + 5, Enemy.Damage);
}

[Test]
public void MultipleEffectUnitCallbacks()
{
AddRecipe("DamageEffectUnitCallbacks")
.Effect(new DamageEffect(1), EffectOn.CallbackEffectUnits)
.CallbackEffectUnits(CallbackType.StatusEffectAdded, effect => (origTarget, origSource) =>
new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Stun))
((ICallbackEffect)effect).CallbackEffect(origTarget, origSource);
}))
.Effect(new DamageEffect(2), EffectOn.CallbackEffectUnits2)
.CallbackEffectUnits(CallbackType.StatusEffectAdded, effect => (origTarget, origSource) =>
new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Freeze))
((ICallbackEffect)effect).CallbackEffect(origTarget, origSource);
}))
.Effect(new DamageEffect(3), EffectOn.CallbackEffectUnits3)
.CallbackEffectUnits(CallbackType.StatusEffectAdded, effect => (origTarget, origSource) =>
new StatusEffectEvent(
(target, source, appliedStatusEffect, oldLegalAction, newLegalAction) =>
{
if (appliedStatusEffect.HasStatusEffect(StatusEffectType.Root))
((ICallbackEffect)effect).CallbackEffect(origTarget, origSource);
}));
Setup();

Unit.AddModifierSelf("DamageEffectUnitCallbacks");
Unit.StatusEffectController.ChangeStatusEffect(0, 0, StatusEffectType.Stun, 1f, Unit);
Assert.AreEqual(UnitHealth - 1, Unit.Health);
Unit.StatusEffectController.ChangeStatusEffect(1, 0, StatusEffectType.Freeze, 1f, Unit);
Assert.AreEqual(UnitHealth - 1 - 2, Unit.Health);
Unit.StatusEffectController.ChangeStatusEffect(2, 0, StatusEffectType.Root, 1f, Unit);
Assert.AreEqual(UnitHealth - 1 - 2 - 3, Unit.Health);
}
}
}
Loading
Loading