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 ModifiedIdCard extends TriggeredEvent {
39
40 /***
41 * Creates a new instance of ModifiedIdCard <br>
42 * <ul>
43 * Structure of InputStream : Data[size]
44 * <li>[super]</li>
45 * <li>test [Test]</li>
46 * <li>idCard [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 ModifiedIdCard(InputStream inputFile, MCard card) throws IOException {
58 super(inputFile, card);
59 idCard = ExpressionFactory.readNextExpression(inputFile);
60 }
61
62 /***
63 * Creates a new instance of MEventModifiedIdCard <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 idCard
72 */
73 public ModifiedIdCard(int idZone, Test test, MCard card, Expression idCard) {
74 super(idZone, test, card);
75 this.idCard = idCard;
76 }
77
78 @Override
79 public MEventListener clone(MCard card) {
80 return new ModifiedIdCard(idZone, test, card, idCard);
81 }
82
83 /***
84 * Tell if the current event matches with this event. If there is an
85 * additional code to check, it'would be checked if the main event matches
86 * with the main event
87 *
88 * @param ability
89 * is the ability owning this test. The card component of this
90 * ability should correspond to the card owning this test too.
91 * @param target
92 * is the targetable owning the modified idcard
93 * @param idCard
94 * the modified color
95 * @return true if the current event match with this event
96 */
97 public boolean isMatching(Ability ability, Targetable target, int idCard) {
98 return (this.idCard.getValue(ability, target, null) & idCard) != 0
99 && test(ability, target);
100 }
101
102 /***
103 * Dispatch this event to all active event listeners able to understand this
104 * event. The listening events able to understand this event are <code>this
105 * </code>
106 * and other multiple event listeners. For each event listeners having
107 * responded they have been activated, the corresponding ability is added to
108 * the triggered buffer zone of player owning this ability
109 *
110 * @param target
111 * is the targetable owning the modified property
112 * @param idCard
113 * the modified card type
114 * @see #isMatching(Ability, Targetable, int)
115 */
116 public static void dispatchEvent(Targetable target, int idCard) {
117 for (Ability ability : TRIGGRED_ABILITIES.get(EVENT)) {
118 if (ability.isMatching()
119 && ((ModifiedIdCard) ability.eventComing()).isMatching(ability,
120 target, idCard)) {
121 ability.triggerIt(new MContextCardCardIntInt(target, idCard));
122 }
123 }
124 }
125
126 @Override
127 public final Event getIdEvent() {
128 return EVENT;
129 }
130
131 /***
132 * The event type.
133 */
134 public static final Event EVENT = Event.MODIFIED_IDCARD;
135
136 /***
137 * The tested card identifier
138 */
139 private Expression idCard;
140
141 }