1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.ability.AbilityFactory;
28 import net.sf.magicproject.clickable.targetable.card.MCard;
29 import net.sf.magicproject.clickable.targetable.card.SystemCard;
30 import net.sf.magicproject.event.context.ContextEventListener;
31 import net.sf.magicproject.operation.Operation;
32 import net.sf.magicproject.operation.OperationFactory;
33 import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
34
35 /***
36 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
37 * @since 0.82
38 */
39 public class AbilityModifierModel extends ModifierModel {
40
41 /***
42 * Creates a new instance of AbilityModifierModel <br>
43 * <ul>
44 * Structure of InputStream : Data[size]
45 * <li>[super]</li>
46 * <li>operation [Operation]</li>
47 * <li>abilities [Ability[]]</li>
48 * </ul>
49 *
50 * @param inputFile
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 AbilityModifierModel(InputStream inputFile) throws IOException {
57 super(inputFile);
58 this.op = OperationFactory.readNextOperation(inputFile);
59 abilitiesToAdd = new Ability[inputFile.read()];
60 for (int i = abilitiesToAdd.length; i-- > 0;) {
61 abilitiesToAdd[i] = AbilityFactory.readAbility(inputFile,
62 SystemCard.instance);
63 }
64 }
65
66 @Override
67 public void addModifierFromModel(Ability ability, MCard target) {
68 final Ability[] copyAbilities = new Ability[abilitiesToAdd.length];
69 for (int i = copyAbilities.length; i-- > 0;) {
70 copyAbilities[i] = abilitiesToAdd[i].clone(target);
71 }
72 final AbilityModifier newModifier = new AbilityModifier(name, target,
73 ability, whileCondition, linkedEvents, until, linked, layer, op,
74 copyAbilities);
75 target.addModifier(newModifier);
76 newModifier.refresh();
77 if (next != null) {
78 next.addModifierFromModel(ability, target);
79 }
80 }
81
82 /***
83 * Create a new instance from the given model.
84 *
85 * @param other
86 * the instance to copy
87 */
88 protected AbilityModifierModel(AbilityModifierModel other) {
89 super(other);
90 op = other.op;
91 abilitiesToAdd = other.abilitiesToAdd;
92 }
93
94 @Override
95 public AbilityModifierModel clone() {
96 return new AbilityModifierModel(this);
97 }
98
99 @Override
100 public String toHtmlString(Ability ability, ContextEventListener context) {
101 if (abilitiesToAdd.length == 0) {
102 return LanguageManagerMDB.getString("add-ability-modifier-"
103 + op.getOperator());
104 }
105 return LanguageManagerMDB.getString("add-ability-modifier-"
106 + (op.getOperator()), abilitiesToAdd[0].toHtmlString(context));
107 }
108
109 /***
110 * The list of abilities added/removed by this modifier
111 */
112 protected Ability[] abilitiesToAdd;
113
114 /***
115 * Indicates the operation applied to previous value with the value of this
116 * modifier.
117 */
118 protected Operation op;
119 }