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