From ed422215f0fa30eb1545be59ec64f79e36c0a283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 15:40:01 +0400 Subject: [PATCH] Added class AbstractMap --- AbstractMap.java | 102 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 AbstractMap.java diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..ce5d4b5 --- /dev/null +++ b/AbstractMap.java @@ -0,0 +1,102 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +public abstract class AbstractMap { + private IDrawingObject _drawingObject = null; + protected int[][] _map = null; + protected int _width; + protected int _height; + protected float _size_x; + protected float _size_y; + protected final Random _random = new Random(); + protected final int _freeRoad = 0; + protected final int _barrier = 1; + + public Image createMap(int width, int height, IDrawingObject drawingObject) { + _width = width; + _height = height; + _drawingObject = drawingObject; + do { + generateMap(); + } while (!setObjectOnMap()); + return drawMapWithObject(); + } + + public Image moveObject(Direction direction) { + _drawingObject.moveObject(direction); + if (objectIntersects()) { + switch (direction) { + case Left -> _drawingObject.moveObject(Direction.Right); + case Right -> _drawingObject.moveObject(Direction.Left); + case Up -> _drawingObject.moveObject(Direction.Down); + case Down -> _drawingObject.moveObject(Direction.Up); + } + } + return drawMapWithObject(); + } + + private boolean setObjectOnMap() { + if (_drawingObject == null || _map == null) + { + return false; + } + + for (int i = 2; i < _map.length; i++) + { + for (int j = 2; j < _map[i].length; j++) + { + _drawingObject.setObject((int) (i * _size_x), (int) (j * _size_y), _width, _height); + if (!objectIntersects()) return true; + } + } + + return true; + } + + private boolean objectIntersects() { + float[] location = _drawingObject.getCurrentPosition(); + for (int i = 0; i < _map.length; i++) + { + for (int j = 0; j < _map[i].length; j++) + { + 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]) + { + return true; + } + } + } + } + + return false; + } + + private Image drawMapWithObject() { + Image img = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_ARGB); + if (_drawingObject == null || _map == null) { + return img; + } + Graphics2D g = (Graphics2D) img.getGraphics(); + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[i].length; ++j) + { + if (_map[i][j] == _freeRoad) + { + drawRoadPart(g, i, j); + } else if (_map[i][j] == _barrier) + { + drawBarrierPart(g, i, j); + } + } + } + _drawingObject.drawingObject(g); + return img; + } + + protected abstract void generateMap(); + protected abstract void drawRoadPart(Graphics2D g, int i, int j); + protected abstract void drawBarrierPart(Graphics2D g, int i, int j); +}