diff --git a/AntiAirCraftGun/src/AntiAircraftGun.form b/AntiAirCraftGun/src/AntiAircraftGun.form index fc971e7..e656c92 100644 --- a/AntiAirCraftGun/src/AntiAircraftGun.form +++ b/AntiAirCraftGun/src/AntiAircraftGun.form @@ -1,29 +1,29 @@
- + - + - + - + - + - + - + @@ -31,7 +31,7 @@ - + @@ -41,7 +41,7 @@ - + @@ -49,7 +49,7 @@ - + @@ -57,9 +57,36 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/AntiAirCraftGun/src/AntiAircraftGun.java b/AntiAirCraftGun/src/AntiAircraftGun.java index d10b0f1..bb373b4 100644 --- a/AntiAirCraftGun/src/AntiAircraftGun.java +++ b/AntiAirCraftGun/src/AntiAircraftGun.java @@ -1,18 +1,27 @@ +import Drawings.DirectionType; +import Drawings.DrawingAntiAircraftGun; +import Drawings.DrawingGun; +import Drawings.EnumerateWheels; +import MovementStrategy.*; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; -import java.util.concurrent.ThreadLocalRandom; /** * Форма */ public class AntiAircraftGun extends JFrame { + /** + * Стратегия перемещения + */ + private AbstractStrategy abstractStrategy; /** * Объект сущности зенитной установки */ - private DrawingAntiAircraftGun _drawingAntiAircraftGun; + private DrawingGun _drawingGun; /** * Панель */ @@ -20,7 +29,7 @@ public class AntiAircraftGun extends JFrame { /** * Кнопка создания */ - private JButton buttonCreate; + private JButton buttonCreateGun; /** * Кнопка движения вниз */ @@ -37,6 +46,9 @@ public class AntiAircraftGun extends JFrame { * Кнопка движения вправо */ private JButton buttonRight; + private JButton buttonCreateAntiAircraftGun; + private JButton buttonStep; + private JComboBox comboBoxStrategy; /** * Метод отрисовки @@ -45,11 +57,12 @@ public class AntiAircraftGun extends JFrame { */ public void paint(Graphics g) { super.paint(g); - if (_drawingAntiAircraftGun == null) { + if (_drawingGun == null) { System.out.println("Значение пустое, я ничего не нарисовал"); return; } - _drawingAntiAircraftGun.DrawTransport(g); + System.out.println(_drawingGun.getClass().getSimpleName()); + _drawingGun.DrawTransport(g); System.out.println("Ура, я что-то нарисовал"); } @@ -59,9 +72,10 @@ public class AntiAircraftGun extends JFrame { public AntiAircraftGun() { add(panelAntiAircraftGun); setVisible(true); - setSize(600, 600); + setSize(800, 800); setResizable(false); Graphics g = getGraphics().create(); + abstractStrategy = null; Icon iconDown = new ImageIcon("AntiAirCraftGun\\src\\Resources\\Down123.jpg"); Icon iconUp = new ImageIcon("AntiAirCraftGun\\src\\Resources\\up123.jpg"); Icon iconLeft = new ImageIcon("AntiAirCraftGun\\src\\Resources\\left123.jpg"); @@ -70,42 +84,33 @@ public class AntiAircraftGun extends JFrame { buttonUp.setIcon(iconUp); buttonRight.setIcon(iconRight); buttonLeft.setIcon(iconLeft); - _drawingAntiAircraftGun = new DrawingAntiAircraftGun(); - buttonCreate.addActionListener(new ActionListener() { + comboBoxStrategy.addItem("К центру"); + comboBoxStrategy.addItem("К краю"); + buttonCreateGun.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Я нажал на кнопку"); System.out.println(e.paramString()); - Random random = new Random(); - - _drawingAntiAircraftGun.Init( - random.nextInt(10, 100), - random.nextInt(10, 1000), - new Color(random.nextInt(0, 255), random.nextInt(0, 255), random.nextInt(0, 255)), - new Color(random.nextInt(0, 255), random.nextInt(0, 255), random.nextInt(0, 255)), - random.nextBoolean(), - random.nextBoolean(), - EnumerateWheels.getRandomEnumerateWheels() - - ); - _drawingAntiAircraftGun.SetPictureSize( - panelAntiAircraftGun.getWidth(), - panelAntiAircraftGun.getHeight() - ); - _drawingAntiAircraftGun.SetPosition( - random.nextInt(50, 150), - random.nextInt(50, 150) - ); - paint(g); + CreateObj(g, "EntityGun"); } }); - if (_drawingAntiAircraftGun == null) { + buttonCreateAntiAircraftGun.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + System.out.println("Я нажал на кнопку"); + System.out.println(actionEvent.paramString()); + CreateObj(g, "EntityAntiAircraftGun"); + } + }); + if (_drawingGun == null) { + System.out.println("Это выход из формы"); return; } + System.out.println("Все нормально, идем дальше"); buttonDown.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { - boolean result = _drawingAntiAircraftGun.MoveTransport(DirectionType.Down); + boolean result = _drawingGun.MoveTransport(DirectionType.Down); if (result) { paint(g); System.out.println(actionEvent.paramString()); @@ -115,7 +120,7 @@ public class AntiAircraftGun extends JFrame { buttonUp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { - _drawingAntiAircraftGun.MoveTransport(DirectionType.Up); + _drawingGun.MoveTransport(DirectionType.Up); paint(g); System.out.println(actionEvent.paramString()); } @@ -123,7 +128,7 @@ public class AntiAircraftGun extends JFrame { buttonLeft.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { - _drawingAntiAircraftGun.MoveTransport(DirectionType.Left); + _drawingGun.MoveTransport(DirectionType.Left); paint(g); System.out.println(actionEvent.paramString()); } @@ -131,10 +136,85 @@ public class AntiAircraftGun extends JFrame { buttonRight.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { - _drawingAntiAircraftGun.MoveTransport(DirectionType.Right); + _drawingGun.MoveTransport(DirectionType.Right); paint(g); System.out.println(actionEvent.paramString()); } }); + + buttonStep.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (_drawingGun == null) { + return; + } + if (comboBoxStrategy.isEnabled()) { + abstractStrategy = null; + switch (comboBoxStrategy.getSelectedIndex()) { + case 0: { + abstractStrategy = new MoveToCenter(); + break; + } + case 1: { + abstractStrategy = new MoveToBorder(); + break; + } + } + if (abstractStrategy == null) { + return; + } + abstractStrategy.SetData(new MoveableGun(_drawingGun), + panelAntiAircraftGun.getWidth(), panelAntiAircraftGun.getHeight()); + } + if (abstractStrategy == null) { + return; + } + comboBoxStrategy.setEnabled(false); + abstractStrategy.MakeStep(); + paint(g); + if (abstractStrategy.GetStatus() == StrategyStatus.Finish) { + comboBoxStrategy.setEnabled(true); + abstractStrategy = null; + } + } + }); + } + + private void CreateObj(Graphics g, String type) { + Random random = new Random(); + switch (type) { + case "EntityAntiAircraftGun": { + System.out.println("Я зашле внутрь зенитной установик"); + _drawingGun = + new DrawingAntiAircraftGun( + random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(0, 255), random.nextInt(0, 255), random.nextInt(0, 255)), + new Color(random.nextInt(0, 255), random.nextInt(0, 255), random.nextInt(0, 255)), + random.nextBoolean(), + random.nextBoolean(), + EnumerateWheels.getRandomEnumerateWheels() + ); + break; + } + case "EntityGun": { + _drawingGun = new DrawingGun( + random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(0, 255), random.nextInt(0, 255), random.nextInt(0, 255)), + EnumerateWheels.getRandomEnumerateWheels() + ); + break; + } + default: + break; + } + System.out.println("А здесь мы начинаем рисовать"); + _drawingGun.SetPictureSize(panelAntiAircraftGun.getWidth(), panelAntiAircraftGun.getHeight()); + _drawingGun.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + abstractStrategy = null; + comboBoxStrategy.setEnabled(true); + paint(g); } } + diff --git a/AntiAirCraftGun/src/DirectionType.java b/AntiAirCraftGun/src/Drawings/DirectionType.java similarity index 72% rename from AntiAirCraftGun/src/DirectionType.java rename to AntiAirCraftGun/src/Drawings/DirectionType.java index 1c59407..a94de2d 100644 --- a/AntiAirCraftGun/src/DirectionType.java +++ b/AntiAirCraftGun/src/Drawings/DirectionType.java @@ -1,3 +1,5 @@ +package Drawings; + /** * Пересичление напрваления */ @@ -17,5 +19,9 @@ public enum DirectionType { /** * Вправо */ - Right + Right, + /** + * Без направления + */ + Unknow, } diff --git a/AntiAirCraftGun/src/DrawingAntiAircraftGun.java b/AntiAirCraftGun/src/Drawings/DrawingAntiAircraftGun.java similarity index 74% rename from AntiAirCraftGun/src/DrawingAntiAircraftGun.java rename to AntiAirCraftGun/src/Drawings/DrawingAntiAircraftGun.java index a9b1ebc..a4ff1a0 100644 --- a/AntiAirCraftGun/src/DrawingAntiAircraftGun.java +++ b/AntiAirCraftGun/src/Drawings/DrawingAntiAircraftGun.java @@ -1,13 +1,23 @@ +package Drawings; + +import Entites.EntityAntiAirCraftGun; +import Entites.EntityGun; + import java.awt.*; /** * Класс отрисовки зенитной установки */ -public class DrawingAntiAircraftGun { +public class DrawingAntiAircraftGun extends DrawingGun { + + public DrawingAntiAircraftGun(int speed, double weight, Color bodyColor, Color optionalElementsColor, boolean hatchHeight, boolean radar,EnumerateWheels enumerateWheels){ + super(140,65); + setEntityGun(new EntityAntiAirCraftGun(speed,weight,bodyColor,optionalElementsColor,hatchHeight,radar,enumerateWheels)); + } /** * Класс-сущность */ - private EntityAntiAirCraftGun EntityAntiAirCraftGun; + private Entites.EntityAntiAirCraftGun EntityAntiAirCraftGun; /** * Объект прорисовки колес */ @@ -93,39 +103,6 @@ public class DrawingAntiAircraftGun { */ private final int _drawingGunHeight = 115; - /** - * Инициализация - * - * @param speed - скорость - * @param weight - вес - * @param bodyColor - основной цвет - * @param optionalElement - дополнительный элемент - * @param hatchHeight - - * @param radar - радар - */ - public void Init(int speed, - double weight, - Color bodyColor, - Color optionalElement, - boolean hatchHeight, - boolean radar, - EnumerateWheels countWheels) { - EntityAntiAirCraftGun = new EntityAntiAirCraftGun(); - - EntityAntiAirCraftGun.Init( - speed, - weight, - bodyColor, - optionalElement, - hatchHeight, - radar, - countWheels - ); - _pictureHeight = null; - _pictureWidth = null; - _startPosX = null; - _startPosY = null; - } /** * Установка границ поля @@ -192,25 +169,25 @@ public class DrawingAntiAircraftGun { int heightCap=28; double step = EntityAntiAirCraftGun.getStep(); switch (direction) { - case Left: + case DirectionType.Left: if (_startPosX - step > 0) { System.out.println("Left"); _startPosX -= (int) step; } return true; - case Right: + case DirectionType.Right: if (_startPosX + step + _drawingGunWidth < _pictureWidth) { System.out.println("Right"); _startPosX += (int) step; } return true; - case Down: + case DirectionType.Down: if (_startPosY + step + _drawingGunHeight < _pictureHeight+heightCap) { System.out.println("Down"); _startPosY += (int) step; } return true; - case Up: + case DirectionType.Up: if (_startPosY - step > heightCap) { System.out.println("Up"); _startPosY -= (int) step; @@ -227,25 +204,19 @@ public class DrawingAntiAircraftGun { * @param g - объект графики */ public void DrawTransport(Graphics g) { - if (EntityAntiAirCraftGun == null || _startPosX == null || _startPosY == null) { + if(EntityGun()==null){ + + System.out.println("Здесь EntityGun уже не существует"); return; } - drawingWheels = new DrawingWheels(_startPosX, _startPosY, EntityAntiAirCraftGun.getCountWheels()); - // Башня - g.setColor(EntityAntiAirCraftGun.getBodyColor()); - g.fillRect(_startPosX + 50, _startPosY + 50, 60, 25); - g.fillRect(_startPosX + 25, _startPosY + 75, 110, 10); - // Гусеницы - g.drawArc(_startPosX + 110, _startPosY + 85, 40, 30, 270, 180); - g.drawArc(_startPosX + 10, _startPosY + 85, 40, 30, 90, 180); - g.drawLine(_startPosX + 30, _startPosY + 115, _startPosX + 130, _startPosY + 115); - //Катки большие - g.drawOval(_startPosX + 13, _startPosY + 93, 20, 20); - g.drawOval(_startPosX + 126, _startPosY + 93, 20, 20); - // Катки малые - drawingWheels.drawWheels(g, EntityAntiAirCraftGun.getBodyColor()); + _startPosX+=10; + _startPosY+=50; + super.DrawTransport(g); + _startPosX-=10; + _startPosY-=50; //Орудие g.drawLine(_startPosX + 100, _startPosY + 70, _startPosX + 150, _startPosY + 10); + System.out.println(EntityAntiAirCraftGun.isHatch()+"здЕСЬ НЕ ДОЛЖНО БЫТЬ ПУСТО"); //Люк if (EntityAntiAirCraftGun.isHatch()) { g.setColor(EntityAntiAirCraftGun.getOptionalElementsColor()); diff --git a/AntiAirCraftGun/src/Drawings/DrawingGun.java b/AntiAirCraftGun/src/Drawings/DrawingGun.java new file mode 100644 index 0000000..0f55ca9 --- /dev/null +++ b/AntiAirCraftGun/src/Drawings/DrawingGun.java @@ -0,0 +1,244 @@ +package Drawings; + +import Entites.EntityGun; + +import java.awt.*; + +public class DrawingGun { + private DrawingWheels drawingWheels; + private EntityGun entityGun; + + public EntityGun EntityGun() { + return entityGun; + } + + protected void setEntityGun(EntityGun entityGun) { + this.entityGun = entityGun; + } + + private Integer _pictureWidth; + + public Integer GetPictureWidth() { + return _pictureWidth; + } + + ; + private Integer _pictureHeight; + + public Integer GetPictureHeight() { + return _pictureHeight; + } + + /** + * Позиция по Х + */ + private Integer _startPosX; + + /** + * Геттер для стартовой позиции Y + * + * @return стартовую позицию по Y + */ + public Integer get_startPosY() { + return _startPosY; + } + + /** + * Геттер для стартовой позиции X + * + * @return стартовую позицию по X + */ + public Integer get_startPosX() { + return _startPosX; + } + + /** + * Сеттер для стартовой позиции X + * + * @param _startPosX - стартовая позиция X + */ + public void set_startPosX(Integer _startPosX) { + this._startPosX = _startPosX; + } + + /** + * Сеттер для стартовой позиции Y + * + * @param _startPosY - стартовая позиция Y + */ + public void set_startPosY(Integer _startPosY) { + this._startPosY = _startPosY; + } + + /** + * Позиция по Y + */ + private Integer _startPosY; + + private Integer _drawingGunWidth = 140; + private Integer _drawingGunHeight = 65; + + public Integer GetWidth() { + return _drawingGunWidth; + } + + public Integer GetHeight() { + return _drawingGunHeight; + } + + private DrawingGun() { + _pictureHeight = null; + _pictureWidth = null; + _startPosX = null; + _startPosY = null; + } + + public DrawingGun(int speed, double weight, Color bodyColor, EnumerateWheels countWheels) { + super(); + setEntityGun(new EntityGun(speed, weight, bodyColor, countWheels)); + } + + protected DrawingGun(int _drawingGunWidth, int _drawingGunHeight) { + super(); + this._drawingGunWidth = _drawingGunWidth; + this._drawingGunHeight = _drawingGunHeight; + } + + /** + * Установка границ поля + * + * @param width - ширина + * @param height - высота + * @return false - проверка не пройдена, нельзя разместить объект в этих границах, + * true - проверка пройдена + */ + public boolean SetPictureSize(int width, int height) { + if (_drawingGunHeight > height || _drawingGunWidth > width) { + return false; + } + if (_startPosY != null && _startPosX != null) { + if (_startPosX + _drawingGunWidth > width) { + _startPosX = width - _drawingGunWidth; + } + if (_startPosY + _drawingGunHeight > height) { + _startPosY = height - _drawingGunHeight; + } + } + _pictureWidth = width; + _pictureHeight = height; + return true; + } + + /** + * Установка позиции + * + * @param x - позиция по x + * @param y - позиция по y + */ + public void SetPosition(int x, int y) { + if (_pictureHeight == null || _pictureWidth == null) { + return; + } + if (x + _drawingGunWidth > _pictureWidth || x < 0) { + _startPosX = _pictureWidth - _drawingGunWidth; + } else { + _startPosX = x; + } + if (y + _drawingGunHeight > _pictureHeight || y < 0) { + _startPosY = _pictureHeight - _drawingGunHeight; + } else { + _startPosY = y; + } + } + + /** + * Изменение направления перемещения + * + * @param direction - тип перемещения + * @return true - перемещение удалось; + * false - не удалось переместить объект + */ + public boolean MoveTransport(DirectionType direction) { + if ((EntityGun() == null) || (_startPosX == null) || (_startPosY == null)) { + System.out.println("Все плохо"); + return false; + } + /** + * Высота шапки экрана + */ + int heightCap = 28; + double step = EntityGun().getStep(); + switch (direction) { + case DirectionType.Left: + if (_startPosX - step > 0) { + System.out.println("Left"); + _startPosX -= (int) step; + } + return true; + case DirectionType.Right: + if (_startPosX + step + _drawingGunWidth < _pictureWidth) { + System.out.println("Right"); + _startPosX += (int) step; + } + return true; + case DirectionType.Down: + if (_startPosY + step + _drawingGunHeight < _pictureHeight + heightCap) { + System.out.println("Down"); + _startPosY += (int) step; + } + return true; + case DirectionType.Up: + if (_startPosY - step > heightCap) { + System.out.println("Up"); + _startPosY -= (int) step; + } + return true; + default: + return false; + } + } + + + /** + * Прорисовка объекта + * + * @param g - объект графики + */ + public void DrawTransport(Graphics g) { + if (EntityGun() == null || _startPosX == null || _startPosY == null) { + System.out.println("При вызове DrawTransport, получилось исключение"); + return; + } + drawingWheels = new DrawingWheels(_startPosX, _startPosY, EntityGun().getCountWheels()); + // Башня + g.setColor(EntityGun().getBodyColor()); + g.fillRect(_startPosX + 50 - 10, _startPosY + 50 - 50, 60, 25); + g.fillRect(_startPosX + 25 - 10, _startPosY + 75 - 50, 110, 10); + // Гусеницы + g.drawArc(_startPosX + 110 - 10, _startPosY + 85 - 50, 40, 30, 270, 180); + g.drawArc(_startPosX + 10 - 10, _startPosY + 85 - 50, 40, 30, 90, 180); + g.drawLine(_startPosX + 30 - 10, _startPosY + 115 - 50, _startPosX + 130 - 10, _startPosY + 115 - 50); + //Катки большие + g.drawOval(_startPosX + 13 - 10, _startPosY + 93 - 50, 20, 20); + g.drawOval(_startPosX + 126 - 10, _startPosY + 93 - 50, 20, 20); + // Катки малые + drawingWheels.drawWheels(g, EntityGun().getBodyColor()); +// //Орудие +// g.drawLine(_startPosX + 100, _startPosY + 70, _startPosX + 150, _startPosY + 10); +// //Люк +// if (EntityGun().isHatch()) { +// g.setColor(EntityGun().getOptionalElementsColor()); +// g.fillRect(_startPosX + 85, _startPosY + 45, 20, 5); +// } +// //Радар +// if (EntityGun().isRadar()) { +// g.setColor(Color.BLACK); +// g.drawLine(_startPosX + 65, _startPosY + 50, _startPosX + 65, _startPosY + 25); +// g.fillOval(_startPosX + 35, _startPosY, 60, 25); +// g.setColor(Color.GREEN); +// g.drawLine(_startPosX + 65, _startPosY + 25, _startPosX + 65, _startPosY); +// g.drawLine(_startPosX + 35, _startPosY + 13, _startPosX + 95, _startPosY + 13); +// } + } + +} diff --git a/AntiAirCraftGun/src/DrawingWheels.java b/AntiAirCraftGun/src/Drawings/DrawingWheels.java similarity index 74% rename from AntiAirCraftGun/src/DrawingWheels.java rename to AntiAirCraftGun/src/Drawings/DrawingWheels.java index bb66bd7..6346d4a 100644 --- a/AntiAirCraftGun/src/DrawingWheels.java +++ b/AntiAirCraftGun/src/Drawings/DrawingWheels.java @@ -1,3 +1,5 @@ +package Drawings; + import java.awt.*; public class DrawingWheels { @@ -21,17 +23,17 @@ public class DrawingWheels { // */ // public void setEnumerateWheels(int countWheels) { // if (4 > countWheels || 6 < countWheels) { -// enumerateWheels = EnumerateWheels.getRandomEnumerateWheels(); +// enumerateWheels = Drawings.EnumerateWheels.getRandomEnumerateWheels(); // } // switch (countWheels) { // case 4: -// enumerateWheels = EnumerateWheels.Four; +// enumerateWheels = Drawings.EnumerateWheels.Four; // break; // case 5: -// enumerateWheels = EnumerateWheels.Five; +// enumerateWheels = Drawings.EnumerateWheels.Five; // break; // case 6: -// enumerateWheels = EnumerateWheels.Six; +// enumerateWheels = Drawings.EnumerateWheels.Six; // break; // // } @@ -39,6 +41,7 @@ public class DrawingWheels { /** * Геттер для объекта перечисления + * * @return - объект перечисления */ public EnumerateWheels getEnumerateWheels() { @@ -47,8 +50,9 @@ public class DrawingWheels { /** * Конструктор - * @param _startPosX - координата по Х - * @param _startPosY - координата по У + * + * @param _startPosX - координата по Х + * @param _startPosY - координата по У * @param enumerateWheels - объект перечисления */ public DrawingWheels(Integer _startPosX, Integer _startPosY, EnumerateWheels enumerateWheels) { @@ -59,17 +63,18 @@ public class DrawingWheels { /** * Геттер для кол-ва колес + * * @return кол-во колес */ public Integer getEnumerateWheelsCount() { switch (getEnumerateWheels()) { - case Five -> { + case EnumerateWheels.Five -> { return 5; } - case Four -> { + case EnumerateWheels.Four -> { return 4; } - case Six -> { + case EnumerateWheels.Six -> { return 6; } default -> { @@ -80,7 +85,8 @@ public class DrawingWheels { /** * Отрисовка колес - * @param g - объект графики + * + * @param g - объект графики * @param optionColor - цвет колес */ public void drawWheels(Graphics g, Color optionColor) { @@ -88,7 +94,7 @@ public class DrawingWheels { System.out.println("Дошел до drawWheels"); g.setColor(optionColor); for (int i = 1; i <= getEnumerateWheelsCount(); i++) { - g.drawOval(_startPosX + 20 + count, _startPosY + 105, 10, 10); + g.drawOval(_startPosX + 20 + count - 10, _startPosY + 105 - 50, 10, 10); count += 15; } } diff --git a/AntiAirCraftGun/src/EnumerateWheels.java b/AntiAirCraftGun/src/Drawings/EnumerateWheels.java similarity index 97% rename from AntiAirCraftGun/src/EnumerateWheels.java rename to AntiAirCraftGun/src/Drawings/EnumerateWheels.java index a388956..fd2815e 100644 --- a/AntiAirCraftGun/src/EnumerateWheels.java +++ b/AntiAirCraftGun/src/Drawings/EnumerateWheels.java @@ -1,3 +1,5 @@ +package Drawings; + import java.util.Random; public enum EnumerateWheels { diff --git a/AntiAirCraftGun/src/Entites/EntityAntiAirCraftGun.java b/AntiAirCraftGun/src/Entites/EntityAntiAirCraftGun.java new file mode 100644 index 0000000..bef9ecc --- /dev/null +++ b/AntiAirCraftGun/src/Entites/EntityAntiAirCraftGun.java @@ -0,0 +1,96 @@ +package Entites; + +import Drawings.EnumerateWheels; + +import java.awt.*; + +/** + * Класс-сущность зенитной установки + */ +public class EntityAntiAirCraftGun extends EntityGun{ + private EntityGun EntityGun; + private Color optionalElementsColor; + + /** + * Геттер для опциональных элементов + * + * @return цвет для опциональных элементов + */ + public Color getOptionalElementsColor() { + return optionalElementsColor; + } + + /** + * Сеттер для опциональных элементов + * + * @param optionalElementsColor - цвет для опциональных элементов + */ + public void setOptionalElementsColor(Color optionalElementsColor) { + this.optionalElementsColor = optionalElementsColor; + } + + /** + * Люк + */ + private boolean Hatch; + + /** + * Геттер на наличие люка + * + * @return true - люк есть, false - люка нет + */ + public boolean isHatch() { + return Hatch; + } + + /** + * Сеттер для люка + * + * @param hatch - наличие люка + */ + private void setHatch(boolean hatch) { + Hatch = hatch; + } + + /** + * Радар + */ + private boolean Radar; + + /** + * Геттер для радара + * + * @return true - радар есть, false - радара нет + */ + public boolean isRadar() { + return Radar; + } + + /** + * Сеттер для радара + * + * @param radar - наличие радара + */ + private void setRadar(boolean radar) { + Radar = radar; + } + + /** + * Инициализация + * + * @param speed - скорость + * @param weight - вес + * @param bodyColor - основной цвет + * @param optionalElementsColor - дополнительный цве + * @param hatch - люк + * @param radar - радара + */ + + public EntityAntiAirCraftGun(int speed, double weight, Color bodyColor, Color optionalElementsColor, boolean hatch, boolean radar, EnumerateWheels countWheels) { + super(speed, weight, bodyColor,countWheels); + EntityGun = new EntityGun(speed,weight,bodyColor,countWheels); + this.optionalElementsColor = optionalElementsColor; + Hatch = hatch; + Radar = radar; + } +} diff --git a/AntiAirCraftGun/src/Entites/EntityGun.java b/AntiAirCraftGun/src/Entites/EntityGun.java new file mode 100644 index 0000000..11a5f45 --- /dev/null +++ b/AntiAirCraftGun/src/Entites/EntityGun.java @@ -0,0 +1,115 @@ +package Entites; + +import Drawings.EnumerateWheels; + +import java.awt.*; + +public class EntityGun { + + /** + * Скорость + */ + private int speed; + + /** + * Геттер для скорости + * + * @return скорость + */ + public int getSpeed() { + return speed; + } + + /** + * Сеттер для скорости + * + * @param speed - скорость + */ + private void setSpeed(int speed) { + this.speed = speed; + } + + /** + * Вес + */ + private double weight; + + /** + * Геттер для веса + * + * @return вес + */ + public double getWeight() { + return weight; + } + + /** + * Сеттер для веса + * + * @param weight - вес + */ + private void setWeight(double weight) { + this.weight = weight; + } + + /** + * Основной цвет + */ + private Color bodyColor; + + /** + * Геттер для основого цвета + * + * @return основной цвет + */ + public Color getBodyColor() { + return bodyColor; + } + + /** + * Сеттер для основого цвета + * + * @param bodyColor - основной цвет + */ + private void setBodyColor(Color bodyColor) { + this.bodyColor = bodyColor; + } + + /** + * Кол-во колес + */ + private EnumerateWheels countWheels; + + /** + * Геттер для кол-ва колес + * + * @return кол-во колес + */ + public EnumerateWheels getCountWheels() { + return countWheels; + } + + /** + * Сеттер для кол-ва колес + * + * @param countWheels - кол-во колес + */ + public void setCountWheels(EnumerateWheels countWheels) { + this.countWheels = countWheels; + } + /** + * Геттер для шага + * + * @return шаг + */ + public Double getStep() { + return getSpeed() * 100 / getWeight(); + } + + public EntityGun(int speed, double weight, Color bodyColor, EnumerateWheels countWheels) { + this.speed = speed; + this.weight = weight; + this.bodyColor = bodyColor; + this.countWheels = countWheels; + } +} diff --git a/AntiAirCraftGun/src/EntityAntiAirCraftGun.java b/AntiAirCraftGun/src/EntityAntiAirCraftGun.java deleted file mode 100644 index 557c503..0000000 --- a/AntiAirCraftGun/src/EntityAntiAirCraftGun.java +++ /dev/null @@ -1,204 +0,0 @@ -import java.awt.*; - -/** - * Класс-сущность зенитной установки - */ -public class EntityAntiAirCraftGun { - /** - * Скорость - */ - private int speed; - - /** - * Геттер для скорости - * - * @return скорость - */ - public int getSpeed() { - return speed; - } - - /** - * Сеттер для скорости - * - * @param speed - скорость - */ - private void setSpeed(int speed) { - this.speed = speed; - } - - /** - * Вес - */ - private double weight; - - /** - * Геттер для веса - * - * @return вес - */ - public double getWeight() { - return weight; - } - - /** - * Сеттер для веса - * - * @param weight - вес - */ - private void setWeight(double weight) { - this.weight = weight; - } - - /** - * Основной цвет - */ - private Color bodyColor; - - /** - * Геттер для основого цвета - * - * @return основной цвет - */ - public Color getBodyColor() { - return bodyColor; - } - - /** - * Сеттер для основого цвета - * - * @param bodyColor - основной цвет - */ - private void setBodyColor(Color bodyColor) { - this.bodyColor = bodyColor; - } - - /** - * Опциональные элементы - */ - private Color optionalElementsColor; - - /** - * Геттер для опциональных элементов - * - * @return цвет для опциональных элементов - */ - public Color getOptionalElementsColor() { - return optionalElementsColor; - } - - /** - * Сеттер для опциональных элементов - * - * @param optionalElementsColor - цвет для опциональных элементов - */ - public void setOptionalElementsColor(Color optionalElementsColor) { - this.optionalElementsColor = optionalElementsColor; - } - - /** - * Люк - */ - private boolean Hatch; - - /** - * Геттер на наличие люка - * - * @return true - люк есть, false - люка нет - */ - public boolean isHatch() { - return Hatch; - } - - /** - * Сеттер для люка - * - * @param hatch - наличие люка - */ - private void setHatch(boolean hatch) { - Hatch = hatch; - } - - /** - * Радар - */ - private boolean Radar; - - /** - * Геттер для радара - * - * @return true - радар есть, false - радара нет - */ - public boolean isRadar() { - return Radar; - } - - /** - * Сеттер для радара - * - * @param radar - наличие радара - */ - private void setRadar(boolean radar) { - Radar = radar; - } - - /** - * Геттер для шага - * - * @return шаг - */ - public double getStep() { - return getSpeed() * 100 / getWeight(); - } - - /** - * Кол-во колес - */ - private EnumerateWheels countWheels; - - /** - * Геттер для кол-ва колес - * - * @return кол-во колес - */ - public EnumerateWheels getCountWheels() { - return countWheels; - } - - /** - * Сеттер для кол-ва колес - * - * @param countWheels - кол-во колес - */ - public void setCountWheels(EnumerateWheels countWheels) { - this.countWheels = countWheels; - } - - /** - * Инициализация - * - * @param speed - скорость - * @param weight - вес - * @param bodyColor - основной цвет - * @param optionalElementsColor - дополнительный цве - * @param hatch - люк - * @param radar - радара - */ - public void Init( - int speed, - double weight, - Color bodyColor, - Color optionalElementsColor, - boolean hatch, - boolean radar, - EnumerateWheels countWheels - ) { - setSpeed(speed); - setWeight(weight); - setBodyColor(bodyColor); - setOptionalElementsColor(optionalElementsColor); - setHatch(hatch); - setRadar(radar); - setCountWheels(countWheels); - } -} diff --git a/AntiAirCraftGun/src/MovementStrategy/AbstractStrategy.java b/AntiAirCraftGun/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..111d01a --- /dev/null +++ b/AntiAirCraftGun/src/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,183 @@ +package MovementStrategy; + +/** + * Класс стратегии + */ +public abstract class AbstractStrategy { + /** + * Перемещаемый объект + */ + private IMoveableObject _moveableObject; + /** + * Статус перемещения + */ + private StrategyStatus _state = StrategyStatus.NotInit; + /** + * Ширина поля + */ + private Integer fieldWidth; + + /** + * Геттер для ширины поля + * + * @return ширину поля + */ + protected Integer FieldWidth() { + return fieldWidth; + } + + /** + * Сеттер для ширины поля + * + * @param fieldWidth ширина поля + */ + private void setFieldWidth(Integer fieldWidth) { + this.fieldWidth = fieldWidth; + } + + /** + * Высота поля + */ + private Integer fieldHeight; + + /** + * Геттер для высоты поля + * + * @return высота поля + */ + protected Integer FieldHeight() { + return fieldHeight; + } + + /** + * Сеттер для высоты поля + * + * @param fieldHeight высота поля + */ + + private void setFieldHeight(Integer fieldHeight) { + this.fieldHeight = fieldHeight; + } + + /** + * Геттер для состояния + * + * @return состояние + */ + public StrategyStatus GetStatus() { + return _state; + } + + ; + + /** + * Установка данных + * + * @param moveableObject перемещаемый объект + * @param width ширина + * @param height высота + */ + public void SetData(IMoveableObject moveableObject, Integer width, Integer height) { + if (moveableObject == null) { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObject; + setFieldWidth(width); + setFieldHeight(height); + } + + /** + * Шаг перемещения + */ + public void MakeStep() { + if (_state != StrategyStatus.InProgress) { + return; + } + if (IsTargetDestinaion()) { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + + /** + * Влево + * + * @return Результат перемещения (true - удалось переместиться, false -неудача) + */ + protected boolean MoveLeft() { + return MoveTo(MovementDirection.Left); + } + + /** + * Вправо + * + * @return Результат перемещения (true - удалось переместиться, false -неудача) + */ + protected boolean MoveRight() { + return MoveTo(MovementDirection.Right); + } + + /** + * Наверх + * + * @return Результат перемещения (true - удалось переместиться, false -неудача) + */ + protected boolean MoveUp() { + return MoveTo(MovementDirection.Up); + } + + /** + * Вниз + * + * @return Результат перемещения (true - удалось переместиться, false -неудача) + */ + protected boolean MoveDown() { + return MoveTo(MovementDirection.Down); + } + + /** + * Параметры объекта + * + * @return + */ + protected ObjectParameters GetObjectParameters() { + return _moveableObject.GetObjectPosition(); + } + + /** + * Шаг объекта + * + * @return + */ + protected Integer GetStep() { + if (_state != StrategyStatus.InProgress) { + return null; + } + return _moveableObject.GetStep(); + } + + /** + * Перемещение к цели + */ + protected abstract void MoveToTarget(); + + /** + * Достигнута ли цель + */ + protected abstract boolean IsTargetDestinaion(); + + /** + * Попытка перемещения в требуемом направлении + * + * @return Результат попытки (true - удалось переместиться, false -неудача) + */ + private boolean MoveTo(MovementDirection movementDirection) { + if (_state != StrategyStatus.InProgress) { + return false; + } + return _moveableObject.TryMoveObject(movementDirection) && _moveableObject.TryMoveObject(movementDirection); + } +} diff --git a/AntiAirCraftGun/src/MovementStrategy/IMoveableObject.java b/AntiAirCraftGun/src/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..ac45186 --- /dev/null +++ b/AntiAirCraftGun/src/MovementStrategy/IMoveableObject.java @@ -0,0 +1,10 @@ +package MovementStrategy; + +/** + * Интерфейс для работы с перемещаемым объектом + */ +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean TryMoveObject(MovementDirection direction); +} diff --git a/AntiAirCraftGun/src/MovementStrategy/MoveToBorder.java b/AntiAirCraftGun/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..9563c09 --- /dev/null +++ b/AntiAirCraftGun/src/MovementStrategy/MoveToBorder.java @@ -0,0 +1,27 @@ +package MovementStrategy; + +public class MoveToBorder extends AbstractStrategy { + @Override + protected void MoveToTarget() { + ObjectParameters objectParameters = GetObjectParameters(); + if (objectParameters == null) { + return; + } + if (objectParameters.RightBorder() < FieldWidth()) { + MoveRight(); + } + if (objectParameters.DownBorder() < FieldHeight()) { + MoveDown(); + } + } + + @Override + protected boolean IsTargetDestinaion() { + ObjectParameters objectParameters = GetObjectParameters(); + if (objectParameters == null) { + return false; + } + return objectParameters.RightBorder() <= FieldWidth() && objectParameters.RightBorder() + GetStep() >= FieldWidth() && + objectParameters.DownBorder() <= FieldHeight() && objectParameters.DownBorder() + GetStep() >= FieldHeight(); + } +} diff --git a/AntiAirCraftGun/src/MovementStrategy/MoveToCenter.java b/AntiAirCraftGun/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..9b3caae --- /dev/null +++ b/AntiAirCraftGun/src/MovementStrategy/MoveToCenter.java @@ -0,0 +1,39 @@ +package MovementStrategy; + +public class MoveToCenter extends AbstractStrategy { + @Override + protected void MoveToTarget() { + ObjectParameters objectParameters = GetObjectParameters(); + if (objectParameters == null) { + return; + } + int diffx = objectParameters.ObjectMiddleHorizontal() - FieldWidth() / 2; + if (Math.abs(diffx) > GetStep()) { + if (diffx > 0) { + MoveLeft(); + } else { + MoveRight(); + } + } + int diffy = objectParameters.ObjectMiddleVertical() - FieldHeight() / 2; + if (Math.abs(diffx) > GetStep()) { + if (diffx > 0) { + MoveUp(); + } else { + MoveDown(); + } + } + } + + @Override + protected boolean IsTargetDestinaion() { + ObjectParameters objectParameters = GetObjectParameters(); + if (objectParameters == null) { + return false; + } + return objectParameters.ObjectMiddleHorizontal() - GetStep() <= FieldWidth() / 2 + && objectParameters.ObjectMiddleHorizontal() + GetStep() >= FieldWidth() / 2 + && objectParameters.ObjectMiddleVertical() - GetStep() <= FieldHeight() / 2 + && objectParameters.ObjectMiddleVertical() + GetStep() >= FieldHeight() / 2; + } +} diff --git a/AntiAirCraftGun/src/MovementStrategy/MoveableGun.java b/AntiAirCraftGun/src/MovementStrategy/MoveableGun.java new file mode 100644 index 0000000..20c6084 --- /dev/null +++ b/AntiAirCraftGun/src/MovementStrategy/MoveableGun.java @@ -0,0 +1,60 @@ +package MovementStrategy; + +import Drawings.DirectionType; +import Drawings.DrawingAntiAircraftGun; +import Drawings.DrawingGun; + +public class MoveableGun implements IMoveableObject{ + + private DrawingGun _drawingGun = null; + + public MoveableGun(DrawingGun drawingGun) { + this._drawingGun=drawingGun; + } + + @Override + public ObjectParameters GetObjectPosition() { + if(_drawingGun==null || _drawingGun.EntityGun()==null + || _drawingGun.get_startPosX()==null + ||_drawingGun.get_startPosY()==null){ + return null; + } + return new ObjectParameters( + _drawingGun.get_startPosX(), + _drawingGun.get_startPosY(), + _drawingGun.GetWidth(), + _drawingGun.GetHeight() + ); + } + + @Override + public int GetStep() { + return (int)(_drawingGun.EntityGun().getStep()!=null ? _drawingGun.EntityGun().getStep():0); + } + + @Override + public boolean TryMoveObject(MovementDirection direction) { + if(_drawingGun==null||_drawingGun.EntityGun()==null)return false; + return _drawingGun.MoveTransport(GetObjectType(direction)); + } + + private static DirectionType GetObjectType(MovementDirection direction){ + switch (direction){ + case Left -> { + return DirectionType.Left; + } + case Up -> { + return DirectionType.Up; + } + case Down -> { + return DirectionType.Down; + } + case Right -> { + return DirectionType.Right; + } + case null -> { + return DirectionType.Unknow; + } + } + } +} diff --git a/AntiAirCraftGun/src/MovementStrategy/MovementDirection.java b/AntiAirCraftGun/src/MovementStrategy/MovementDirection.java new file mode 100644 index 0000000..892c47a --- /dev/null +++ b/AntiAirCraftGun/src/MovementStrategy/MovementDirection.java @@ -0,0 +1,23 @@ +package MovementStrategy; + +/** + * Направление перемещения + */ +public enum MovementDirection { + /** + * Влево + */ + Left, + /** + * Вправо + */ + Right, + /** + * Вверх + */ + Up, + /** + * Вниз + */ + Down +} diff --git a/AntiAirCraftGun/src/MovementStrategy/ObjectParameters.java b/AntiAirCraftGun/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..4d32aa9 --- /dev/null +++ b/AntiAirCraftGun/src/MovementStrategy/ObjectParameters.java @@ -0,0 +1,69 @@ +package MovementStrategy; + +public class ObjectParameters { + /** + * Координата по х + */ + private final Integer _x; + /** + * Координат по y + */ + private final Integer _y; + /** + * Ширина объекта + */ + private final Integer _width; + /** + * Высоат объекта + */ + private final Integer _height; + + /** + * Левая граница + * @return - х + */ + public Integer LeftBorder(){return _x;} + + /** + * Правая граница + * @return - х + высота + */ + public Integer RightBorder() {return _x+_width;} + + /** + * Верхняя граница + * @return - у + */ + public Integer TopBorder(){return _y;} + + /** + * Нижняя граница + * @return - y + высота + */ + public Integer DownBorder(){return _y+_height;} + + /** + * Середина обеъкта + * @return - середину объекта по горизонтали + */ + public Integer ObjectMiddleHorizontal(){return _x+_width/2;} + /** + * Середина обеъкта + * @return - середину объекта по вертикали + */ + public Integer ObjectMiddleVertical(){return _y+_height/2;} + + /** + * Констурктор + * @param _x - x + * @param _y - y + * @param _width - ширина + * @param _height - высота + */ + public ObjectParameters(Integer _x, Integer _y, Integer _width, Integer _height) { + this._x = _x; + this._y = _y; + this._width = _width; + this._height = _height; + } +} diff --git a/AntiAirCraftGun/src/MovementStrategy/StrategyStatus.java b/AntiAirCraftGun/src/MovementStrategy/StrategyStatus.java new file mode 100644 index 0000000..a5e68bb --- /dev/null +++ b/AntiAirCraftGun/src/MovementStrategy/StrategyStatus.java @@ -0,0 +1,16 @@ +package MovementStrategy; + +public enum StrategyStatus { + /** + * Все готово к началу + */ + NotInit, + /** + * В процессе + */ + InProgress, + /** + * Завершено + */ + Finish +}