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 List<TilePanel> getValidMovementPanels(Tile from, Unit unit) { List<TilePanel> result = new ArrayList<TilePanel>(); List<Position> validMoves = movementStrategy.getValidMoves(unit, from.getPosition(), unitMap); for (Position validMove : validMoves) { TilePanel e = map.get(board.getTileAtPosition(validMove)); if (e != null) { result.add(e); } } return result; }
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 void tileChanged(Position p) { map.get(board.getTileAtPosition(p)).repaint(); }
private void resetPositions() { for (Tile tile : board.getTiles()) { unitMap.put(tile.getPosition(), tile.getUnits()); } }