View Javadoc

1   /*
2    * Created on Dec 28, 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.modifier;
22  
23  import java.util.List;
24  
25  import net.sf.magicproject.clickable.ability.Ability;
26  import net.sf.magicproject.clickable.targetable.card.MCard;
27  import net.sf.magicproject.event.MEventListener;
28  import net.sf.magicproject.operation.Add;
29  import net.sf.magicproject.operation.Operation;
30  import net.sf.magicproject.operation.Remove;
31  import net.sf.magicproject.stack.StackManager;
32  import net.sf.magicproject.test.Test;
33  
34  /***
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   * @since 0.82
37   */
38  public class AbilityModifier extends Modifier {
39  
40  	/***
41  	 * Creates a new instance of AbilityModifier <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 op
63  	 *          the operation applied to previous value with the value of this
64  	 *          modifier.
65  	 * @param abilities
66  	 *          the list of abilities added/removed by this modifier
67  	 */
68  	AbilityModifier(String name, MCard to, Ability ability, Test whileCondition,
69  			List<MEventListener> linkedEvents, List<MEventListener> until,
70  			boolean linked, int layer, Operation op, Ability[] abilitiesToAdd) {
71  		super(name, to, ability, whileCondition, linkedEvents, until, linked, layer);
72  		this.abilitiesToAdd = abilitiesToAdd;
73  		this.op = op;
74  	}
75  
76  	@Override
77  	public Modifier removeModifier(Modifier modifier) {
78  		if (this == modifier) {
79  			if (activated) {
80  				StackManager.postRefreshAbilities(to);
81  			}
82  			return next;
83  		}
84  		return super.removeModifier(modifier);
85  	}
86  
87  	@Override
88  	public final void removeFromManager() {
89  		super.removeFromManager();
90  		if (!unregisteredModifier) {
91  			to.removeModifier(this);
92  			if (activated && op == Add.getInstance()) {
93  				// unregister the granted abilities
94  				for (Ability ability : abilitiesToAdd) {
95  					ability.removeFromManager();
96  				}
97  			}
98  		}
99  		unregisteredModifier = true;
100 	}
101 
102 	@Override
103 	public void refresh() {
104 		boolean oldActivated = activated;
105 		activated = whileCondition.test(ability, to);
106 
107 		// this card type identifiant has changed
108 		if (oldActivated != activated) {
109 			StackManager.postRefreshAbilities(to);
110 		}
111 	}
112 
113 	/***
114 	 * Add/ remove ability of attached object
115 	 * 
116 	 * @param toAdd
117 	 *          list of abilities attached to the object
118 	 */
119 	public void calculateDeltaAbilities(List<Ability> toAdd) {
120 		if (activated) {
121 			if (op == Add.getInstance()) {
122 				// add the granted abilities
123 				for (Ability ability : abilitiesToAdd) {
124 					toAdd.add(ability);
125 				}
126 			} else if (op == Remove.getInstance()) {
127 				// remove the specified abilities
128 				for (Ability ability : abilitiesToAdd) {
129 					toAdd.remove(ability);
130 				}
131 			} else {
132 				// remove all abilities
133 				toAdd.clear();
134 			}
135 		}
136 		if (next != null) {
137 			((AbilityModifier) next).calculateDeltaAbilities(toAdd);
138 		}
139 	}
140 
141 	/***
142 	 * Indicates the operation applied to previous value with the value of this
143 	 * modifier. Only Remove/add/clear instance.
144 	 */
145 	protected Operation op;
146 
147 	/***
148 	 * The list of abilities added/removed by this modifier
149 	 */
150 	private Ability[] abilitiesToAdd;
151 }