1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sf.magicproject.modifier;
23
24 import java.util.List;
25
26 import net.sf.magicproject.clickable.ability.Ability;
27 import net.sf.magicproject.clickable.targetable.card.MCard;
28 import net.sf.magicproject.event.MEventListener;
29 import net.sf.magicproject.stack.AdditionalCost;
30 import net.sf.magicproject.stack.StackManager;
31 import net.sf.magicproject.test.Test;
32
33 /***
34 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
35 * @since 0.86 Operation is used instead of add/remove boolean
36 * <code>hasNot</code>
37 */
38 public class AdditionalCostModifier extends Modifier {
39
40 /***
41 * Creates a new instance of ColorModifier <br>
42 *
43 * @param name
44 * the modifier name
45 * @param to
46 * the component to be attached to
47 * @param ability
48 * is the ability owning this test. The card component of this
49 * ability should correspond to the card owning this test too.
50 * @param whileCondition
51 * the test conditionning the activation of this modifier.
52 * @param linkedEvents
53 * the events to be registered. This events would cause this modifier
54 * to be updated.
55 * @param until
56 * this modifier exists while this test is true.
57 * @param linked
58 * is the creator is linked to this modifier.
59 * @param layer
60 * is the strategy used to add this modifier within the existing
61 * chain.
62 * @param additionalCost
63 * The additional cost.
64 */
65 AdditionalCostModifier(String name, MCard to, Ability ability,
66 Test whileCondition, List<MEventListener> linkedEvents,
67 List<MEventListener> until, boolean linked, int layer,
68 AdditionalCost additionalCost) {
69 super(name, to, ability, whileCondition, linkedEvents, until, linked, layer);
70 this.additionalCost = additionalCost;
71 }
72
73 @Override
74 public Modifier removeModifier(Modifier modifier) {
75 if (this == modifier) {
76 if (activated) {
77 StackManager.getInstance().getAdditionalCost().remove(additionalCost);
78 }
79 return next;
80 }
81 return super.removeModifier(modifier);
82 }
83
84 @Override
85 public final void removeFromManager() {
86 super.removeFromManager();
87 unregisteredModifier = true;
88 }
89
90 @Override
91 public void refresh() {
92 boolean oldActivated = activated;
93 activated = whileCondition.test(ability, null);
94
95
96 if (oldActivated != activated) {
97 if (activated)
98 StackManager.getInstance().getAdditionalCost().add(additionalCost);
99 else
100 StackManager.getInstance().getAdditionalCost().remove(additionalCost);
101 }
102 }
103
104 /***
105 * The additional cost.
106 */
107 protected final AdditionalCost additionalCost;
108
109 }