View Javadoc

1   /*
2    * Created on Nov 2, 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.clickable.targetable.player.Player;
29  import net.sf.magicproject.event.MEventListener;
30  import net.sf.magicproject.stack.StackManager;
31  import net.sf.magicproject.test.Test;
32  import net.sf.magicproject.test.TestOn;
33  
34  /***
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   */
37  public class ControllerModifier extends Modifier {
38  
39  	/***
40  	 * Creates a new instance of ControllerModifier <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  	 *          the modifier layer.
60  	 * @param newController
61  	 *          the new controller to set
62  	 */
63  	ControllerModifier(String name, MCard to, Ability ability,
64  			Test whileCondition, List<MEventListener> linkedEvents,
65  			List<MEventListener> until, boolean linked, int layer,
66  			TestOn newController) {
67  		super(name, to, ability, whileCondition, linkedEvents, until, linked, layer);
68  		this.newController = newController;
69  	}
70  
71  	/***
72  	 * Return the modified controller
73  	 * 
74  	 * @param oldPlayer
75  	 *          The old player.
76  	 * @return the modified controller.
77  	 */
78  	public Player getPlayer(Player oldPlayer) {
79  		if (activated) {
80  			if (next != null) {
81  				return ((ControllerModifier) next).getPlayer(newController.getPlayer(
82  						ability, null));
83  			}
84  			return newController.getPlayer(ability, null);
85  		}
86  		if (next != null) {
87  			return ((ControllerModifier) next).getPlayer(oldPlayer);
88  		}
89  		return oldPlayer;
90  	}
91  
92  	@Override
93  	public Modifier removeModifier(Modifier modifier) {
94  		if (this == modifier) {
95  			if (activated) {
96  				StackManager.postRefreshController(to);
97  			}
98  			return next;
99  		}
100 		return super.removeModifier(modifier);
101 	}
102 
103 	@Override
104 	public void removeFromManager() {
105 		super.removeFromManager();
106 		if (!unregisteredModifier) {
107 			to.removeModifier(this);
108 		}
109 		unregisteredModifier = true;
110 	}
111 
112 	@Override
113 	public void refresh() {
114 		boolean oldActivated = activated;
115 		activated = whileCondition.test(ability, to);
116 
117 		// this controller has changed
118 		if (oldActivated != activated) {
119 			StackManager.postRefreshController(to);
120 		}
121 	}
122 
123 	/***
124 	 * The new controller to set
125 	 */
126 	private TestOn newController;
127 
128 }