public void ExecuteActionWithGlobalTrigger_UnitDealsDamageToAnotherUnitWithDamage_EventsWasFiredOnBothActions() { EventManager gem = new EventManager(); ActionManager actman = new ActionManager(gem); Unit A = new Unit(); Unit B = new Unit(); A.ActionManager = actman; B.ActionManager = actman; int dmg = int.MaxValue; int actualTimesFired = 0; Trigger T = new Trigger<UnitTakesDamagePostEvent>(_ => actualTimesFired++); EntityXmasAction ga1 = new DamageUnitTargetAction(B, dmg); EntityXmasAction ga2 = new DamageUnitTargetAction(A, dmg); gem.Register(T); gem.AddEntity(A); gem.AddEntity(B); A.QueueAction(ga1); B.QueueAction(ga2); actman.ExecuteActions(); int expectedTimeFired = 2; Assert.AreEqual(expectedTimeFired, actualTimesFired); }
public void AddTrigger_triggerIsAddedToUnitAfterItIsAddedToManagger_EventFired() { EventManager gem = new EventManager(); ActionManager actman = new ActionManager(gem); Unit A = new Unit(); Unit B = new Unit(); A.ActionManager = actman; B.ActionManager = actman; int dmg = int.MaxValue; bool eventFired = false; Trigger T = new Trigger<UnitTakesDamagePostEvent>(_ => eventFired = true); EntityXmasAction ga1 = new DamageUnitTargetAction(B, dmg); gem.AddEntity(B); B.Register(T); A.QueueAction(ga1); actman.ExecuteActions(); Assert.IsTrue(eventFired); }
public void Register(Trigger trigger) { foreach (Type evt in trigger.Events) { triggers.Add(evt, trigger); } if (trigger is MultiTrigger) regMulti((MultiTrigger) trigger); }
public void Deregister(Trigger trigger) { foreach (Type t in trigger.Events) { triggers.Remove(t, trigger); } if (trigger is MultiTrigger) deregMulti((MultiTrigger) trigger); }
/// <summary> /// Registers a trigger to the EventManager, once registered the trigger will be triggered each time an event is raised on the EventManager that shares an event with the trigger /// </summary> /// <param name="trigger">The trigger that is registered</param> public void Register(Trigger trigger) { triggerManager.Register(trigger); }
/// <summary> /// Deregisters a trigger from the Eventmanager so it will no longer be triggered /// </summary> /// <param name="trigger">The trigger that is deregistered from the eventmanager</param> public void Deregister(Trigger trigger) { triggerManager.Deregister(trigger); }
protected override void Execute() { var spell = card.ConstructSpell(); this.CastedSpell = spell; int index = 0; if (IllegalManaUsed()) throw new ManaMismatchException(card, castingPlayer, selectedMana); else { if(selectedMana.Length > 0) this.castingPlayer.ManaStorage.Spend(selectedMana); foreach (IEnumerable<object> targetList in targets) { spell.SetTarget(index, targetList.ToArray()); index++; } this.EventManager.Raise(new EnqueueAbilityEvent(spell)); this.RunAction(new ResetPrioritiesAction()); Trigger<DequeueAbilityEvent> trig = null; Action<DequeueAbilityEvent> action = evt => { this.card.Owner.Library.AddBottom(this.CastedCard); this.EventManager.Deregister(trig); }; trig = new Trigger<DequeueAbilityEvent>(evt => evt.Ability == spell,action); this.EventManager.Register(trig); } }
public void RemoveTrigger_triggerIsRemovedFromUnit_NoEventFired() { EventManager gem = new EventManager(); ActionManager actman = new ActionManager(gem); Unit A = new Unit(); Unit B = new Unit(); A.ActionManager = actman; B.ActionManager = actman; int dmg = int.MaxValue; bool eventFired = false; Trigger T = new Trigger<UnitTakesDamagePostEvent>(_ => eventFired = true); EntityXmasAction ga1 = new DamageUnitTargetAction(B, dmg); B.Register(T); gem.AddEntity(B); B.Deregister(T); A.QueueAction(ga1); Assert.IsFalse(eventFired); }
public void ExecuteActionWithSpecificTargetEvent_UnitDealsDamageToAnotherUnit_TheOtherUnitTakesDamage() { EventManager gem = new EventManager(); ActionManager actman = new ActionManager(gem); Unit expectedDealer = new Unit(); Unit expectedTaker = new Unit(); expectedDealer.ActionManager = actman; expectedTaker.ActionManager = actman; int expectedDmg = 10; //ignore initialization values Unit actualDealer = null; Unit actualTaker = null; int actualDmg = new int(); Trigger t = new Trigger<UnitTakesDamagePostEvent>(e => { actualDealer = e.Source; actualTaker = e.Target; actualDmg = e.Damage; }); DamageUnitTargetAction ga = new DamageUnitTargetAction(expectedTaker, expectedDmg); expectedTaker.Register(t); gem.AddEntity(expectedTaker); expectedDealer.QueueAction(ga); actman.ExecuteActions(); Assert.AreEqual(expectedDealer, actualDealer); Assert.AreEqual(expectedTaker, actualTaker); Assert.AreEqual(expectedDmg, actualDmg); }
public void ExecuteActionWithSpecificTargetEvent_UnitDealsDamageToAnotherUnitWithDamagePrevetionImplemented_TheTargetUnitTakesNoDamage() { EventManager gem = new EventManager(); ActionManager actman = new ActionManager(gem); Unit expectedDealer = new Unit(); Unit expectedTaker = new Unit(); expectedDealer.ActionManager = actman; expectedTaker.ActionManager = actman; int dmg = 10; int prevent = 10; int expectedDmg = 10; //ignore initialization values Unit actualDealer = null; Unit actualTaker = null; int actualDmg = new int(); Trigger preT = new Trigger<UnitTakesDamagePreEvent>(e => e.ModDmgPreMultiplier(-prevent)); Trigger postT = new Trigger<UnitTakesDamagePostEvent>(e => { actualDealer = e.Source; actualTaker = e.Target; actualDmg = e.Damage; }); EntityXmasAction ga = new DamageUnitTargetAction(expectedTaker, dmg); expectedTaker.Register(preT); expectedTaker.Register(postT); gem.AddEntity(expectedTaker); expectedDealer.QueueAction(ga); actman.ExecuteActions(); Assert.AreEqual(expectedDealer, actualDealer); Assert.AreEqual(expectedTaker, actualTaker); Assert.AreEqual(expectedDmg, actualDmg); }