View Javadoc

1   /*
2    * MEventBecomeTapped.java
3    * Created on 29 janv. 2004
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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.card.MCard;
29  import net.sf.magicproject.event.context.MContextCardCardIntInt;
30  import net.sf.magicproject.test.Or;
31  import net.sf.magicproject.test.Test;
32  
33  /***
34   * Event generated when a card is faced up.
35   * 
36   * @author <a href="mailto:hoani.cross@gmail.com">Hoani CROSS</a>
37   * @since 0.90
38   */
39  public class FacedUp extends TriggeredEvent {
40  
41  	/***
42  	 * Create an instance of FacedUp by reading a file Offset's file must pointing
43  	 * on the first byte of this event <br>
44  	 * <ul>
45  	 * Structure of InputStream : Data[size]
46  	 * <li>idZone [1]</li>
47  	 * <li>test [...]</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  	FacedUp(InputStream inputFile, MCard card) throws IOException {
59  		super(inputFile, card);
60  	}
61  
62  	/***
63  	 * Creates a new instance of FacedUp specifying all attributes of this class.
64  	 * All parameters are copied, not cloned. So this new object shares the card
65  	 * and the specified codes
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  	 */
74  	public FacedUp(int idZone, Test test, MCard card) {
75  		super(idZone, test, card);
76  	}
77  
78  	/***
79  	 * Return a copy of this with the specified owner
80  	 * 
81  	 * @param card
82  	 *          is the card of the ability of this event
83  	 * @return copy of this event
84  	 */
85  	@Override
86  	public MEventListener clone(MCard card) {
87  		return new FacedUp(idZone, test, card);
88  	}
89  
90  	/***
91  	 * Tell if the current event matches with this event. If there is an
92  	 * additional code to check, it'would be checked if the main event matches
93  	 * with the main event
94  	 * 
95  	 * @param facedUpCard
96  	 *          the became taped/untapped
97  	 * @param ability
98  	 *          is the ability owning this test. The card component of this
99  	 *          ability should correspond to the card owning this test too.
100 	 * @return true if the current event match with this event
101 	 */
102 	public boolean isMatching(Ability ability, MCard facedUpCard) {
103 		return test(ability, facedUpCard);
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 facedUpCard
115 	 *          the became taped/untapped
116 	 * @see #isMatching(Ability, MCard)
117 	 */
118 	public static void dispatchEvent(MCard facedUpCard) {
119 		for (Ability ability : TRIGGRED_ABILITIES.get(EVENT)) {
120 			if (ability.isMatching()
121 					&& ((FacedUp) ability.eventComing()).isMatching(ability, facedUpCard)) {
122 				ability.triggerIt(new MContextCardCardIntInt(facedUpCard));
123 			}
124 		}
125 	}
126 
127 	@Override
128 	public final Event getIdEvent() {
129 		return EVENT;
130 	}
131 
132 	/***
133 	 * The event type.
134 	 */
135 	public static final Event EVENT = Event.FACED_UP;
136 
137 	/***
138 	 * Create and returns an union of this event and the specified one. Both event
139 	 * must have the same type. Test(s) and events attributes may be groupped
140 	 * depending instance of this event. If no possible append is possible
141 	 * <code>null</code> is returned.
142 	 * 
143 	 * @param other
144 	 *          the event to append with 'or' operator.
145 	 * @return a new event reprensenting 'this' or 'other'
146 	 */
147 	@Override
148 	public MEventListener appendOr(MEventListener other) {
149 		return new FacedUp(idZone, Or.append(this.test, other.test), card);
150 	}
151 }