public BoardPanel( final JFrame frame, Game game, UnitTypeConfiguration unitTypeConfiguration, TileTypeConfiguration tileTypeConfiguration, TileStateConfiguration tileStateConfiguration, ChessMovementStrategy movementStrategy, UnitSelectorMode unitSelectorMode, BattleStrategyConfiguration battleStrategyConfiguration) { this.frame = frame; this.battleStrategyConfiguration = battleStrategyConfiguration; thisPanel = this; this.game = game; game.addGameObserver(this); this.board = game.getBoard(); this.movementStrategy = movementStrategy; int rows = board.getDimension().getHeight(); int columns = board.getDimension().getWidth(); setLayout(new GridLayout(rows, columns)); for (Tile tile : board.getTiles()) { final TilePanel tilePanel = new TilePanel(tileTypeConfiguration, unitTypeConfiguration, tileStateConfiguration, tile); if (UnitSelectorMode.MULTIPLE.equals(unitSelectorMode)) { tilePanel.addMouseListener( new MultipleUnitSelectorMouseListener(tilePanel, frame, thisPanel)); } if (UnitSelectorMode.SINGLE.equals(unitSelectorMode)) { tilePanel.addMouseListener( new SingleUnitSelectorMouseListener(tilePanel, frame, thisPanel)); } map.put(tile, tilePanel); add(tilePanel); } resetPositions(); }
private void deselectValidMovements(Tile from, Unit unit) { List<TilePanel> validMovement = getValidMovementPanels(from, unit); for (TilePanel panel : validMovement) { panel.removeTileStateFromKey(selectedStateKey); } repaintPanels(validMovement); }
private void repaintPanels(Iterable<TilePanel> panels) { long start = System.currentTimeMillis(); for (TilePanel panel : panels) { game.notifyGameObservers(panel.getTile().getPosition()); } long stop = System.currentTimeMillis(); Logger.getLogger(BoardPanel.class).debug("Repaint time: " + (stop - start) / 1000.0); }
public void redraw() { for (TilePanel row[] : tileGrid) { for (TilePanel tp : row) { tp.update(); } } revalidate(); repaint(); }
public void finishedSelectingUnits(TilePanel tilePanel, List<Unit> chosenUnits) { UnitActionDetector unitActionDetector = new UnitActionDetector(selectedPanel, selectedUnit, tilePanel, chosenUnits); if (unitActionDetector.isSelectingUnit()) { selectedPanel = tilePanel; selectedPanel.addTileStateFromKey(selectedStateKey); repaintPanels(selectedPanel); selectedUnit = getUnit(chosenUnits); selectValidMovements(selectedPanel.getTile(), selectedUnit); } else if (unitActionDetector.isDeselectingUnit()) { selectedPanel.removeTileStateFromKey(selectedStateKey); deselectValidMovements(selectedPanel.getTile(), selectedUnit); repaintPanels(selectedPanel); selectedPanel = null; selectedUnit = null; } else if (unitActionDetector.isMovingUnit()) { Position from = selectedPanel.getTile().getPosition(); Position to = tilePanel.getTile().getPosition(); if (movementStrategy.isValidMove(selectedUnit, from, to, unitMap)) { deselectValidMovements(selectedPanel.getTile(), selectedUnit); selectedPanel.removeTileStateFromKey(selectedStateKey); List<Unit> units = unitMap.get(to); if (units != null && !units.isEmpty()) { Unit defending = units.get(0); BattleStrategy strategy = battleStrategyConfiguration.getBattleStrategy( selectedUnit, defending, selectedPanel.getTile(), tilePanel.getTile()); Unit winner = strategy.getWinner(selectedUnit, defending); if (winner.equals(selectedUnit)) { units.remove(defending); } } board.move(selectedUnit, from, to); repaintPanels(selectedPanel); selectedPanel = null; selectedUnit = null; } } frame.setEnabled(true); resetPositions(); }
public GamePanel(ActionListener inQuitAction, GameManager inGameManager) { mQuitAction = inQuitAction; mGameManager = inGameManager; // Create the GUI to be a grid for all the tiles setLayout(new GridLayout(boardSize, boardSize)); tileGrid = new TilePanel[boardSize][boardSize]; // Create each grid square // Either give it a Start/Home label panel, or a TilePanel for (int y = 0; y < boardSize; ++y) { for (int x = 0; x < boardSize; ++x) { if (x == 4 && y == 2) { tileGrid[x][y] = new StartLabelPanel(GameHelpers.getIndexFromColor(Color.YELLOW)); } else if (x == 2 && y == 7) { tileGrid[x][y] = new HomeLabelPanel(GameHelpers.getIndexFromColor(Color.YELLOW)); } else if (x == 13 && y == 4) { tileGrid[x][y] = new StartLabelPanel(GameHelpers.getIndexFromColor(Color.GREEN)); } else if (x == 8 && y == 2) { tileGrid[x][y] = new HomeLabelPanel(GameHelpers.getIndexFromColor(Color.GREEN)); } else if (x == 11 && y == 13) { tileGrid[x][y] = new StartLabelPanel(GameHelpers.getIndexFromColor(Color.RED)); } else if (x == 13 && y == 8) { tileGrid[x][y] = new HomeLabelPanel(GameHelpers.getIndexFromColor(Color.RED)); } else if (x == 2 && y == 11) { tileGrid[x][y] = new StartLabelPanel(GameHelpers.getIndexFromColor(Color.BLUE)); } else if (x == 7 && y == 13) { tileGrid[x][y] = new HomeLabelPanel(GameHelpers.getIndexFromColor(Color.BLUE)); } else { tileGrid[x][y] = new TilePanel(mGameManager.getTile(x, y)); } add(tileGrid[x][y]); } } logo = new ImageIcon("src/fonts/sorry.png"); // Set in the card TilePanel cardLabelTile = tileGrid[boardSize / 2 - 1][boardSize / 2 - 1]; cardLabelTile.setLayout(new GridLayout(1, 1)); cardLabelTile.add(cardLabel); TilePanel cardButtonTile = tileGrid[boardSize / 2][boardSize / 2 - 1]; cardButtonTile.setLayout(new GridLayout(1, 1)); cardButtonTile.add(cardButton); // This is used to make sure the GameManager can redraw the GUI inGameManager.setGamePanel(this); redraw(); }