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.io.IOException;
24  import java.io.InputStream;
25  
26  import net.sf.magicproject.clickable.ability.Ability;
27  import net.sf.magicproject.clickable.targetable.card.MCard;
28  import net.sf.magicproject.test.Test;
29  
30  /***
31   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
32   * @since 0.82
33   */
34  public class ObjectAbilityModifierModel extends AbilityModifierModel implements
35  		ObjectModifierModel {
36  
37  	/***
38  	 * Is this object can be paint.
39  	 */
40  	private final boolean paint;
41  
42  	/***
43  	 * Creates a new instance of ObjectAbilityModifierModel <br>
44  	 * <ul>
45  	 * Structure of InputStream : Data[size]
46  	 * <li>[super]</li>
47  	 * <li>is this object can be paint [boolean]</li>
48  	 * </ul>
49  	 * 
50  	 * @param inputStream
51  	 *          the input stream where action will be read
52  	 * @throws IOException
53  	 *           if error occurred during the reading process from the specified
54  	 *           input stream
55  	 */
56  	ObjectAbilityModifierModel(InputStream inputStream) throws IOException {
57  		super(inputStream);
58  		paint = inputStream.read() == 1;
59  
60  		// do not remove modifier if the move is PLAY to PLAY
61  		linked = false;
62  	}
63  
64  	/***
65  	 * Create a new instance from the given model.
66  	 * 
67  	 * @param other
68  	 *          the instance to copy
69  	 */
70  	private ObjectAbilityModifierModel(ObjectModifierModel other) {
71  		super((AbilityModifierModel) other);
72  		this.paint = ((ObjectAbilityModifierModel) other).paint;
73  	}
74  
75  	@Override
76  	public void addModifierFromModel(Ability ability, MCard target) {
77  		final Ability[] copyAbilities = new Ability[abilitiesToAdd.length];
78  		for (int i = copyAbilities.length; i-- > 0;) {
79  			copyAbilities[i] = abilitiesToAdd[i].clone(target);
80  		}
81  		final ObjectAbilityModifier newModifier = new ObjectAbilityModifier(name,
82  				target, ability, whileCondition, linkedEvents, until, linked, layer,
83  				op, copyAbilities, paint);
84  		target.addModifier(newModifier);
85  		newModifier.refresh();
86  		if (next != null) {
87  			next.addModifierFromModel(ability, target);
88  		}
89  	}
90  
91  	@Override
92  	public ObjectAbilityModifierModel clone() {
93  		return new ObjectAbilityModifierModel(this);
94  	}
95  
96  	public String getObjectName() {
97  		return name;
98  	}
99  
100 	@Override
101 	public void removeObject(MCard fromCard, Test objectTest) {
102 		if (fromCard.abilityModifier != null) {
103 			fromCard.abilityModifier = (AbilityModifier) fromCard.abilityModifier
104 					.removeObject(name, objectTest);
105 		}
106 		super.removeObject(fromCard, objectTest);
107 	}
108 
109 	public int getNbObject(MCard card, Test objectTest) {
110 		return card.abilityModifier == null ? 0 : card.abilityModifier
111 				.getNbObjects(name, objectTest);
112 	}
113 
114 }