1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.magicproject.action;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27
28 import net.sf.magicproject.action.context.ActionContextWrapper;
29 import net.sf.magicproject.action.handler.RollBackAction;
30 import net.sf.magicproject.action.handler.StandardAction;
31 import net.sf.magicproject.clickable.ability.Ability;
32 import net.sf.magicproject.event.context.ContextEventListener;
33 import net.sf.magicproject.stack.StackManager;
34
35 /***
36 * Add to the target list card(s) or player(s) following the specified mode and
37 * the specified type. If mode is 'choose' or 'opponentchoose', then a
38 * 'targeted' event is raised when the choice is made. In case of 'all',
39 * 'random', 'myself' or any target known with acces register, no event is
40 * generated. The friendly test, and the type are required only for the modes
41 * 'random', 'all', 'choose' and 'opponentchoose' to list the valids targets.
42 * The target list is used by the most actions. <br>
43 * For example, if the next action 'damage', all cards/player from the target
44 * list would be damaged. When a new ability is added to the stack, a new list
45 * is created and attached to it. Actions may modify, acceess it until the
46 * ability is completely resolved.
47 *
48 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
49 */
50 public abstract class Target extends UserAction implements StandardAction,
51 RollBackAction {
52
53 /***
54 * Create an instance of MAction by reading a file Offset's file must pointing
55 * on the first byte of this action <br>
56 * <ul>
57 * Structure of InputStream : Data[size]
58 * <li>super [Action]</li>
59 * </ul>
60 * For the available options, see interface IdTargets
61 *
62 * @param actionName
63 * the action's name.
64 */
65 protected Target(String actionName) {
66 super(actionName);
67 }
68
69 /***
70 * Create an instance of this class from input stream.
71 * <ul>
72 * Structure of Stream : Data[size]
73 * <li>[super]</li>
74 * </ul>
75 *
76 * @param inputStream
77 * stream containing this action.
78 * @throws IOException
79 * if error occurred during the reading process from the specified
80 * input stream
81 */
82 protected Target(InputStream inputStream) throws IOException {
83 super(inputStream);
84 }
85
86 @Override
87 public final Actiontype getIdAction() {
88 return Actiontype.TARGET;
89 }
90
91 public abstract boolean play(ContextEventListener context, Ability ability);
92
93 /***
94 * Generate event associated to this action. Only one or several events are
95 * generated and may be collected by event listeners. Then play this action
96 *
97 * @param actionContext
98 * the context containing data saved by this action during the
99 * 'choose" proceess.
100 * @param ability
101 * is the ability owning this test. The card component of this
102 * ability should correspond to the card owning this test too.
103 * @param context
104 * is the context attached to this action.
105 * @return true if the stack can be resolved just after this call.
106 */
107 public abstract boolean replay(ActionContextWrapper actionContext,
108 ContextEventListener context, Ability ability);
109
110 public void rollback(ActionContextWrapper actionContext,
111 ContextEventListener context, Ability ability) {
112 StackManager.getInstance().getTargetedList().removeLast();
113 }
114
115 @Override
116 public abstract String toString(Ability ability);
117
118 }