PIbd-21_Potapov_N.S._Stormt.../ProjectStormtrooper/DrawingPlane.java

211 lines
7.1 KiB
Java

package ProjectStormtrooper;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingPlane extends JPanel {
public EntityPlane EntityPlane;
protected IDrawingEngines _drawingEngines;
protected int _pictureWidth;
protected int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected int _planeWidth = 110;
protected int _planeHeight = 110;
public int GetPosX() {
return _startPosX;
}
public int GetPosY() {
return _startPosY;
}
public int GetWidth() {
return _planeWidth;
}
public int GetHeight() {
return _planeHeight;
}
public DrawingPlane(int speed, double weight, Color bodyColor, int width, int height) {
if (width < _planeWidth && height < _planeHeight) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityPlane = new EntityPlane(speed, weight, bodyColor);
Random random = new Random();
int drawingEnginesType = random.nextInt(0, 3);
if (drawingEnginesType == 0)
_drawingEngines = new DrawingEnginesSimple();
else if (drawingEnginesType == 1)
_drawingEngines = new DrawingEnginesPyramid();
else if (drawingEnginesType == 2)
_drawingEngines = new DrawingEnginesEllipse();
}
protected DrawingPlane(int speed, double weight, Color bodyColor,
int width, int height, int planeWidth, int planeHeight) {
this(speed, weight, bodyColor, width, height);
_planeWidth = planeWidth;
_planeHeight = planeHeight;
}
protected DrawingPlane(EntityPlane entityPlane, IDrawingEngines drawingEngines, int width, int height) {
EntityPlane = entityPlane;
_drawingEngines = drawingEngines;
_pictureWidth = width;
_pictureHeight = height;
}
protected DrawingPlane(EntityPlane entityPlane, IDrawingEngines drawingEngines, int width, int height, int planeWidth, int planeHeight) {
this(entityPlane, drawingEngines, width, height);
_planeWidth = planeWidth;
_planeHeight = planeHeight;
}
public void SetEnginesCount(int enginesCount) {
_drawingEngines.SetEnumEnginesCount(enginesCount);
}
public boolean CanMove(EnumDirectionType direction) {
if (EntityPlane == null) {
return false;
}
boolean result = false;
switch (direction) {
case Up -> result = _startPosY - EntityPlane.Step() > 0;
case Down -> result = _startPosY + _planeHeight + EntityPlane.Step() < _pictureHeight;
case Left -> result = _startPosX - EntityPlane.Step() > 0;
case Right -> result = _startPosX + _planeWidth + EntityPlane.Step() < _pictureWidth;
}
return result;
}
public void SetPosition(int x, int y) {
if (x < 0) {
x = 0;
} else if (x > _pictureWidth - _planeWidth) {
x = _pictureWidth - _planeWidth;
}
_startPosX = x;
if (y < 0) {
y = 0;
} else if (y > _pictureHeight - _planeHeight) {
y = _pictureHeight - _planeHeight;
}
_startPosY = y;
}
public void MoveTransport(EnumDirectionType direction) {
if (EntityPlane == null) {
return;
}
switch (direction) {
case Up -> {
if (_startPosY - EntityPlane.Step() >= 0) {
_startPosY -= (int) EntityPlane.Step();
}
}
case Down -> {
if (_startPosY + _planeHeight + EntityPlane.Step() <= _pictureHeight) {
_startPosY += (int) EntityPlane.Step();
}
}
case Left -> {
if (_startPosX - EntityPlane.Step() >= 0) {
_startPosX -= (int) EntityPlane.Step();
}
}
case Right -> {
if (_startPosX + _planeWidth + EntityPlane.Step() <= _pictureWidth) {
_startPosX += (int) EntityPlane.Step();
}
}
}
}
public void DrawTransport(Graphics g) {
if (EntityPlane == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(2));
Color bodyColor = EntityPlane.BodyColor;
Color blackColor = Color.BLACK;
// Длина фюзеляжа
int bodyHeight = _planeHeight / 9;
int bodyWidth = _planeWidth - _planeWidth / 8;
_drawingEngines.DrawEngines(g, bodyColor, _startPosX, _startPosY, _planeWidth, _planeHeight);
// Рисуем нос
Polygon cockPitPolygon = new Polygon();
cockPitPolygon.addPoint(_startPosX, _startPosY + _planeHeight / 2);
cockPitPolygon.addPoint(_startPosX + _planeWidth / 8, _startPosY + _planeHeight / 2 - bodyHeight / 2);
cockPitPolygon.addPoint(_startPosX + _planeWidth / 8, _startPosY + _planeHeight / 2 + bodyHeight / 2);
g2d.setColor(blackColor);
g2d.fillPolygon(cockPitPolygon);
// Рисуем крылья
Polygon wingsPolygon = new Polygon();
wingsPolygon.addPoint(_startPosX + _planeWidth / 2, _startPosY);
wingsPolygon.addPoint(_startPosX + _planeWidth / 2 + _planeWidth / 15, _startPosY);
wingsPolygon.addPoint(_startPosX + _planeWidth / 2 + _planeWidth / 6, _startPosY + _planeHeight / 2);
wingsPolygon.addPoint(_startPosX + _planeWidth / 2 + _planeWidth / 15, _startPosY + _planeHeight);
wingsPolygon.addPoint(_startPosX + _planeWidth / 2, _startPosY + _planeHeight);
g2d.setColor(bodyColor);
g.fillPolygon(wingsPolygon);
g2d.setColor(blackColor);
g.drawPolygon(wingsPolygon);
// Рисуем хвостовое оперение
Polygon tailPolygon = new Polygon();
tailPolygon.addPoint(_startPosX + _planeWidth, _startPosY + _planeHeight / 2 - _planeHeight / 3);
tailPolygon.addPoint(_startPosX + bodyWidth, _startPosY + _planeHeight / 2 - _planeHeight / 8);
tailPolygon.addPoint(_startPosX + bodyWidth, _startPosY + _planeHeight / 2 + _planeHeight / 8);
tailPolygon.addPoint(_startPosX + _planeWidth, _startPosY + _planeHeight / 2 + _planeHeight / 3);
g2d.setColor(bodyColor);
g.fillPolygon(tailPolygon);
g2d.setColor(blackColor);
g.drawPolygon(tailPolygon);
// Рисуем фюзеляж
g2d.setColor(bodyColor);
g2d.fillRect(
_startPosX + _planeWidth / 8,
_startPosY + _planeHeight / 2 - bodyHeight / 2,
bodyWidth,
bodyHeight
);
g2d.setColor(blackColor);
g2d.drawRect(
_startPosX + _planeWidth / 8,
_startPosY + _planeHeight / 2 - bodyHeight / 2,
bodyWidth,
bodyHeight
);
}
public IMoveableObject GetMoveableObject() {
return new DrawingObjectPlane(this);
}
public void SetDrawingBounds(int width, int height) {
_pictureWidth = width;
_pictureHeight = height;
}
;
}