Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a8eebce810 | ||
|
|
569e97e909 | ||
|
|
db702d2459 | ||
|
|
a2b50fbfae | ||
|
|
0f627662d9 |
173
src/AbstractMap.java
Normal file
173
src/AbstractMap.java
Normal file
@@ -0,0 +1,173 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
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 Random _random;
|
||||
protected int _freeWater = 0;
|
||||
protected int _barrier = 1;
|
||||
protected Graphics graphics;
|
||||
|
||||
public void CreateMap(Graphics g, int width, int height, IDrawingObject drawingObject) {
|
||||
graphics = g;
|
||||
_width = width;
|
||||
_height = height;
|
||||
__drawingObject = drawingObject;
|
||||
GenerateMap();
|
||||
while (!SetObjectOnMap()) {
|
||||
GenerateMap();
|
||||
}
|
||||
DrawMapWithObject();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Функция для передвижения объекта по карте
|
||||
/// </summary>
|
||||
/// <param name="direction"></param>
|
||||
/// <returns>Bitmap карты с объектом</returns>
|
||||
public void MoveObject(EnumDirection enumDirection) {
|
||||
// Проверка, что объект может переместится в требуемом направлении
|
||||
// Получаем текщую позицию объекта
|
||||
Position objectPosition = __drawingObject.GetCurrentPosition();
|
||||
float currentX = objectPosition.Left;
|
||||
float currentY = objectPosition.Top;
|
||||
|
||||
// В зависимости от направления уставналиваем dx, dy
|
||||
float dx = 0;
|
||||
float dy = 0;
|
||||
// TODO дописать код
|
||||
switch (enumDirection) {
|
||||
case (EnumDirection.None):
|
||||
break;
|
||||
case (EnumDirection.Up): {
|
||||
dy = -__drawingObject.Step();
|
||||
}
|
||||
break;
|
||||
case (EnumDirection.Down): {
|
||||
dy = __drawingObject.Step();
|
||||
}
|
||||
break;
|
||||
case (EnumDirection.Left): {
|
||||
dx = -__drawingObject.Step();
|
||||
}
|
||||
break;
|
||||
case (EnumDirection.Right): {
|
||||
dx = __drawingObject.Step();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// ЕСли нет коллизии, то перемещаем объект
|
||||
if (!CheckCollision(currentX + dx, currentY + dy)) {
|
||||
__drawingObject.MoveObject(enumDirection);
|
||||
}
|
||||
DrawMapWithObject();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Функция пытается поместить объект на карту
|
||||
/// </summary>
|
||||
/// <returns>Если удачно возвращает true, иначе - false</returns>
|
||||
private boolean SetObjectOnMap() {
|
||||
if (__drawingObject == null || _map == null) {
|
||||
return false;
|
||||
}
|
||||
// Генерируем новые координаты объекта
|
||||
int x = _random.nextInt(100, 200);
|
||||
int y = _random.nextInt(100, 200);
|
||||
__drawingObject.SetObject(x, y, _width, _height);
|
||||
|
||||
// Проверка, что объект не "накладывается" на закрытые участки
|
||||
return !CheckCollision(x, y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Функция для проверки коллизии объекта с препятствиями на карте.
|
||||
/// </summary>
|
||||
/// <param name="x">Координата x объекта</param>
|
||||
/// <param name="y">Координата y объекта</param>
|
||||
/// <returns>Возвращает true если есть коллизия и false - если ее нет</returns>
|
||||
protected boolean CheckCollision(float x, float y) {
|
||||
// Получаем ширину и высоту отображаемого объекта
|
||||
var objectPosition = __drawingObject.GetCurrentPosition();
|
||||
float objectWidth = objectPosition.Right - objectPosition.Left;
|
||||
float objectHeight = objectPosition.Bottom - objectPosition.Top;
|
||||
|
||||
// Теперь узнаем сколько клеток в ширину и высоту объект занимает на карте
|
||||
int objectCellsCountX = (int) Math.ceil(objectWidth / _size_x);
|
||||
int objectCellsCountY = (int) Math.ceil(objectHeight / _size_y);
|
||||
|
||||
// Получим координаты объекта в сетке карты
|
||||
int objectMapX = (int) Math.floor((float) x / _size_x);
|
||||
int objectMapY = (int) Math.floor((float) y / _size_y);
|
||||
|
||||
// В цикле проверяем все клетки карты на коллизию с объектом
|
||||
int dy = 0;
|
||||
int mapCellsX = _map[0].length;
|
||||
int mapCellsY = _map.length;
|
||||
int mapState = _freeWater;
|
||||
|
||||
while (objectMapY + dy < mapCellsY && dy <= objectCellsCountY) {
|
||||
int dx = 0;
|
||||
while (objectMapX + dx < mapCellsX && dx <= objectCellsCountX) {
|
||||
mapState = _map[objectMapX + dx, objectMapY + dy];
|
||||
if (mapState == _barrier) {
|
||||
return true;
|
||||
}
|
||||
dx++;
|
||||
}
|
||||
dy++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Функция для отрисовки карты с объектом
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private void DrawMapWithObject() {
|
||||
if (__drawingObject == null || _map == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _map.length; ++i) {
|
||||
for (int j = 0; j < _map[0].length; ++j) {
|
||||
if (_map[i][j] == _freeWater) {
|
||||
DrawWaterPart(graphics, i, j);
|
||||
} else if (_map[i][j] == _barrier) {
|
||||
DrawBarrierPart(graphics, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
__drawingObject.DrawingObject(graphics);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Метод для генерации карты
|
||||
/// </summary>
|
||||
protected abstract void GenerateMap();
|
||||
|
||||
/// <summary>
|
||||
/// Метод для отрисовки свободного участка на экране
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
/// <param name="i"></param>
|
||||
/// <param name="j"></param>
|
||||
protected abstract void DrawWaterPart(Graphics g, int i, int j);
|
||||
|
||||
/// <summary>
|
||||
/// Метод для отрисовки закрытого участка на экране
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
/// <param name="i"></param>
|
||||
/// <param name="j"></param>
|
||||
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
|
||||
}
|
||||
@@ -13,9 +13,9 @@ public class DrawingBoat {
|
||||
// Координата Y верхнего левого угла лодки
|
||||
private float __startPosY;
|
||||
// Ширина отрисовки лодки
|
||||
private final int __boatWidth = 100;
|
||||
private int __boatWidth = 100;
|
||||
// Высота отрисовки лодки
|
||||
private final int __boatHeight = 60;
|
||||
private int __boatHeight = 60;
|
||||
// Ширина области отрисовки
|
||||
private Integer __pictureWidth = null;
|
||||
// Высота области отрисовки
|
||||
@@ -23,13 +23,19 @@ public class DrawingBoat {
|
||||
|
||||
|
||||
// Инициализатор класса
|
||||
public void Init(int speed, float weight, Color bodyColor) {
|
||||
entityBoat = new EntityBoat();
|
||||
entityBoat.Init(speed, weight, bodyColor);
|
||||
public DrawingBoat(int speed, float weight, Color bodyColor) {
|
||||
entityBoat = new EntityBoat(speed, weight, bodyColor);
|
||||
__drawingBoatPaddle = new DrawingBoatPaddle();
|
||||
SetPaddlesCount();
|
||||
}
|
||||
|
||||
// Инициализатор свойств
|
||||
protected DrawingBoat(int speed, float weight, Color bodyColor, int boatWidth, int boatHeight) {
|
||||
this(speed, weight, bodyColor);
|
||||
__boatWidth = boatWidth;
|
||||
__boatHeight = boatHeight;
|
||||
}
|
||||
|
||||
// Метод для установки количества весел лодки
|
||||
public void SetPaddlesCount() {
|
||||
Random rnd = new Random();
|
||||
@@ -196,4 +202,9 @@ public class DrawingBoat {
|
||||
__startPosY = __pictureHeight - __boatHeight;
|
||||
}
|
||||
}
|
||||
|
||||
// Метод для получения текущих координат объекта
|
||||
Position GetCurrentPosition() {
|
||||
return new Position(__startPosX, __startPosY, __startPosX + __boatWidth, __startPosY + __boatHeight);
|
||||
}
|
||||
}
|
||||
|
||||
23
src/DrawingCatamaran.java
Normal file
23
src/DrawingCatamaran.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingCatamaran extends DrawingBoat {
|
||||
public DrawingCatamaran(int speed, float weight, Color bodyColor, Color dopColor, boolean bobbers, boolean sail) {
|
||||
super(speed, weight, bodyColor, 110, 80);
|
||||
entityBoat = new EntityCatamaran(speed, weight, bodyColor, dopColor, bobbers, sail);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTransport(Graphics g) {
|
||||
// Если объект-сущность не того класса - выходим
|
||||
if (!(entityBoat instanceof EntityCatamaran)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Передаем управление отрисовкой родительскому классу
|
||||
super.DrawTransport(g);
|
||||
|
||||
// TODO дописать отрисовку поплавков и паруса
|
||||
}
|
||||
}
|
||||
46
src/DrawingObjectBoat.java
Normal file
46
src/DrawingObjectBoat.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingObjectBoat implements IDrawingObject {
|
||||
private DrawingBoat __drawingBoat = null;
|
||||
|
||||
public DrawingObjectBoat(DrawingBoat drawingBoat) {
|
||||
__drawingBoat = drawingBoat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position GetCurrentPosition() {
|
||||
if (__drawingBoat == null)
|
||||
return null;
|
||||
return __drawingBoat.GetCurrentPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void MoveObject(EnumDirection enumDirection) {
|
||||
if (__drawingBoat == null)
|
||||
return;
|
||||
__drawingBoat.MoveTransport(enumDirection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float Step() {
|
||||
if (__drawingBoat == null)
|
||||
return 0;
|
||||
if (__drawingBoat.entityBoat == null)
|
||||
return 0;
|
||||
return __drawingBoat.entityBoat.Step();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void SetObject(int x, int y, int width, int height) {
|
||||
__drawingBoat.SetPosition(x, y, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawingObject(Graphics g) {
|
||||
if (__drawingBoat == null)
|
||||
return;
|
||||
__drawingBoat.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ public class EntityBoat {
|
||||
return Speed * 100 / Weight;
|
||||
}
|
||||
|
||||
public void Init(int speed, float weight, Color bodyColor) {
|
||||
public EntityBoat(int speed, float weight, Color bodyColor) {
|
||||
Random rnd = new Random();
|
||||
Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed;
|
||||
Weight = weight <= 0 ? rnd.nextInt(40, 70) : weight;
|
||||
|
||||
19
src/EntityCatamaran.java
Normal file
19
src/EntityCatamaran.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityCatamaran extends EntityBoat {
|
||||
// Дополнительный цвет
|
||||
public Color DopColor;
|
||||
// Признак наличия поплавков
|
||||
public boolean Bobbers;
|
||||
// Признак наличия паруса
|
||||
public boolean Sail;
|
||||
|
||||
public EntityCatamaran(int speed, float weight, Color bodyColor, Color dopColor, boolean bobbers, boolean sail) {
|
||||
super(speed, weight, bodyColor);
|
||||
DopColor = dopColor;
|
||||
Bobbers = bobbers;
|
||||
Sail = sail;
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,15 @@ package src;
|
||||
|
||||
// Перечисление для направлений движения лодки
|
||||
public enum EnumDirection {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
None(0),
|
||||
Up(1),
|
||||
Down(2),
|
||||
Left(3),
|
||||
Right(3);
|
||||
|
||||
public final int enumNumber;
|
||||
|
||||
EnumDirection(int i) {
|
||||
this.enumNumber = i;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class FormBoat {
|
||||
protected DrawingBoat _drawingBoat = new DrawingBoat();
|
||||
protected DrawingBoat _drawingBoat;
|
||||
|
||||
JPanel PanelWrapper;
|
||||
private JPanel PictureBox;
|
||||
@@ -42,12 +42,18 @@ public class FormBoat {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// Создаем новый объект отрисовки лодки
|
||||
_drawingBoat = new DrawingBoat();
|
||||
Random random = new Random();
|
||||
|
||||
// Инициализируем обхект отрисовки
|
||||
_drawingBoat.Init(random.nextInt(100, 300), random.nextInt(1000, 2000),
|
||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
|
||||
_drawingBoat = new DrawingBoat(
|
||||
random.nextInt(100, 300),
|
||||
random.nextInt(1000, 2000),
|
||||
new Color(
|
||||
random.nextInt(256),
|
||||
random.nextInt(256),
|
||||
random.nextInt(256)
|
||||
)
|
||||
);
|
||||
|
||||
// Устанавливаем позицию и размеры объекта отрисовки
|
||||
_drawingBoat.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100),
|
||||
@@ -69,6 +75,10 @@ public class FormBoat {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
super.componentResized(e);
|
||||
// Если объект null - выходим
|
||||
if (_drawingBoat == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Изменяем граници области отрисовки для объекта отрисовки
|
||||
_drawingBoat.ChangeBorders(PictureBox.getWidth(), PictureBox.getHeight());
|
||||
@@ -81,6 +91,11 @@ public class FormBoat {
|
||||
ActionListener buttonMoveClickedListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// Если объект null - выходим
|
||||
if (_drawingBoat == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Получаем имя кнопки перемещения
|
||||
String buttonName = ((JButton) e.getSource()).getName();
|
||||
|
||||
@@ -117,8 +132,8 @@ public class FormBoat {
|
||||
|
||||
// Метод отрисовки
|
||||
public void Draw() {
|
||||
// Если сущности внутри объекта отрисовки нет - выходим
|
||||
if (_drawingBoat.entityBoat == null) {
|
||||
// Если объект null - выходим
|
||||
if (_drawingBoat == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
16
src/IDrawingObject.java
Normal file
16
src/IDrawingObject.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
|
||||
public interface IDrawingObject {
|
||||
float Step();
|
||||
|
||||
void SetObject(int x, int y, int width, int height);
|
||||
|
||||
void MoveObject(EnumDirection enumDirection);
|
||||
|
||||
void DrawingObject(Graphics g);
|
||||
|
||||
Position GetCurrentPosition();
|
||||
}
|
||||
16
src/Position.java
Normal file
16
src/Position.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package src;
|
||||
|
||||
// Класс для определения координат объекта
|
||||
public class Position {
|
||||
public float Left;
|
||||
public float Top;
|
||||
public float Right;
|
||||
public float Bottom;
|
||||
|
||||
public Position(float left, float top, float right, float bottom) {
|
||||
Left = left;
|
||||
Top = top;
|
||||
Right = right;
|
||||
Bottom = bottom;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user