This commit is contained in:
Zakharov_Rostislav 2023-11-09 15:50:47 +04:00
parent 3124dd9292
commit be5dba9a9a
17 changed files with 532 additions and 140 deletions

1
.idea/.name Normal file
View File

@ -0,0 +1 @@
Main.java

View File

@ -4,97 +4,23 @@ import entities.EntityBattleship;
import java.awt.*;
public class DrawingBattleship {
private EntityBattleship entityBattleship;
public EntityBattleship getEntityBattleship() {
return entityBattleship;
}
private int pictureWidth;
private int pictureHeight;
private int startPosX;
private int startPosY;
private final int SHIP_WIDTH = 150;
public int getShipWidth() {return SHIP_WIDTH;}
private final int SHIP_HEIGHT = 50;
public int getShipHeight() {return SHIP_HEIGHT;}
private DrawingBlocks drawingBlocks;
public boolean init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean turret, boolean rocketLauncher, int width, int height, int blocksNumber) {
if (width < SHIP_WIDTH || height < SHIP_HEIGHT)
return false;
pictureWidth = width;
pictureHeight = height;
entityBattleship = new EntityBattleship();
entityBattleship.init(speed, weight, bodyColor, additionalColor, turret, rocketLauncher);
drawingBlocks = new DrawingBlocks();
drawingBlocks.setNumber(blocksNumber);
return true;
}
public void setPosition(int x, int y) {
if (x < 0 || y < 0 || x + SHIP_WIDTH > pictureWidth || y + SHIP_HEIGHT > pictureHeight) {
x = 0;
y = 0;
}
startPosX = x;
startPosY = y;
}
public void moveTransport(DirectionType direction) {
if (entityBattleship == null)
return;
switch (direction) {
//влево
case LEFT -> {
if (startPosX - entityBattleship.step.get().intValue() > 0)
startPosX -= entityBattleship.step.get().intValue();
}
//вверх
case UP -> {
if (startPosY - entityBattleship.step.get().intValue() > 0)
startPosY -= entityBattleship.step.get().intValue();
}
// вправо
case RIGHT -> {
if (startPosX + SHIP_WIDTH + entityBattleship.step.get().intValue() < pictureWidth)
startPosX += entityBattleship.step.get().intValue();
}
//вниз
case DOWN -> {
if (startPosY + SHIP_HEIGHT + entityBattleship.step.get().intValue() < pictureHeight)
startPosY += entityBattleship.step.get().intValue();
}
}
public class DrawingBattleship extends DrawingShip {
public DrawingBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean turret,
boolean rocketLauncher, int width, int height, int blocksType, int blocksNumber) {
super(speed, weight, bodyColor, width, height, blocksType, blocksNumber);
if (entityShip != null)
entityShip = new EntityBattleship(speed, weight, bodyColor, additionalColor, turret, rocketLauncher);
}
@Override
public void drawTransport(Graphics2D graphics2D) {
if (entityBattleship == null)
if (!(entityShip instanceof EntityBattleship))
return;
BasicStroke pen = new BasicStroke(2);
graphics2D.setStroke(pen);
Color bodyColor = entityBattleship.getBodyColor();
Color additionalColor = entityBattleship.getAdditionalColor();
//корпус
int[] hullX = new int[] {startPosX + 5, startPosX + 100, startPosX + 150, startPosX + 100, startPosX + 5};
int[] hullY = new int[] {startPosY, startPosY, startPosY + 25, startPosY + 50, startPosY + 50};
graphics2D.setPaint(bodyColor);
graphics2D.fillPolygon(hullX, hullY, 5);
graphics2D.setPaint(Color.BLACK);
graphics2D.drawPolygon(hullX, hullY, 5);
graphics2D.fillRect(startPosX, startPosY + 6, 5, 13);
graphics2D.fillRect(startPosX, startPosY + 31, 5, 13);
//надстройки
graphics2D.setPaint(Color.DARK_GRAY);
graphics2D.fillRect(startPosX + 40, startPosY + 20, 30, 10);
graphics2D.fillRect(startPosX + 70, startPosY + 12, 18, 26);
graphics2D.fillOval(startPosX + 94, startPosY + 19, 12, 12);
graphics2D.setPaint(Color.BLACK);
graphics2D.drawRect(startPosX + 40, startPosY + 20, 30, 10);
graphics2D.drawRect(startPosX + 70, startPosY + 12, 18, 26);
graphics2D.drawOval(startPosX + 94, startPosY + 19, 12, 12);
//блоки
if (drawingBlocks != null){
drawingBlocks.drawBlocks(graphics2D, startPosX, startPosY);
}
Color additionalColor = ((EntityBattleship)entityShip).getAdditionalColor();
super.drawTransport(graphics2D);
//орудийная башня
if (entityBattleship.getTurret()) {
if (((EntityBattleship)entityShip).getTurret()) {
int[] shieldX = new int[] {startPosX + 112, startPosX + 112, startPosX + 119, startPosX + 119, };
int[] shieldY = new int[] {startPosY + 19, startPosY + 31, startPosY + 28, startPosY + 22};
graphics2D.setPaint(additionalColor);
@ -105,7 +31,7 @@ public class DrawingBattleship {
graphics2D.drawRect(startPosX + 119, startPosY + 24, 12, 2);
}
//ячейки для ракет
if (entityBattleship.getRocketLauncher()) {
if (((EntityBattleship)entityShip).getRocketLauncher()) {
graphics2D.setPaint(additionalColor);
graphics2D.fillRect(startPosX + 14, startPosY + 14, 10, 10);
graphics2D.fillRect(startPosX + 26, startPosY + 14, 10, 10);

View File

@ -2,8 +2,9 @@ package drawing_objects;
import java.awt.*;
public class DrawingBlocks {
public class DrawingBlocks implements IDrawBlocks{
private BlocksNumber number;
@Override
public void setNumber(int x){
if(x <= 2)
number = BlocksNumber.TWO;
@ -12,6 +13,7 @@ public class DrawingBlocks {
if(x >= 6)
number = BlocksNumber.SIX;
}
@Override
public void drawBlocks(Graphics2D graphics2D, int _startX, int _startY){
graphics2D.fillRect(_startX+52, _startY+12, 6, 6);
graphics2D.fillRect(_startX+52, _startY+32, 6, 6);

View File

@ -0,0 +1,35 @@
package drawing_objects;
import java.awt.*;
public class DrawingCrossBlocks implements IDrawBlocks{
private BlocksNumber number;
@Override
public void setNumber(int x){
if(x <= 2)
number = BlocksNumber.TWO;
if(x == 4)
number = BlocksNumber.FOUR;
if(x >= 6)
number = BlocksNumber.SIX;
}
@Override
public void drawBlocks(Graphics2D graphics2D, int _startX, int _startY){
graphics2D.fillRect(_startX+54, _startY+12, 2, 6);
graphics2D.fillRect(_startX+52, _startY+14, 6, 2);
graphics2D.fillRect(_startX+54, _startY+32, 2, 6);
graphics2D.fillRect(_startX+52, _startY+34, 6, 2);
if (number == BlocksNumber.FOUR || number == BlocksNumber.SIX){
graphics2D.fillRect(_startX+64, _startY+12, 2, 6);
graphics2D.fillRect(_startX+62, _startY+14, 6, 2);
graphics2D.fillRect(_startX+64, _startY+32, 2, 6);
graphics2D.fillRect(_startX+62, _startY+34, 6, 2);
if (number == BlocksNumber.SIX){
graphics2D.fillRect(_startX+44, _startY+12, 2, 6);
graphics2D.fillRect(_startX+42, _startY+14, 6, 2);
graphics2D.fillRect(_startX+44, _startY+32, 2, 6);
graphics2D.fillRect(_startX+42, _startY+34, 6, 2);
}
}
}
}

View File

@ -0,0 +1,29 @@
package drawing_objects;
import java.awt.*;
public class DrawingRoundBlocks implements IDrawBlocks{
private BlocksNumber number;
@Override
public void setNumber(int x){
if(x <= 2)
number = BlocksNumber.TWO;
if(x == 4)
number = BlocksNumber.FOUR;
if(x >= 6)
number = BlocksNumber.SIX;
}
@Override
public void drawBlocks(Graphics2D graphics2D, int _startX, int _startY){
graphics2D.fillOval(_startX+50, _startY+11, 8, 8);
graphics2D.fillOval(_startX+50, _startY+31, 8, 8);
if (number == BlocksNumber.FOUR || number == BlocksNumber.SIX){
graphics2D.fillOval(_startX+60, _startY+11, 8, 8);
graphics2D.fillOval(_startX+60, _startY+31, 8, 8);
if (number == BlocksNumber.SIX){
graphics2D.fillOval(_startX+40, _startY+11, 8, 8);
graphics2D.fillOval(_startX+40, _startY+31, 8, 8);
}
}
}
}

View File

@ -0,0 +1,116 @@
package drawing_objects;
import entities.EntityShip;
import java.awt.*;
public class DrawingShip {
protected EntityShip entityShip;
public EntityShip getEntityShip(){return entityShip;}
private IDrawBlocks drawingBlocks;
private int pictureWidth;
private int pictureHeight;
protected int startPosX;
public int getPosX() {return startPosX;}
protected int startPosY;
public int getPosY() {return startPosY;}
private int shipWidth = 150;
public int getWidth() {return shipWidth;}
private int shipHeight = 50;
public int getHeight() {return shipHeight;}
public DrawingShip(int speed, double weight, Color bodyColor, int width, int height, int blocksType, int blocksNumber) {
if (width < shipWidth || height < shipHeight)
return;
pictureWidth = width;
pictureHeight = height;
entityShip = new EntityShip(speed, weight, bodyColor);
switch (blocksType) {
case 1 -> drawingBlocks = new DrawingRoundBlocks();
case 2 -> drawingBlocks = new DrawingCrossBlocks();
default -> drawingBlocks = new DrawingBlocks();
}
drawingBlocks.setNumber(blocksNumber);
}
protected DrawingShip(int speed, double weight, Color bodyColor, int width, int height, int shipWidth,
int shipHeight, int blocksType, int blocksNumber) {
if (width < shipWidth || height < shipHeight)
return;
pictureWidth = width;
pictureHeight = height;
this.shipWidth = shipWidth;
this.shipHeight = shipHeight;
entityShip = new EntityShip(speed, weight, bodyColor);
switch (blocksType) {
case 1 -> drawingBlocks = new DrawingRoundBlocks();
case 2 -> drawingBlocks = new DrawingCrossBlocks();
default -> drawingBlocks = new DrawingBlocks();
}
drawingBlocks.setNumber(blocksNumber);
}
public void setPosition(int x, int y) {
if (x < 0 || y < 0 || x + shipWidth > pictureWidth || y + shipHeight > pictureHeight) {
x = 0;
y = 0;
}
startPosX = x;
startPosY = y;
}
public void drawTransport(Graphics2D graphics2D) {
if (entityShip == null) {
return;
}
BasicStroke pen = new BasicStroke(2);
graphics2D.setStroke(pen);
Color bodyColor = entityShip.getBodyColor();
//корпус
int[] hullX = new int[] {startPosX + 5, startPosX + 100, startPosX + 150, startPosX + 100, startPosX + 5};
int[] hullY = new int[] {startPosY, startPosY, startPosY + 25, startPosY + 50, startPosY + 50};
graphics2D.setPaint(bodyColor);
graphics2D.fillPolygon(hullX, hullY, 5);
graphics2D.setPaint(Color.BLACK);
graphics2D.drawPolygon(hullX, hullY, 5);
graphics2D.fillRect(startPosX, startPosY + 6, 5, 13);
graphics2D.fillRect(startPosX, startPosY + 31, 5, 13);
//надстройки
graphics2D.setPaint(Color.DARK_GRAY);
graphics2D.fillRect(startPosX + 40, startPosY + 20, 30, 10);
graphics2D.fillRect(startPosX + 70, startPosY + 12, 18, 26);
graphics2D.fillOval(startPosX + 94, startPosY + 19, 12, 12);
graphics2D.setPaint(Color.BLACK);
graphics2D.drawRect(startPosX + 40, startPosY + 20, 30, 10);
graphics2D.drawRect(startPosX + 70, startPosY + 12, 18, 26);
graphics2D.drawOval(startPosX + 94, startPosY + 19, 12, 12);
//блоки
if (drawingBlocks != null){
drawingBlocks.drawBlocks(graphics2D, startPosX, startPosY);
}
}
public boolean canMove(DirectionType direction) {
if (entityShip == null) {
return false;
}
return switch (direction) {
case LEFT -> startPosX - entityShip.step.get().intValue() > 0;
case UP -> startPosY - entityShip.step.get().intValue() > 0;
case RIGHT -> startPosX + entityShip.step.get().intValue() + shipWidth < pictureWidth;
case DOWN -> startPosY + entityShip.step.get().intValue() + shipHeight < pictureHeight;
default -> false;
};
}
public void moveTransport(DirectionType direction) {
if (!canMove(direction) || entityShip == null)
{
return;
}
switch (direction) {
//влево
case LEFT -> startPosX -= entityShip.step.get().intValue();
//вверх
case UP -> startPosY -= entityShip.step.get().intValue();
// вправо
case RIGHT -> startPosX += entityShip.step.get().intValue();
//вниз
case DOWN -> startPosY += entityShip.step.get().intValue();
}
}
}

View File

@ -0,0 +1,8 @@
package drawing_objects;
import java.awt.*;
public interface IDrawBlocks {
void setNumber(int x);
void drawBlocks(Graphics2D graphics2D, int _startX, int _startY);
}

View File

@ -1,39 +1,18 @@
package entities;
import java.awt.*;
import java.util.function.Supplier;
public class EntityBattleship {
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 class EntityBattleship extends EntityShip {
private Color additionalColor;
public Color getAdditionalColor(){
return additionalColor;
}
private boolean turret;
public boolean getTurret() {
return turret;
}
public boolean getTurret() {return turret;}
private boolean rocketLauncher;
public boolean getRocketLauncher() {
return rocketLauncher;
}
public Supplier<Double> step = () -> (double) speed * 100 / weight;
public void init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean turret, boolean rocketLauncher) {
this.speed = speed;
this.weight = weight;
this.bodyColor = bodyColor;
public boolean getRocketLauncher() {return rocketLauncher;}
public EntityBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean turret, boolean rocketLauncher) {
super(speed, weight, bodyColor);
this.additionalColor = additionalColor;
this.turret = turret;
this.rocketLauncher = rocketLauncher;

View File

@ -0,0 +1,25 @@
package entities;
import java.awt.*;
import java.util.function.Supplier;
public class EntityShip{
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<Double> step = () -> (double) speed * 100 / weight;
public EntityShip(int speed, double weight, Color bodyColor){
this.speed = speed;
this.weight = weight;
this.bodyColor = bodyColor;
}
}

View File

@ -1,7 +1,13 @@
package frames;
import drawing_objects.DrawingBattleship;
import drawing_objects.DirectionType;
import drawing_objects.DrawingBattleship;
import drawing_objects.DrawingShip;
import movement_strategy.AbstractStrategy;
import movement_strategy.MoveToCenter;
import movement_strategy.MoveToBorder;
import movement_strategy.DrawingObjectShip;
import movement_strategy.Status;
import javax.imageio.ImageIO;
import javax.swing.*;
@ -12,23 +18,28 @@ import java.io.IOException;
import java.util.Random;
public class FrameBattleship extends JFrame {
private DrawingBattleship drawingBattleship;
private JComponent pictureBox;
private DrawingShip drawingShip;
private AbstractStrategy abstractStrategy;
private JComboBox<String> comboBoxStrategy;
private JComponent pictureBoxBattleship;
public FrameBattleship() throws IOException {
super("Линкор");
setSize(new Dimension(900,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//components initialisation
pictureBox = new JComponent(){
pictureBoxBattleship = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingBattleship != null) drawingBattleship.drawTransport(graphics2D);
if (drawingShip != null) drawingShip.drawTransport(graphics2D);
super.repaint();
}
};
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
JButton createButton = new JButton("Создать");
pictureBoxBattleship.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
comboBoxStrategy = new JComboBox<>(new String[]{"к центру", "к границе"});
JButton stepButton = new JButton("Шаг");
JButton createShipButton = new JButton("Создать корабль");
JButton createBattleshipButton = new JButton("Создать линкор");
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png"))));
rightButton.setPreferredSize(new Dimension(30,30));
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png"))));
@ -38,7 +49,9 @@ public class FrameBattleship extends JFrame {
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png"))));
downButton.setPreferredSize(new Dimension(30,30));
//ActionListeners and ActionCommand addition
createButton.addActionListener(e -> buttonCreateClick());
createShipButton.addActionListener(e -> buttonCreateShipClick());
createBattleshipButton.addActionListener(e -> buttonCreateBattleshipClick());
stepButton.addActionListener(e -> buttonStepClick());
rightButton.setActionCommand("right");
rightButton.addActionListener(this::buttonMoveClick);
leftButton.setActionCommand("left");
@ -49,14 +62,19 @@ public class FrameBattleship extends JFrame {
downButton.addActionListener(this::buttonMoveClick);
//panels and constraints initialisation
JPanel panelBattleship = new JPanel(new BorderLayout());
JPanel createPanel = new JPanel(new BorderLayout());
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
JPanel movementPanel = new JPanel(new GridBagLayout());
JPanel rightPanel = new JPanel(new BorderLayout());
JPanel leftPanel = new JPanel(new BorderLayout());
JPanel createPanel = new JPanel(new GridBagLayout());
JPanel movementPanel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
//addition to createPanel
createPanel.add(createButton, BorderLayout.SOUTH);
constraints.gridx = 0;
constraints.gridy = 0;
createPanel.add(createShipButton, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
createPanel.add(createBattleshipButton, constraints);
//addition to movementPanel
constraints.gridx = 2;
constraints.gridy = 1;
@ -70,40 +88,84 @@ public class FrameBattleship extends JFrame {
constraints.gridx = 1;
constraints.gridy = 1;
movementPanel.add(downButton, constraints);
//addition to 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
setLayout(new BorderLayout());
add(pictureBox);
add(pictureBoxBattleship);
rightPanel.add(movementPanel, BorderLayout.SOUTH);
rightPanel.add(stepPanel, BorderLayout.NORTH);
leftPanel.add(createPanel, BorderLayout.SOUTH);
panelBattleship.add(rightPanel, BorderLayout.EAST);
panelBattleship.add(createPanel, BorderLayout.WEST);
panelBattleship.add(leftPanel, BorderLayout.WEST);
add(panelBattleship,BorderLayout.CENTER);
setVisible(true);
}
private void buttonCreateClick() {
private void buttonCreateBattleshipClick() {
Random random = new Random();
drawingBattleship = new DrawingBattleship();
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingBattleship.init(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)+1)*2);
drawingBattleship.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
pictureBoxBattleship.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingShip = new DrawingBattleship(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(), pictureBoxBattleship.getWidth(), pictureBoxBattleship.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
drawingShip.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
draw();
}
private void buttonCreateShipClick(){
Random random = new Random();
pictureBoxBattleship.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingShip = new DrawingShip(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
pictureBoxBattleship.getWidth(), pictureBoxBattleship.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
drawingShip.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
draw();
}
private void buttonStepClick(){
if (drawingShip == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
switch (comboBoxStrategy.getSelectedIndex()) {
case 0 -> abstractStrategy = new MoveToCenter();
case 1 -> abstractStrategy = new MoveToBorder();
default -> abstractStrategy = null;
}
if (abstractStrategy == null) {
return;
}
abstractStrategy.setData(new DrawingObjectShip(drawingShip), pictureBoxBattleship.getWidth(), pictureBoxBattleship.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(drawingBattleship == null || drawingBattleship.getEntityBattleship() == null)
if(drawingShip == null || drawingShip.getEntityShip() == null)
return;
switch (event.getActionCommand()) {
case "left" -> drawingBattleship.moveTransport(DirectionType.LEFT);
case "right" -> drawingBattleship.moveTransport(DirectionType.RIGHT);
case "up" -> drawingBattleship.moveTransport(DirectionType.UP);
case "down" -> drawingBattleship.moveTransport(DirectionType.DOWN);
case "left" -> drawingShip.moveTransport(DirectionType.LEFT);
case "right" -> drawingShip.moveTransport(DirectionType.RIGHT);
case "up" -> drawingShip.moveTransport(DirectionType.UP);
case "down" -> drawingShip.moveTransport(DirectionType.DOWN);
}
draw();
}
private void draw() {
if (drawingBattleship == null)
if (drawingShip == null)
{
return;
}
pictureBox.repaint();
pictureBoxBattleship.repaint();
}
}

View File

@ -0,0 +1,64 @@
package movement_strategy;
import drawing_objects.DirectionType;
public abstract class AbstractStrategy {
private IMoveableObject moveableObject;
private Status state = Status.NOT_INIT;
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.NOT_INIT;
return;
}
state = Status.IN_PROGRESS;
this.moveableObject = moveableObject;
fieldWidth = width;
fieldHeight = height;
}
public void makeStep(){
if (state != Status.IN_PROGRESS) {
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 ObjectParameters getObjectParameters(){
if(moveableObject != null)
return moveableObject.getObjectPosition();
else return null;
}
protected Integer getStep() {
if (state != Status.IN_PROGRESS)
{
return null;
}
return moveableObject.getStep();
}
protected abstract void moveToTarget();
protected abstract boolean isTargetDestination();
private boolean moveTo(DirectionType directionType) {
if (state != Status.IN_PROGRESS)
{
return false;
}
if (moveableObject.checkCanMove(directionType))
{
moveableObject.moveObject(directionType);
return true;
}
return false;
}
}

View File

@ -0,0 +1,36 @@
package movement_strategy;
import drawing_objects.DrawingShip;
import drawing_objects.DirectionType;
public class DrawingObjectShip implements IMoveableObject{
private final DrawingShip drawingShip;
public DrawingObjectShip(DrawingShip drawingShip){
this.drawingShip = drawingShip;
}
@Override
public ObjectParameters getObjectPosition(){
if(drawingShip == null || drawingShip.getEntityShip() == null)
return null;
return new ObjectParameters(drawingShip.getPosX(), drawingShip.getPosY(),
drawingShip.getWidth(), drawingShip.getHeight());
}
@Override
public int getStep(){
if(drawingShip.getEntityShip() == null)
return 0;
return drawingShip.getEntityShip().step.get().intValue();
}
@Override
public boolean checkCanMove(DirectionType direction){
if(drawingShip == null)
return false;
return drawingShip.canMove(direction);
}
@Override
public void moveObject(DirectionType direction){
if(drawingShip == null)
return;
drawingShip.moveTransport(direction);
}
}

View File

@ -0,0 +1,10 @@
package movement_strategy;
import drawing_objects.DirectionType;
public interface IMoveableObject {
ObjectParameters getObjectPosition();
int getStep();
boolean checkCanMove(DirectionType direction);
void moveObject(DirectionType direction);
}

View File

@ -0,0 +1,32 @@
package movement_strategy;
public class MoveToBorder extends AbstractStrategy {
@Override
protected boolean isTargetDestination() {
var objParams = getObjectParameters();
if (objParams == null) {
return false;
}
return objParams.getRightBorder() + getStep() >= getFieldWidth() &&
objParams.getDownBorder() + getStep() >= getFieldHeight();
}
@Override
protected void moveToTarget() {
var objParams = getObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.getRightBorder() - getFieldWidth();
if (Math.abs(diffX) >= getStep()) {
if (diffX < 0) {
moveRight();
}
}
var diffY = objParams.getDownBorder() - getFieldHeight();
if (Math.abs(diffY) >= getStep()) {
if (diffY < 0) {
moveDown();
}
}
}
}

View File

@ -0,0 +1,39 @@
package movement_strategy;
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean isTargetDestination(){
var objParams = getObjectParameters();
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() {
ObjectParameters objParams = getObjectParameters();
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();
}
}
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,7 @@
package movement_strategy;
public enum Status {
NOT_INIT,
IN_PROGRESS,
FINISH
}