From 05308d07cb44a5887d9ead57270db94f974a8218 Mon Sep 17 00:00:00 2001 From: MayDayR Date: Fri, 15 Dec 2023 20:06:51 +0300 Subject: [PATCH 1/2] Initial commit --- src/Entities/EntityBus.java | 29 ++---- src/FormBus.java | 179 +++++++++++++++++++++++++++++------- 2 files changed, 150 insertions(+), 58 deletions(-) diff --git a/src/Entities/EntityBus.java b/src/Entities/EntityBus.java index 509e9cd..337a3dc 100644 --- a/src/Entities/EntityBus.java +++ b/src/Entities/EntityBus.java @@ -6,11 +6,7 @@ public class EntityBus { private int Speed; private float Weight; private Color BodyColor; - private Color AdditionalColor; - private boolean IsSecondFloor; - private boolean IsStairs; - - public int Step; + public float Step; public int getSpeed() { return Speed; @@ -21,27 +17,14 @@ public class EntityBus { public Color getBodyColor() { return BodyColor; } - public Color getAdditionalColor() { - return AdditionalColor; - } - public boolean IsSecondFloor() { - return IsSecondFloor; - } - public boolean IsStairs() { - return IsStairs; - } - public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isSecondFloor, boolean isStairs) + public EntityBus(int speed, float weight, Color bodyColor) { + Weight = weight; + Speed = speed; + BodyColor = bodyColor; - Weight = weight; - Speed = speed; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - IsSecondFloor = isSecondFloor; - IsStairs = isStairs; - - Step = Speed * 100 / (int) Weight; + Step = Speed * 100 / (int) Weight; } } diff --git a/src/FormBus.java b/src/FormBus.java index 3018d1b..734b23a 100644 --- a/src/FormBus.java +++ b/src/FormBus.java @@ -7,62 +7,97 @@ import MovementStrategy.*; public class FormBus extends JFrame { - private DrawingBus _drawingBus; - private Canvas canvas = new Canvas(); + private int width; + private int height; + private DrawningBus _drawningBus; + private AbstractStrategy _abstractStrategy; + private Canvas canvas; - JLabel labelCount = new JLabel("Введите число дверей:"); - private JTextField fieldCount = new JTextField(); - private JButton buttonCreate; + // выбор кол-ва иллюминаторов + JLabel labelCount; + private JTextField fieldCount; + + // выбор стратегии + JLabel labelStrategy; + JComboBox comboBoxStrategy; + JButton buttonStrategy; + + private JButton buttonCreateBus; + private JButton buttonCreateDoubleDeckerBus; private JButton buttonUp; private JButton buttonDown; private JButton buttonRight; private JButton buttonLeft; public FormBus() { - super("Создание автобуса"); + super("Создание бензовоза"); InitializeComponent(); setVisible(true); } private void InitializeComponent() { - buttonCreate = new JButton("Создать автобус"); + canvas = new Canvas(); + + labelCount = new JLabel("Введите число дверей:"); + fieldCount = new JTextField(); + + labelStrategy = new JLabel("Шаг стратегии:"); + comboBoxStrategy = new JComboBox(new Integer[] {0, 1}); + buttonStrategy = new JButton("Выбрать стратегию"); + buttonStrategy.setMargin(new Insets(0, 0, 0, 0)); + + buttonCreateBus = new JButton("Создать автобус"); + buttonCreateBus.setMargin(new Insets(0, 0, 0, 0)); + + buttonCreateDoubleDeckerBus = new JButton("Создать двухэтажный автобус"); + buttonCreateDoubleDeckerBus.setMargin(new Insets(0, 0, 0, 0)); buttonUp = new JButton(); buttonUp.setName("up"); - buttonUp.setIcon(new ImageIcon("images/KeyUp.png")); - buttonUp.setSize(48, 44); + buttonUp.setIcon(new ImageIcon("images\\KeyUp.png")); buttonRight = new JButton(); buttonRight.setName("right"); - buttonRight.setIcon(new ImageIcon("images/KeyRight.png")); - buttonRight.setSize(48, 44); + buttonRight.setIcon(new ImageIcon("images\\KeyRight.png")); buttonLeft = new JButton(); - buttonLeft.setName("left"); - buttonLeft.setIcon(new ImageIcon("images/KeyLeft.png")); - buttonLeft.setSize(48, 44); + buttonLeft.setIcon(new ImageIcon("images\\KeyLeft.png")); buttonDown = new JButton(); buttonDown.setName("down"); - buttonDown.setIcon(new ImageIcon("images/KeyDown.png")); - buttonDown.setSize(48, 44); + buttonDown.setIcon(new ImageIcon("images\\KeyDown.png")); setSize(800,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null); - buttonCreate.setBounds(12, 375, 170, 44); + buttonCreateBus.setBounds(12, 375, 146, 33); + buttonCreateDoubleDeckerBus.setBounds(182, 375, 146, 33); + + labelCount.setBounds(42, 405, 240, 20); + fieldCount.setBounds(240, 407, 48, 20); + + labelStrategy.setBounds(630, 20, 146, 33); + comboBoxStrategy.setBounds(630, 50, 146, 20); + buttonStrategy.setBounds(630, 80, 146, 33); + buttonUp.setBounds(679, 363, 48, 44); buttonRight.setBounds( 728, 408, 48, 44); buttonLeft.setBounds(630, 408, 48, 44); buttonDown.setBounds( 679, 408, 48, 44); labelCount.setBounds(12, 435, 240, 20); - fieldCount.setBounds(160, 437, 48, 20); + fieldCount.setBounds(150, 437, 48, 20); canvas.setBounds(0,0,800, 460); - add(buttonCreate); + add(buttonCreateBus); + add(buttonCreateDoubleDeckerBus); + add(labelCount); + add(fieldCount); + add(labelStrategy); + add(comboBoxStrategy); + add(buttonStrategy); add(buttonUp); add(buttonRight); add(buttonDown); @@ -71,14 +106,48 @@ public class FormBus extends JFrame { add(fieldCount); add(canvas); - buttonCreate.addActionListener(buttonCreateListener); + // логика формы + buttonCreateBus.addActionListener(buttonCreateBusListener); + buttonCreateDoubleDeckerBus.addActionListener(buttonCreateDoubleDeckerBusListener); + buttonStrategy.addActionListener(buttonStrategyListener); buttonUp.addActionListener(buttonsMoveListener); buttonRight.addActionListener(buttonsMoveListener); buttonDown.addActionListener(buttonsMoveListener); buttonLeft.addActionListener(buttonsMoveListener); } - ActionListener buttonCreateListener = new ActionListener() { + ActionListener buttonCreateBusListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int countDoors; + try + { + countDoors = Integer.parseInt(fieldCount.getText()); + } + catch (Exception ex) + { + countDoors = 0; + } + if (countDoors != 2 && countDoors != 3 && countDoors != 4) + { + JOptionPane.showMessageDialog(null, "Число должно быть равно 3, 4 или 5.\nКол-во дверей приравнено к 3"); + countDoors = 2; + } + + Random rand = new Random(); + _drawningBus = new DrawningBus(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, + new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)), + countDoors, + canvas.getWidth(), canvas.getHeight()); + + _drawningBus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10); + comboBoxStrategy.setEnabled(true); + canvas.repaint(); + } + }; + + ActionListener buttonCreateDoubleDeckerBusListener = new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { int countDoors; try @@ -96,15 +165,56 @@ public class FormBus extends JFrame { } Random rand = new Random(); - _drawingBus = new DrawingBus(); - _drawingBus.Init(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, + _drawningBus = new DrawningDoubleDeckerBus(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)), + countDoors, new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), rand.nextBoolean(), rand.nextBoolean(), - countDoors, canvas.getWidth(), canvas.getHeight()); - _drawingBus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10); + _drawningBus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10); + comboBoxStrategy.setEnabled(true); + canvas.repaint(); + } + }; + + ActionListener buttonStrategyListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_drawningBus == null) + { + return; + } + if (comboBoxStrategy.isEnabled()) { + + switch (comboBoxStrategy.getSelectedIndex()) { + case 0: + _abstractStrategy = new MoveToCenter(); + break; + case 1: + _abstractStrategy = new MoveToBorder(); + break; + default: + _abstractStrategy = null; + break; + } + ; + if (_abstractStrategy == null) { + return; + } + _abstractStrategy.SetData(new DrawningObjectBus(_drawningBus), canvas.getWidth(), canvas.getHeight()); + comboBoxStrategy.setEnabled(false); + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.setEnabled(true); + _abstractStrategy = null; + } canvas.repaint(); } }; @@ -112,23 +222,23 @@ public class FormBus extends JFrame { ActionListener buttonsMoveListener = new ActionListener() { // реакция на нажатие public void actionPerformed(ActionEvent e) { - if (_drawingBus == null) + if (_drawningBus == null) { return; } String command = ((JButton)(e.getSource())).getName(); switch (command) { case "up": - _drawingBus.MoveTransport(Direction.Up); + _drawningBus.MoveTransport(Direction.Up); break; case "down": - _drawingBus.MoveTransport(Direction.Down); + _drawningBus.MoveTransport(Direction.Down); break; case "right": - _drawingBus.MoveTransport(Direction.Right); + _drawningBus.MoveTransport(Direction.Right); break; case "left": - _drawingBus.MoveTransport(Direction.Left); + _drawningBus.MoveTransport(Direction.Left); break; } canvas.repaint(); @@ -136,15 +246,14 @@ public class FormBus extends JFrame { }; class Canvas extends JComponent{ - public Canvas(){ - } + public Canvas() {} public void paintComponent (Graphics g){ - if (_drawingBus == null){ + if (_drawningBus == null){ return; } - super.paintComponents (g); + super.paintComponents (g) ; Graphics2D g2d = (Graphics2D)g; - _drawingBus.DrawTransport(g2d); + _drawningBus.DrawTransport(g2d); super.repaint(); } } -- 2.25.1 From b1bc08fc7974c40b8d6cac60ed41ebc0de1705e8 Mon Sep 17 00:00:00 2001 From: MayDayR Date: Fri, 15 Dec 2023 20:07:38 +0300 Subject: [PATCH 2/2] Initial commit --- src/Drawnings/DrawningBus.java | 139 ++++++++++++++++++++ src/Drawnings/DrawningDoorsCircle.java | 10 ++ src/Drawnings/DrawningDoorsRect.java | 46 +++++++ src/Drawnings/DrawningDoorsStar.java | 12 ++ src/Drawnings/DrawningDoubleDeckerBus.java | 54 ++++++++ src/Drawnings/IDrawningDoors.java | 10 ++ src/Entities/EntityDoubleDeckerBus.java | 20 +++ src/MovementStrategy/AbstractStrategy.java | 76 +++++++++++ src/MovementStrategy/DrawningObjectBus.java | 24 ++++ src/MovementStrategy/IMoveableObject.java | 8 ++ src/MovementStrategy/MoveToBorder.java | 53 ++++++++ src/MovementStrategy/MoveToCenter.java | 53 ++++++++ src/MovementStrategy/ObjectParameters.java | 25 ++++ src/MovementStrategy/Status.java | 7 + 14 files changed, 537 insertions(+) create mode 100644 src/Drawnings/DrawningBus.java create mode 100644 src/Drawnings/DrawningDoorsCircle.java create mode 100644 src/Drawnings/DrawningDoorsRect.java create mode 100644 src/Drawnings/DrawningDoorsStar.java create mode 100644 src/Drawnings/DrawningDoubleDeckerBus.java create mode 100644 src/Drawnings/IDrawningDoors.java create mode 100644 src/Entities/EntityDoubleDeckerBus.java create mode 100644 src/MovementStrategy/AbstractStrategy.java create mode 100644 src/MovementStrategy/DrawningObjectBus.java create mode 100644 src/MovementStrategy/IMoveableObject.java create mode 100644 src/MovementStrategy/MoveToBorder.java create mode 100644 src/MovementStrategy/MoveToCenter.java create mode 100644 src/MovementStrategy/ObjectParameters.java create mode 100644 src/MovementStrategy/Status.java diff --git a/src/Drawnings/DrawningBus.java b/src/Drawnings/DrawningBus.java new file mode 100644 index 0000000..b9e9d36 --- /dev/null +++ b/src/Drawnings/DrawningBus.java @@ -0,0 +1,139 @@ +package Drawnings; + +import java.awt.*; +import java.util.Random; +import Entities.*; +import MovementStrategy.*; + +public class DrawningBus { + + public EntityBus entityBus; + public IDrawningDoors _doors; + private int _pictureWidth; + private int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + private int _busWidth = 185; + private int _busHeight = 115; + + public int GetPosX() { return _startPosX; } + public int GetPosY() { return _startPosY; } + public int GetWidth() { return _busWidth; } + public int GetHeight() { return _busHeight; } + + public DrawningBus(int speed, float weight, Color bodyColor, int countDoors, int width, int height) { + if (width < _busHeight || height < _busWidth) + return; + _pictureWidth = width; + _pictureHeight = height; + entityBus = new EntityBus(speed, weight, bodyColor); + + Random random = new Random(); + switch (random.nextInt(0,3)) { + case 0: + _doors = new DrawningDoorsRect(); + break; + case 1: + _doors = new DrawningDoorsStar(); + break; + case 2: + _doors = new DrawningDoorsCircle(); + break; + default: + _doors = new DrawningDoorsRect(); + break; + } + _doors.SetCount(countDoors); + } + + public void SetPosition (int x, int y) { + if (x + _busWidth > _pictureWidth || y + _busHeight > _pictureHeight) { + _startPosX = _pictureWidth - _busWidth; + _startPosY = _pictureHeight - _busHeight; + } + else + { + _startPosX = x; + _startPosY = y; + } + } + + public boolean CanMove(Direction direction) + { + if (entityBus == null) + { + return false; + } + switch (direction) + { + case Left: + return _startPosX - entityBus.Step > 0; + case Right: + return _startPosX + _busWidth + entityBus.Step < _pictureWidth; + case Up: + return _startPosY - entityBus.Step > 0; + case Down: + return _startPosY + _busHeight + entityBus.Step < _pictureHeight; + default: + return false; + } + } + + public void MoveTransport(Direction direction){ + if (entityBus == null) { + return; + } + + switch (direction) { + case Left: + _startPosX -= entityBus.Step; + break; + case Right: + _startPosX += entityBus.Step; + break; + case Up: + _startPosY -= entityBus.Step; + break; + case Down: + _startPosY += entityBus.Step; + break; + } + } + + public void DrawTransport(Graphics2D g) { + + if (entityBus == null) { + return; + } + //тело + g.setColor(entityBus.getBodyColor()); + g.fillRect(_startPosX + 147, _startPosY + 52, 21, 20); + g.fillRect(_startPosX + 147, _startPosY + 71, 30, 27); + g.fillRect(_startPosX + 10, _startPosY + 52, 137, 46); + g.setColor(Color.BLACK); + g.drawRect(_startPosX + 147, _startPosY + 52, 21, 20); + g.drawRect(_startPosX + 147, _startPosY + 71, 30, 27); + g.drawRect(_startPosX + 10, _startPosY + 52, 137, 46); + + g.setColor(Color.blue); + g.fillRect(_startPosX + 150, _startPosY + 55, 15, 15); + g.fillRect(_startPosX + 42, _startPosY + 55, 15, 15); + g.fillRect(_startPosX + 69, _startPosY + 55, 15, 15); + g.fillRect(_startPosX + 96, _startPosY + 55, 15, 15); + g.fillRect(_startPosX + 123, _startPosY + 55, 15, 15); + + g.setColor(Color.BLACK); + g.drawLine(_startPosX + 30, _startPosY + 52, _startPosX + 30, _startPosY + 98); + g.drawLine(_startPosX + 35, _startPosY + 52, _startPosX + 35, _startPosY + 98); + + //колёса + g.fillOval(_startPosX + 23, _startPosY + 88, 25, 25); + g.fillOval(_startPosX + 123, _startPosY + 88, 25, 25); + g.setColor(Color.gray); + g.fillOval(_startPosX + 25, _startPosY + 90, 21, 21); + g.fillOval(_startPosX + 125, _startPosY + 90, 21, 21); + + // двери + _doors.Draw(g, _startPosX, _startPosY); + } +} diff --git a/src/Drawnings/DrawningDoorsCircle.java b/src/Drawnings/DrawningDoorsCircle.java new file mode 100644 index 0000000..bc03cf1 --- /dev/null +++ b/src/Drawnings/DrawningDoorsCircle.java @@ -0,0 +1,10 @@ +package Drawnings; + +import java.awt.*; + +public class DrawningDoorsCircle extends DrawningDoorsRect { + protected void drawDoor(Graphics2D g, int posX, int posY){ + g.setColor(Color.gray); + g.fillOval(posX, posY, 12, 20); + } +} \ No newline at end of file diff --git a/src/Drawnings/DrawningDoorsRect.java b/src/Drawnings/DrawningDoorsRect.java new file mode 100644 index 0000000..25462f9 --- /dev/null +++ b/src/Drawnings/DrawningDoorsRect.java @@ -0,0 +1,46 @@ +package Drawnings; + +import java.awt.*; +import Entities.*; + +public class DrawningDoorsRect implements IDrawningDoors { + private CountDoors _wheel; + + public CountDoors getCount() + { + return _wheel; + } + public void SetCount (int count) { + switch (count) { + case 2: + _wheel = CountDoors.Three; + break; + case 3: + _wheel = CountDoors.Four; + break; + case 4: + _wheel = CountDoors.Five; + break; + default: + _wheel = CountDoors.Three; + break; + } + } + protected void drawDoor(Graphics2D g, int posX, int posY){ + g.setColor(Color.gray); + g.fillRect(posX,posY, 12, 20); + } + public void Draw (Graphics2D g, int _startPosX, int _startPosY) { + drawDoor(g,_startPosX + 40, _startPosY + 75); + drawDoor(g, _startPosX + 60, _startPosY + 75); + drawDoor(g, _startPosX + 80, _startPosY + 75); + if (_wheel == CountDoors.Three) { + return; + } + drawDoor(g, _startPosX + 100, _startPosY + 75); + if (_wheel == CountDoors.Four) { + return; + } + drawDoor(g, _startPosX + 120, _startPosY + 75); + } +} \ No newline at end of file diff --git a/src/Drawnings/DrawningDoorsStar.java b/src/Drawnings/DrawningDoorsStar.java new file mode 100644 index 0000000..4038828 --- /dev/null +++ b/src/Drawnings/DrawningDoorsStar.java @@ -0,0 +1,12 @@ +package Drawnings; + +import java.awt.*; + +public class DrawningDoorsStar extends DrawningDoorsRect { + protected void drawDoor(Graphics2D g, int posX, int posY) { + int[] StarX = {posX + 6, posX + 8, posX + 12, posX + 8, posX + 6, posX + 4, posX, posX + 4}; + int[] StarY = {posY, posY + 6, posY + 10, posY + 14, posY + 20, posY + 14, posY + 10, posY + 6}; + g.setColor(Color.gray); + g.fillPolygon(StarX, StarY, StarX.length); + } +} \ No newline at end of file diff --git a/src/Drawnings/DrawningDoubleDeckerBus.java b/src/Drawnings/DrawningDoubleDeckerBus.java new file mode 100644 index 0000000..892ce5d --- /dev/null +++ b/src/Drawnings/DrawningDoubleDeckerBus.java @@ -0,0 +1,54 @@ +package Drawnings; + +import Entities.EntityDoubleDeckerBus; + +import java.awt.*; + +public class DrawningDoubleDeckerBus extends DrawningBus { + + public DrawningDoubleDeckerBus(int speed, float weight, Color bodyColor, int countWheels, Color additionalColor, boolean isSecondFloor, boolean isStairs, int width, int height) + { + super(speed, weight, bodyColor, countWheels, width, height); + if (entityBus != null) { + entityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, isSecondFloor, isStairs); + } + } + @Override + public void DrawTransport(Graphics2D g) { + + if (entityBus == null) + { + return; + } + super.DrawTransport(g); + + Color additionalColor = ((EntityDoubleDeckerBus) entityBus).getAdditionalColor(); + // 2 этаж + if (((EntityDoubleDeckerBus) entityBus).IsSecondFloor()) { + g.setColor(additionalColor); + g.fillRect(_startPosX + 7, _startPosY + 12, 172, 40); //большой прямоугольник + g.setColor(Color.BLACK); + g.drawRect(_startPosX + 7, _startPosY + 12, 172, 40); + g.drawLine(_startPosX + 7, _startPosY + 36, _startPosX + 178, _startPosY + 36); + + g.setColor(Color.blue); + g.fillRect(_startPosX + 15, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 42, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 69, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 96, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 123, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 150, _startPosY + 15, 15, 15); + } + // лестница + if (((EntityDoubleDeckerBus) entityBus).IsStairs()) { + g.setColor(Color.BLACK); + g.drawLine(_startPosX + 10, _startPosY + 55, _startPosX + 34, _startPosY + 55); + g.drawLine(_startPosX + 10, _startPosY + 58, _startPosX + 34, _startPosY + 58); + g.drawLine(_startPosX + 10, _startPosY + 64, _startPosX + 34, _startPosY + 64); + g.drawLine(_startPosX + 10, _startPosY + 72, _startPosX + 34, _startPosY + 72); + g.drawLine(_startPosX + 10, _startPosY + 80, _startPosX + 34, _startPosY + 80); + g.drawLine(_startPosX + 10, _startPosY + 88, _startPosX + 34, _startPosY + 88); + g.drawLine(_startPosX + 10, _startPosY + 94, _startPosX + 34, _startPosY + 94); + } + } +} diff --git a/src/Drawnings/IDrawningDoors.java b/src/Drawnings/IDrawningDoors.java new file mode 100644 index 0000000..4564d6e --- /dev/null +++ b/src/Drawnings/IDrawningDoors.java @@ -0,0 +1,10 @@ +package Drawnings; + +import java.awt.*; +import Entities.CountDoors; + +public interface IDrawningDoors { + public CountDoors getCount(); + public void SetCount (int count); + public void Draw (Graphics2D g, int _startPosX, int _startPoxY); +} \ No newline at end of file diff --git a/src/Entities/EntityDoubleDeckerBus.java b/src/Entities/EntityDoubleDeckerBus.java new file mode 100644 index 0000000..85744a2 --- /dev/null +++ b/src/Entities/EntityDoubleDeckerBus.java @@ -0,0 +1,20 @@ +package Entities; + +import java.awt.*; + +public class EntityDoubleDeckerBus extends EntityBus { + private Color AdditionalColor; + private boolean IsSecondFloor; + private boolean IsStairs; + + public EntityDoubleDeckerBus(int speed, float weight, Color bodyColor, Color additionalColor, boolean isSecondFloor, boolean isStairs) { + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + IsSecondFloor = isSecondFloor; + IsStairs = isStairs; + } + + public Color getAdditionalColor() { return AdditionalColor; } + public boolean IsSecondFloor() { return IsSecondFloor; } + public boolean IsStairs() { return IsStairs; } +} diff --git a/src/MovementStrategy/AbstractStrategy.java b/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..742c4df --- /dev/null +++ b/src/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,76 @@ +package MovementStrategy; + +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(Direction.Left); } + protected boolean MoveRight() { return MoveTo(Direction.Right); } + protected boolean MoveUp() { return MoveTo(Direction.Up); } + protected boolean MoveDown() { return MoveTo(Direction.Down); } + + // параметры + protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); } + // шаг + protected int GetStep() + { + if (_state != Status.InProgress) + { + return 0; + } + return _moveableObject.GetStep(); + } + // перемещение + protected abstract void MoveToTarget(); + + // достигнута ли цель + protected abstract boolean IsTargetDestination(); + + // попытка перемещения по направлению + private boolean MoveTo(Direction directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject.CheckCanMove(directionType)) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } +} diff --git a/src/MovementStrategy/DrawningObjectBus.java b/src/MovementStrategy/DrawningObjectBus.java new file mode 100644 index 0000000..2858468 --- /dev/null +++ b/src/MovementStrategy/DrawningObjectBus.java @@ -0,0 +1,24 @@ +package MovementStrategy; +import Drawnings.*; + +public class DrawningObjectBus implements IMoveableObject { + private DrawningBus _drawningBus = null; + + public DrawningObjectBus(DrawningBus drawningBus) + { + _drawningBus = drawningBus; + } + + public ObjectParameters GetObjectPosition() + { + if (_drawningBus == null || _drawningBus.entityBus == null) + { + return null; + } + return new ObjectParameters(_drawningBus.GetPosX(), _drawningBus.GetPosY(), _drawningBus.GetWidth(), _drawningBus.GetHeight()); + } + + public int GetStep() { return (int) _drawningBus.entityBus.Step; } + public boolean CheckCanMove(Direction direction) { return _drawningBus.CanMove(direction); } + public void MoveObject(Direction direction) { _drawningBus.MoveTransport(direction); } +} diff --git a/src/MovementStrategy/IMoveableObject.java b/src/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..ffd427e --- /dev/null +++ b/src/MovementStrategy/IMoveableObject.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean CheckCanMove(Direction direction); + void MoveObject(Direction direction); +} diff --git a/src/MovementStrategy/MoveToBorder.java b/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..08e2b44 --- /dev/null +++ b/src/MovementStrategy/MoveToBorder.java @@ -0,0 +1,53 @@ +package MovementStrategy; + +public class MoveToBorder extends AbstractStrategy { + @Override + protected boolean IsTargetDestination() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.RightBorder() <= FieldWidth && + objParams.RightBorder() + GetStep() >= FieldWidth && + objParams.DownBorder() <= FieldHeight && + objParams.DownBorder() + GetStep() >= FieldHeight; + } + + // движение к цели + @Override + protected void MoveToTarget() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + var diffX = FieldWidth; + if (Math.abs(diffX) > GetStep()) + { + if (diffX < 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + var diffY = FieldHeight; + if (Math.abs(diffY) > GetStep()) + { + if (diffY < 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/src/MovementStrategy/MoveToCenter.java b/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..1b278cb --- /dev/null +++ b/src/MovementStrategy/MoveToCenter.java @@ -0,0 +1,53 @@ +package MovementStrategy; + +public class MoveToCenter extends AbstractStrategy { + @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; + } + + // движение к цели + @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(); + } + } + } +} diff --git a/src/MovementStrategy/ObjectParameters.java b/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..a741f5a --- /dev/null +++ b/src/MovementStrategy/ObjectParameters.java @@ -0,0 +1,25 @@ +package MovementStrategy; + +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/src/MovementStrategy/Status.java b/src/MovementStrategy/Status.java new file mode 100644 index 0000000..586b538 --- /dev/null +++ b/src/MovementStrategy/Status.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public enum Status { + NotInit, + InProgress, + Finish +} -- 2.25.1