View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   * 
19   */
20  package net.sf.magicproject.action;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import net.sf.magicproject.action.handler.StandardAction;
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.clickable.ability.AbilityFactory;
30  import net.sf.magicproject.clickable.ability.RemoveModifier;
31  import net.sf.magicproject.clickable.targetable.Targetable;
32  import net.sf.magicproject.clickable.targetable.card.MCard;
33  import net.sf.magicproject.clickable.targetable.card.SystemCard;
34  import net.sf.magicproject.clickable.targetable.player.Player;
35  import net.sf.magicproject.event.EventFactory;
36  import net.sf.magicproject.event.MEventListener;
37  import net.sf.magicproject.event.TriggeredEvent;
38  import net.sf.magicproject.event.context.ContextEventListener;
39  import net.sf.magicproject.stack.StackManager;
40  
41  /***
42   * Create a new ability and add it to the current target list. This ability is
43   * linked to many optional 'until' events.
44   * 
45   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
46   * @since 0.81
47   */
48  class AddAbility extends UserAction implements StandardAction {
49  
50  	/***
51  	 * The ability to add
52  	 */
53  	private final Ability abilityToAdd;
54  
55  	/***
56  	 * When this event is generated this modifier is removed from the attached
57  	 * object. If this event is equal to null, this modifier can be removed only
58  	 * by the attached component.
59  	 */
60  	private final List<MEventListener> until;
61  
62  	/***
63  	 * Create an instance of AddAbility by reading a file Offset's file must
64  	 * pointing on the first byte of this action
65  	 * <ul>
66  	 * Structure of InputStream : Data[size]
67  	 * <li>idAction [1]</li>
68  	 * <li>ability to Add [Ability]</li>
69  	 * <li>exists until this triggered event [Event[]]</li>
70  	 * </ul>
71  	 * 
72  	 * @param inputFile
73  	 *          file containing this action
74  	 * @throws IOException
75  	 *           If some other I/O error occurs
76  	 */
77  	AddAbility(InputStream inputFile) throws IOException {
78  		super(inputFile);
79  
80  		abilityToAdd = AbilityFactory.readAbility(inputFile, SystemCard.instance);
81  
82  		// until events
83  		int count = inputFile.read();
84  		until = new ArrayList<MEventListener>(count);
85  		while (count-- > 0) {
86  			until.add(EventFactory.readNextEvent(inputFile, SystemCard.instance));
87  		}
88  
89  	}
90  
91  	@Override
92  	public final Actiontype getIdAction() {
93  		return Actiontype.ADD_ABILITY;
94  	}
95  
96  	public boolean play(ContextEventListener context, Ability ability) {
97  		// create ability, and add it to the card
98  		for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
99  			Targetable to = StackManager.getInstance().getTargetedList().get(i);
100 			Ability clonedAbility = null;
101 			if (to.isCard()) {
102 				clonedAbility = abilityToAdd.clone((MCard) to);
103 			} else {
104 				// the target is a player
105 				clonedAbility = abilityToAdd.clone(((Player) to).playerCard);
106 			}
107 
108 			// create the 'until' triggers of this ability
109 			for (int j = until.size(); j-- > 0;) {
110 				clonedAbility.addLinkedAbility(new RemoveModifier(
111 						(TriggeredEvent) until.get(j), clonedAbility));
112 			}
113 
114 			// register the ability
115 			clonedAbility.registerToManager();
116 			to.cachedAbilities.add(clonedAbility);
117 		}
118 		return true;
119 	}
120 
121 	@Override
122 	public String toString(Ability ability) {
123 		return "Add ability";
124 	}
125 
126 }