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

119 lines
3.2 KiB
Java
Raw Normal View History

package fr.iut_fbleau.Avalam.ui;
2025-11-25 16:02:23 -05:00
import fr.iut_fbleau.Avalam.AvalamBoard;
import fr.iut_fbleau.Avalam.Tower;
import javax.swing.*;
2025-11-25 16:02:23 -05:00
import java.awt.*;
/**
2025-11-25 16:02:23 -05:00
* BoardView est la vue principale du plateau Avalam.
*
2025-11-25 16:02:23 -05:00
* Elle gère :
* - laffichage des tours (PieceLayer)
* - laffichage des coups possibles (HighlightLayer)
* - les clics via InteractionController
*
2025-11-25 16:02:23 -05:00
* Toute la logique de jeu est déléguée au moteur AvalamBoard
* et au contrôleur InteractionController.
*/
public class BoardView extends JLayeredPane {
2025-11-25 16:02:23 -05:00
/** Référence au moteur Avalam */
private AvalamBoard board;
2025-11-25 16:02:23 -05:00
/** Couche daffichage des rond verts */
private HighlightLayer highlightLayer;
2025-11-25 16:02:23 -05:00
/** Couche daffichage des tours */
private PieceLayer pieceLayer;
2025-11-25 16:02:23 -05:00
/** Contrôleur des interactions */
private InteractionController controller;
2025-11-25 16:02:23 -05:00
/** 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.
*
2025-11-25 16:02:23 -05:00
* @param board moteur Avalam utilisé pour afficher la grille
* @param boardUpdateCallback callback appelé après chaque coup
*/
2025-11-25 16:02:23 -05:00
public BoardView(AvalamBoard board, Runnable boardUpdateCallback) {
this.board = board;
this.boardUpdateCallback = boardUpdateCallback;
setLayout(null);
2025-11-25 16:02:23 -05:00
// Contrôleur
this.controller = new InteractionController(board, this);
2025-11-25 16:02:23 -05:00
// Couche Highlight
highlightLayer = new HighlightLayer(xBase, yBase, spacing, size);
add(highlightLayer, JLayeredPane.DEFAULT_LAYER);
2025-11-25 16:02:23 -05:00
// Couche des pièces
pieceLayer = new PieceLayer();
add(pieceLayer, JLayeredPane.PALETTE_LAYER);
2025-11-25 16:02:23 -05:00
setPreferredSize(new Dimension(800, 800));
refresh();
}
/**
2025-11-25 16:02:23 -05:00
* 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() {
2025-11-25 16:02:23 -05:00
// Mise à jour des pièces
pieceLayer.displayGrid(
2025-11-25 16:02:23 -05:00
boardGrid(),
xBase, yBase, spacing, size,
(r, c) -> controller.onPieceClicked(r, c)
);
2025-11-25 16:02:23 -05:00
// Mise à jour des highlights
highlightLayer.setLegalMoves(controller.getLegalMoves());
highlightLayer.repaint();
2025-11-25 16:02:23 -05:00
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;
}
}