diff --git a/src/Main.java b/src/Main.java index b191374..9e1285e 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,5 @@ +import form.FrameAirBomber; + import java.io.IOException; public class Main { diff --git a/src/drawing_objects/DrawingAirBomber.java b/src/drawing_objects/DrawingAirBomber.java new file mode 100644 index 0000000..e484584 --- /dev/null +++ b/src/drawing_objects/DrawingAirBomber.java @@ -0,0 +1,54 @@ +package drawing_objects; + +import entities.*; + +import java.awt.*; + +public class DrawingAirBomber extends DrawingPlane { + + public DrawingAirBomber(int speed, double weight, Color bodyColor, + Color additionalColor, boolean bombs, boolean fuel, int width, int height, int enginesType, int enginesNumber){ + super(speed, weight, bodyColor, width, height, enginesType, enginesNumber); + if(entityPlane != null) + entityPlane = new EntityAirBomber(speed, weight, bodyColor, additionalColor,bombs, fuel); + } + + private DrawingEngines1 drawingEngines1; + public void drawTransport(Graphics2D g) + { + if (!(entityPlane instanceof EntityAirBomber)) + return; + EntityAirBomber entityAirBomber = (EntityAirBomber) entityPlane; + BasicStroke pen = new BasicStroke(2); + Color penColor = Color.BLACK; + Color additionalColor = entityAirBomber.getAdditionalColor(); + g.setStroke(pen); + super.drawTransport(g); + // топливо + if (entityAirBomber.getFuel()) + { + g.setColor(additionalColor); + g.fillOval(this.startPosX + 60, this.startPosY - 1, 40, 10); + g.fillOval(this.startPosX + 60, this.startPosY + 150, 40, 10); + g.setColor(penColor); + g.drawOval(startPosX + 60, startPosY - 1, 40, 10); + g.drawOval(startPosX + 60, startPosY + 150, 40, 10); + } + //бомбы + if (entityAirBomber.getBombs()) + { + int[] pointX = new int[]{startPosX+50, startPosX+70, startPosX+80, startPosX+90, startPosX+90, startPosX+80, startPosX+70, startPosX+50}; + int[] pointY = new int[]{startPosY+75, startPosY+75, startPosY+80, startPosY+75, startPosY+85, startPosY+80, startPosY+85, startPosY+85}; + g.setColor(additionalColor); + g.fillPolygon(pointX, pointY, 8); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 8); + pointX = new int[]{startPosX+100, startPosX+120, startPosX+130, startPosX+140, startPosX+140, startPosX+130, startPosX+120, startPosX+100}; + pointY = new int[]{startPosY+75, startPosY+75, startPosY+80, startPosY+75, startPosY+85, startPosY+80, startPosY+85, startPosY+85}; + g.setColor(additionalColor); + g.fillPolygon(pointX, pointY, 8); + g.setColor(penColor); + g.drawPolygon(pointX, pointY,8); + } + } +} \ No newline at end of file diff --git a/src/DrawingEngines.java b/src/drawing_objects/DrawingEngines.java similarity index 96% rename from src/DrawingEngines.java rename to src/drawing_objects/DrawingEngines.java index a92d056..24f5885 100644 --- a/src/DrawingEngines.java +++ b/src/drawing_objects/DrawingEngines.java @@ -1,3 +1,6 @@ +package drawing_objects; + +import enums.*; import java.awt.*; public class DrawingEngines { diff --git a/src/drawing_objects/DrawingEngines1.java b/src/drawing_objects/DrawingEngines1.java new file mode 100644 index 0000000..b23012a --- /dev/null +++ b/src/drawing_objects/DrawingEngines1.java @@ -0,0 +1,34 @@ +package drawing_objects; + +import enums.*; +import java.awt.*; + +public class DrawingEngines1 implements IDrawEngines { + private EngineNumber number; + public void setNumber(int x){ + if(x <= 2) + number = EngineNumber.TWO; + if(x == 4) + number = EngineNumber.FOUR; + if(x >= 6) + number = EngineNumber.SIX; + } + public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){ + graphics2D.fillRect(_startX+70, _startY+20, 20, 15); + graphics2D.fillOval(_startX+80, _startY+20, 20, 15); + graphics2D.fillRect(_startX+70, _startY+125, 20, 15); + graphics2D.fillOval(_startX+80, _startY+125, 20, 15); + if (number == EngineNumber.FOUR || number == EngineNumber.SIX){ + graphics2D.fillRect(_startX+70, _startY+40, 20, 15); + graphics2D.fillOval(_startX+80, _startY+40, 20, 15); + graphics2D.fillRect(_startX+70, _startY+105, 20, 15); + graphics2D.fillOval(_startX+80, _startY+105, 20, 15); + } + if (number == EngineNumber.SIX){ + graphics2D.fillRect(_startX+130, _startY+50, 25, 15); + graphics2D.fillOval(_startX+145, _startY+50, 20, 15); + graphics2D.fillRect(_startX+130, _startY+95, 25, 15); + graphics2D.fillOval(_startX+145, _startY+95, 20, 15); + } + } +} \ No newline at end of file diff --git a/src/drawing_objects/DrawingEngines2.java b/src/drawing_objects/DrawingEngines2.java new file mode 100644 index 0000000..4c1819f --- /dev/null +++ b/src/drawing_objects/DrawingEngines2.java @@ -0,0 +1,28 @@ +package drawing_objects; + +import enums.*; +import java.awt.*; + +public class DrawingEngines2 implements IDrawEngines{ + private EngineNumber number; + public void setNumber(int x){ + if(x <= 2) + number = EngineNumber.TWO; + if(x == 4) + number = EngineNumber.FOUR; + if(x >= 6) + number = EngineNumber.SIX; + } + public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){ + graphics2D.fillRect(_startX+70, _startY+20, 20, 15); + graphics2D.fillRect(_startX+70, _startY+125, 20, 15); + if (number == EngineNumber.FOUR || number == EngineNumber.SIX){ + graphics2D.fillRect(_startX+70, _startY+40, 20, 15); + graphics2D.fillRect(_startX+70, _startY+105, 20, 15); + } + if (number == EngineNumber.SIX){ + graphics2D.fillRect(_startX+130, _startY+50, 25, 15); + graphics2D.fillRect(_startX+130, _startY+95, 25, 15); + } + } +} \ No newline at end of file diff --git a/src/drawing_objects/DrawingEngines3.java b/src/drawing_objects/DrawingEngines3.java new file mode 100644 index 0000000..1f2b43c --- /dev/null +++ b/src/drawing_objects/DrawingEngines3.java @@ -0,0 +1,34 @@ +package drawing_objects; + +import enums.*; +import java.awt.*; + +public class DrawingEngines3 implements IDrawEngines { + private EngineNumber number; + public void setNumber(int x){ + if(x <= 2) + number = EngineNumber.TWO; + if(x == 4) + number = EngineNumber.FOUR; + if(x >= 6) + number = EngineNumber.SIX; + } + public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){ + graphics2D.fillRect(_startX+70, _startY+20, 20, 15); + graphics2D.fillOval(_startX+60, _startY+20, 20, 15); + graphics2D.fillRect(_startX+70, _startY+125, 20, 15); + graphics2D.fillOval(_startX+60, _startY+125, 20, 15); + if (number == EngineNumber.FOUR || number == EngineNumber.SIX){ + graphics2D.fillRect(_startX+70, _startY+40, 20, 15); + graphics2D.fillOval(_startX+60, _startY+40, 20, 15); + graphics2D.fillRect(_startX+70, _startY+105, 20, 15); + graphics2D.fillOval(_startX+60, _startY+105, 20, 15); + } + if (number == EngineNumber.SIX){ + graphics2D.fillRect(_startX+130, _startY+50, 25, 15); + graphics2D.fillOval(_startX+120, _startY+50, 20, 15); + graphics2D.fillRect(_startX+130, _startY+95, 25, 15); + graphics2D.fillOval(_startX+120, _startY+95, 20, 15); + } + } +} \ No newline at end of file diff --git a/src/drawing_objects/DrawingPlane.java b/src/drawing_objects/DrawingPlane.java new file mode 100644 index 0000000..ee665ea --- /dev/null +++ b/src/drawing_objects/DrawingPlane.java @@ -0,0 +1,141 @@ +package drawing_objects; + +import entities.EntityPlane; +import enums.*; +import javax.swing.*; +import java.awt.*; +import java.util.function.Supplier; + +public class DrawingPlane { + protected EntityPlane entityPlane; + protected void setEntityPlane(EntityPlane entityPlane){this.entityPlane = entityPlane;} + public EntityPlane getEntityPlane() { + return entityPlane; + } + private IDrawEngines drawingEngines; + private int pictureWidth; + private int pictureHeight; + protected int startPosX; + public int getPosX(){return startPosX;} + protected int startPosY; + public int getPosY(){return startPosY;} + private final int PLANE_WIDTH = 160; + public int getWidth(){return PLANE_WIDTH;} + private final int PLANE_HEIGHT = 160; + public int getHeight(){return PLANE_HEIGHT;} + public DrawingPlane(int speed, double weight, Color bodyColor, int width, int height, int enginesType, int enginesNumber) { + if (width < PLANE_WIDTH || height < PLANE_HEIGHT) + return; + pictureWidth = width; + pictureHeight = height; + entityPlane = new EntityPlane(speed, weight, bodyColor); + switch (enginesType){ + case 1: + drawingEngines = new DrawingEngines2(); + break; + case 2: + drawingEngines = new DrawingEngines3(); + break; + default: + drawingEngines = new DrawingEngines1(); + break; + } + drawingEngines.setNumber(enginesNumber); + } + public void setPosition(int x, int y) { + if (x < 0 || y < 0 || x + PLANE_WIDTH > pictureWidth || y + PLANE_HEIGHT > pictureHeight) + x = y = 0; + startPosX = x; + startPosY = y; + } + public void drawTransport(Graphics2D g) { + if (entityPlane == null) + return; + BasicStroke pen = new BasicStroke(2); + Color penColor = Color.BLACK; + Color bodyColor = entityPlane.getBodyColor(); + g.setStroke(pen); + g.setColor(bodyColor); + //фюзеляж + g.fillRect(startPosX + 20, startPosY + 70, 140, 20); + //кабина + int[] pointX = new int[]{startPosX, startPosX + 20, startPosX + 20}; + int[] pointY = new int[]{startPosY + 80, startPosY + 70, startPosY + 90}; + g.setColor(Color.BLUE); + g.fillPolygon(pointX, pointY, 3); + //границы самолета + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 3); + g.drawRect(startPosX + 20, startPosY + 70, 140, 20); + //Крылья + pointX = new int[]{startPosX + 70, startPosX + 70, startPosX + 90, startPosX + 100}; + pointY = new int[]{startPosY + 70, startPosY, startPosY, startPosY + 70}; + g.setColor(bodyColor); + g.fillPolygon(pointX, pointY, 4); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 4); + pointX = new int[]{startPosX + 70, startPosX + 70, startPosX + 90, startPosX + 100}; + pointY = new int[]{startPosY + 90, startPosY + 160, startPosY + 160, startPosY + 90}; + g.setColor(bodyColor); + g.fillPolygon(pointX, pointY, 4); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 4); + pointX = new int[]{startPosX + 130, startPosX + 130, startPosX + 160, startPosX + 160}; + pointY = new int[]{startPosY + 70, startPosY + 50, startPosY + 30, startPosY + 70}; + g.setColor(bodyColor); + g.fillPolygon(pointX, pointY, 4); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 4); + pointX = new int[]{startPosX + 130, startPosX + 130, startPosX + 160, startPosX + 160}; + pointY = new int[]{startPosY + 90, startPosY + 110, startPosY + 130, startPosY + 90}; + g.setColor(bodyColor); + g.fillPolygon(pointX, pointY, 4); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 4); + //двигатели + drawingEngines.drawEngines(g, startPosX, startPosY); + } + public boolean canMove(DirectionType direction) { + if (entityPlane == null) { + return false; + } + switch(direction) { + case LEFT: + return startPosX - entityPlane.step.get().intValue() > 0; + case UP: + return startPosY - entityPlane.step.get().intValue() > 0; + case RIGHT: + return startPosX + entityPlane.step.get().intValue() + PLANE_WIDTH < pictureWidth; + case DOWN: + return startPosY + entityPlane.step.get().intValue() + PLANE_WIDTH < pictureHeight; + default: + return false; + } + } + public void moveTransport(DirectionType direction) + { + if (!canMove(direction) || entityPlane == null) + { + return; + } + switch (direction) + { + //влево + case LEFT: + startPosX -= entityPlane.step.get().intValue(); + break; + //вверх + case UP: + startPosY -= entityPlane.step.get().intValue(); + break; + // вправо + case RIGHT: + startPosX += entityPlane.step.get().intValue(); + break; + //вниз + case DOWN: + startPosY += entityPlane.step.get().intValue(); + break; + } + } +} diff --git a/src/drawing_objects/IDrawEngines.java b/src/drawing_objects/IDrawEngines.java new file mode 100644 index 0000000..7f88167 --- /dev/null +++ b/src/drawing_objects/IDrawEngines.java @@ -0,0 +1,8 @@ +package drawing_objects; + +import java.awt.*; + +public interface IDrawEngines { + public void setNumber(int x); + public void drawEngines(Graphics2D graphics2D, int _startX, int _startY); +} diff --git a/src/entities/EntityAirBomber.java b/src/entities/EntityAirBomber.java new file mode 100644 index 0000000..11ead45 --- /dev/null +++ b/src/entities/EntityAirBomber.java @@ -0,0 +1,25 @@ +package entities; + +import java.awt.*; + +public class EntityAirBomber extends EntityPlane { + private Color additionalColor; + public Color getAdditionalColor(){ + return additionalColor; + } + private boolean isFuel; + public boolean getFuel() { + return isFuel; + } + private boolean isBombs; + public boolean getBombs() { + return isBombs; + } + public EntityAirBomber(int speed, double weight, Color bodyColor, Color + additionalColor, boolean isBombs, boolean isFuel) { + super(speed, weight, bodyColor); + this.additionalColor = additionalColor; + this.isFuel = isFuel; + this.isBombs = isBombs; + } +} \ No newline at end of file diff --git a/src/entities/EntityPlane.java b/src/entities/EntityPlane.java new file mode 100644 index 0000000..98da4a8 --- /dev/null +++ b/src/entities/EntityPlane.java @@ -0,0 +1,24 @@ +package entities; + +import java.awt.*; +import java.util.function.Supplier; +public class EntityPlane { + private int speed; + public int getSpeed() { + return speed; + } + private double weight; + public double getWeight() { + return weight; + } + private Color bodyColor; + public Color getBodyColor() { + return bodyColor; + } + public Supplier step = () -> (double) speed * 100 / weight; + public EntityPlane(int speed, double weight, Color bodyColor){ + this.speed = speed; + this.weight = weight; + this.bodyColor = bodyColor; + } +} diff --git a/src/DirectionType.java b/src/enums/DirectionType.java similarity index 79% rename from src/DirectionType.java rename to src/enums/DirectionType.java index 0d664bb..1f59378 100644 --- a/src/DirectionType.java +++ b/src/enums/DirectionType.java @@ -1,3 +1,5 @@ +package enums; + public enum DirectionType { UP, DOWN, diff --git a/src/EngineNumber.java b/src/enums/EngineNumber.java similarity index 76% rename from src/EngineNumber.java rename to src/enums/EngineNumber.java index dea68cb..0f5044f 100644 --- a/src/EngineNumber.java +++ b/src/enums/EngineNumber.java @@ -1,3 +1,5 @@ +package enums; + public enum EngineNumber { TWO, FOUR, diff --git a/src/enums/Status.java b/src/enums/Status.java new file mode 100644 index 0000000..e866117 --- /dev/null +++ b/src/enums/Status.java @@ -0,0 +1,7 @@ +package enums; + +public enum Status { + NOTINIT, + INPROGRESS, + FINISH +} diff --git a/src/form/FrameAirBomber.java b/src/form/FrameAirBomber.java new file mode 100644 index 0000000..4fc84dd --- /dev/null +++ b/src/form/FrameAirBomber.java @@ -0,0 +1,184 @@ +package form; + +import drawing_objects.DrawingAirBomber; +import drawing_objects.DrawingPlane; +import enums.*; +import movement_strategy.*; +import movement_strategy.DrawingObjectPlane; + +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.io.File; +import java.io.IOException; +import java.util.Random; +public class FrameAirBomber extends JFrame { + private DrawingPlane drawingPlane; + private AbstractStrategy abstractStrategy; + private JComboBox comboBoxStrategy; + private final JComponent pictureBox; + public FrameAirBomber() throws IOException { + super("Бомбардировщик"); + setSize(new Dimension(900,500)); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + //components initialisation + pictureBox = new JComponent(){ + public void paintComponent(Graphics graphics){ + super.paintComponent(graphics); + Graphics2D graphics2D = (Graphics2D) graphics; + if (drawingPlane != null) drawingPlane.drawTransport(graphics2D); + super.repaint(); + } + }; + comboBoxStrategy = new JComboBox<>(new String[]{"К центру", "К границе"}); + JButton stepButton = new JButton("Шаг"); + JButton createPlaneButton = new JButton("Создать самолет"); + JButton createAirBomberButton = new JButton("Создать бомбардировщик"); + JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png")))); + JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png")))); + JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png")))); + JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png")))); + pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight()); + //ActionListeners and ActionCommand addition + createPlaneButton.addActionListener(e -> buttonCreatePlaneClick()); + createAirBomberButton.addActionListener(e -> buttonCreateAirBomberClick()); + stepButton.addActionListener(e -> buttonStepClick()); + rightButton.setActionCommand("right"); + rightButton.addActionListener(this::buttonMoveClick); + leftButton.setActionCommand("left"); + leftButton.addActionListener(this::buttonMoveClick); + upButton.setActionCommand("up"); + upButton.addActionListener(this::buttonMoveClick); + downButton.setActionCommand("down"); + downButton.addActionListener(this::buttonMoveClick); + //component addition + setLayout(new BorderLayout()); + JPanel panelAirBomber = new JPanel(new BorderLayout()); + JPanel rightPanel = new JPanel(new BorderLayout()); + JPanel leftPanel = new JPanel(new BorderLayout()); + GridBagConstraints constraints = new GridBagConstraints(); + constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2; + //createPanel + JPanel createPanel = new JPanel(new GridBagLayout()); + constraints.gridx = 0; + constraints.gridy = 0; + createPanel.add(createPlaneButton, constraints); + constraints.gridx = 1; + constraints.gridy = 0; + createPanel.add(createAirBomberButton, constraints); + //movementPanel + JPanel movementPanel = new JPanel(new GridBagLayout()); + rightButton.setPreferredSize(new Dimension(30,30)); + constraints.gridx = 2; + constraints.gridy = 1; + movementPanel.add(rightButton, constraints); + leftButton.setPreferredSize(new Dimension(30,30)); + constraints.gridx = 0; + constraints.gridy = 1; + movementPanel.add(leftButton, constraints); + upButton.setPreferredSize(new Dimension(30,30)); + constraints.gridx = 1; + constraints.gridy = 0; + movementPanel.add(upButton, constraints); + downButton.setPreferredSize(new Dimension(30,30)); + constraints.gridx = 1; + constraints.gridy = 1; + movementPanel.add(downButton, constraints); + //stepPanel + JPanel stepPanel = new JPanel(new GridBagLayout()); + constraints.gridx = 0; + constraints.gridy = 0; + stepPanel.add(comboBoxStrategy, constraints); + constraints.gridx = 0; + constraints.gridy = 1; + stepPanel.add(stepButton, constraints); + //addition to frame + add(pictureBox); + rightPanel.add(movementPanel, BorderLayout.SOUTH); + rightPanel.add(stepPanel, BorderLayout.NORTH); + leftPanel.add(createPanel, BorderLayout.SOUTH); + panelAirBomber.add(rightPanel, BorderLayout.EAST); + panelAirBomber.add(leftPanel, BorderLayout.WEST); + add(panelAirBomber,BorderLayout.CENTER); + setVisible(true); + } + private void buttonCreateAirBomberClick() { + Random random = new Random(); + pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); + drawingPlane = new DrawingAirBomber(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2); + drawingPlane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + draw(); + } + private void buttonCreatePlaneClick(){ + Random random = new Random(); + pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); + drawingPlane = new DrawingPlane(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2); + drawingPlane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + draw(); + } + private void buttonStepClick(){ + if (drawingPlane == 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 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; + } + } + private void buttonMoveClick(ActionEvent event) { + if(drawingPlane == null || drawingPlane.getEntityPlane() == null) + return; + switch (event.getActionCommand()) + { + case "left": + drawingPlane.moveTransport(DirectionType.LEFT); + break; + case "right": + drawingPlane.moveTransport(DirectionType.RIGHT); + break; + case "up": + drawingPlane.moveTransport(DirectionType.UP); + break; + case "down": + drawingPlane.moveTransport(DirectionType.DOWN); + break; + } + draw(); + } + private void draw() { + if (drawingPlane == null) + return; + pictureBox.repaint(); + } +} \ No newline at end of file diff --git a/src/movement_strategy/AbstractStrategy.java b/src/movement_strategy/AbstractStrategy.java new file mode 100644 index 0000000..82ae35f --- /dev/null +++ b/src/movement_strategy/AbstractStrategy.java @@ -0,0 +1,60 @@ +package movement_strategy; +import enums.DirectionType; +import enums.Status; + +import java.util.function.Supplier; +public abstract class AbstractStrategy { + private IMoveableObject moveableObject; + private Status state = Status.NOTINIT; + private int FieldWidth; + protected int getFieldWidth(){return FieldWidth;} + private int FieldHeight; + protected int getFieldHeight(){return 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; + this.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(DirectionType.LEFT);} + protected boolean MoveRight() {return MoveTo(DirectionType.RIGHT);} + protected boolean MoveUp() {return MoveTo(DirectionType.UP);} + protected boolean MoveDown() {return MoveTo(DirectionType.DOWN);} + protected Supplier getObjectParameters = () -> moveableObject.getObjectPosition(); + + protected Integer GetStep(){ + if(state != Status.INPROGRESS) + return null; + return moveableObject.getStep(); + } + + protected abstract void MoveToTarget(); + protected abstract boolean IsTargetDestination(); + + private boolean MoveTo(DirectionType direction){ + if(state != Status.INPROGRESS) + return false; + if(moveableObject.checkCanMove(direction)){ + moveableObject.moveObject(direction); + return true; + } + return false; + } +} diff --git a/src/movement_strategy/DrawingObjectPlane.java b/src/movement_strategy/DrawingObjectPlane.java new file mode 100644 index 0000000..3bbb181 --- /dev/null +++ b/src/movement_strategy/DrawingObjectPlane.java @@ -0,0 +1,30 @@ +package movement_strategy; + +import drawing_objects.DrawingPlane; +import enums.DirectionType; + +public class DrawingObjectPlane implements IMoveableObject{ + private final DrawingPlane drawingPlane; + public DrawingObjectPlane(DrawingPlane drawingPlane){ + this.drawingPlane = drawingPlane; + } + public ObjectParameters getObjectPosition(){ + if(drawingPlane == null || drawingPlane.getEntityPlane() == null) + return null; + return new ObjectParameters(drawingPlane.getPosX(), drawingPlane.getPosY(), + drawingPlane.getWidth(), drawingPlane.getHeight()); + } + public int getStep(){ + if(drawingPlane.getEntityPlane() == null) + return 0; + return drawingPlane.getEntityPlane().step.get().intValue(); + } + public boolean checkCanMove(DirectionType direction){ + if(drawingPlane == null) + return false; + return drawingPlane.canMove(direction); + } + public void moveObject(DirectionType direction){ + drawingPlane.moveTransport(direction); + } +} diff --git a/src/movement_strategy/IMoveableObject.java b/src/movement_strategy/IMoveableObject.java new file mode 100644 index 0000000..be21313 --- /dev/null +++ b/src/movement_strategy/IMoveableObject.java @@ -0,0 +1,8 @@ +package movement_strategy; +import enums.DirectionType; +public interface IMoveableObject { + ObjectParameters getObjectPosition(); + int getStep(); + boolean checkCanMove(DirectionType direction); + void moveObject(DirectionType direction); +} diff --git a/src/movement_strategy/MoveToBorder.java b/src/movement_strategy/MoveToBorder.java new file mode 100644 index 0000000..c9d2614 --- /dev/null +++ b/src/movement_strategy/MoveToBorder.java @@ -0,0 +1,24 @@ +package movement_strategy; + +public class MoveToBorder extends AbstractStrategy{ + @Override + protected boolean IsTargetDestination() { + var objParams = getObjectParameters.get(); + if(objParams == null) + return false; + return objParams.getRightBorder() + GetStep() >= getFieldWidth() && + objParams.getDownBorder() + GetStep() >= getFieldHeight(); + } + @Override + protected void MoveToTarget() { + var objParams = getObjectParameters.get(); + if(objParams == null) + return; + var diffX = objParams.getRightBorder() - getFieldWidth(); + if (Math.abs(diffX) > GetStep()) + MoveRight(); + var diffY = objParams.getDownBorder() - getFieldHeight(); + if (Math.abs(diffY) > GetStep()) + MoveDown(); + } +} diff --git a/src/movement_strategy/MoveToCenter.java b/src/movement_strategy/MoveToCenter.java new file mode 100644 index 0000000..9ea8105 --- /dev/null +++ b/src/movement_strategy/MoveToCenter.java @@ -0,0 +1,35 @@ +package movement_strategy; + +public class MoveToCenter extends AbstractStrategy{ + @Override + protected boolean IsTargetDestination() { + var objParams = getObjectParameters.get(); + if(objParams == null) + return false; + + return objParams.getObjectMiddleHorizontal() <= getFieldWidth() / 2 && + objParams.getObjectMiddleHorizontal()+GetStep() >= getFieldWidth() / 2 && + objParams.getObjectMiddleVertical() <= getFieldHeight() / 2 && + objParams.getObjectMiddleVertical() + GetStep() >= getFieldHeight() / 2; + } + @Override + protected void MoveToTarget() { + var objParams = getObjectParameters.get(); + if(objParams == null) + return; + var diffX = objParams.getObjectMiddleHorizontal() - getFieldWidth() / 2; + if(Math.abs(diffX) > GetStep()){ + if(diffX > 0) + MoveLeft(); + else + MoveRight(); + } + var diffY = objParams.getObjectMiddleVertical() - getFieldHeight() / 2; + if(Math.abs(diffY) > GetStep()){ + if(diffY > 0) + MoveUp(); + else + MoveDown(); + } + } +} diff --git a/src/movement_strategy/ObjectParameters.java b/src/movement_strategy/ObjectParameters.java new file mode 100644 index 0000000..5bc3410 --- /dev/null +++ b/src/movement_strategy/ObjectParameters.java @@ -0,0 +1,21 @@ +package movement_strategy; + +public class ObjectParameters { + private final int POS_X; + private final int POS_Y; + private final int WIDTH; + private final int HEIGHT; + public int getLeftBorder() {return POS_X;} + public int getTopBorder() {return POS_Y;} + public int getRightBorder() {return POS_X + WIDTH;} + public int getDownBorder() {return POS_Y + HEIGHT;} + public int getObjectMiddleHorizontal() {return POS_X + this.WIDTH / 2;} + public int getObjectMiddleVertical() {return POS_Y + this.HEIGHT / 2;} + public ObjectParameters(int x, int y, int width, int height) + { + POS_X = x; + POS_Y = y; + WIDTH = width; + HEIGHT = height; + } +}