View Javadoc

1   /*
2    * Created on Nov 15, 2004 
3    * Original filename was PlayableZoneModifier.java
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   */
23  package net.sf.magicproject.modifier;
24  
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.MEventListener;
30  import net.sf.magicproject.stack.StackManager;
31  import net.sf.magicproject.test.Test;
32  
33  /***
34   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
35   * @since 0.80
36   */
37  public class PlayableZoneModifier extends Modifier {
38  
39  	/***
40  	 * Creates a new instance of PlayableZoneModifier <br>
41  	 * 
42  	 * @param name
43  	 *          the modifier name
44  	 * @param to
45  	 *          the component to be attached to
46  	 * @param ability
47  	 *          is the ability owning this test. The card component of this
48  	 *          ability should correspond to the card owning this test too.
49  	 * @param whileCondition
50  	 *          the test conditionning the activation of this modifier.
51  	 * @param linkedEvents
52  	 *          the events to be registered. This events would cause this modifier
53  	 *          to be updated.
54  	 * @param until
55  	 *          this modifier exists while this test is true.
56  	 * @param linked
57  	 *          is the creator is linked to this modifier.
58  	 * @param layer
59  	 *          is the strategy used to add this modifier within the existing
60  	 *          chain.
61  	 * @param idZone
62  	 *          the zone to add/remove as playable zone
63  	 * @param hasNot
64  	 *          Indicates if this modifier remove occurence of the given zone. If
65  	 *          False, it adds one instance. modifier.
66  	 */
67  	PlayableZoneModifier(String name, MCard to, Ability ability,
68  			Test whileCondition, List<MEventListener> linkedEvents,
69  			List<MEventListener> until, boolean linked, int layer, int idZone,
70  			boolean hasNot) {
71  		super(name, to, ability, whileCondition, linkedEvents, until, linked, layer);
72  		this.idZone = idZone;
73  		this.hasNot = hasNot;
74  	}
75  
76  	/***
77  	 * Indicates if this modifier allow the specified zone
78  	 * 
79  	 * @param idZone
80  	 *          The previous idCard.
81  	 * @param found
82  	 *          indicates that this zone has been previously allowed
83  	 * @return the modified idCard.
84  	 */
85  	public boolean playableIn(int idZone, boolean found) {
86  		if (activated) {
87  			if (this.idZone == idZone) {
88  				return !hasNot;
89  			}
90  			if (next != null) {
91  				return playableIn(idZone, found);
92  			}
93  			return found;
94  		}
95  		if (next != null) {
96  			return ((PlayableZoneModifier) next).playableIn(idZone, found);
97  		}
98  		return found;
99  	}
100 
101 	@Override
102 	public Modifier removeModifier(Modifier modifier) {
103 		if (this == modifier) {
104 			if (activated) {
105 				StackManager.postRefreshIdCard(to);
106 			}
107 			return next;
108 		}
109 		return super.removeModifier(modifier);
110 	}
111 
112 	@Override
113 	public final void removeFromManager() {
114 		super.removeFromManager();
115 		if (!unregisteredModifier) {
116 			to.removeModifier(this);
117 		}
118 		unregisteredModifier = true;
119 	}
120 
121 	@Override
122 	public void refresh() {
123 		boolean oldActivated = activated;
124 		activated = whileCondition.test(ability, to);
125 
126 		// this card type identifiant has changed
127 		if (oldActivated != activated) {
128 			to.updateAbilities();
129 		}
130 	}
131 
132 	/***
133 	 * The zone to add/remove as playable zone
134 	 */
135 	private int idZone;
136 
137 	/***
138 	 * Indicates if this modifier remove occurence of the given zone. If False, it
139 	 * adds one instance.
140 	 */
141 	private boolean hasNot;
142 }