PIbd-12_Karamushko_M.K._Air.../MyMap.java

73 lines
1.7 KiB
Java
Raw Normal View History

2022-11-10 16:44:45 +04:00
import java.awt.*;
public class MyMap extends AbstractMap
{
private Color barrierColor = Color.BLACK;
private Color roadColor = Color.GRAY;
@Override
protected void DrawBarrierPart(Graphics2D g, int i, int j)
{
g.setPaint(barrierColor);
g.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@Override
protected void DrawRoadPart(Graphics2D g, int i, int j)
{
g.setPaint(roadColor);
g.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@Override
protected void GenerateMap()
{
_map = new int[100][100];
_size_x = (float)_width / _map.length;
_size_y = (float)_height / _map[0].length;
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[0].length; ++j)
{
_map[i][j] = _freeRoad;
}
}
for(int i = 0; i < 20; ++i)
{
int x = _random.nextInt(0, 100);
int y = _random.nextInt(0, 100);
GenerateMap(x, y, _random.nextInt(13, 23));
}
}
private void GenerateMap(int x, int y, int depth)
{
if (depth <= 0) return;
boolean check = false;
while (!check)
{
int deltaX = _random.nextInt(-1, 2);
int deltaY = _random.nextInt(-1, 2);
if (x + deltaX < 0 || x + deltaX >= 100) continue;
if (y + deltaY < 0 || y + deltaY >= 100) continue;
if (_map[y + deltaY][x + deltaX] == _barrier) depth--;
x += deltaX;
y += deltaY;
_map[y][x] = _barrier;
check = true;
}
GenerateMap(x, y, depth - 1);
}
}