-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from BlazingTwist/39-hex2p-implementieren
39 hex2p implementieren
- Loading branch information
Showing
26 changed files
with
563 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/jchess/gamemode/hex2p/Hex2PlayerGame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package jchess.gamemode.hex2p; | ||
|
||
import dx.schema.types.Vector2I; | ||
import jchess.common.BaseChessGame; | ||
import jchess.common.components.TileComponent; | ||
import jchess.ecs.Entity; | ||
import jchess.gamemode.IPieceLayoutProvider; | ||
import jchess.gamemode.PieceStore; | ||
|
||
import java.awt.Point; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
public class Hex2PlayerGame extends BaseChessGame { | ||
|
||
private static final int numTilesHorizontal = 6 + 5; | ||
|
||
private static final int numTilesVertical = 21; | ||
|
||
private final Entity[][] tiles = new Entity[numTilesVertical][numTilesHorizontal]; | ||
|
||
public Hex2PlayerGame(PieceStore pieceStore, IPieceLayoutProvider layoutProvider) { | ||
super(2, pieceStore, layoutProvider); | ||
} | ||
|
||
@Override | ||
public dx.schema.types.Entity applyPerspective(dx.schema.types.Entity tile, int playerIndex) { | ||
if (playerIndex < 0 || playerIndex > 1) { | ||
throw new IllegalArgumentException("playerIndex must be 0 or 1, but was " + playerIndex); | ||
} | ||
if (playerIndex == 0) return tile; | ||
if (tile == null || tile.getTile() == null || tile.getTile().getDisplayPos() == null) return tile; | ||
|
||
Vector2I displayPos = tile.getTile().getDisplayPos(); | ||
displayPos.setX(numTilesHorizontal - displayPos.getX()); | ||
displayPos.setY(numTilesVertical - displayPos.getY()); | ||
return tile; | ||
} | ||
|
||
@Override | ||
protected Entity getEntityAtPosition(int x, int y) { | ||
if (x < 0 || x >= numTilesHorizontal) return null; | ||
if (y < 0 || y >= numTilesVertical) return null; | ||
|
||
return tiles[y][x]; | ||
} | ||
|
||
@Override | ||
protected int getDirectionFromOwnerId(int ownerId) { | ||
return ownerId == 0 ? 0 : 180; | ||
} | ||
|
||
@Override | ||
protected void generateBoard() { | ||
for (int y = 0; y < numTilesVertical; y++) { | ||
Entity[] tileRow = tiles[y]; | ||
final int x0; | ||
final int x1; | ||
|
||
if (y < 5) { // top part | ||
x0 = 5 - y; | ||
x1 = x0 + 2 * (y + 1); | ||
} else if (y < 16) { // middle part | ||
x0 = (y % 2 == 0) ? 1 : 0; | ||
x1 = numTilesHorizontal; | ||
} else { // bottom part | ||
x0 = y - 15; | ||
x1 = x0 + (6 - x0) * 2; | ||
} | ||
|
||
for (int x = x0; x < x1; x += 2) { | ||
tileRow[x] = entityManager.createEntity(); | ||
tileRow[x].tile = new TileComponent(new Point(x, y), y % 3); | ||
} | ||
} | ||
|
||
// second pass: generate neighbors | ||
Arrays.stream(tiles).flatMap(Arrays::stream) | ||
.filter(Objects::nonNull) | ||
.forEach(entity -> { | ||
TileComponent tile = entity.tile; | ||
assert tile != null; | ||
int x = tile.position.x; | ||
int y = tile.position.y; | ||
tile.neighborsByDirection.put(0, getEntityAtPosition(x, y - 2)); | ||
tile.neighborsByDirection.put(30, getEntityAtPosition(x + 1, y - 3)); | ||
tile.neighborsByDirection.put(60, getEntityAtPosition(x + 1, y - 1)); | ||
tile.neighborsByDirection.put(90, getEntityAtPosition(x + 2, y)); | ||
tile.neighborsByDirection.put(120, getEntityAtPosition(x + 1, y + 1)); | ||
tile.neighborsByDirection.put(150, getEntityAtPosition(x + 1, y + 3)); | ||
tile.neighborsByDirection.put(180, getEntityAtPosition(x, y + 2)); | ||
tile.neighborsByDirection.put(210, getEntityAtPosition(x - 1, y + 3)); | ||
tile.neighborsByDirection.put(240, getEntityAtPosition(x - 1, y + 1)); | ||
tile.neighborsByDirection.put(270, getEntityAtPosition(x - 2, y)); | ||
tile.neighborsByDirection.put(300, getEntityAtPosition(x - 1, y - 1)); | ||
tile.neighborsByDirection.put(330, getEntityAtPosition(x - 1, y - 3)); | ||
}); | ||
} | ||
|
||
} |
149 changes: 149 additions & 0 deletions
149
src/main/java/jchess/gamemode/hex2p/Hex2pPieceLayouts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package jchess.gamemode.hex2p; | ||
|
||
import jchess.common.IChessGame; | ||
import jchess.ecs.Entity; | ||
import jchess.gamemode.IPieceLayoutProvider; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
public enum Hex2pPieceLayouts implements IPieceLayoutProvider { | ||
Standard((game, tileProvider) -> populateStandardBoard(new BoardController(game, tileProvider))), | ||
Custom((game, tileProvider) -> populateCustomBoard(new BoardController(game, tileProvider))), | ||
; | ||
|
||
private static final int PLAYER_LIGHT = 0; | ||
private static final int PLAYER_DARK = 1; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(Hex2pPieceLayouts.class); | ||
|
||
public final IPieceLayoutProvider layoutProvider; | ||
|
||
Hex2pPieceLayouts(IPieceLayoutProvider layoutProvider) { | ||
this.layoutProvider = layoutProvider; | ||
} | ||
|
||
@Override | ||
public void placePieces(IChessGame game, BiFunction<Integer, Integer, Entity> tileProvider) { | ||
layoutProvider.placePieces(game, tileProvider); | ||
} | ||
|
||
private static void populateStandardBoard(BoardController boardController) { | ||
// place kings | ||
boardController.placeKing(6, 19, PLAYER_LIGHT); | ||
boardController.placeKing(6, 1, PLAYER_DARK); | ||
|
||
// place queens | ||
boardController.placeQueen(4, 19, PLAYER_LIGHT); | ||
boardController.placeQueen(4, 1, PLAYER_DARK); | ||
|
||
|
||
// place bishops | ||
for (int i = 0; i <= 4; i+=2) { | ||
boardController.placeBishop(5, i, PLAYER_DARK); | ||
boardController.placeBishop(5, 20 - i, PLAYER_LIGHT); | ||
} | ||
|
||
// place knights | ||
for (int x : new int[]{3, 7}) { | ||
boardController.placeKnight(x,18, PLAYER_LIGHT); | ||
boardController.placeKnight(x,2, PLAYER_DARK); | ||
} | ||
|
||
// place rooks | ||
for (int x : new int[] {2, 8}) { | ||
boardController.placeRook(x,17, PLAYER_LIGHT); | ||
boardController.placeRook(x,3, PLAYER_DARK); | ||
} | ||
|
||
|
||
// place pawns | ||
for (int i = 0; i < 5; i++) { | ||
for (int x : new int[] {1+i, 9-i}) { | ||
boardController.placePawn(x, 16 - i, PLAYER_LIGHT); | ||
boardController.placePawn(x, 4 + i, PLAYER_DARK); | ||
} | ||
} | ||
} | ||
|
||
private static void populateCustomBoard(BoardController boardController) { | ||
populateStandardBoard(boardController); | ||
|
||
boardController.placeArcher(4,15, PLAYER_LIGHT); | ||
boardController.placeArcher(6,15, PLAYER_LIGHT); | ||
boardController.placeArcher(4,5, PLAYER_DARK); | ||
boardController.placeArcher(6,5, PLAYER_DARK); | ||
|
||
boardController.placePegasus(5,14, PLAYER_LIGHT); | ||
boardController.placePegasus(5,6, PLAYER_DARK); | ||
|
||
boardController.placeSkrull(3,16, PLAYER_LIGHT); | ||
boardController.placeSkrull(7,16, PLAYER_LIGHT); | ||
boardController.placeSkrull(3,4, PLAYER_DARK); | ||
boardController.placeSkrull(7,4, PLAYER_DARK); | ||
|
||
boardController.placeCatapult(4,17, PLAYER_LIGHT); | ||
boardController.placeCatapult(6,17, PLAYER_LIGHT); | ||
boardController.placeCatapult(4,3, PLAYER_DARK); | ||
boardController.placeCatapult(6,3, PLAYER_DARK); | ||
|
||
} | ||
|
||
@SuppressWarnings("SameParameterValue") | ||
private record BoardController(IChessGame game, BiFunction<Integer, Integer, Entity> tileProvider) { | ||
|
||
private Entity getEntityAtPosition(int x, int y) { | ||
return tileProvider.apply(x, y); | ||
} | ||
|
||
private void placeRook(int x, int y, int playerColor) { | ||
placePiece(x, y, playerColor, Hex2pPieces.Rook); | ||
} | ||
|
||
private void placeKnight(int x, int y, int playerColor) { | ||
placePiece(x, y, playerColor, Hex2pPieces.Knight); | ||
} | ||
|
||
private void placeBishop(int x, int y, int playerColor) { | ||
placePiece(x, y, playerColor, Hex2pPieces.Bishop); | ||
} | ||
|
||
private void placeQueen(int x, int y, int playerColor) { | ||
placePiece(x, y, playerColor, Hex2pPieces.Queen); | ||
} | ||
|
||
private void placeKing(int x, int y, int playerColor) { | ||
placePiece(x, y, playerColor, Hex2pPieces.King); | ||
} | ||
|
||
private void placePawn(int x, int y, int playerColor) { | ||
placePiece(x, y, playerColor, Hex2pPieces.Pawn); | ||
} | ||
|
||
private void placeArcher(int x, int y, int playerColor) { | ||
placePiece(x, y, playerColor, Hex2pPieces.Archer); | ||
} | ||
|
||
private void placePegasus(int x, int y, int playerColor) { | ||
placePiece(x, y, playerColor, Hex2pPieces.Pegasus); | ||
} | ||
|
||
private void placeSkrull(int x, int y, int playerColor) { | ||
placePiece(x, y, playerColor, Hex2pPieces.Skrull); | ||
} | ||
|
||
private void placeCatapult(int x, int y, int playerColor) { | ||
placePiece(x, y, playerColor, Hex2pPieces.Catapult); | ||
} | ||
|
||
private void placePiece(int x, int y, int playerColor, Hex2pPieces piece) { | ||
Entity tile = getEntityAtPosition(x, y); | ||
if (tile == null) { | ||
logger.error("cannot place piece on tile ({}, {}). No tile found.", x, y); | ||
return; | ||
} | ||
game.createPiece(tile, piece.getPieceType(), playerColor); | ||
} | ||
} | ||
} |
Oops, something went wrong.