2023-12-28 20:56:39 +04:00

132 lines
4.4 KiB
Java

import java.awt.*;
import java.util.Random;
public class DrawingShip {
public EntityShip entityShip;
public EntityShip GetEntityWarmlyShip(){
return entityShip;
}
private void SetEntityWarmlyShip(EntityShip entityShip){
this.entityShip = entityShip;
}
private IDrawingDecks drawingDecks;
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
private int _shipWidth = 100;
private int _shipHeight = 70;
public int GetPosX() {
return _startPosX;
}
public int GetPosY() {
return _startPosY;
}
public int GetWidth() {
return _shipWidth;
}
public int GetHeight() {
return _shipHeight;
}
public DrawingShip(int speed, double weight, Color mainColor, int width, int height, int numberDecks){
if (width < _shipWidth || height <_shipHeight){
return;
}
_pictureWidth = width;
_pictureHeight = height;
entityShip = new EntityShip(speed, weight, mainColor);
Random rnd = new Random();
drawingDecks = switch (rnd.nextInt(3)){
case 0 -> new DrawingDecks();
case 1 -> new DrawingRoundDecks();
case 2 -> new DrawingHexagoneDecks();
default -> new DrawingDecks();
};
drawingDecks.SetNumberDecks(numberDecks);
}
protected DrawingShip(int speed, double weight, Color mainColor, int width, int height, int numberDecks, int shipWidth, int shipHeight){
if (width < _shipWidth || height <_shipHeight){
}
_pictureWidth = width;
_pictureHeight = height;
_shipWidth = shipWidth;
_shipHeight = shipHeight;
entityShip = new EntityShip(speed, weight, mainColor);
Random rnd = new Random();
drawingDecks = switch (rnd.nextInt(3)){
case 0 -> new DrawingDecks();
case 1 -> new DrawingRoundDecks();
case 2 -> new DrawingHexagoneDecks();
default -> new DrawingDecks();
};
drawingDecks.SetNumberDecks(numberDecks);
}
public void SetPosition(int x, int y){
if (x < 0 || x + _shipWidth > _pictureWidth){
x = 20;
}
if (y < 0 || y + _shipHeight > _pictureHeight){
y = 20;
}
_startPosX = x;
_startPosY = y;
}
public boolean CanMove(DirectionType direction)
{
if (entityShip == null){
return false;
}
return switch (direction){
case Left -> _startPosX - entityShip.GetStep() > 0;
case Up -> _startPosY - entityShip.GetStep() > 0;
case Right -> _startPosX + entityShip.GetStep() + _shipWidth <= _pictureWidth;
case Down -> _startPosY + entityShip.GetStep() + _shipHeight <= _pictureHeight;
};
}
public void MoveTransport(DirectionType direction){
if (!CanMove(direction) || entityShip == null) {
return;
}
switch (direction)
{
case Left:
_startPosX -= (int)entityShip.GetStep();
break;
case Up:
_startPosY -= (int)entityShip.GetStep();
break;
case Right:
_startPosX += (int)entityShip.GetStep();
break;
case Down:
_startPosY += (int)entityShip.GetStep();
break;
}
}
public void DrawTransport(Graphics g){
if (entityShip == null || drawingDecks == null){
System.out.println("?????????");
return;
}
//палуба
drawingDecks.DrawingDecks(_startPosX, _startPosY, entityShip.GetMainColor(), g);
//корпус
int[] xPoints = {_startPosX, _startPosX + 100, _startPosX + 90, _startPosX + 20, _startPosX};
int[] yPoints = {_startPosY + 40, _startPosY + 40, _startPosY + 60, _startPosY + 60, _startPosY + 40};
int nPoints = 5;
g.setColor(entityShip.GetMainColor());
g.fillPolygon(xPoints, yPoints, nPoints);
g.setColor(Color.black);
g.drawPolygon(xPoints, yPoints, nPoints);
//якорь
g.drawLine(_startPosX + 25, _startPosY + 45, _startPosX + 25, _startPosY + 55);
g.drawLine(_startPosX + 20, _startPosY + 50, _startPosX + 30, _startPosY + 50);
g.drawLine(_startPosX + 23, _startPosY + 55, _startPosX + 27, _startPosY + 55);
}
}