This commit is contained in:
Сергей Полевой 2022-10-08 17:51:17 +04:00
parent 7758a3b7cc
commit 0abd2c2ca3
3 changed files with 13 additions and 12 deletions

View File

@ -56,13 +56,14 @@ public abstract class AbstractMap {
private boolean objectIntersects() { private boolean objectIntersects() {
float[] location = _drawingObject.getCurrentPosition(); float[] location = _drawingObject.getCurrentPosition();
Rectangle self = new Rectangle((int) location[0], (int) location[2], (int) location[1] - (int) location[0], (int) location[3] - (int) location[2]);
for (int i = 0; i < _map.length; i++) for (int i = 0; i < _map.length; i++)
{ {
for (int j = 0; j < _map[i].length; j++) for (int j = 0; j < _map[i].length; j++)
{ {
if (_map[i][j] == _barrier) if (_map[i][j] == _barrier)
{ {
if (i * _size_x >= location[0] && (i + 1) * _size_x <= location[1] && j * _size_y >= location[2] && (j + 1) * _size_y <= location[3]) if (self.intersects(new Rectangle(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y)))
{ {
return true; return true;
} }

View File

@ -9,14 +9,14 @@ public class ForestMap extends AbstractMap {
protected void drawBarrierPart(Graphics2D g, int i, int j) protected void drawBarrierPart(Graphics2D g, int i, int j)
{ {
g.setColor(barrierColor); g.setColor(barrierColor);
g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1)); g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y);
} }
@Override @Override
protected void drawRoadPart(Graphics2D g, int i, int j) protected void drawRoadPart(Graphics2D g, int i, int j)
{ {
g.setColor(roadColor); g.setColor(roadColor);
g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1)); g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y);
} }
@Override @Override
@ -32,16 +32,16 @@ public class ForestMap extends AbstractMap {
{ {
int x = _random.nextInt(2, 49); int x = _random.nextInt(2, 49);
int y = _random.nextInt(3, 50); 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 points = new int[] { _map[y][x], _map[y - 1][x], _map[y - 2][x], _map[y - 2][x - 1], _map[y - 2][x + 1], _map[y - 3][x] };
var forComparison = new int[] { _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad }; var forComparison = new int[] { _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad };
if (Arrays.equals(points, forComparison)) if (Arrays.equals(points, forComparison))
{ {
_map[x][y] = _barrier; _map[y][x] = _barrier;
_map[x][y - 1] = _barrier; _map[y - 1][x] = _barrier;
_map[x][y - 2] = _barrier; _map[y - 2][x] = _barrier;
_map[x - 1][y - 2] = _barrier; _map[y - 2][x - 1] = _barrier;
_map[x + 1][y - 2] = _barrier; _map[y - 2][x + 1] = _barrier;
_map[x][y - 3] = _barrier; _map[y - 3][x] = _barrier;
counter++; counter++;
} }
} }

View File

@ -8,13 +8,13 @@ public class SimpleMap extends AbstractMap {
@Override @Override
protected void drawBarrierPart(Graphics2D g, int i, int j) { protected void drawBarrierPart(Graphics2D g, int i, int j) {
g.setColor(barrierColor); g.setColor(barrierColor);
g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1)); g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y);
} }
@Override @Override
protected void drawRoadPart(Graphics2D g, int i, int j) { protected void drawRoadPart(Graphics2D g, int i, int j) {
g.setColor(roadColor); g.setColor(roadColor);
g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1)); g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y);
} }
@Override @Override