Added class ForestMap

This commit is contained in:
Сергей Полевой 2022-10-08 16:16:03 +04:00
parent 0df20f4008
commit a2e79b08bd

49
ForestMap.java Normal file
View File

@ -0,0 +1,49 @@
import java.awt.*;
import java.util.Arrays;
public class ForestMap extends AbstractMap {
private final Color barrierColor = Color.green;
private final Color roadColor = new Color(165, 42, 42, 255);
@Override
protected void drawBarrierPart(Graphics2D g, int i, int j)
{
g.setColor(barrierColor);
g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1));
}
@Override
protected void drawRoadPart(Graphics2D g, int i, int j)
{
g.setColor(roadColor);
g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1));
}
@Override
protected void generateMap() {
_map = new int[50][50];
_size_x = (float)_width / _map[0].length;
_size_y = (float)_height / _map.length;
int counter = 0;
for (int[] row : _map) {
Arrays.fill(row, _freeRoad);
}
while (counter < 20)
{
int x = _random.nextInt(2, 49);
int y = _random.nextInt(3, 50);
var points = new int[] { _map[x][y], _map[x][y - 1], _map[x][y - 2], _map[x - 1][y - 2], _map[x + 1][y - 2], _map[x][y - 3] };
var forComparison = new int[] { _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad };
if (Arrays.equals(points, forComparison))
{
_map[x][y] = _barrier;
_map[x][y - 1] = _barrier;
_map[x][y - 2] = _barrier;
_map[x - 1][y - 2] = _barrier;
_map[x + 1][y - 2] = _barrier;
_map[x][y - 3] = _barrier;
counter++;
}
}
}
}