Skip to content
Snippets Groups Projects
Commit 712b7f63 authored by MasterPyo's avatar MasterPyo
Browse files

click piece and click tile are now both in Main controller, instead of boardView

parent 8fa1f5db
No related merge requests found
...@@ -6,9 +6,23 @@ import javafx.scene.input.MouseEvent; ...@@ -6,9 +6,23 @@ import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent; import javafx.scene.input.ScrollEvent;
import javafx.stage.Stage; import javafx.stage.Stage;
import model.Board; import model.Board;
import model.Position;
import view.*; import view.*;
import java.util.ArrayList;
public class Main extends Application { public class Main extends Application {
private CustomCamera camera;
private Group group;
private CustomScene scene;
private CustomMaterial material;
private CustomLight light;
private Custom3dModel model;
private Desk desk;
private Board board;
private BoardView boardView;
@Override @Override
public void start(Stage stage) { public void start(Stage stage) {
...@@ -19,31 +33,31 @@ public class Main extends Application { ...@@ -19,31 +33,31 @@ public class Main extends Application {
stage.setTitle("Chess3D - Tuna Acikbas & Olivier Pillods"); // title stage.setTitle("Chess3D - Tuna Acikbas & Olivier Pillods"); // title
// init - camera // init - camera
CustomCamera camera = new CustomCamera(1, 1000, 70); // create a camera and some settings camera = new CustomCamera(1, 1000, 70); // create a camera and some settings
// init - scene, main container // init - scene, main container
Group group = new Group(); // will contain all the elements "to show" on scene group = new Group(); // will contain all the elements "to show" on scene
CustomScene scene = new CustomScene(group, camera.getCamera(), SceneAntialiasing.BALANCED); scene = new CustomScene(group, camera.getCamera(), SceneAntialiasing.BALANCED);
// init - materials and shaders // init - materials and shaders
CustomMaterial material = new CustomMaterial(); // prepare all the 3D materials (shaders) material = new CustomMaterial(); // prepare all the 3D materials (shaders)
// init - all lights // init - all lights
CustomLight light = new CustomLight(); // setups different custom lights light = new CustomLight(); // setups different custom lights
light.addAllToScene(group); // adds all the lights to scene light.addAllToScene(group); // adds all the lights to scene
// init - import all 3D models from .obj files // init - import all 3D models from .obj files
Custom3dModel model = new Custom3dModel(); model = new Custom3dModel();
// init - creates a wooden desk // init - creates a wooden desk
Desk desk = new Desk(material); desk = new Desk(material);
desk.addAllToScene(group); // adds the desk to scene desk.addAllToScene(group); // adds the desk to scene
// init - creates a (model) list of all pieces // init - creates a (model) list of all pieces
Board board = new Board(); board = new Board();
// init - creates a (visual) chessboard of 8x8, and all (visual) 3d pieces // init - creates a (visual) chessboard of 8x8, and all (visual) 3d pieces
BoardView boardView = new BoardView(model, material, board); boardView = new BoardView(model, material, board);
boardView.addAllToScene(group); // adds the board and the pieces to scene boardView.addAllToScene(group); // adds the board and the pieces to scene
// event -------------------------------------------------------------- // event --------------------------------------------------------------
...@@ -58,14 +72,14 @@ public class Main extends Application { ...@@ -58,14 +72,14 @@ public class Main extends Application {
for(int n = 0 ; n < 64 ; n++) { for(int n = 0 ; n < 64 ; n++) {
PieceView piece = boardView.getPiece(n); PieceView piece = boardView.getPiece(n);
if(piece != null) { if(piece != null) {
piece.getObj().addEventHandler(MouseEvent.MOUSE_CLICKED, event -> boardView.clickPiece(material, piece, board)); piece.getObj().addEventHandler(MouseEvent.MOUSE_CLICKED, event -> clickPiece(piece));
} }
} }
// event - click on (visual) "colored tiles" to move the selected piece there // event - click on (visual) "colored tiles" to move the selected piece there
for(int n = 0 ; n < 64 ; n++) { for(int n = 0 ; n < 64 ; n++) {
TileView tile = boardView.getTile(n); TileView tile = boardView.getTile(n);
tile.getObj().addEventHandler(MouseEvent.MOUSE_CLICKED, event -> boardView.clickTile(material, model, tile, board)); tile.getObj().addEventHandler(MouseEvent.MOUSE_CLICKED, event -> clickTile(tile));
} }
// start -------------------------------------------------------------- // start --------------------------------------------------------------
...@@ -78,4 +92,102 @@ public class Main extends Application { ...@@ -78,4 +92,102 @@ public class Main extends Application {
public static void main(String[] args) { public static void main(String[] args) {
launch(); launch();
} }
public void clickPiece(PieceView clickedPiece) {
// clicking a piece will ask her available moves
Position position = clickedPiece.getPosition();
board.setSelected(position);
boardView.resetColor(material);
boardView.setColor(material, position, CustomMaterial.SELECTED_TILE);
ArrayList<Position> moves = board.getPiece(position).getAvailableMoves(board);
// shows all available moves on the tiles, with colors, each tile-color having a meaning
for (Position move : moves) {
if(board.isFree(move)) {
boardView.setColor(material, move, CustomMaterial.MOVABLE_TILE);
} else if(board.isEnemy(move, position)) {
boardView.setColor(material, move, CustomMaterial.ATTACK_TILE);
} else {
boardView.setColor(material, move, CustomMaterial.SPECIAL_TILE); // any special move
}
// PROMOTION (attacking or not)
if(board.isPawn(position) && (move.getY() == 8 || move.getY() == 1)) {
boardView.setColor(material, move, CustomMaterial.SPECIAL_TILE);
}
}
}
public void clickTile(TileView clickedTile) {
int state = clickedTile.getState();
Position selected = board.getSelected();
Position arrival = clickedTile.getPosition();
if(state != TileView.NORMAL && state != TileView.SELECTED) {
// --- ALL SPECIAL MOVES ---
if(state == TileView.SPECIAL) {
if(board.isPawn(selected)) {
// --- PROMOTION ---
if(arrival.getY() == 8 || arrival.getY() == 1) {
// --- ATTACK (while doing promotion) ---
if (!board.isFree(arrival)) {
boardView.killPiece(arrival); // place piece on graveyard (visual)
}
// Promotion to Queen (by default, Queen for now)
board.mutationOfSelectedPiece(Board.QUEEN); // (on model)
boardView.mutationPiece(model, selected, Custom3dModel.QUEEN); // (on view)
// --- MOVE (while doing promotion) ---
board.movePiece(selected, arrival); // move piece (on model)
boardView.movePiece(selected, arrival); // move piece (on 3d view)
}
// --- PRISE EN PASSANT ---
else {
// PRE : if it's not a promotion, then it's "prise en passant", because of pawn rules
// we don't need to test y-position
}
}
// --- CASTLING (from king) ---
if(board.isKing(selected)) {
// PRE (1) : the arrival piece CAN ONLY BE ROOK, because of king rules
// PRE (2) : we don't need to test if rook !hasBeenMoved(), because of king rules
// PRE (3) : we don't need to test if king !hasBeenMoved(), because of king rules
int x = Board.D; // king x arrival, for west-rook
if(arrival.getX() == Board.H) {
x = Board.F; // king x arrival, for east-rook
}
Position kingArrival = new Position(x, selected.getY());
board.movePiece(selected, kingArrival); // move king (on model)
boardView.movePiece(selected, kingArrival); // move king (on 3d view)
board.movePiece(arrival, selected); // move rook (on model)
boardView.movePiece(arrival, selected); // move rook (on 3d view)
}
// --- CASTLING (from rook) ---
else if(board.isRook(selected)) {
// PRE (1) : the arrival piece CAN ONLY BE KING, because of rook rules
// PRE (2) : we don't need to test if king !hasBeenMoved(), because of rook rules
// PRE (3) : we don't need to test if rook !hasBeenMoved(), because of rook rules
int x = Board.D; // king x arrival, for west-rook
if (selected.getX() == Board.H) {
x = Board.F; // king x arrival, for east-rook
}
Position kingArrival = new Position(x, arrival.getY());
board.movePiece(arrival, kingArrival); // move king (on model)
boardView.movePiece(arrival, kingArrival); // move king (on 3d view)
board.movePiece(selected, arrival); // move rook (on model)
boardView.movePiece(selected, arrival); // move rook (on 3d view)
}
}
// --- STANDARD MOVES ---
else {
// --- ATTACK ---
if(state == TileView.ATTACK) {
boardView.killPiece(arrival); // place piece on graveyard (visual)
}
// --- MOVE ---
board.movePiece(selected, arrival); // move piece (on model)
boardView.movePiece(selected, arrival); // move piece (on 3d view)
}
}
// --- Resets board coloring, doing any action or not ---
boardView.resetColor(material);
}
} }
\ No newline at end of file
...@@ -4,8 +4,6 @@ import javafx.scene.Group; ...@@ -4,8 +4,6 @@ import javafx.scene.Group;
import javafx.scene.shape.*; import javafx.scene.shape.*;
import model.*; import model.*;
import java.util.ArrayList;
public class BoardView { public class BoardView {
public static final int BOARD_SIZE = 16, CENTERED_ORIGIN = -BOARD_SIZE*7/2, BOARD_HEIGHT = -5, GRAVEYARD_HEIGHT = -1; public static final int BOARD_SIZE = 16, CENTERED_ORIGIN = -BOARD_SIZE*7/2, BOARD_HEIGHT = -5, GRAVEYARD_HEIGHT = -1;
...@@ -123,7 +121,7 @@ public class BoardView { ...@@ -123,7 +121,7 @@ public class BoardView {
piece.getObj().setTranslateY(GRAVEYARD_HEIGHT); // place at right height (Y axis) on table piece.getObj().setTranslateY(GRAVEYARD_HEIGHT); // place at right height (Y axis) on table
} }
private void resetColor(CustomMaterial material) { public void resetColor(CustomMaterial material) {
for(int n = 0, j = 1 ; j <= 8 ; j++) { for(int n = 0, j = 1 ; j <= 8 ; j++) {
for (int i = 1; i <= 8; i++, n++) { for (int i = 1; i <= 8; i++, n++) {
// resets board with black and white - bottom left (first case) is black, then alternates // resets board with black and white - bottom left (first case) is black, then alternates
...@@ -137,7 +135,7 @@ public class BoardView { ...@@ -137,7 +135,7 @@ public class BoardView {
} }
} }
private void setColor(CustomMaterial material, Position position, int type) { public void setColor(CustomMaterial material, Position position, int type) {
int id = position.getID(); int id = position.getID();
// set the new color-type (visually) // set the new color-type (visually)
getTileObj(id).setMaterial(material.get(type)); getTileObj(id).setMaterial(material.get(type));
...@@ -151,105 +149,7 @@ public class BoardView { ...@@ -151,105 +149,7 @@ public class BoardView {
} }
} }
public void clickPiece(CustomMaterial material, PieceView clickedPiece, Board board) { public void movePiece(Position origin, Position arrival) {
// clicking a piece will ask her available moves
Position position = clickedPiece.getPosition();
board.setSelected(position);
resetColor(material);
setColor(material, position, CustomMaterial.SELECTED_TILE);
ArrayList<Position> moves = board.getPiece(position).getAvailableMoves(board);
// shows all available moves on the tiles, with colors, each tile-color having a meaning
for (Position move : moves) {
if(board.isFree(move)) {
setColor(material, move, CustomMaterial.MOVABLE_TILE);
} else if(board.isEnemy(move, position)) {
setColor(material, move, CustomMaterial.ATTACK_TILE);
} else {
setColor(material, move, CustomMaterial.SPECIAL_TILE); // any special move
}
// PROMOTION (attacking or not)
if(board.isPawn(position) && (move.getY() == 8 || move.getY() == 1)) {
setColor(material, move, CustomMaterial.SPECIAL_TILE);
}
}
}
public void clickTile(CustomMaterial material, Custom3dModel model, TileView clickedTile, Board board) {
int state = clickedTile.getState();
Position selected = board.getSelected();
Position arrival = clickedTile.getPosition();
if(state != TileView.NORMAL && state != TileView.SELECTED) {
// --- ALL SPECIAL MOVES ---
if(state == TileView.SPECIAL) {
if(board.isPawn(selected)) {
// --- PROMOTION ---
if(arrival.getY() == 8 || arrival.getY() == 1) {
// --- ATTACK (while doing promotion) ---
if (!board.isFree(arrival)) {
killPiece(arrival); // place piece on graveyard (visual)
}
// Promotion to Queen (by default, Queen for now)
board.mutationOfSelectedPiece(Board.QUEEN); // (on model)
mutationPiece(model, selected, Custom3dModel.QUEEN); // (on view)
// --- MOVE (while doing promotion) ---
board.movePiece(selected, arrival); // move piece (on model)
movePiece(selected, arrival); // move piece (on 3d view)
}
// --- PRISE EN PASSANT ---
else {
// PRE : if it's not a promotion, then it's "prise en passant", because of pawn rules
// we don't need to test y-position
}
}
// --- CASTLING (from king) ---
if(board.isKing(selected)) {
// PRE (1) : the arrival piece CAN ONLY BE ROOK, because of king rules
// PRE (2) : we don't need to test if rook !hasBeenMoved(), because of king rules
// PRE (3) : we don't need to test if king !hasBeenMoved(), because of king rules
int x = Board.D; // king x arrival, for west-rook
if(arrival.getX() == Board.H) {
x = Board.F; // king x arrival, for east-rook
}
Position kingArrival = new Position(x, selected.getY());
board.movePiece(selected, kingArrival); // move king (on model)
movePiece(selected, kingArrival); // move king (on 3d view)
board.movePiece(arrival, selected); // move rook (on model)
movePiece(arrival, selected); // move rook (on 3d view)
}
// --- CASTLING (from rook) ---
else if(board.isRook(selected)) {
// PRE (1) : the arrival piece CAN ONLY BE KING, because of rook rules
// PRE (2) : we don't need to test if king !hasBeenMoved(), because of rook rules
// PRE (3) : we don't need to test if rook !hasBeenMoved(), because of rook rules
int x = Board.D; // king x arrival, for west-rook
if (selected.getX() == Board.H) {
x = Board.F; // king x arrival, for east-rook
}
Position kingArrival = new Position(x, arrival.getY());
board.movePiece(arrival, kingArrival); // move king (on model)
movePiece(arrival, kingArrival); // move king (on 3d view)
board.movePiece(selected, arrival); // move rook (on model)
movePiece(selected, arrival); // move rook (on 3d view)
}
}
// --- STANDARD MOVES ---
else {
// --- ATTACK ---
if(state == TileView.ATTACK) {
killPiece(arrival); // place piece on graveyard (visual)
}
// --- MOVE ---
board.movePiece(selected, arrival); // move piece (on model)
movePiece(selected, arrival); // move piece (on 3d view)
}
}
// --- Resets board coloring, doing any action or not ---
resetColor(material);
}
private void movePiece(Position origin, Position arrival) {
PieceView piece = getPiece(origin.getID()); PieceView piece = getPiece(origin.getID());
piece.setPosition(arrival); // changes position to arrival piece.setPosition(arrival); // changes position to arrival
setPiece(piece); // copies the piece from origin ID to arrival ID setPiece(piece); // copies the piece from origin ID to arrival ID
...@@ -257,7 +157,7 @@ public class BoardView { ...@@ -257,7 +157,7 @@ public class BoardView {
updatePiece(arrival); // updates 3d model position (visual) updatePiece(arrival); // updates 3d model position (visual)
} }
private void killPiece(Position p) { // graveyards are specified as negative x public void killPiece(Position p) { // graveyards are specified as negative x
PieceView piece = getPiece(p.getID()); PieceView piece = getPiece(p.getID());
if(piece.getColor() == Piece.WHITE){ if(piece.getColor() == Piece.WHITE){
white_death_count++; white_death_count++;
...@@ -269,7 +169,7 @@ public class BoardView { ...@@ -269,7 +169,7 @@ public class BoardView {
updateGraveyardPiece(piece); updateGraveyardPiece(piece);
} }
private void mutationPiece(Custom3dModel model, Position selected, int type) { public void mutationPiece(Custom3dModel model, Position selected, int type) {
// for view side, we can't "overwrite" the piece, we need to "edit" it, // for view side, we can't "overwrite" the piece, we need to "edit" it,
// otherwise the old 3d model would stay on the scene // otherwise the old 3d model would stay on the scene
MeshView obj = getPieceObj(selected.getID()); MeshView obj = getPieceObj(selected.getID());
......
No preview for this file type
No preview for this file type
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment