View Javadoc

1   /*
2    * Created on Sep 6, 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   */
22  package net.sf.magicproject.modifier;
23  
24  import java.util.List;
25  
26  import net.sf.magicproject.clickable.ability.Ability;
27  import net.sf.magicproject.clickable.targetable.card.MCard;
28  import net.sf.magicproject.event.MEventListener;
29  import net.sf.magicproject.stack.AdditionalCost;
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.86 Operation is used instead of add/remove boolean
36   *        <code>hasNot</code>
37   */
38  public class AdditionalCostModifier extends Modifier {
39  
40  	/***
41  	 * Creates a new instance of ColorModifier <br>
42  	 * 
43  	 * @param name
44  	 *          the modifier name
45  	 * @param to
46  	 *          the component to be attached to
47  	 * @param ability
48  	 *          is the ability owning this test. The card component of this
49  	 *          ability should correspond to the card owning this test too.
50  	 * @param whileCondition
51  	 *          the test conditionning the activation of this modifier.
52  	 * @param linkedEvents
53  	 *          the events to be registered. This events would cause this modifier
54  	 *          to be updated.
55  	 * @param until
56  	 *          this modifier exists while this test is true.
57  	 * @param linked
58  	 *          is the creator is linked to this modifier.
59  	 * @param layer
60  	 *          is the strategy used to add this modifier within the existing
61  	 *          chain.
62  	 * @param additionalCost
63  	 *          The additional cost.
64  	 */
65  	AdditionalCostModifier(String name, MCard to, Ability ability,
66  			Test whileCondition, List<MEventListener> linkedEvents,
67  			List<MEventListener> until, boolean linked, int layer,
68  			AdditionalCost additionalCost) {
69  		super(name, to, ability, whileCondition, linkedEvents, until, linked, layer);
70  		this.additionalCost = additionalCost;
71  	}
72  
73  	@Override
74  	public Modifier removeModifier(Modifier modifier) {
75  		if (this == modifier) {
76  			if (activated) {
77  				StackManager.getInstance().getAdditionalCost().remove(additionalCost);
78  			}
79  			return next;
80  		}
81  		return super.removeModifier(modifier);
82  	}
83  
84  	@Override
85  	public final void removeFromManager() {
86  		super.removeFromManager();
87  		unregisteredModifier = true;
88  	}
89  
90  	@Override
91  	public void refresh() {
92  		boolean oldActivated = activated;
93  		activated = whileCondition.test(ability, null);
94  
95  		// this color has changed
96  		if (oldActivated != activated) {
97  			if (activated)
98  				StackManager.getInstance().getAdditionalCost().add(additionalCost);
99  			else
100 				StackManager.getInstance().getAdditionalCost().remove(additionalCost);
101 		}
102 	}
103 
104 	/***
105 	 * The additional cost.
106 	 */
107 	protected final AdditionalCost additionalCost;
108 
109 }