View Javadoc

1   /*
2    * ArrangedZone.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.test.Test;
30  import net.sf.magicproject.token.IdZones;
31  
32  /***
33   * When a zone is arranged.
34   * 
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   * @since 0.54
37   */
38  public class ArrangedZone extends TriggeredEvent {
39  
40  	/***
41  	 * Create an instance of MEventListener by reading a file Offset's file must
42  	 * pointing on the first byte of this event <br>
43  	 * <ul>
44  	 * Structure of InputStream : Data[size]
45  	 * <li>idZone [1]</li>
46  	 * <li>test [...]</li>
47  	 * <li>matched zone [1]</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  	ArrangedZone(InputStream inputFile, MCard card) throws IOException {
59  		super(inputFile, card);
60  		arrangedZone = inputFile.read();
61  	}
62  
63  	/***
64  	 * Creates a new instance of CanICast specifying all attributes of this class.
65  	 * All parameters are copied, not cloned. So this new object shares the card
66  	 * and the specified codes
67  	 * 
68  	 * @param idZone
69  	 *          the place constraint to activate this event
70  	 * @param test
71  	 *          the additional test of this event
72  	 * @param card
73  	 *          is the card owning this card
74  	 * @param arrangedZone
75  	 *          the arranged zone
76  	 */
77  	public ArrangedZone(int idZone, Test test, MCard card, int arrangedZone) {
78  		super(idZone, test, card);
79  		this.arrangedZone = arrangedZone;
80  	}
81  
82  	@Override
83  	public MEventListener clone(MCard card) {
84  		return new ArrangedZone(idZone, test, card, arrangedZone);
85  	}
86  
87  	/***
88  	 * Tell if the current event matches with this event. If there is an
89  	 * additional code to check, it'would be checked if the main event matches
90  	 * with the main event
91  	 * 
92  	 * @param ability
93  	 *          is the ability owning this test. The card component of this
94  	 *          ability should correspond to the card owning this test too.
95  	 * @param source
96  	 *          the source of the spell/ability
97  	 * @param arrangedZone
98  	 *          the arranged zone
99  	 * @return true if the current event match with this event
100 	 */
101 	public boolean isMatching(Ability ability, MCard source, int arrangedZone) {
102 		return test.test(ability, source)
103 				&& (this.arrangedZone == IdZones.ANYWHERE || this.arrangedZone == arrangedZone);
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 arrangedZone
115 	 *          the arranged zone
116 	 * @see #isMatching(Ability, MCard, int)
117 	 */
118 	public static void dispatchEvent(int arrangedZone) {
119 		for (Ability ability : TRIGGRED_ABILITIES.get(EVENT)) {
120 			if (((ArrangedZone) ability.eventComing()).isMatching(ability, ability
121 					.getCard(), arrangedZone)) {
122 				ability.triggerIt(null);
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.ARRANGED_ZONE;
136 
137 	/***
138 	 * The arrangedZone
139 	 */
140 	private int arrangedZone;
141 
142 }