View Javadoc

1   /*
2    * Created on 25 feb. 2004
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package net.sf.magicproject.test;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.List;
26  
27  import net.sf.magicproject.clickable.ability.Ability;
28  import net.sf.magicproject.clickable.targetable.card.MCard;
29  import net.sf.magicproject.event.BecomeTapped;
30  import net.sf.magicproject.event.BecomeUnTapped;
31  import net.sf.magicproject.event.MEventListener;
32  import net.sf.magicproject.event.MovedCard;
33  import net.sf.magicproject.token.IdZones;
34  
35  /***
36   * Test the previous place of component. Valid only for card component.
37   * 
38   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39   * @since 0.91
40   */
41  public class PreviousZone extends TestCard {
42  
43  	/***
44  	 * Create an instance of HasColor by reading a file. Offset's file must
45  	 * pointing on the first byte of this test <br>
46  	 * <ul>
47  	 * Structure of InputStream : Data[size]
48  	 * <li>idZone to test [1]</li>
49  	 * </ul>
50  	 * 
51  	 * @param inputFile
52  	 *          is the file containing this event
53  	 * @throws IOException
54  	 *           if error occurred during the reading process from the specified
55  	 *           input stream
56  	 */
57  	PreviousZone(InputStream inputFile) throws IOException {
58  		super(inputFile);
59  		idZone = inputFile.read();
60  	}
61  
62  	/***
63  	 * Creates a new instance of InZone <br>
64  	 * 
65  	 * @param idZone
66  	 * @param on
67  	 */
68  	public PreviousZone(int idZone, TestOn on) {
69  		super(on);
70  		this.idZone = idZone;
71  	}
72  
73  	@Override
74  	public boolean testCard(Ability ability, MCard tested) {
75  		//TODO Previous zone is not yet implemented
76  		MCard card = on.getCard(ability, tested);
77  		switch (idZone) {
78  		case IdZones.PLAY_TAPPED:
79  			return card.getIdZone() == IdZones.PLAY && card.tapped;
80  		case IdZones.PLAY_UNTAPPED:
81  			return card.getIdZone() == IdZones.PLAY && !card.tapped;
82  		default:
83  			return idZone == card.getIdZone();
84  		}
85  	}
86  
87  	@Override
88  	public void extractTriggeredEvents(List<MEventListener> res, MCard source,
89  			Test globalTest) {
90  		if (idZone >= IdZones.PLAY_TAPPED) {
91  			// we are looking for the [un]tapped state too
92  			res.add(new BecomeTapped(IdZones.PLAY, And.append(new IsTested(on),
93  					globalTest), source));
94  			res.add(new BecomeUnTapped(IdZones.PLAY, And.append(new IsTested(on),
95  					globalTest), source));
96  		}
97  		res.add(new MovedCard(IdZones.PLAY, And.append(new IsTested(on),
98  				new PreviousZone(idZone, on)), globalTest, source));
99  		res.add(new MovedCard(IdZones.PLAY, new IsTested(on), And.append(
100 				new PreviousZone(idZone, on), globalTest), source));
101 	}
102 
103 	/***
104 	 * Is the color to match during each test
105 	 */
106 	private int idZone;
107 
108 }