diff --git a/ProjectBomber/src/ProjectBomber/AbstractStrategy.java b/ProjectBomber/src/ProjectBomber/AbstractStrategy.java new file mode 100644 index 0000000..94cce4d --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/AbstractStrategy.java @@ -0,0 +1,82 @@ +package ProjectBomber; + +public abstract class AbstractStrategy { + private IMoveableObject _moveableObject; + private Status _state = Status.NotInit; + protected int FieldWidth; + protected int FieldHeight; + + public Status GetStatus() { + return _state; + } + + public void SetData(IMoveableObject moveableObject, int width, int height) { + if (moveableObject == null) { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + public void MakeStep() { + if (_state != Status.InProgress) { + return; + } + if (IsTargetDestination()) { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + + protected boolean MoveLeft() { + return MoveTo(EnumDirectionType.Left); + } + + protected boolean MoveRight() { + return MoveTo(EnumDirectionType.Right); + } + + protected boolean MoveUp() { + return MoveTo(EnumDirectionType.Up); + } + + protected boolean MoveDown() { + return MoveTo(EnumDirectionType.Down); + } + + protected ObjectParameters GetObjectParameters() { + if (_moveableObject == null) + return null; + return _moveableObject.GetObjectPosition(); + } + + protected Integer GetStep() { + if (_state != Status.InProgress) { + return null; + } + if (_moveableObject == null) + return null; + return _moveableObject.GetStep(); + } + + protected abstract void MoveToTarget(); + + protected abstract boolean IsTargetDestination(); + + private boolean MoveTo(EnumDirectionType directionType) { + if (_state != Status.InProgress) { + return false; + } + if (_moveableObject == null) + return false; + if (_moveableObject.CheckCanMove(directionType)) { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } +} diff --git a/ProjectBomber/src/ProjectBomber/DrawingBomber.java b/ProjectBomber/src/ProjectBomber/DrawingBomber.java index f0eb819..87e99fe 100644 --- a/ProjectBomber/src/ProjectBomber/DrawingBomber.java +++ b/ProjectBomber/src/ProjectBomber/DrawingBomber.java @@ -2,112 +2,49 @@ package ProjectBomber; import java.awt.*; -public class DrawingBomber { - public EntityBomber EntityBomber; - private DrawingEngines _drawingEngines; - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private final int _bomberWidth = 110; - private final int _bomberHeight = 110; - - public boolean Init(int speed, double weight, Color bodyColor, - Color additionalColor, boolean fuelTanks, boolean bombs, - int width, int height) { - if (width < _bomberWidth && height < _bomberHeight) { - return false; - } - _pictureWidth = width; - _pictureHeight = height; - _drawingEngines = new DrawingEngines(); - EntityBomber = new EntityBomber(); - EntityBomber.Init(speed, weight, bodyColor, additionalColor, fuelTanks, bombs); - return true; - } - - public void SetEnginesCount(int enginesCount) { - _drawingEngines.SetEnumEnginesCount(enginesCount); - } - - public void SetPosition(int x, int y) { - if (x < 0) { - x = 0; - } else if (x > _pictureWidth - _bomberWidth) { - x = _pictureWidth - _bomberWidth; - } - _startPosX = x; - - if (y < 0) { - y = 0; - } else if (y > _pictureHeight - _bomberHeight) { - y = _pictureHeight - _bomberHeight; - } - _startPosY = y; - } - - public void MoveTransport(EnumDirectionType direction) { - if (EntityBomber == null) { - return; - } - switch (direction) { - case Up -> { - if (_startPosY - EntityBomber.Step() >= 0) { - _startPosY -= (int) EntityBomber.Step(); - } - } - case Down -> { - if (_startPosY + _bomberHeight + EntityBomber.Step() <= _pictureHeight) { - _startPosY += (int) EntityBomber.Step(); - } - } - case Left -> { - if (_startPosX - EntityBomber.Step() >= 0) { - _startPosX -= (int) EntityBomber.Step(); - } - } - case Right -> { - if (_startPosX + _bomberWidth + EntityBomber.Step() <= _pictureWidth) { - _startPosX += (int) EntityBomber.Step(); - } - } +public class DrawingBomber extends DrawingPlane { + public DrawingBomber(int speed, double weight, Color bodyColor, + Color additionalColor, boolean fuelTanks, boolean bombs, + int width, int height) { + super(speed, weight, bodyColor, width, height, 140, 90); + if (EntityPlane != null) { + EntityPlane = new EntityBomber(speed, weight, bodyColor, additionalColor, fuelTanks, bombs); } } public void DrawTransport(Graphics g) { - if (EntityBomber == null) { + if (EntityPlane == null) { + return; + } + if (!(EntityPlane instanceof EntityBomber EntityBomber)) { return; } Graphics2D g2d = (Graphics2D) g; - Color bodyColor = EntityBomber.BodyColor; - Color additionalColor = EntityBomber.AdditionalColor; + int bodyHeight = _planeHeight / 9; + Color blackColor = Color.BLACK; - - _drawingEngines.DrawEngines(g, additionalColor, _startPosX, _startPosY, _bomberWidth, _bomberHeight); - - // Длина фюзеляжа - int bodyHeight = _bomberHeight / 8; - int bodyWidth = _bomberWidth - _bomberWidth / 7; + Color redColor = Color.RED; + Color additionalColor = EntityBomber.AdditionalColor; // Рисуем бомбы if (EntityBomber.Bombs) { Polygon bombTailPolygon = new Polygon(); - bombTailPolygon.addPoint(_startPosX + _bomberWidth / 2 - _bomberWidth / 8 + bodyHeight * 3 - 5, - _startPosY + _bomberHeight / 2 - bodyHeight / 2 - _bomberHeight / 3 + bodyHeight / 2); - bombTailPolygon.addPoint(_startPosX + _bomberWidth / 2 - _bomberWidth / 8 + bodyHeight * 3 + 5, - _startPosY + _bomberHeight / 2 - bodyHeight / 2 - _bomberHeight / 3 + bodyHeight / 2 - 5); - bombTailPolygon.addPoint(_startPosX + _bomberWidth / 2 - _bomberWidth / 8 + bodyHeight * 3 + 5, - _startPosY + _bomberHeight / 2 - bodyHeight / 2 - _bomberHeight / 3 + bodyHeight / 2 + 5); - bombTailPolygon.addPoint(_startPosX + _bomberWidth / 2 - _bomberWidth / 8 + bodyHeight * 3 - 5, - _startPosY + _bomberHeight / 2 - bodyHeight / 2 - _bomberHeight / 3 + bodyHeight / 2); + bombTailPolygon.addPoint(_startPosX + _planeWidth / 2 - _planeWidth / 8 + bodyHeight * 3 - 5, + _startPosY + _planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3 + bodyHeight / 2); + bombTailPolygon.addPoint(_startPosX + _planeWidth / 2 - _planeWidth / 8 + bodyHeight * 3 + 5, + _startPosY + _planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3 + bodyHeight / 2 - 5); + bombTailPolygon.addPoint(_startPosX + _planeWidth / 2 - _planeWidth / 8 + bodyHeight * 3 + 5, + _startPosY + _planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3 + bodyHeight / 2 + 5); + bombTailPolygon.addPoint(_startPosX + _planeWidth / 2 - _planeWidth / 8 + bodyHeight * 3 - 5, + _startPosY + _planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3 + bodyHeight / 2); g2d.setColor(additionalColor); g2d.fillPolygon(bombTailPolygon); g2d.setColor(blackColor); g2d.drawPolygon(bombTailPolygon); - bombTailPolygon.translate(0, (int) (_bomberHeight - 2 * (_bomberHeight / 2 - bodyHeight / 2 - _bomberHeight / 3 + bodyHeight / 2 - 5) - bombTailPolygon.getBounds2D().getHeight())); + bombTailPolygon.translate(0, (int) (_planeHeight - 2 * (_planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3 + bodyHeight / 2 - 5) - bombTailPolygon.getBounds2D().getHeight())); g2d.setColor(additionalColor); g2d.fillPolygon(bombTailPolygon); @@ -116,26 +53,26 @@ public class DrawingBomber { g2d.setColor(additionalColor); g2d.fillOval( - _startPosX + _bomberWidth / 2 - _bomberWidth / 8, - _startPosY + _bomberHeight / 2 - bodyHeight / 2 - _bomberHeight / 3, + _startPosX + _planeWidth / 2 - _planeWidth / 8, + _startPosY + _planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3, bodyHeight * 3, bodyHeight); g2d.fillOval( - _startPosX + _bomberWidth / 2 - _bomberWidth / 8, - _startPosY + _bomberHeight / 2 - bodyHeight / 2 + _bomberHeight / 3, + _startPosX + _planeWidth / 2 - _planeWidth / 8, + _startPosY + _planeHeight / 2 - bodyHeight / 2 + _planeHeight / 3, bodyHeight * 3, bodyHeight); g2d.setColor(blackColor); g2d.drawOval( - _startPosX + _bomberWidth / 2 - _bomberWidth / 8, - _startPosY + _bomberHeight / 2 - bodyHeight / 2 - _bomberHeight / 3, + _startPosX + _planeWidth / 2 - _planeWidth / 8, + _startPosY + _planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3, bodyHeight * 3, bodyHeight); g2d.drawOval( - _startPosX + _bomberWidth / 2 - _bomberWidth / 8, - _startPosY + _bomberHeight / 2 - bodyHeight / 2 + _bomberHeight / 3, + _startPosX + _planeWidth / 2 - _planeWidth / 8, + _startPosY + _planeHeight / 2 - bodyHeight / 2 + _planeHeight / 3, bodyHeight * 3, bodyHeight); } @@ -146,75 +83,29 @@ public class DrawingBomber { int fuelTanksHeight = bodyHeight / 2; g2d.setColor(additionalColor); g2d.fillRect( - _startPosX + _bomberWidth / 2 - _bomberWidth / 7, - _startPosY + _bomberHeight / 2 - bodyHeight - bodyHeight / 2, + _startPosX + _planeWidth / 2 - _planeWidth / 7, + _startPosY + _planeHeight / 2 - bodyHeight - bodyHeight / 2, fuelTanksWidth, fuelTanksHeight); g2d.fillRect( - _startPosX + _bomberWidth / 2 - _bomberWidth / 7, - _startPosY + _bomberHeight / 2 + bodyHeight / 2 + bodyHeight / 2, + _startPosX + _planeWidth / 2 - _planeWidth / 7, + _startPosY + _planeHeight / 2 + bodyHeight / 2 + bodyHeight / 2, fuelTanksWidth, fuelTanksHeight); g2d.setColor(blackColor); g2d.drawRect( - _startPosX + _bomberWidth / 2 - _bomberWidth / 7, - _startPosY + _bomberHeight / 2 - bodyHeight - bodyHeight / 2, + _startPosX + _planeWidth / 2 - _planeWidth / 7, + _startPosY + _planeHeight / 2 - bodyHeight - bodyHeight / 2, fuelTanksWidth, fuelTanksHeight); g2d.drawRect( - _startPosX + _bomberWidth / 2 - _bomberWidth / 7, - _startPosY + _bomberHeight / 2 + bodyHeight / 2 + bodyHeight / 2, + _startPosX + _planeWidth / 2 - _planeWidth / 7, + _startPosY + _planeHeight / 2 + bodyHeight / 2 + bodyHeight / 2, fuelTanksWidth, fuelTanksHeight); } - // Рисуем нос - Polygon cockPitPolygon = new Polygon(); - cockPitPolygon.addPoint(_startPosX, _startPosY + _bomberHeight / 2); - cockPitPolygon.addPoint(_startPosX + _bomberWidth / 8, _startPosY + _bomberHeight / 2 - bodyHeight / 2); - cockPitPolygon.addPoint(_startPosX + _bomberWidth / 8, _startPosY + _bomberHeight / 2 + bodyHeight / 2); - g2d.setColor(blackColor); - g2d.fillPolygon(cockPitPolygon); - - // Рисуем крылья - Polygon wingsPolygon = new Polygon(); - wingsPolygon.addPoint(_startPosX + _bomberWidth / 2, _startPosY); - wingsPolygon.addPoint(_startPosX + _bomberWidth / 2 + _bomberWidth / 15, _startPosY); - wingsPolygon.addPoint(_startPosX + _bomberWidth / 2 + _bomberWidth / 6, _startPosY + _bomberHeight / 2); - wingsPolygon.addPoint(_startPosX + _bomberWidth / 2 + _bomberWidth / 15, _startPosY + _bomberHeight); - wingsPolygon.addPoint(_startPosX + _bomberWidth / 2, _startPosY + _bomberHeight); - - g2d.setColor(bodyColor); - g.fillPolygon(wingsPolygon); - g2d.setColor(blackColor); - g.drawPolygon(wingsPolygon); - - // Рисуем хвостовое оперение - Polygon tailPolygon = new Polygon(); - tailPolygon.addPoint(_startPosX + _bomberWidth, _startPosY + _bomberHeight / 2 - _bomberHeight / 3); - tailPolygon.addPoint(_startPosX + bodyWidth, _startPosY + _bomberHeight / 2 - _bomberHeight / 8); - tailPolygon.addPoint(_startPosX + bodyWidth, _startPosY + _bomberHeight / 2 + _bomberHeight / 8); - tailPolygon.addPoint(_startPosX + _bomberWidth, _startPosY + _bomberHeight / 2 + _bomberHeight / 3); - - g2d.setColor(bodyColor); - g.fillPolygon(tailPolygon); - g2d.setColor(blackColor); - g.drawPolygon(tailPolygon); - - // Рисуем фюзеляж - g2d.setColor(bodyColor); - g2d.fillRect( - _startPosX + _bomberWidth / 8, - _startPosY + _bomberHeight / 2 - bodyHeight / 2, - bodyWidth, - bodyHeight - ); - g2d.setColor(blackColor); - g2d.drawRect( - _startPosX + _bomberWidth / 8, - _startPosY + _bomberHeight / 2 - bodyHeight / 2, - bodyWidth, - bodyHeight - ); + super.DrawTransport(g); } } + diff --git a/ProjectBomber/src/ProjectBomber/DrawingEnginesEllipse.java b/ProjectBomber/src/ProjectBomber/DrawingEnginesEllipse.java new file mode 100644 index 0000000..d30315f --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/DrawingEnginesEllipse.java @@ -0,0 +1,31 @@ +package ProjectBomber; + +import java.awt.*; + +public class DrawingEnginesEllipse implements IDrawingEngines { + private EnumEnginesCount _enumEnginesCount; + + @Override + public void SetEnumEnginesCount(int enginesCount) { + for (EnumEnginesCount val : EnumEnginesCount.values()) { + if (val.count == enginesCount) { + this._enumEnginesCount = val; + return; + } + } + this._enumEnginesCount = EnumEnginesCount.Two; + } + + @Override + public EnumEnginesCount GetEnumEnginesCount() { + return _enumEnginesCount; + } + + @Override + public void DrawEngine(Graphics2D g2d, Color color, int x, int y, int w, int h) { + g2d.setColor(Color.WHITE); + g2d.fillOval(x, y, w, h); + g2d.setColor(Color.BLACK); + g2d.drawOval(x, y, w, h); + } +} diff --git a/ProjectBomber/src/ProjectBomber/DrawingEnginesPyramid.java b/ProjectBomber/src/ProjectBomber/DrawingEnginesPyramid.java new file mode 100644 index 0000000..760b71c --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/DrawingEnginesPyramid.java @@ -0,0 +1,39 @@ +package ProjectBomber; + +import java.awt.*; + +public class DrawingEnginesPyramid implements IDrawingEngines { + private EnumEnginesCount _enumEnginesCount; + + @Override + public void SetEnumEnginesCount(int enginesCount) { + for (EnumEnginesCount val : EnumEnginesCount.values()) { + if (val.count == enginesCount) { + this._enumEnginesCount = val; + return; + } + } + this._enumEnginesCount = EnumEnginesCount.Two; + } + + @Override + public EnumEnginesCount GetEnumEnginesCount() { + return _enumEnginesCount; + } + + @Override + public void DrawEngine(Graphics2D g2d, Color color, int x, int y, int w, int h) { + Polygon enginePolygon = new Polygon(); + enginePolygon.addPoint(x, y + h / 2); + enginePolygon.addPoint(x + w / 10, y); + enginePolygon.addPoint(x + w - w / 2, y + h / 2); + enginePolygon.addPoint(x + w, y); + enginePolygon.addPoint(x + w, y + h); + enginePolygon.addPoint(x + w - w / 2, y + h / 2); + enginePolygon.addPoint(x + w / 10, y + h); + g2d.setColor(Color.WHITE); + g2d.fillPolygon(enginePolygon); + g2d.setColor(Color.BLACK); + g2d.drawPolygon(enginePolygon); + } +} diff --git a/ProjectBomber/src/ProjectBomber/DrawingEnginesSimple.java b/ProjectBomber/src/ProjectBomber/DrawingEnginesSimple.java new file mode 100644 index 0000000..38b963d --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/DrawingEnginesSimple.java @@ -0,0 +1,33 @@ +package ProjectBomber; + +import java.awt.*; + +public class DrawingEnginesSimple implements IDrawingEngines { + private EnumEnginesCount _enumEnginesCount; + + @Override + public void SetEnumEnginesCount(int enginesCount) { + for (EnumEnginesCount val : EnumEnginesCount.values()) { + if (val.count == enginesCount) { + this._enumEnginesCount = val; + return; + } + } + this._enumEnginesCount = EnumEnginesCount.Two; + } + + @Override + public EnumEnginesCount GetEnumEnginesCount() { + return _enumEnginesCount; + } + + @Override + public void DrawEngine(Graphics2D g2d, Color color, int x, int y, int w, int h) { + g2d.setColor(Color.WHITE); + g2d.fillRect(x, y, w, h); + g2d.setColor(Color.BLACK); + g2d.drawRect(x, y, w, h); + g2d.setColor(color); + g2d.fillRect(x, y, 6, h); + } +} diff --git a/ProjectBomber/src/ProjectBomber/DrawingObjectPlane.java b/ProjectBomber/src/ProjectBomber/DrawingObjectPlane.java new file mode 100644 index 0000000..3c3bbb9 --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/DrawingObjectPlane.java @@ -0,0 +1,43 @@ +package ProjectBomber; + +public class DrawingObjectPlane implements IMoveableObject { + private DrawingPlane _drawingPlane; + + public DrawingObjectPlane(DrawingPlane drawingPlane) { + _drawingPlane = drawingPlane; + } + + @Override + public ObjectParameters GetObjectPosition() { + if (_drawingPlane == null || _drawingPlane.EntityPlane == null) { + return null; + } + return new ObjectParameters( + _drawingPlane.GetPosX(), + _drawingPlane.GetPosY(), + _drawingPlane.GetWidth(), + _drawingPlane.GetHeight() + ); + } + + @Override + public int GetStep() { + if (_drawingPlane != null) + if (_drawingPlane.EntityPlane != null) + return (int) _drawingPlane.EntityPlane.Step(); + return 0; + } + + @Override + public boolean CheckCanMove(EnumDirectionType direction) { + if (_drawingPlane == null) + return false; + return _drawingPlane.CanMove(direction); + } + + @Override + public void MoveObject(EnumDirectionType direction) { + if (_drawingPlane != null) + _drawingPlane.MoveTransport(direction); + } +} diff --git a/ProjectBomber/src/ProjectBomber/DrawingPlane.java b/ProjectBomber/src/ProjectBomber/DrawingPlane.java new file mode 100644 index 0000000..17d5114 --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/DrawingPlane.java @@ -0,0 +1,182 @@ +package ProjectBomber; + +import java.awt.*; +import java.util.Random; + +public class DrawingPlane { + 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; + } + 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; + + Color bodyColor = EntityPlane.BodyColor; + Color blackColor = Color.BLACK; + + // Длина фюзеляжа + int bodyHeight = _planeHeight / 8; + int bodyWidth = _planeWidth - _planeWidth / 7; + + _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 + ); + } +} diff --git a/ProjectBomber/src/ProjectBomber/EntityBomber.java b/ProjectBomber/src/ProjectBomber/EntityBomber.java index fb9b6df..5415641 100644 --- a/ProjectBomber/src/ProjectBomber/EntityBomber.java +++ b/ProjectBomber/src/ProjectBomber/EntityBomber.java @@ -2,22 +2,14 @@ package ProjectBomber; import java.awt.*; -public class EntityBomber { - public int Speed; - public double Weight; - public Color BodyColor; +public class EntityBomber extends EntityPlane { public Color AdditionalColor; public boolean FuelTanks; public boolean Bombs; - public double Step() { - return (double) Speed * 250 / Weight; - } - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean fuelTanks, boolean bombs) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; + public EntityBomber(int speed, double weight, Color bodyColor, + Color additionalColor, boolean fuelTanks, boolean bombs) { + super(speed, weight, bodyColor); AdditionalColor = additionalColor; FuelTanks = fuelTanks; Bombs = bombs; diff --git a/ProjectBomber/src/ProjectBomber/EntityPlane.java b/ProjectBomber/src/ProjectBomber/EntityPlane.java new file mode 100644 index 0000000..b94e7db --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/EntityPlane.java @@ -0,0 +1,19 @@ +package ProjectBomber; + +import java.awt.*; + +public class EntityPlane { + public int Speed; + public double Weight; + public Color BodyColor; + + public double Step() { + return (double) Speed * 250 / Weight; + } + + public EntityPlane(int speed, double weight, Color bodyColor) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/ProjectBomber/src/ProjectBomber/FormBomber.form b/ProjectBomber/src/ProjectBomber/FormBomber.form index 569d8c7..8046b3c 100644 --- a/ProjectBomber/src/ProjectBomber/FormBomber.form +++ b/ProjectBomber/src/ProjectBomber/FormBomber.form @@ -1,6 +1,6 @@
diff --git a/ProjectBomber/src/ProjectBomber/FormBomber.java b/ProjectBomber/src/ProjectBomber/FormBomber.java index 839af30..70f3ddd 100644 --- a/ProjectBomber/src/ProjectBomber/FormBomber.java +++ b/ProjectBomber/src/ProjectBomber/FormBomber.java @@ -2,17 +2,21 @@ package ProjectBomber; import javax.swing.*; import java.awt.*; -import java.awt.event.ActionListener; import java.util.Random; +import java.awt.event.ActionListener; public class FormBomber { - DrawingBomber _drawingBomber = new DrawingBomber(); - private JButton buttonCreate; + DrawingPlane _drawingPlane; + AbstractStrategy _abstractStrategy; + private JButton buttonCreateBomber; private JPanel pictureBox; private JButton buttonDown; private JButton buttonUp; private JButton buttonLeft; private JButton buttonRight; + private JButton buttonCreatePlane; + private JComboBox comboBoxStrategy; + private JButton buttonStep; public JPanel getPictureBox() { return pictureBox; @@ -24,11 +28,11 @@ public class FormBomber { buttonLeft.setName("buttonLeft"); buttonRight.setName("buttonRight"); - buttonCreate.addActionListener(e -> { - _drawingBomber = new DrawingBomber(); + + buttonCreateBomber.addActionListener(e -> { Random random = new Random(); - _drawingBomber.Init( + _drawingPlane = new DrawingBomber( random.nextInt(100, 300), random.nextInt(1000, 3000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), @@ -39,27 +43,70 @@ public class FormBomber { pictureBox.getHeight() ); - _drawingBomber.SetEnginesCount(random.nextInt(2, 7)); - _drawingBomber.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + _drawingPlane.SetEnginesCount(random.nextInt(2, 7)); + _drawingPlane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); Draw(); }); + buttonCreatePlane.addActionListener(e -> { + Random random = new Random(); + + _drawingPlane = new DrawingPlane( + random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + pictureBox.getWidth(), + pictureBox.getHeight() + ); + + _drawingPlane.SetEnginesCount(random.nextInt(2, 7)); + _drawingPlane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + + Draw(); + }); + + buttonStep.addActionListener(e -> { + if (_drawingPlane == null) + return; + if (comboBoxStrategy.isEnabled()) { + _abstractStrategy = null; + int comboBoxStrategySelectedIndex = comboBoxStrategy.getSelectedIndex(); + if (comboBoxStrategySelectedIndex == 0) { + _abstractStrategy = new MoveToCenter(); + } else if (comboBoxStrategySelectedIndex == 1) { + _abstractStrategy = new MoveToRightBottom(); + } + if (_abstractStrategy == null) + return; + _abstractStrategy.SetData(new DrawingObjectPlane(_drawingPlane), pictureBox.getWidth(), pictureBox.getHeight()); + comboBoxStrategy.setEnabled(false); + } + if (_abstractStrategy == null) + return; + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) { + comboBoxStrategy.setEnabled(true); + _abstractStrategy = null; + } + }); + ActionListener buttonMoveClickedListener = e -> { String buttonName = ((JButton) e.getSource()).getName(); switch (buttonName) { case ("buttonUp") -> { - _drawingBomber.MoveTransport(EnumDirectionType.Up); + _drawingPlane.MoveTransport(EnumDirectionType.Up); } case ("buttonDown") -> { - _drawingBomber.MoveTransport(EnumDirectionType.Down); + _drawingPlane.MoveTransport(EnumDirectionType.Down); } case ("buttonLeft") -> { - _drawingBomber.MoveTransport(EnumDirectionType.Left); + _drawingPlane.MoveTransport(EnumDirectionType.Left); } case ("buttonRight") -> { - _drawingBomber.MoveTransport(EnumDirectionType.Right); + _drawingPlane.MoveTransport(EnumDirectionType.Right); } } @@ -73,12 +120,20 @@ public class FormBomber { } public void Draw() { - if (_drawingBomber.EntityBomber == null) { + if (_drawingPlane == null) { return; } Graphics g = pictureBox.getGraphics(); pictureBox.paint(g); - _drawingBomber.DrawTransport(g); + _drawingPlane.DrawTransport(g); + } + + private void createUIComponents() { + String[] strategiesList = { + "MoveToCenter", + "MoveToRightBottom" + }; + comboBoxStrategy = new JComboBox(strategiesList); } } diff --git a/ProjectBomber/src/ProjectBomber/DrawingEngines.java b/ProjectBomber/src/ProjectBomber/IDrawingEngines.java similarity index 64% rename from ProjectBomber/src/ProjectBomber/DrawingEngines.java rename to ProjectBomber/src/ProjectBomber/IDrawingEngines.java index db7abc2..79fdac2 100644 --- a/ProjectBomber/src/ProjectBomber/DrawingEngines.java +++ b/ProjectBomber/src/ProjectBomber/IDrawingEngines.java @@ -2,30 +2,16 @@ package ProjectBomber; import java.awt.*; -public class DrawingEngines { - private EnumEnginesCount _enumEnginesCount; +public interface IDrawingEngines { + void SetEnumEnginesCount(int enginesCount); - public void SetEnumEnginesCount(int enginesCount) { - for (EnumEnginesCount val : EnumEnginesCount.values()) { - if (val.count == enginesCount) { - this._enumEnginesCount = val; - return; - } - } - this._enumEnginesCount = EnumEnginesCount.Two; - } + EnumEnginesCount GetEnumEnginesCount(); - private void DrawEngine(Graphics2D g2d, Color color, int x, int y, int w, int h) { - g2d.setColor(Color.WHITE); - g2d.fillRect(x, y, w, h); - g2d.setColor(Color.BLACK); - g2d.drawRect(x, y, w, h); - g2d.setColor(color); - g2d.fillRect(x, y, 6, h); - } + void DrawEngine(Graphics2D g2d, Color color, int x, int y, int w, int h); - public void DrawEngines(Graphics g, Color color, int startPosX, int startPosY, int drawingWidth, int drawingHeight) { - if (_enumEnginesCount == null) { + default void DrawEngines(Graphics g, Color color, int startPosX, int startPosY, int drawingWidth, int drawingHeight) { + EnumEnginesCount enginesCount = GetEnumEnginesCount(); + if (enginesCount == null) { return; } Graphics2D g2d = (Graphics2D) g; @@ -33,7 +19,7 @@ public class DrawingEngines { int engineWidth = 20; int engineHeight = 6; - if (_enumEnginesCount.count >= EnumEnginesCount.Two.count) { + if (enginesCount.count >= EnumEnginesCount.Two.count) { DrawEngine( g2d, color, startPosX + drawingWidth - drawingWidth / 5, @@ -49,7 +35,7 @@ public class DrawingEngines { engineHeight ); } - if (_enumEnginesCount.count >= EnumEnginesCount.Four.count) { + if (enginesCount.count >= EnumEnginesCount.Four.count) { DrawEngine( g2d, color, startPosX + drawingWidth - drawingWidth / 5 + 5, @@ -65,7 +51,7 @@ public class DrawingEngines { engineHeight ); } - if (_enumEnginesCount.count >= EnumEnginesCount.Six.count) { + if (enginesCount.count >= EnumEnginesCount.Six.count) { DrawEngine( g2d, color, startPosX + drawingWidth / 2 - 10, diff --git a/ProjectBomber/src/ProjectBomber/IMoveableObject.java b/ProjectBomber/src/ProjectBomber/IMoveableObject.java new file mode 100644 index 0000000..82809e7 --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/IMoveableObject.java @@ -0,0 +1,8 @@ +package ProjectBomber; + +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean CheckCanMove(EnumDirectionType direction); + void MoveObject(EnumDirectionType direction); +} diff --git a/ProjectBomber/src/ProjectBomber/MoveToCenter.java b/ProjectBomber/src/ProjectBomber/MoveToCenter.java new file mode 100644 index 0000000..d2c28ce --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/MoveToCenter.java @@ -0,0 +1,37 @@ +package ProjectBomber; + +public class MoveToCenter extends AbstractStrategy { + @Override + protected void MoveToTarget() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return; + } + var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()) { + if (diffX > 0) { + MoveLeft(); + } else { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()) { + if (diffY > 0) { + MoveUp(); + } else { + MoveDown(); + } + } + } + + @Override + protected boolean IsTargetDestination() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + return objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical() <= FieldHeight / 2 && objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2; + } +} diff --git a/ProjectBomber/src/ProjectBomber/MoveToRightBottom.java b/ProjectBomber/src/ProjectBomber/MoveToRightBottom.java new file mode 100644 index 0000000..94452a6 --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/MoveToRightBottom.java @@ -0,0 +1,26 @@ +package ProjectBomber; + +public class MoveToRightBottom extends AbstractStrategy { + @Override + protected void MoveToTarget() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return; + } + if (objParams.RightBorder() < FieldWidth - GetStep()) { + MoveRight(); + } + if (objParams.TopBorder() < FieldHeight - GetStep()) { + MoveDown(); + } + } + + @Override + protected boolean IsTargetDestination() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + return objParams.RightBorder() >= FieldWidth - GetStep() && objParams.DownBorder() >= FieldHeight - GetStep(); + } +} diff --git a/ProjectBomber/src/ProjectBomber/ObjectParameters.java b/ProjectBomber/src/ProjectBomber/ObjectParameters.java new file mode 100644 index 0000000..90cc3a6 --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/ObjectParameters.java @@ -0,0 +1,39 @@ +package ProjectBomber; + +public class ObjectParameters { + private int _x; + private int _y; + private int _width; + private int _height; + + public int LeftBorder() { + return _x; + } + + public int TopBorder() { + return _y; + } + + public int RightBorder() { + return _x + _width; + } + + public int DownBorder() { + return _y + _height; + } + + public int ObjectMiddleHorizontal() { + return _x + _width / 2; + } + + public int ObjectMiddleVertical() { + return _y + _height / 2; + } + + public ObjectParameters(int x, int y, int width, int height) { + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/ProjectBomber/src/ProjectBomber/Status.java b/ProjectBomber/src/ProjectBomber/Status.java new file mode 100644 index 0000000..49460db --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/Status.java @@ -0,0 +1,7 @@ +package ProjectBomber; + +public enum Status { + NotInit, + InProgress, + Finish +}