View Javadoc

1   /**
2    * This program is free software: you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License as published by
4    * the Free Software Foundation, version 3.
5    *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program. If not, see <http://www.gnu.org/licenses/>.
13   */
14  
15  package net.curre.prefcount.gui;
16  
17  import java.awt.geom.Point2D;
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  /**
24   * This class determines locaitons of all items
25   * on the score board given the current dimensions
26   * of the table/board.
27   * <p/>
28   * Created date: Jun 16, 2007
29   *
30   * @author Yevgeny Nyden
31   */
32  public class ScoreBoardLocationsMap {
33  
34    /** Location Map key that represents player name. */
35    public static final String PLAYER_NAME = "NAME";
36  
37    /** Location Map key that represents player mountain. */
38    public static final String PLAYER_MOUNT = "MOUNT";
39  
40    /** Location Map key that represents player bullet. */
41    public static final String PLAYER_BULLET = "BULLET";
42  
43    /**
44     * Location Map key prefix that represents player's
45     * vists agains another player. Full key string
46     * is comprised of the prefix and the other player's index.
47     */
48    public static final String VISTS_FOR = "VISTS_FOR_";
49  
50    /**
51     * Location Map key that represents player's vist saldo
52     * against other players. Full key string is comprised
53     * of the prefix and the other player's index.
54     */
55    public static final String VIST_SALDO_FOR = "VIST_SALDO_FOR_";
56  
57    /** Location Map key that represents player's total vist saldo. */
58    public static final String VIST_SALDO = "VIST_SALDO";
59  
60    /** Location Map key that represents player's final mountain. */
61    public static final String FINAL_MOUNT = "FINAL_MOUNT";
62  
63    /** Location Map key that represents player's final score. */
64    public static final String FINAL_SCORE = "FINAL_SCORE";
65  
66    /** Reference to the score board panel. */
67    private ScoreBoardPanel scoreBoard;
68  
69    /** Current board width. */
70    private float width;
71  
72    /** Current board height. */
73    private float height;
74  
75    /**
76     * Total number of players in the game
77     * (supported values are 3 and 4)
78     */
79    private int numberOfPlayers;
80  
81    /**
82     * List of maps per each player in the game that
83     * store locations of all items on the score board.
84     */
85    private List<Map<String, Point2D.Float>> locationMapsList;
86  
87    /**
88     * Constructor. After the number of players in the
89     * game is known (scoreBoard.results.getPlayerStats() is created),
90     * the initialize() method must be called.
91     *
92     * @param scoreBoard Reference to the score board panel.
93     */
94    public ScoreBoardLocationsMap(ScoreBoardPanel scoreBoard) {
95      this.scoreBoard = scoreBoard;
96      this.width = 0;   // will be set in computeLocations()
97      this.height = 0;  // will be set in computeLocations()
98    }
99  
100   /**
101    * Method that initializes properties and computes
102    * the locations for the first time. This method must
103    * be called as soon as the number of players is set
104    * (scoreBoard.results.getPlayerStats() is created).
105    *
106    * @param numberOfPlayers Number of players in the game.
107    * @throws IllegalArgumentException If number of players is not supported.
108    */
109   public void initialize(int numberOfPlayers) {
110     if (numberOfPlayers != 3 && numberOfPlayers != 4) {
111       throw new IllegalArgumentException(numberOfPlayers + " number of players is NOT supported!");
112     }
113     this.numberOfPlayers = numberOfPlayers;
114     locationMapsList = new ArrayList<Map<String, Point2D.Float>>(numberOfPlayers);
115     for (int ind = 0; ind < numberOfPlayers; ++ind) {
116       locationMapsList.add(ind, new HashMap<String, Point2D.Float>());
117     }
118     computeLocations();
119   }
120 
121   /**
122    * Getter for the locations maps for the given player.
123    *
124    * @param playerIndex Players index.
125    * @return The locations maps for the given player.
126    */
127   public Map<String, Point2D.Float> getLocationsMap(int playerIndex) {
128     if (playerIndex >= locationMapsList.size() || locationMapsList.size() < 0) {
129       throw new IllegalArgumentException("Wrong index " + playerIndex +
130                                          " for map of size " + locationMapsList.size());
131     }
132     return locationMapsList.get(playerIndex);
133   }
134 
135   /**
136    * Computes or recomputes if necessary locations of
137    * all items on the score board and stores the values
138    * in the locationsMap map.
139    *
140    * @throws UnsupportedOperationException If numberof player is not supported.
141    */
142   public void computeLocations() {
143 
144     // if the frame size didn't change from last time,
145     // returning - we can use the already computed values
146     final float newWidth = scoreBoard.getWidth();
147     final float newHeight = scoreBoard.getHeight();
148     if (width == newWidth && height == newHeight) {
149       return;
150     }
151     width = newWidth;
152     height = newHeight;
153 
154     final float centerX = newWidth / 2f;
155     final float centerY = newHeight / 2f;
156     final float incrX = centerX / 3f;
157     final float incrY = centerY / 3f - 7f;
158 
159     final float oneFourthX = newWidth / 4f;
160     final float threeFourthX = oneFourthX * 3f;
161 
162     final float oneThirdX = newWidth / 3f;
163     final float twoThirdX = oneThirdX * 2f;
164     final float oneThirdY = newHeight / 3f;
165     final float twoThirdY = oneThirdY * 2f;
166 
167     final float centX = centerX - 5f;
168     final float centY = centerY + 5f;
169     final float tempX = newWidth - incrX;
170     final int moreX = ((int) incrX) >> 1; // dividing by 2
171     final float tempY = newHeight - 12f;
172 
173     // generating score board items locations
174     switch (numberOfPlayers) {
175       case(3):
176 
177         // computing player 0 items locations
178         Map<String, Point2D.Float> locationsMap = locationMapsList.get(0);
179         locationsMap.put(PLAYER_NAME, new Point2D.Float(centX + 12f, centY - 7f));
180         locationsMap.put(PLAYER_MOUNT, new Point2D.Float(centerX + 14f, oneThirdY + 12f));
181         locationsMap.put(PLAYER_BULLET, new Point2D.Float(centerX + 14f, incrY + 26f));
182         locationsMap.put(VISTS_FOR + "1", new Point2D.Float(tempX + 10f, incrY + 34f));
183         locationsMap.put(VISTS_FOR + "2", new Point2D.Float(centerX + 20f, 24f));
184         locationsMap.put(VIST_SALDO_FOR + "1", new Point2D.Float(tempX + 25f, newHeight - incrY - 30f));
185         locationsMap.put(VIST_SALDO_FOR + "2", new Point2D.Float(centerX + moreX + 20f, 34f));
186         locationsMap.put(VIST_SALDO, new Point2D.Float(tempX + 25f, centerY));
187         locationsMap.put(FINAL_MOUNT, new Point2D.Float(centerX + 40f, centerY - 20f));
188         locationsMap.put(FINAL_SCORE, new Point2D.Float(centerX + 100f, incrY + 70f));
189 
190         // computing player 1 items locations
191         locationsMap = locationMapsList.get(1);
192         locationsMap.put(PLAYER_NAME, new Point2D.Float(centX - 2f, centY + 17f));
193         locationsMap.put(PLAYER_MOUNT, new Point2D.Float(oneThirdX + 28f, twoThirdY - 2f));
194         locationsMap.put(PLAYER_BULLET, new Point2D.Float(incrX + 32f, (newHeight - incrY) - 14f));
195         locationsMap.put(VISTS_FOR + "0", new Point2D.Float(centerX + 20f, tempY));
196         locationsMap.put(VISTS_FOR + "2", new Point2D.Float(40f, tempY));
197         locationsMap.put(VIST_SALDO_FOR + "0", new Point2D.Float(twoThirdX + 12f + moreX, tempY - 10f));
198         locationsMap.put(VIST_SALDO_FOR + "2", new Point2D.Float(38f + moreX, tempY - 10f));
199         locationsMap.put(VIST_SALDO, new Point2D.Float(centerX + 20f, tempY - 30f));
200         locationsMap.put(FINAL_MOUNT, new Point2D.Float(centerX, twoThirdY - 22f));
201         locationsMap.put(FINAL_SCORE, new Point2D.Float(centerX, newHeight - incrY - 34f));
202 
203         // computing player 2 items locations
204         locationsMap = locationMapsList.get(2);
205         locationsMap.put(PLAYER_NAME, new Point2D.Float(centX - 17f, centY - 7f));
206         locationsMap.put(PLAYER_MOUNT, new Point2D.Float(oneThirdX + 4f, oneThirdY + 12f));
207         locationsMap.put(PLAYER_BULLET, new Point2D.Float(incrX + 10f, incrY + 26f));
208         locationsMap.put(VISTS_FOR + "0", new Point2D.Float(40f, 24f));
209         locationsMap.put(VISTS_FOR + "1", new Point2D.Float(16f, incrY + 34f));
210         locationsMap.put(VIST_SALDO_FOR + "0", new Point2D.Float(centerX - 60f, 34f));
211         locationsMap.put(VIST_SALDO_FOR + "1", new Point2D.Float(31f, newHeight - incrY - 30f));
212         locationsMap.put(VIST_SALDO, new Point2D.Float(41f, centerY));
213         locationsMap.put(FINAL_MOUNT, new Point2D.Float(oneThirdX + 10f, centerY + 20f));
214         locationsMap.put(FINAL_SCORE, new Point2D.Float(oneFourthX - 20f, centerY + 40f));
215         break;
216 
217       case(4):
218         // computing player 0 items locations
219         locationsMap = locationMapsList.get(0);
220         locationsMap.put(PLAYER_NAME, new Point2D.Float(centX, centY - 17f));
221         locationsMap.put(PLAYER_MOUNT, new Point2D.Float(oneThirdX + 24f, oneThirdY + 12f));
222         locationsMap.put(PLAYER_BULLET, new Point2D.Float(incrX + 32f, incrY + 26f));
223         locationsMap.put(VISTS_FOR + "1", new Point2D.Float(twoThirdX + 14f, 24f));
224         locationsMap.put(VISTS_FOR + "2", new Point2D.Float(oneThirdX + 14f, 24f));
225         locationsMap.put(VISTS_FOR + "3", new Point2D.Float(40f, 24f));
226         locationsMap.put(VIST_SALDO_FOR + "1", new Point2D.Float(twoThirdX + moreX, 34f));
227         locationsMap.put(VIST_SALDO_FOR + "2", new Point2D.Float(oneThirdX + moreX, 34f));
228         locationsMap.put(VIST_SALDO_FOR + "3", new Point2D.Float(40f + moreX, 34f));
229         locationsMap.put(VIST_SALDO, new Point2D.Float(centerX, 54));
230         locationsMap.put(FINAL_MOUNT, new Point2D.Float(centerX, oneThirdY + 22f));
231         locationsMap.put(FINAL_SCORE, new Point2D.Float(centerX, incrY + 50f));
232 
233         // computing player 1 items locations
234         locationsMap = locationMapsList.get(1);
235         locationsMap.put(PLAYER_NAME, new Point2D.Float(centX + 17f, centY));
236         locationsMap.put(PLAYER_MOUNT, new Point2D.Float(twoThirdX - 44f, oneThirdY + 60f));
237         locationsMap.put(PLAYER_BULLET, new Point2D.Float(twoThirdX + 16f, oneThirdY + 12f));
238         locationsMap.put(VISTS_FOR + "0", new Point2D.Float(tempX + 10f, incrY + 22f));
239         locationsMap.put(VISTS_FOR + "2", new Point2D.Float(tempX + 10f, twoThirdY + 22f));
240         locationsMap.put(VISTS_FOR + "3", new Point2D.Float(tempX + 10f, oneThirdY + 22f));
241         locationsMap.put(VIST_SALDO_FOR + "0", new Point2D.Float(tempX + 25f, incrY + 47f));
242         locationsMap.put(VIST_SALDO_FOR + "2", new Point2D.Float(tempX + 25f, twoThirdY + 47f));
243         locationsMap.put(VIST_SALDO_FOR + "3", new Point2D.Float(tempX + 25f, oneThirdY + 47f));
244         locationsMap.put(VIST_SALDO, new Point2D.Float(tempX + 35f, centerY));
245         locationsMap.put(FINAL_MOUNT, new Point2D.Float(twoThirdX - 29f, centerY + 20f));
246         locationsMap.put(FINAL_SCORE, new Point2D.Float(threeFourthX, centerY - 20f));
247 
248         // computing player 2 items locations
249         locationsMap = locationMapsList.get(2);
250         locationsMap.put(PLAYER_NAME, new Point2D.Float(centX, centY + 17f));
251         locationsMap.put(PLAYER_MOUNT, new Point2D.Float(oneThirdX + 28f, twoThirdY - 2f));
252         locationsMap.put(PLAYER_BULLET, new Point2D.Float(incrX + 32f, newHeight - incrY - 14f));
253         locationsMap.put(VISTS_FOR + "0", new Point2D.Float(oneThirdX + 12f, tempY));
254         locationsMap.put(VISTS_FOR + "1", new Point2D.Float(twoThirdX + 12f, tempY));
255         locationsMap.put(VISTS_FOR + "3", new Point2D.Float(38f, tempY));
256         locationsMap.put(VIST_SALDO_FOR + "0", new Point2D.Float(oneThirdX + 12f + moreX, tempY - 10f));
257         locationsMap.put(VIST_SALDO_FOR + "1", new Point2D.Float(twoThirdX + 12f + moreX, tempY - 10f));
258         locationsMap.put(VIST_SALDO_FOR + "3", new Point2D.Float(38f + moreX, tempY - 10f));
259         locationsMap.put(VIST_SALDO, new Point2D.Float(centerX, tempY - 30f));
260         locationsMap.put(FINAL_MOUNT, new Point2D.Float(centerX, twoThirdY - 22f));
261         locationsMap.put(FINAL_SCORE, new Point2D.Float(centerX, newHeight - incrY - 34f));
262 
263         // computing player 3 items locations
264         locationsMap = locationMapsList.get(3);
265         locationsMap.put(PLAYER_NAME, new Point2D.Float(centX - 17f, centY));
266         locationsMap.put(PLAYER_MOUNT, new Point2D.Float(oneThirdX + 4f, oneThirdY + 60f));
267         locationsMap.put(PLAYER_BULLET, new Point2D.Float(incrX + 10f, oneThirdY - 8f));
268         locationsMap.put(VISTS_FOR + "0", new Point2D.Float(16f, oneThirdY - 8f));
269         locationsMap.put(VISTS_FOR + "1", new Point2D.Float(16f, twoThirdY - 8f));
270         locationsMap.put(VISTS_FOR + "2", new Point2D.Float(16f, newHeight - 66f));
271         locationsMap.put(VIST_SALDO_FOR + "0", new Point2D.Float(31f, oneThirdY - 33f));
272         locationsMap.put(VIST_SALDO_FOR + "1", new Point2D.Float(31f, twoThirdY - 33f));
273         locationsMap.put(VIST_SALDO_FOR + "2", new Point2D.Float(31f, newHeight - 91f));
274         locationsMap.put(VIST_SALDO, new Point2D.Float(41f, centerY));
275         locationsMap.put(FINAL_MOUNT, new Point2D.Float(oneThirdX + 10f, centerY + 20f));
276         locationsMap.put(FINAL_SCORE, new Point2D.Float(oneFourthX - 10f, centerY + 40f));
277         break;
278 
279       default:
280         throw new UnsupportedOperationException(numberOfPlayers + " number of players is NOT supported!");
281     }
282   }
283 
284   /** Private methods ***********************/
285 
286 }