1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.magicproject.modifier;
22
23 import java.util.List;
24
25 import net.sf.magicproject.clickable.ability.Ability;
26 import net.sf.magicproject.clickable.targetable.card.MCard;
27 import net.sf.magicproject.event.MEventListener;
28 import net.sf.magicproject.operation.Add;
29 import net.sf.magicproject.operation.Operation;
30 import net.sf.magicproject.operation.Remove;
31 import net.sf.magicproject.stack.StackManager;
32 import net.sf.magicproject.test.Test;
33
34 /***
35 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36 * @since 0.82
37 */
38 public class AbilityModifier extends Modifier {
39
40 /***
41 * Creates a new instance of AbilityModifier <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 op
63 * the operation applied to previous value with the value of this
64 * modifier.
65 * @param abilities
66 * the list of abilities added/removed by this modifier
67 */
68 AbilityModifier(String name, MCard to, Ability ability, Test whileCondition,
69 List<MEventListener> linkedEvents, List<MEventListener> until,
70 boolean linked, int layer, Operation op, Ability[] abilitiesToAdd) {
71 super(name, to, ability, whileCondition, linkedEvents, until, linked, layer);
72 this.abilitiesToAdd = abilitiesToAdd;
73 this.op = op;
74 }
75
76 @Override
77 public Modifier removeModifier(Modifier modifier) {
78 if (this == modifier) {
79 if (activated) {
80 StackManager.postRefreshAbilities(to);
81 }
82 return next;
83 }
84 return super.removeModifier(modifier);
85 }
86
87 @Override
88 public final void removeFromManager() {
89 super.removeFromManager();
90 if (!unregisteredModifier) {
91 to.removeModifier(this);
92 if (activated && op == Add.getInstance()) {
93
94 for (Ability ability : abilitiesToAdd) {
95 ability.removeFromManager();
96 }
97 }
98 }
99 unregisteredModifier = true;
100 }
101
102 @Override
103 public void refresh() {
104 boolean oldActivated = activated;
105 activated = whileCondition.test(ability, to);
106
107
108 if (oldActivated != activated) {
109 StackManager.postRefreshAbilities(to);
110 }
111 }
112
113 /***
114 * Add/ remove ability of attached object
115 *
116 * @param toAdd
117 * list of abilities attached to the object
118 */
119 public void calculateDeltaAbilities(List<Ability> toAdd) {
120 if (activated) {
121 if (op == Add.getInstance()) {
122
123 for (Ability ability : abilitiesToAdd) {
124 toAdd.add(ability);
125 }
126 } else if (op == Remove.getInstance()) {
127
128 for (Ability ability : abilitiesToAdd) {
129 toAdd.remove(ability);
130 }
131 } else {
132
133 toAdd.clear();
134 }
135 }
136 if (next != null) {
137 ((AbilityModifier) next).calculateDeltaAbilities(toAdd);
138 }
139 }
140
141 /***
142 * Indicates the operation applied to previous value with the value of this
143 * modifier. Only Remove/add/clear instance.
144 */
145 protected Operation op;
146
147 /***
148 * The list of abilities added/removed by this modifier
149 */
150 private Ability[] abilitiesToAdd;
151 }