Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package pl.mperor.lab.java.design.pattern.structural.flyweight;

import pl.mperor.lab.java.design.pattern.structural.flyweight.terrain.Terrain;

import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

public class GameWorld {

private final int width;
private final int height;
private final Terrain[][] tiles;

public GameWorld(int width, int height) {
assert width >= 0;
assert height >= 0;

this.width = width;
this.height = height;
this.tiles = generateTiles();
}

private Terrain[][] generateTiles() {
Terrain[][] tiles = new Terrain[width][height];
ThreadLocalRandom random = ThreadLocalRandom.current();
Terrain[] terrains = Terrain.values();

for (int x = 0; x < tiles.length; x++) {
for (int y = 0; y < tiles[0].length; y++) {
tiles[x][y] = terrains[random.nextInt(terrains.length)];
}
}

return tiles;
}

public Terrain getTiles(int x, int y) {
assert x >= 0 && x < tiles.length ;
assert y >= 0 && y < tiles[0].length;

return tiles[x][y];
}

@Override
public String toString() {
return Arrays.stream(tiles)
.map(Arrays::toString)
.collect(Collectors.joining(System.lineSeparator()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package pl.mperor.lab.java.design.pattern.structural.flyweight.terrain;

public record GrassTexture() implements TerrainTexture {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package pl.mperor.lab.java.design.pattern.structural.flyweight.terrain;

public record SandTexture() implements TerrainTexture {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pl.mperor.lab.java.design.pattern.structural.flyweight.terrain;

public enum Terrain {

WATER("W", new WaterTexture(), 1),
SAND("S", new SandTexture(), 2),
GRASS("G", new GrassTexture(), 3);

private final String name;
private final TerrainTexture terrainTexture;
private final int movementCost;

Terrain(String name, TerrainTexture terrainTexture, int movementCost) {
this.name = name;
this.terrainTexture = terrainTexture;
this.movementCost = movementCost;
}

public TerrainTexture getTerrainTexture() {
return terrainTexture;
}

public int getMovementCost() {
return movementCost;
}

@Override
public String toString() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package pl.mperor.lab.java.design.pattern.structural.flyweight.terrain;

public sealed interface TerrainTexture permits GrassTexture, SandTexture, WaterTexture {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package pl.mperor.lab.java.design.pattern.structural.flyweight.terrain;

public record WaterTexture() implements TerrainTexture {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pl.mperor.lab.java.design.pattern.structural.flyweight;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import pl.mperor.lab.java.design.pattern.structural.flyweight.terrain.Terrain;

import java.util.Arrays;


public class GameWorldTest {

@Test
public void testCreatedGameWorldContainsOnlyTerrainTilesWithTheSameTexture() {
GameWorld gw = new GameWorld(20, 20);
System.out.println(gw);
Terrain firstTile = gw.getTiles(0, 0);

Assertions.assertTrue(Arrays.stream(Terrain.values())
.map(Terrain::getTerrainTexture)
.anyMatch(texture -> texture == firstTile.getTerrainTexture()),
"The same instance is used for the terrain texture!");
}

}
Loading