View Javadoc
1 package org.argosfields.battlefield; 2 3 import java.io.Serializable; 4 5 import org.argosfields.model.Tile; 6 import org.argosfields.model.Unit; 7 import org.eclipse.swt.SWT; 8 import org.eclipse.swt.graphics.Point; 9 import org.eclipse.swt.graphics.Rectangle; 10 11 /*** 12 * BattleField.java 13 * 14 * @author Xavier Cho 15 * @version $Revision: 1.4 $ $Date: 2003/12/01 07:38:05 $ 16 */ 17 public class BattleField implements Serializable { 18 private String name; 19 private String description; 20 private int playerCount; 21 private int columns; 22 private int rows; 23 private Tile[][] tiles; 24 25 public BattleField(final int columns, final int rows) { 26 if (columns <= 0 || rows <= 0) { 27 SWT.error(SWT.ERROR_INVALID_RANGE); 28 } 29 30 this.columns = columns; 31 this.rows = rows; 32 33 this.tiles = new Tile[rows][columns]; 34 } 35 36 public Tile getTile(final int column, final int row) { 37 return tiles[row][column]; 38 } 39 40 public Tile getTile(final int index) { 41 Point postion = getPositionFromIndex(index); 42 43 return tiles[postion.y][postion.x]; 44 } 45 46 public int getIndexFromPoint(final Point point) { 47 if (point == null) { 48 SWT.error(SWT.ERROR_NULL_ARGUMENT); 49 } 50 51 return getIndexFromPoint(point.x, point.y); 52 } 53 54 public int getIndexFromPoint(final int x, final int y) { 55 float ratio = (float) Tile.HEIGHT / (float) Tile.WIDTH * 3f / 2f * -1; 56 57 for (int row = 0; row < rows; row++) { 58 for (int col = 0; col < columns; col++) { 59 Rectangle bounds = getTileBounds(col, row); 60 61 if (bounds.contains(x, y)) { 62 //Check upper left corner 63 float a = ratio; 64 65 float b = 66 bounds.y + (float) Tile.HEIGHT * 0.5f - a * bounds.x; 67 68 float value = a * x + b; 69 70 if (value > y) { 71 continue; 72 } 73 74 //Check upper right corner 75 a = -ratio; 76 b = 77 bounds.y 78 + (float) Tile.HEIGHT * 0.5f 79 - a * (bounds.x + bounds.width); 80 81 value = a * x + b; 82 83 if (value > y) { 84 continue; 85 } 86 87 //Check lower right corner 88 a = ratio; 89 b = 90 bounds.y 91 + (float) Tile.HEIGHT * 0.5f 92 - a * (bounds.x + bounds.width); 93 94 value = a * x + b; 95 96 if (value <= y) { 97 continue; 98 } 99 100 //Check lower left corner 101 a = -ratio; 102 b = bounds.y + (float) Tile.HEIGHT * 0.5f - a * bounds.x; 103 104 value = a * x + b; 105 106 if (value <= y) { 107 continue; 108 } 109 110 return col + row * columns; 111 } 112 } 113 } 114 115 return -1; 116 } 117 118 public void setTile(final int column, final int row, final Tile tile) { 119 if (tile == null) { 120 SWT.error(SWT.ERROR_NULL_ARGUMENT); 121 } 122 123 tiles[row][column] = tile; 124 } 125 126 public Unit getUnit(final int column, final int row) { 127 return tiles[row][column].getUnit(); 128 } 129 130 public Unit getUnit(final int index) { 131 Tile tile = getTile(index); 132 return tile.getUnit(); 133 } 134 135 public void setUnit(final int column, final int row, final Unit unit) { 136 if (unit == null) { 137 SWT.error(SWT.ERROR_NULL_ARGUMENT); 138 } 139 140 tiles[row][column].setUnit(unit); 141 } 142 143 /*** 144 * @return 145 */ 146 public String getDescription() { 147 return description; 148 } 149 150 /*** 151 * @return 152 */ 153 public int getRows() { 154 return rows; 155 } 156 157 /*** 158 * @return 159 */ 160 public String getName() { 161 return name; 162 } 163 164 /*** 165 * @return 166 */ 167 public int getColumns() { 168 return columns; 169 } 170 171 /*** 172 * @return 173 */ 174 public int getPlayerCount() { 175 return playerCount; 176 } 177 178 /*** 179 * @param i 180 */ 181 public void setPlayerCount(final int i) { 182 playerCount = i; 183 } 184 185 /*** 186 * @param string 187 */ 188 public void setDescription(final String string) { 189 description = string; 190 } 191 192 /*** 193 * @param string 194 */ 195 public void setName(final String string) { 196 name = string; 197 } 198 199 public Point getPositionFromIndex(final int index) { 200 if (index < 0 || index >= columns * rows) { 201 SWT.error(SWT.ERROR_INVALID_RANGE); 202 } 203 204 int y = (int) (index / columns); 205 int x = index % columns; 206 207 return new Point(x, y); 208 } 209 210 public int getIndexFromPosition(final int column, final int row) { 211 return column + row * columns; 212 } 213 214 public Rectangle getTileBounds(final int index) { 215 Point position = getPositionFromIndex(index); 216 217 return getTileBounds(position.x, position.y); 218 } 219 220 public Rectangle getTileBounds(final int column, final int row) { 221 if (column < 0 || column >= columns) { 222 SWT.error(SWT.ERROR_INVALID_RANGE); 223 } else if (row < 0 || row >= rows) { 224 SWT.error(SWT.ERROR_INVALID_RANGE); 225 } 226 227 int offset = 0; 228 229 if (column % 2 == 1) { 230 offset = (int) (Tile.HEIGHT * 0.5); 231 } 232 233 Rectangle bounds = 234 new Rectangle( 235 column * (int) ((float) Tile.WIDTH * 2 / 3f), 236 row * Tile.HEIGHT + offset, 237 Tile.WIDTH, 238 Tile.HEIGHT); 239 240 return bounds; 241 } 242 243 public int[] getNeighboringIndexes(final int index) { 244 int[] indexes = new int[6]; 245 int i = -1; 246 247 Point position = getPositionFromIndex(index); 248 if (position != null) { 249 if (checkBounds(position.x, position.y - 1)) { 250 indexes[++i] = getIndexFromPosition(position.x, position.y - 1); 251 } 252 253 if (checkBounds(position.x, position.y + 1)) { 254 indexes[++i] = getIndexFromPosition(position.x, position.y + 1); 255 } 256 257 if (checkBounds(position.x - 1, position.y)) { 258 indexes[++i] = getIndexFromPosition(position.x - 1, position.y); 259 } 260 261 if (checkBounds(position.x + 1, position.y)) { 262 indexes[++i] = getIndexFromPosition(position.x + 1, position.y); 263 } 264 265 if (position.x % 2 == 0) { 266 if (checkBounds(position.x - 1, position.y - 1)) { 267 indexes[++i] = 268 getIndexFromPosition(position.x - 1, position.y - 1); 269 } 270 271 if (checkBounds(position.x + 1, position.y - 1)) { 272 indexes[++i] = 273 getIndexFromPosition(position.x + 1, position.y - 1); 274 } 275 } else { 276 if (checkBounds(position.x + 1, position.y + 1)) { 277 indexes[++i] = 278 getIndexFromPosition(position.x + 1, position.y + 1); 279 } 280 281 if (checkBounds(position.x - 1, position.y + 1)) { 282 indexes[++i] = 283 getIndexFromPosition(position.x - 1, position.y + 1); 284 } 285 } 286 } 287 288 int length = i + 1; 289 if (length != 6) { 290 int[] arr = new int[length]; 291 System.arraycopy(indexes, 0, arr, 0, length); 292 293 indexes = arr; 294 } 295 296 return indexes; 297 } 298 299 private boolean checkBounds(final int column, final int row) { 300 return (column < columns) 301 && (column > -1) 302 && (row < rows) 303 && (row > -1); 304 } 305 }

This page was automatically generated by Maven