Pibd-23_Zhelovanov_D.Y._Bat.../DrawningBattleship.java

199 lines
6.9 KiB
Java
Raw Normal View History

2022-10-24 22:01:54 +04:00
import java.awt.*;
2022-12-06 17:23:28 +04:00
import java.util.Random;
2022-10-24 22:01:54 +04:00
public class DrawningBattleship {
EntityBattleship Battleship;
private DrawningBlocks drawingBlocks;
2022-12-06 17:23:28 +04:00
private IDrawningBlocks iDrawingBlocks;
2022-10-24 22:01:54 +04:00
public EntityBattleship Battleship()
{return Battleship; }
/// <summary>
/// Левая координата отрисовки корабля
/// </summary>
2022-12-06 17:23:28 +04:00
protected float _startPosX;
2022-10-24 22:01:54 +04:00
/// <summary>
/// Верхняя кооридната отрисовки корабля
/// </summary>
2022-12-06 17:23:28 +04:00
protected float _startPosY;
2022-10-24 22:01:54 +04:00
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private Integer _pictureWidth = null;
2022-10-24 22:01:54 +04:00
/// <summary>
/// Высота окна отрисовки
/// </summary>
private Integer _pictureHeight = null;
2022-10-24 22:01:54 +04:00
/// <summary>
/// Ширина отрисовки корабля
/// </summary>
2022-12-06 17:23:28 +04:00
private int _battleshipWidth = 120;
2022-10-24 22:01:54 +04:00
/// <summary>
/// Высота отрисовки корабля
/// </summary>
2022-12-06 17:23:28 +04:00
private int _battleshipHeight = 50;
2022-10-24 22:01:54 +04:00
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес корабля</param>
/// <param name="bodyColor">Цвет корпуса</param>
2022-12-06 17:23:28 +04:00
public DrawningBattleship(int speed, float weight, Color bodyColor)
2022-10-24 22:01:54 +04:00
{
2022-12-06 17:23:28 +04:00
//Battleship.Init(speed, weight, bodyColor);
Random random = new Random();
int[] ArrayBlocks = new int[]{2, 4, 6};
switch (random.nextInt(3)){
case 0:
iDrawingBlocks = new DrawningBlocks(ArrayBlocks[random.nextInt(0, 3)], Color.BLACK);
break;
case 1:
iDrawingBlocks = new DrawningRoundBlocks(ArrayBlocks[random.nextInt(0, 3)]);
break;
case 2:
iDrawingBlocks = new DrawningTriangleBlocks(ArrayBlocks[random.nextInt(0, 3)]);
break;
}
Battleship = new EntityBattleship(speed, weight, bodyColor);
}
2022-12-20 21:50:09 +04:00
public DrawningBattleship(EntityBattleship entity, IDrawningBlocks blocks){
Battleship = entity;
iDrawingBlocks = blocks;
}
2022-12-06 17:23:28 +04:00
protected DrawningBattleship(int speed, float weight, Color bodyColor, int battleshipWidth, int battleshipHeight)
{
this(speed, weight, bodyColor);
_battleshipWidth = battleshipWidth;
_battleshipHeight = battleshipHeight;
2022-10-24 22:01:54 +04:00
}
/// <summary>
/// Установка позиции корабля
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void SetPosition(int x, int y, int width, int height)
{
if ((x > 0 && y > 0) && (_battleshipHeight + y < height) && (_battleshipWidth + x < width))
{
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (_pictureWidth == null || _pictureHeight == null)
{
return;
}
switch (direction)
{
// вправо
case Right:
if (_startPosX + _battleshipWidth + Battleship.GetStep() < _pictureWidth)
{
_startPosX += Battleship.GetStep();
}
break;
//влево
case Left:
if (_startPosX - Battleship.GetStep() >= 0)
{
_startPosX -= Battleship.GetStep();
}
2022-10-24 22:01:54 +04:00
break;
//вверх
case Up:
if (_startPosY - Battleship.GetStep() >= 0)
{
_startPosY -= Battleship.GetStep();
}
2022-10-24 22:01:54 +04:00
break;
//вниз
case Down:
if (_startPosY + _battleshipHeight + Battleship.GetStep() < _pictureHeight)
{
_startPosY += Battleship.GetStep();
}
break;
}
}
/// <summary>
/// Отрисовка корабля
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics2D g)
2022-10-24 22:01:54 +04:00
{
if (_startPosX < 0 || _startPosY < 0
|| _pictureHeight == null || _pictureWidth == null)
{
return;
}
g.setColor(Color.BLACK);
//Корпус корабля
g.setColor(Battleship.bodyColor);
2022-10-24 22:01:54 +04:00
g.fillPolygon(new int[]{(int) _startPosX, (int) _startPosX , (int) _startPosX+80, (int) _startPosX + 120, (int)_startPosX + 80, (int)_startPosX},
new int[]{(int) _startPosY, (int) _startPosY+50 , (int) _startPosY+50, (int) _startPosY + 25, (int)_startPosY, (int)_startPosY}, 6);
2022-10-24 22:01:54 +04:00
//Пушка
g.setColor(Color.BLACK);
g.drawRect((int)_startPosX + 20, (int)_startPosY + 20, 30, 10);
g.drawRect((int)_startPosX + 50,(int) _startPosY + 10, 20, 30);
g.fillRect((int)_startPosX + 20, (int)_startPosY + 20, 30, 10);
g.fillRect((int)_startPosX + 50, (int)_startPosY + 10, 20, 30);
//Отсек
g.setColor(Color.BLUE);
g.drawOval((int)_startPosX+80, (int)_startPosY+15, 20, 20);
g.fillOval((int)_startPosX + 80, (int)_startPosY + 15, 20, 20);
g.setColor(Color.BLACK);
g.fillRect((int)_startPosX-5, (int)_startPosY+10, 5, 5);
g.fillRect((int)_startPosX - 5, (int)_startPosY + 35, 5, 5);
2022-12-06 17:23:28 +04:00
iDrawingBlocks.DrawBlocks(g, (int) _startPosX, (int) _startPosY, Color.BLACK);
2022-10-24 22:01:54 +04:00
}
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _battleshipWidth || _pictureHeight <= _battleshipHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _battleshipWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _battleshipWidth;
}
if (_startPosY + _battleshipHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _battleshipHeight;
}
2022-12-06 17:23:28 +04:00
}
public float[] GetCurrentPosition()
{
return new float[] {_startPosY, _startPosX + _battleshipWidth, /*DOWN*/ _startPosY + _battleshipHeight, /*LEFT*/ _startPosX};
2022-10-24 22:01:54 +04:00
}
}