-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
server/src/main/java/org/schlunzis/kurtama/server/game/GenericSquareTerrainFactory.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,68 @@ | ||
package org.schlunzis.kurtama.server.game; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.schlunzis.kurtama.common.game.GameSettings; | ||
import org.schlunzis.kurtama.server.game.model.*; | ||
|
||
import java.lang.reflect.Array; | ||
|
||
@RequiredArgsConstructor | ||
public class GenericSquareTerrainFactory implements TerrainFactory { | ||
|
||
private final GameSettings gameSettings; | ||
|
||
private ITile<SquareDirection>[][] tiles; | ||
private int columns; | ||
private int rows; | ||
|
||
private static boolean isInbound(int index, int upperBound) { | ||
return index >= 0 && index < upperBound; | ||
} | ||
|
||
@Override | ||
public ITerrain<SquareDirection> create() { | ||
columns = gameSettings.columns(); | ||
rows = gameSettings.rows(); | ||
//noinspection unchecked | ||
tiles = (ITile<SquareDirection>[][]) Array.newInstance(ITile.class, columns, rows); | ||
|
||
for (int i = 0; i < tiles.length; i++) { | ||
for (int j = 0; j < tiles[i].length; j++) { | ||
tiles[i][j] = new GenericTile<>(SquareDirection.TOP); | ||
} | ||
} | ||
|
||
linkTiles(); | ||
ITile<SquareDirection>[] flatTiles = flattenArray(tiles); | ||
|
||
return new GenericTerrain<>(flatTiles, columns, rows); | ||
} | ||
|
||
private ITile<SquareDirection>[] flattenArray(ITile<SquareDirection>[][] tiles) { | ||
//noinspection unchecked | ||
ITile<SquareDirection>[] flatTiles = (ITile<SquareDirection>[]) Array.newInstance(ITile.class, columns * rows); | ||
int index = 0; | ||
for (ITile<SquareDirection>[] tileArray : tiles) { | ||
System.arraycopy(tileArray, 0, flatTiles, index, tileArray.length); | ||
index += tileArray.length; | ||
} | ||
return flatTiles; | ||
} | ||
|
||
private void linkTiles() { | ||
for (int i = 0; i < tiles.length; i++) { | ||
for (int j = 0; j < tiles[i].length; j++) { | ||
final ITile<SquareDirection> tile = tiles[i][j]; | ||
final int above = j - 1; | ||
final int left = i - 1; | ||
final int right = i + 1; | ||
final int below = j + 1; | ||
if (isInbound(above, rows)) tile.put(SquareDirection.TOP, tiles[i][above]); | ||
if (isInbound(left, columns)) tile.put(SquareDirection.LEFT, tiles[left][j]); | ||
if (isInbound(right, columns)) tile.put(SquareDirection.RIGHT, tiles[right][j]); | ||
if (isInbound(below, rows)) tile.put(SquareDirection.BOTTOM, tiles[i][below]); | ||
} | ||
} | ||
} | ||
|
||
} |
11 changes: 7 additions & 4 deletions
11
server/src/main/java/org/schlunzis/kurtama/server/game/model/GenericTerrain.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