View Javadoc

1   /*
2    * ManaPool.java
3    * Created on 27 octobre 2002, 17:24
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   */
22  package net.sf.magicproject.clickable.mana;
23  
24  import java.awt.Color;
25  import java.awt.FlowLayout;
26  import java.io.IOException;
27  import java.io.InputStream;
28  
29  import javax.swing.JPanel;
30  
31  import net.sf.magicproject.clickable.ability.Ability;
32  import net.sf.magicproject.stack.StackManager;
33  import net.sf.magicproject.test.Test;
34  import net.sf.magicproject.token.IdCardColors;
35  import net.sf.magicproject.tools.Configuration;
36  
37  /***
38   * Representes the mana pool of a player : 5 colored manas and one colorless
39   * mana. A graphic for each different mana is displayed with a jButton
40   * representing the amount of manas for this colored(less) mana. The normal
41   * order is : BLACK,BLUE,GREEN,RED,WHITE,COLORLESS, but if we consider the
42   * opponent case, we have a reversed order and a rotation of PI to do.
43   * 
44   * @see net.sf.magicproject.token.IdCommonToken#COLORLESS_MANA
45   * @see net.sf.magicproject.token.IdCommonToken#BLACK_MANA
46   * @see net.sf.magicproject.token.IdCommonToken#BLUE_MANA
47   * @see net.sf.magicproject.token.IdCommonToken#GREEN_MANA
48   * @see net.sf.magicproject.token.IdCommonToken#RED_MANA
49   * @see net.sf.magicproject.token.IdCommonToken#WHITE_MANA
50   * @see net.sf.magicproject.clickable.mana.Mana
51   * @since 0.3 support reversed mana pictures for opponent
52   * @since 0.81 mana buttons are reversed AS NEEDED
53   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
54   */
55  public class ManaPool extends JPanel {
56  
57  	/***
58  	 * Creates a new instance of MManas
59  	 * 
60  	 * @param reverseImage
61  	 *          if true all mana pictures are flipped horizontally and vertically.
62  	 */
63  	public ManaPool(boolean reverseImage) {
64  		super(new FlowLayout(FlowLayout.LEFT, 0, 0));
65  		this.reverseImage = reverseImage;
66  
67  		setVisible(false);
68  		setBackground(Color.BLACK);
69  
70  		// create and fill an array of Mana object
71  		manaButtons = new Mana[IdCardColors.CARD_COLOR_NAMES.length];
72  		for (int idColor = manaButtons.length; idColor-- > 0;) {
73  			final Mana mana = new Mana(idColor, reverseImage);
74  			manaButtons[idColor] = mana;
75  
76  			if (reverseImage && Configuration.getBoolean("reverseSide", false)) {
77  				add(mana);
78  			} else {
79  				add(mana, 0);
80  			}
81  		}
82  	}
83  
84  	/***
85  	 * Return the ammount of mana in the mana pool
86  	 * 
87  	 * @return the ammount of mana in the mana pool
88  	 */
89  	public int allManas() {
90  		return allManas(null);
91  	}
92  
93  	/***
94  	 * Return the ammount of mana in the mana pool
95  	 * 
96  	 * @param abilityRequest
97  	 *          the ability containing action requesting this mana
98  	 * @return the ammount of mana in the mana pool
99  	 */
100 	public int allManas(Ability abilityRequest) {
101 		int result = 0;
102 		for (int a = 6; a-- > 0;) {
103 			result += getMana(a, abilityRequest);
104 		}
105 		return result;
106 	}
107 
108 	/***
109 	 * Return the ammount of mana in the mana pool of one color
110 	 * 
111 	 * @param idColor
112 	 *          is the mana's color
113 	 * @return the ammount of mana in the mana pool of this color
114 	 */
115 	public int getMana(int idColor) {
116 		return getMana(idColor, null);
117 	}
118 
119 	/***
120 	 * Return the ammount of mana in the mana pool of one color
121 	 * 
122 	 * @param idColor
123 	 *          is the mana's color
124 	 * @param abilityRequest
125 	 *          the ability containing action requesting this mana
126 	 * @return the ammount of mana in the mana pool of this color
127 	 */
128 	public int getMana(int idColor, Ability abilityRequest) {
129 		return manaButtons[idColor].getMana(abilityRequest);
130 	}
131 
132 	/***
133 	 * Empty the mana pool
134 	 * 
135 	 * @return the ammount of mana removed from the mana pool
136 	 */
137 	public int setToZero() {
138 		int c = 0;
139 		for (int a = 6; a-- > 0;) {
140 			c += setToZero(a);
141 		}
142 		return c;
143 	}
144 
145 	/***
146 	 * empty the mana pool of one color
147 	 * 
148 	 * @param idColor
149 	 *          empty the mana pool of this color
150 	 * @return the previous amount of mana
151 	 */
152 	public int setToZero(int idColor) {
153 		return manaButtons[idColor].setToZero();
154 	}
155 
156 	/***
157 	 * Add a number of mana of one color
158 	 * 
159 	 * @param idColor
160 	 *          is the color of the mana added
161 	 * @param idNumber
162 	 *          is the number of mana added
163 	 * @param restriction
164 	 *          the test defining mana usage
165 	 * @return the new amount of mana
166 	 */
167 	public int addMana(int idColor, int idNumber, Test restriction) {
168 		return manaButtons[idColor].addMana(idNumber, restriction);
169 	}
170 
171 	/***
172 	 * Remove a number of mana of one color
173 	 * 
174 	 * @param idColor
175 	 *          is the color of the mana to add
176 	 * @param idNumber
177 	 *          is the number of mana to remove
178 	 * @param abilityRequest
179 	 *          the ability containing action requesting this mana
180 	 * @return the new amount of mana
181 	 */
182 	public int removeMana(int idColor, int idNumber, Ability abilityRequest) {
183 		return manaButtons[idColor].removeMana(idNumber, abilityRequest);
184 	}
185 
186 	/***
187 	 * This method is invoked when opponent has clicked on mana. this call should
188 	 * be done from the net.sf.magicproject.network listener
189 	 * 
190 	 * @param input
191 	 *          input stream of our net.sf.magicproject.network connection
192 	 * @throws IOException
193 	 *           if error occurred when reading the message
194 	 */
195 	public static void clickOn(InputStream input) throws IOException {
196 		// waiting for mana information
197 		int idPlayer = input.read();
198 		int idColor = input.read();
199 		StackManager.PLAYERS[idPlayer].mana.manaButtons[idColor].clickOn(input);
200 	}
201 
202 	/***
203 	 * Remove any color of the border
204 	 */
205 	public void disHighLight() {
206 		for (Mana mana : manaButtons) {
207 			mana.disHighLight();
208 		}
209 	}
210 
211 	/***
212 	 * Update the opponent side depending on the "enable reverse" options. By
213 	 * default, nothing is done.
214 	 */
215 	public void updateReversed() {
216 		removeAll();
217 		if (reverseImage && Configuration.getBoolean("reverseSide", false)) {
218 			for (int i = manaButtons.length; i-- > 0;) {
219 				add(manaButtons[i]);
220 			}
221 		} else {
222 			for (Mana mana : manaButtons) {
223 				add(mana);
224 			}
225 		}
226 	}
227 
228 	/***
229 	 * represents all colored mana, and colorless mana The order of MMana objects
230 	 * are stored in this array in this order :
231 	 * COLORLESS,BLACK,BLUE,GREEN,RED,WHITE
232 	 */
233 	public Mana[] manaButtons;
234 
235 	/***
236 	 * Indicates if graphics are reversed (PI rotation)
237 	 */
238 	private boolean reverseImage;
239 }