79 lines
1.6 KiB
Java
79 lines
1.6 KiB
Java
package chess.view.DDDrender.loader;
|
|
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import chess.model.Color;
|
|
import chess.model.Piece;
|
|
import chess.model.PieceVisitor;
|
|
import chess.model.pieces.Bishop;
|
|
import chess.model.pieces.King;
|
|
import chess.model.pieces.Knight;
|
|
import chess.model.pieces.Pawn;
|
|
import chess.model.pieces.Queen;
|
|
import chess.model.pieces.Rook;
|
|
import chess.view.DDDrender.DDDModel;
|
|
|
|
/**
|
|
* Visitor that returns the 3d model of a piece.
|
|
*/
|
|
public class Piece3DModel implements PieceVisitor<String> {
|
|
|
|
private static final String basePath = "3d/";
|
|
private static final Map<String, DDDModel> cache = new HashMap<>();
|
|
|
|
public DDDModel getModel(Piece piece) throws IOException {
|
|
if (piece == null)
|
|
return null;
|
|
|
|
String path = basePath + colorToString(piece.getColor()) + "-" + visit(piece) + ".fbx";
|
|
return getModel(path);
|
|
}
|
|
|
|
private DDDModel getModel(String path) throws IOException {
|
|
DDDModel model = cache.get(path);
|
|
if (model != null)
|
|
return model;
|
|
|
|
model = ModelLoader.loadModel(path);
|
|
cache.put(path, model);
|
|
return model;
|
|
}
|
|
|
|
private String colorToString(Color color) {
|
|
return color == Color.Black ? "black" : "white";
|
|
}
|
|
|
|
@Override
|
|
public String visitPiece(Bishop bishop) {
|
|
return "bishop";
|
|
}
|
|
|
|
@Override
|
|
public String visitPiece(King king) {
|
|
return "king";
|
|
}
|
|
|
|
@Override
|
|
public String visitPiece(Knight knight) {
|
|
return "knight";
|
|
}
|
|
|
|
@Override
|
|
public String visitPiece(Pawn pawn) {
|
|
return "pawn";
|
|
}
|
|
|
|
@Override
|
|
public String visitPiece(Queen queen) {
|
|
return "queen";
|
|
}
|
|
|
|
@Override
|
|
public String visitPiece(Rook rook) {
|
|
return "rook";
|
|
}
|
|
|
|
}
|