Files
BUT3ProjetJeuGroupe/fr/iut_fbleau/Avalam/ui/BoardView.java

119 lines
3.2 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package fr.iut_fbleau.Avalam.ui;
import fr.iut_fbleau.Avalam.AvalamBoard;
import fr.iut_fbleau.Avalam.Tower;
import javax.swing.*;
import java.awt.*;
/**
* BoardView est la vue principale du plateau Avalam.
*
* Elle gère :
* - laffichage des tours (PieceLayer)
* - laffichage des coups possibles (HighlightLayer)
* - les clics via InteractionController
*
* Toute la logique de jeu est déléguée au moteur AvalamBoard
* et au contrôleur InteractionController.
*/
public class BoardView extends JLayeredPane {
/** Référence au moteur Avalam */
private AvalamBoard board;
/** Couche daffichage des rond verts */
private HighlightLayer highlightLayer;
/** Couche daffichage des tours */
private PieceLayer pieceLayer;
/** Contrôleur des interactions */
private InteractionController controller;
/** Décalages et dimensions pour l'affichage */
private final int size = 50;
private final int spacing = 70;
private final int xBase = 60;
private final int yBase = 60;
/** Callback vers AvalamWindow pour mises à jour (score, tour, fin de partie) */
private Runnable boardUpdateCallback;
/**
* Constructeur de la vue du plateau.
*
* @param board moteur Avalam utilisé pour afficher la grille
* @param boardUpdateCallback callback appelé après chaque coup
*/
public BoardView(AvalamBoard board, Runnable boardUpdateCallback) {
this.board = board;
this.boardUpdateCallback = boardUpdateCallback;
setLayout(null);
// Contrôleur
this.controller = new InteractionController(board, this);
// Couche Highlight
highlightLayer = new HighlightLayer(xBase, yBase, spacing, size);
add(highlightLayer, JLayeredPane.DEFAULT_LAYER);
// Couche des pièces
pieceLayer = new PieceLayer();
add(pieceLayer, JLayeredPane.PALETTE_LAYER);
setPreferredSize(new Dimension(800, 800));
refresh();
}
/**
* Appelée par InteractionController quand un coup est joué.
* Permet à AvalamWindow de rafraîchir :
* - scores
* - affichage du joueur courant
* - détection fin de partie
*/
public void onBoardUpdated() {
if (boardUpdateCallback != null) {
boardUpdateCallback.run();
}
}
/**
* Met à jour toutes les couches visuelles.
*/
public void refresh() {
// Mise à jour des pièces
pieceLayer.displayGrid(
boardGrid(),
xBase, yBase, spacing, size,
(r, c) -> controller.onPieceClicked(r, c)
);
// Mise à jour des highlights
highlightLayer.setLegalMoves(controller.getLegalMoves());
highlightLayer.repaint();
pieceLayer.repaint();
repaint();
}
/**
* Renvoie la grille de tours depuis AvalamBoard.
*/
private Tower[][] boardGrid() {
Tower[][] grid = new Tower[AvalamBoard.SIZE][AvalamBoard.SIZE];
for (int r = 0; r < AvalamBoard.SIZE; r++) {
for (int c = 0; c < AvalamBoard.SIZE; c++) {
grid[r][c] = board.getTowerAt(r, c);
}
}
return grid;
}
}