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  package net.sf.magicproject.action;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import net.sf.magicproject.action.context.ActionContextWrapper;
25  import net.sf.magicproject.action.context.BooleanArray;
26  import net.sf.magicproject.action.handler.FollowAction;
27  import net.sf.magicproject.action.handler.StandardAction;
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.clickable.targetable.card.MCard;
30  import net.sf.magicproject.event.BecomeTapped;
31  import net.sf.magicproject.event.BecomeUnTapped;
32  import net.sf.magicproject.event.context.ContextEventListener;
33  import net.sf.magicproject.stack.StackManager;
34  import net.sf.magicproject.tools.Log;
35  import net.sf.magicproject.ui.i18n.LanguageManagerMDB;
36  
37  /***
38   * Tap the target list. Error if target list contains objects not instance of
39   * card.
40   * 
41   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
42   * @since 0.1
43   */
44  class Tap extends UserAction implements StandardAction, FollowAction {
45  
46  	/***
47  	 * Create an instance of Tap by reading a file Offset's file must pointing on
48  	 * the first byte of this action. <br>
49  	 * <ul>
50  	 * Structure of InputStream : Data[size]
51  	 * <li>tap=1,untap=0 [1]</li>
52  	 * </ul>
53  	 * 
54  	 * @param inputFile
55  	 *          file containing this action
56  	 * @throws IOException
57  	 *           if error occurred during the reading process from the specified
58  	 *           input stream
59  	 */
60  	Tap(InputStream inputFile) throws IOException {
61  		super(inputFile);
62  		tap = inputFile.read() != 0;
63  	}
64  
65  	@Override
66  	public final Actiontype getIdAction() {
67  		return Actiontype.TAP;
68  	}
69  
70  	public void rollback(ActionContextWrapper actionContext,
71  			ContextEventListener context, Ability ability) {
72  		final BooleanArray bContext = (BooleanArray) actionContext.actionContext;
73  		for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
74  			if (StackManager.getInstance().getTargetedList().get(i).isCard()) {
75  				final MCard card = (MCard) StackManager.getInstance().getTargetedList()
76  						.get(i);
77  				if (bContext.getBoolean(i)) {
78  					card.tap(!tap);
79  				}
80  			} else {
81  				Log
82  						.warn("In TAP action, target list contains non 'Card' object. Ignored");
83  			}
84  		}
85  	}
86  
87  	public void simulate(ActionContextWrapper actionContext,
88  			ContextEventListener context, Ability ability) {
89  		final BooleanArray bContext = new BooleanArray(StackManager.getInstance()
90  				.getTargetedList().size());
91  		actionContext.actionContext = bContext;
92  		for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
93  			if (StackManager.getInstance().getTargetedList().get(i).isCard()) {
94  				final MCard card = (MCard) StackManager.getInstance().getTargetedList()
95  						.get(i);
96  				if (card.tapped && !tap || !card.tapped && tap) {
97  					bContext.setBoolean(i, true);
98  					card.tap(tap);
99  				}
100 			} else {
101 				Log
102 						.warn("In TAP action, target list contains non 'Card' object. Ignored");
103 			}
104 		}
105 	}
106 
107 	public boolean play(ContextEventListener context, Ability ability) {
108 		for (int i = StackManager.getInstance().getTargetedList().size(); i-- > 0;) {
109 			if (StackManager.getInstance().getTargetedList().get(i).isCard()) {
110 				final MCard card = (MCard) StackManager.getInstance().getTargetedList()
111 						.get(i);
112 				if (checkTimeStamp(context, card) && card.tapped != tap) {
113 					card.tap(tap);
114 					if (tap) {
115 						BecomeTapped.dispatchEvent(card);
116 					} else {
117 						BecomeUnTapped.dispatchEvent(card);
118 					}
119 				} else {
120 					System.out.println("erreur timestamp");
121 				}
122 			} else {
123 				Log
124 						.warn("In TAP action, target list contains non 'Card' object. Ignored");
125 			}
126 		}
127 		return true;
128 	}
129 
130 	@Override
131 	public boolean equal(MAction constraintAction) {
132 		return constraintAction.getIdAction() == Actiontype.TAP
133 				&& ((Tap) constraintAction).tap == tap
134 				&& (constraintAction.getActionName() == null || (constraintAction
135 						.getActionName().equals(getActionName())));
136 	}
137 
138 	@Override
139 	public String toString(Ability ability) {
140 		return LanguageManagerMDB.getString(tap ? "action-tap" : "action-untap");
141 	}
142 
143 	/***
144 	 * Indicates if the list would be tapped or untapped
145 	 */
146 	private boolean tap;
147 
148 }