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.event;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import net.sf.magicproject.clickable.ability.Ability;
28 import net.sf.magicproject.clickable.targetable.Targetable;
29 import net.sf.magicproject.clickable.targetable.card.MCard;
30 import net.sf.magicproject.event.context.MContextCardCardIntInt;
31 import net.sf.magicproject.expression.Expression;
32 import net.sf.magicproject.expression.ExpressionFactory;
33 import net.sf.magicproject.test.Test;
34
35 /***
36 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37 */
38 public class ModifiedProperty extends TriggeredEvent {
39
40 /***
41 * Creates a new instance of MEventModifiedProperty <br>
42 * <ul>
43 * Structure of InputStream : Data[size]
44 * <li>idZone [1]</li>
45 * <li>test [...]</li>
46 * <li>property : Expression [...]</li>
47 * </ul>
48 *
49 * @param inputFile
50 * is the file containing this event
51 * @param card
52 * is the card owning this event
53 * @throws IOException
54 * if error occurred during the reading process from the specified
55 * input stream
56 */
57 ModifiedProperty(InputStream inputFile, MCard card) throws IOException {
58 super(inputFile, card);
59 propertyExpr = ExpressionFactory.readNextExpression(inputFile);
60 }
61
62 /***
63 * Creates a new instance of MEventModifiedProperty <br>
64 *
65 * @param idZone
66 * the place constraint to activate this event
67 * @param test
68 * the test of this event
69 * @param card
70 * is the card owning this card
71 * @param propertyExpr
72 */
73 public ModifiedProperty(int idZone, Test test, MCard card,
74 Expression propertyExpr) {
75 super(idZone, test, card);
76 this.propertyExpr = propertyExpr;
77 }
78
79 /***
80 * Return a copy of this with the specified owner
81 *
82 * @param card
83 * is the card of the ability of this event
84 * @return copy of this event
85 */
86 @Override
87 public MEventListener clone(MCard card) {
88 return new ModifiedProperty(idZone, test, card, propertyExpr);
89 }
90
91 /***
92 * Tell if the current event matches with this event. If there is an
93 * additional code to check, it'would be checked if the main event matches
94 * with the main event
95 *
96 * @param ability
97 * is the ability owning this test. The card component of this
98 * ability should correspond to the card owning this test too.
99 * @param target
100 * the target owning the modified property.
101 * @param idType
102 * the modified property
103 * @return true if the current event match with this event
104 */
105 public boolean isMatching(Ability ability, Targetable target, int idType) {
106 return this.propertyExpr.getValue(ability, target, null) == idType
107 && test(ability, target);
108 }
109
110 /***
111 * Dispatch this event to all active event listeners able to understand this
112 * event. The listening events able to understand this event are <code>this
113 * </code>
114 * and other multiple event listeners. For each event listeners having
115 * responded they have been activated, the corresponding ability is added to
116 * the triggered buffer zone of player owning this ability
117 *
118 * @param target
119 * is the targetable owning the modified property
120 * @param idType
121 * the modified property
122 * @see #isMatching(Ability, Targetable, int)
123 */
124 public static void dispatchEvent(Targetable target, int idType) {
125 for (Ability ability : TRIGGRED_ABILITIES.get(EVENT)) {
126 if (ability.isMatching()
127 && ((ModifiedProperty) ability.eventComing()).isMatching(ability,
128 target, idType)) {
129 ability.triggerIt(new MContextCardCardIntInt(target, idType));
130 }
131 }
132 }
133
134 @Override
135 public final Event getIdEvent() {
136 return EVENT;
137 }
138
139 /***
140 * The event type.
141 */
142 public static final Event EVENT = Event.MODIFIED_PROPERTY;
143
144 /***
145 * Is the property to match during each test
146 */
147 protected Expression propertyExpr;
148
149 }