View Javadoc

1   /*
2    * TargetManager.java
3    * Created on 12 oct. 2003
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.stack;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import net.sf.magicproject.clickable.Clickable;
29  import net.sf.magicproject.clickable.targetable.Targetable;
30  import net.sf.magicproject.clickable.targetable.TargetableFactory;
31  import net.sf.magicproject.token.IdZones;
32  import net.sf.magicproject.zone.MZone;
33  
34  /***
35   * Represents the target settings as color, id card, owner, ... of the target.
36   * Before a target will be added to the targeted list, this test is verified in
37   * order to valid the target.
38   * 
39   * @see net.sf.magicproject.action.Actiontype
40   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
41   * @since 0.86 new algorythme to determine valid targets
42   * @since 0.86 a target cannot be added twice in the target-list
43   */
44  public class TargetManager {
45  
46  	/***
47  	 * Highlight the given targets and the corresponding zones.
48  	 * 
49  	 * @param validTargets
50  	 *          the valid targets.
51  	 */
52  	public void manageTargets(List<Targetable> validTargets) {
53  		final boolean[] highlightedZones = new boolean[IdZones.NB_ZONE * 2];
54  		Targetable.targetize(validTargets, highlightedZones);
55  		if (this.validTargets != null) {
56  			this.validTargets.clear();
57  		}
58  		this.validTargets = validTargets;
59  		zones.clear();
60  		for (int i = highlightedZones.length; i-- > 0;) {
61  			if (highlightedZones[i]) {
62  				final MZone highlightZone = StackManager.PLAYERS[i / IdZones.NB_ZONE].zoneManager
63  						.getContainer(i % IdZones.NB_ZONE);
64  				zones.add(highlightZone);
65  				highlightZone.highLight(TargetableFactory.TARGET_COLOR);
66  			}
67  		}
68  	}
69  
70  	/***
71  	 * Indicates that we're no longer looking for a target
72  	 */
73  	public void clearTarget() {
74  		if (validTargets != null) {
75  			Clickable.disHighlight(validTargets);
76  			validTargets.clear();
77  			validTargets = null;
78  		}
79  		for (MZone zone : zones) {
80  			zone.disHighLight();
81  		}
82  		zones.clear();
83  	}
84  
85  	/***
86  	 * The current list of Zones containing highlighted targets.
87  	 */
88  	private final List<MZone> zones = new ArrayList<MZone>();
89  
90  	/***
91  	 * The current list of valid targets. Is null if there was no choice.
92  	 */
93  	private List<Targetable> validTargets = null;
94  
95  }