Всё с базы

This commit is contained in:
GokaPek 2023-11-19 17:18:17 +04:00
parent ecb63e6e2c
commit 7e0d0d42b1
14 changed files with 397 additions and 285 deletions

View File

@ -1,6 +1,5 @@
package SelfPropelledArtilleryUnit.DrawningObjects;
import SelfPropelledArtilleryUnit.NumbeRollers;
import SelfPropelledArtilleryUnit.SelfPropelledArtilleryUnit.Entities;
import java.awt.Color;
import java.awt.Graphics2D;

View File

@ -4,8 +4,7 @@ import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import SelfPropelledArtilleryUnit.DirectionType;
import SelfPropelledArtilleryUnit.EntitySPAU;
import SelfPropelledArtilleryUnit.SelfPropelledArtilleryUnit.Entities;
import SelfPropelledArtilleryUnit.Entities.EntitySPAU;
public class DrawningSPAU {
@ -15,14 +14,18 @@ public class DrawningSPAU {
protected int startPosX;
protected int startPosY;
private int _numbeRollers;
private final int carWidth = 135;
private final int carHeight = 75;
private int carWidth = 135;
private int carHeight = 75;
protected void setEntitySPAU(EntitySPAU entitySPAU) {
this.EntitySPAU = entitySPAU;
}
public DrawningSPAU(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, int width, int height)
public EntitySPAU getEntitySPAU() {
return EntitySPAU;
}
public DrawningSPAU(int _numbeRollers, int speed, double weight, Color bodyColor, int width, int height)
{
this._numbeRollers = _numbeRollers;
this.pictureWidth = width;
@ -35,10 +38,10 @@ public class DrawningSPAU {
{
return;
}
EntitySPAU = new EntitySPAU(_numbeRollers, speed, weight, bodyColor, additionalColor, bodyKit);
EntitySPAU = new EntitySPAU(speed, weight, bodyColor);
}
public DrawningSPAU(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, int width, int height, int carWidth, int carHeight)
public DrawningSPAU(int _numbeRollers, int speed, double weight, Color bodyColor, int width, int height, int carWidth, int carHeight)
{
this._numbeRollers = _numbeRollers;
this.pictureWidth = width;
@ -53,7 +56,7 @@ public class DrawningSPAU {
{
return;
}
EntitySPAU = new EntitySPAU(_numbeRollers, speed, weight, bodyColor, additionalColor, bodyKit);
EntitySPAU = new EntitySPAU(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
@ -79,19 +82,19 @@ public class DrawningSPAU {
{
//влево
case Left:
this.startPosX -= (int)EntitySPAU.Step;
this.startPosX -= (int)EntitySPAU.getStep();
break;
//вверх
case Up:
this.startPosY -= (int)EntitySPAU.Step;
this.startPosY -= (int)EntitySPAU.getStep();
break;
// вправо
case Right:
this.startPosX += (int)EntitySPAU.Step;
this.startPosX += (int)EntitySPAU.getStep();
break;
//вниз
case Down:
this.startPosY += (int)EntitySPAU.Step;
this.startPosY += (int)EntitySPAU.getStep();
break;
}
}
@ -104,10 +107,7 @@ public class DrawningSPAU {
Graphics2D g2d = (Graphics2D) g;
BasicStroke penBlack = new BasicStroke(1);
//Color additionalColor = EntitySPAU.AdditionalColor;
Color bodyColor = EntitySPAU.BodyColor;
boolean hasBodyKit = EntitySPAU.BodyKit;
Path2D.Double path = new Path2D.Double();
//гусеницы
@ -192,7 +192,7 @@ public class DrawningSPAU {
* @param direction Направление
* @return true - можно переместиться в указанном направлении
*/
public boolean canMove(DirectionType direction) {
public boolean CanMove(DirectionType direction) {
if (EntitySPAU == null) {
return false;
}
@ -202,9 +202,9 @@ public class DrawningSPAU {
case Up: // вверх
return startPosY - EntitySPAU.getStep() > 0;
case Right: // вправо
return startPosX + EntitySPAU.getStep() < _pictureWidth - _carWidth;
return startPosX + EntitySPAU.getStep() < pictureWidth - carWidth;
case Down: // вниз
return startPosY + EntitySPAU.getStep() < _pictureHeight - _carHeight;
return startPosY + EntitySPAU.getStep() < pictureHeight - carHeight;
default:
return false;
}

View File

@ -1,232 +1,55 @@
package SelfPropelledArtilleryUnit.DrawningObjects;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import SelfPropelledArtilleryUnit.DirectionType;
import SelfPropelledArtilleryUnit.EntitySPAU;
import SelfPropelledArtilleryUnit.SelfPropelledArtilleryUnit.Entities;
import SelfPropelledArtilleryUnit.Entities.EntitySPAUchild;
public class DrawningSPAUchild extends DrawningSPAU {
private EntitySPAUchild EntitySPAU;
public class DrawningSPAUchild {
private EntitySPAU EntitySPAU;
private int pictureWidth;
private int pictureHeight;
protected int startPosX;
protected int startPosY;
private int _numbeRollers;
private final int carWidth = 135;
private final int carHeight = 75;
protected void setEntitySPAU(EntitySPAU entitySPAU) {
protected void setEntitySPAU(EntitySPAUchild entitySPAU) {
this.EntitySPAU = entitySPAU;
}
public DrawningSPAUchild(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, int width, int height)
{
this._numbeRollers = _numbeRollers;
this.pictureWidth = width;
this.pictureHeight = height;
if (this.carHeight >= height)
{
return;
}
if (this.carWidth >= width)
{
return;
}
EntitySPAU = new EntitySPAU(_numbeRollers, speed, weight, bodyColor, additionalColor, bodyKit);
public DrawningSPAUchild(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean ballone, int width, int height) {
super(_numbeRollers, speed, weight, bodyColor, width, height);
EntitySPAU = new EntitySPAUchild(speed, weight, bodyColor, additionalColor, bodyKit, ballone);
}
public DrawningSPAU(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, int width, int height, int carWidth, int carHeight)
{
this._numbeRollers = _numbeRollers;
this.pictureWidth = width;
this.pictureHeight = height;
this.carWidth = carWidth;
this.carHeight = carHeight;
if (this.carHeight >= height)
{
return;
}
if (this.carWidth >= width)
{
return;
}
EntitySPAU = new EntitySPAU(_numbeRollers, speed, weight, bodyColor, additionalColor, bodyKit);
}
@Override
public void DrawTransport(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
public void SetPosition(int x, int y)
{
if (x < 0 || x > this.pictureWidth - this.carWidth)
{
x = 0;
}
if (y < 0 || y > this.pictureHeight - this.carHeight)
{
y = 0;
}
this.startPosX = x;
this.startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntitySPAU == null)
{
return;
}
switch (direction)
{
//влево
case Left:
this.startPosX -= (int)EntitySPAU.Step;
break;
//вверх
case Up:
this.startPosY -= (int)EntitySPAU.Step;
break;
// вправо
case Right:
this.startPosX += (int)EntitySPAU.Step;
break;
//вниз
case Down:
this.startPosY += (int)EntitySPAU.Step;
break;
}
}
BasicStroke penBlack = new BasicStroke(1);
Color additionalColor = EntitySPAU.AdditionalColor;
boolean hasBodyKit = EntitySPAU.BodyKit;
public void DrawTransport(Graphics g) {
if (EntitySPAU == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
BasicStroke penBlack = new BasicStroke(1);
//Color additionalColor = EntitySPAU.AdditionalColor;
Color bodyColor = EntitySPAU.BodyColor;
boolean hasBodyKit = EntitySPAU.BodyKit;
Path2D.Double path = new Path2D.Double();
// Обвесы
if (hasBodyKit) {
path.moveTo(startPosX + 15, startPosY + 20);
path.lineTo(startPosX + 35, startPosY + 20);
path.lineTo(startPosX + 35, startPosY + 60);
path.lineTo(startPosX + 15, startPosY + 60);
path.closePath();
g2d.setPaint(additionalColor);
g2d.fill(path);
g2d.setPaint(Color.BLACK);
g2d.setStroke(penBlack);
g2d.draw(path);
path.reset();
Line2D line = new Line2D.Double(startPosX + 5, startPosY + 20, startPosX + 15, startPosY + 25);
g2d.draw(line);
}
//гусеницы
DrawningRollers Rollers = new DrawningRollers(g2d, startPosX, startPosY, bodyColor, _numbeRollers);
Rollers.Draw();
// пушка
path.moveTo(startPosX + 35, startPosY + 40);
path.lineTo(startPosX + 40, startPosY + 45);
path.lineTo(startPosX + 135, startPosY + 5);
path.lineTo(startPosX + 130, startPosY + 0);
Path2D.Double path = new Path2D.Double();
// Обвесы
if (hasBodyKit) {
path.moveTo(startPosX + 15, startPosY + 20);
path.lineTo(startPosX + 35, startPosY + 20);
path.lineTo(startPosX + 35, startPosY + 60);
path.lineTo(startPosX + 15, startPosY + 60);
path.closePath();
g2d.setPaint(bodyColor);
g2d.setPaint(additionalColor);
g2d.fill(path);
g2d.setPaint(Color.BLACK);
g2d.setStroke(penBlack);
g2d.draw(path);
path.reset();
// корпус
path.moveTo(startPosX, startPosY + 60);
path.lineTo(startPosX + 10, startPosY + 30);
path.lineTo(startPosX + 130, startPosY + 30);
path.lineTo(startPosX + 135, startPosY + 60);
path.closePath();
g2d.setPaint(bodyColor);
g2d.fill(path);
g2d.draw(path);
path.reset();
// башня
path.moveTo(startPosX + 40, startPosY + 30);
path.lineTo(startPosX + 45, startPosY + 15);
path.lineTo(startPosX + 70, startPosY + 15);
path.lineTo(startPosX + 75, startPosY + 30);
path.closePath();
g2d.setPaint(bodyColor);
g2d.fill(path);
g2d.draw(path);
}
/**
* Координата X объекта
*
* @return значение координаты X
*/
public int getPosX() {
return this.startPosX;
}
Line2D line = new Line2D.Double(startPosX + 5, startPosY + 20, startPosX + 15, startPosY + 25);
g2d.draw(line);
/**
* Координата Y объекта
*
* @return значение координаты Y
*/
public int getPosY() {
return this.startPosY;
}
/**
* Ширина объекта
*
* @return значение ширины
*/
public int getWidth() {
return this.carWidth;
}
/**
* Высота объекта
*
* @return значение высоты
*/
public int getHeight() {
return this.carHeight;
}
/**
* Проверка, что объект может переместиться в указанном направлении
*
* @param direction Направление
* @return true - можно переместиться в указанном направлении
*/
public boolean canMove(DirectionType direction) {
if (EntitySPAU == null) {
return false;
}
switch (direction) {
case Left: // влево
return startPosX - EntitySPAU.getStep() > 0;
case Up: // вверх
return startPosY - EntitySPAU.getStep() > 0;
case Right: // вправо
return startPosX + EntitySPAU.getStep() < _pictureWidth - _carWidth;
case Down: // вниз
return startPosY + EntitySPAU.getStep() < _pictureHeight - _carHeight;
default:
return false;
}
super.DrawTransport(g);
}
}
}

View File

@ -0,0 +1,19 @@
package SelfPropelledArtilleryUnit.Entities;
import java.awt.Color;
public class EntitySPAU {
public int Speed;
public double Weight;
public Color BodyColor;
public EntitySPAU(int speed, double weight, Color bodyColor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
public double getStep() {
return (double) Speed * 100 / Weight;
}
}

View File

@ -0,0 +1,16 @@
package SelfPropelledArtilleryUnit.Entities;
import java.awt.Color;
public class EntitySPAUchild extends EntitySPAU{
public Color AdditionalColor;
public boolean BodyKit;
public boolean Ballone;
public EntitySPAUchild(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean ballone) {
super(speed, weight, bodyColor); // Вызов конструктора суперкласса
AdditionalColor = additionalColor;
BodyKit = bodyKit;
Ballone = ballone;
}
}

View File

@ -1,25 +0,0 @@
package SelfPropelledArtilleryUnit;
import java.awt.Color;
public class EntitySPAU {
public int Speed;
public double Weight;
public Color BodyColor;
public Color AdditionalColor;
public boolean BodyKit;
public boolean Wing;
public boolean SportLine;
public double Step = (double)Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine) {
Speed = speed;
Weight = weight;
Step = (double)Speed * 100 / Weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
BodyKit = bodyKit;
Wing = wing;
SportLine = sportLine;
}
}

View File

@ -1,6 +1,14 @@
package SelfPropelledArtilleryUnit;
import javax.swing.*;
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAU;
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAUchild;
import SelfPropelledArtilleryUnit.MovementStrategy.AbstractStrategy;
import SelfPropelledArtilleryUnit.MovementStrategy.DrawningObjectSPAU;
import SelfPropelledArtilleryUnit.MovementStrategy.MoveToBorder;
import SelfPropelledArtilleryUnit.MovementStrategy.MoveToCenter;
import SelfPropelledArtilleryUnit.MovementStrategy.Status;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@ -11,12 +19,16 @@ public class Form extends JFrame {
private JPanel mainPanel;
private JButton buttonCreate;
private JButton buttonCreateParent;
private JButton buttonStep;
private JComboBox<String> comboBoxStrategy = new JComboBox<>();
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
private JLabel pictureBoxSPAU;
private DrawningSPAU drawingSPAU;
private DrawningSPAU drawningSPAU;
private AbstractStrategy abstractStrategy;
private JTextField wheelsTextField;
private JLabel wheelsLabel;
@ -25,19 +37,20 @@ public class Form extends JFrame {
}
private void Draw() {
if (drawingSPAU == null) {
if (drawningSPAU == null) {
return;
}
BufferedImage bmp = new BufferedImage(pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bmp.createGraphics();
drawingSPAU.DrawTransport(gr);
drawningSPAU.DrawTransport(gr);
ImageIcon imageIcon = new ImageIcon(bmp);
pictureBoxSPAU.setIcon(imageIcon);
}
private void initComponents() {
mainPanel = new JPanel();
buttonCreate = new JButton("Создать");
buttonCreate = new JButton("Создать c залповой установкой");
buttonCreateParent = new JButton("Создать базовый");
buttonUp = new JButton(new ImageIcon("resources/upper_arrow.png"));
buttonDown = new JButton(new ImageIcon("resources/down_arrow.png"));
@ -60,7 +73,8 @@ public class Form extends JFrame {
wheelsTextField.setBounds(150, 410, 80, 29);
mainPanel.add(wheelsTextField);
buttonCreate.setBounds(250, 410, 100, 29);
// buttonCreate
buttonCreate.setBounds(250, 410, 250, 29);
mainPanel.add(buttonCreate);
buttonCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
@ -68,6 +82,31 @@ public class Form extends JFrame {
}
});
// buttonCreateParent
buttonCreateParent.setBounds(500, 409, 150, 30);
mainPanel.add(buttonCreateParent);
buttonCreateParent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonCreateParent_Click(e);
}
});
// buttonStep
buttonStep = new JButton("Шаг");
mainPanel.add(buttonStep);
buttonStep.setBounds(794, 55, 76, 30);
buttonStep.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonStep_Click(e);
}
});
// comboBoxStrategy
comboBoxStrategy = new JComboBox<>();
mainPanel.add(comboBoxStrategy);
comboBoxStrategy.setModel(new DefaultComboBoxModel<>(new String[] { "0", "1" }));
comboBoxStrategy.setBounds(767, 12, 103, 28);
buttonUp.setBounds(804, 336, 30, 30);
mainPanel.add(buttonUp);
ImageIcon iconUp = new ImageIcon("Arrows/upper-arrow.png");
@ -127,38 +166,87 @@ public class Form extends JFrame {
if (wheelCount > 0) {
Random random = new Random();
drawingSPAU = new DrawningSPAU();
drawingSPAU.Init(wheelCount, 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(),
random.nextBoolean(),
pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
drawingSPAU.SetPosition(random.nextInt(90) + 10,
random.nextInt(90) + 10);
drawningSPAU = new DrawningSPAUchild(wheelCount, random.nextInt(200) + 100,
(double) 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)),
true, true,
pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
drawningSPAU.SetPosition(random.nextInt(90) + 20, random.nextInt(90) + 20);
Draw();
}
}
protected void buttonCreateParent_Click(ActionEvent e) {
int wheelCount;
try {
wheelCount = Integer.parseInt(wheelsTextField.getText());
} catch (NumberFormatException ex) {
wheelCount = 0;
}
if (wheelCount > 0) {
Random random = new Random();
drawningSPAU = new DrawningSPAU(wheelCount, random.nextInt(200) + 100,
(double) random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
drawningSPAU.SetPosition(random.nextInt(90) + 20, random.nextInt(90) + 20);
Draw();
}
}
protected void buttonStep_Click (ActionEvent e) {
if (drawningSPAU == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
int selectedIndex = comboBoxStrategy.getSelectedIndex();
switch (selectedIndex) {
case 0:
abstractStrategy = new MoveToCenter();
break;
case 1:
abstractStrategy = new MoveToBorder();
break;
default:
abstractStrategy = null;
break;
}
if (abstractStrategy == null) {
return;
}
abstractStrategy.setData(new DrawningObjectSPAU(drawningSPAU), pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
comboBoxStrategy.setEnabled(false);
}
if (abstractStrategy == null) {
return;
}
abstractStrategy.makeStep();
Draw();
if (abstractStrategy.getStatus() == Status.Finish) {
comboBoxStrategy.setEnabled(true);
abstractStrategy = null;
}
}
protected void buttonMoveActionPerformed(Object sender, ActionEvent e) {
if (drawingSPAU == null) {
if (drawningSPAU == null) {
return;
}
String name = (sender instanceof JButton) ? ((JButton) sender).getName() : "";
switch (name) {
case "buttonUp":
drawingSPAU.MoveTransport(DirectionType.Up);
drawningSPAU.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
drawingSPAU.MoveTransport(DirectionType.Down);
drawningSPAU.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
drawingSPAU.MoveTransport(DirectionType.Left);
drawningSPAU.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
drawingSPAU.MoveTransport(DirectionType.Right);
drawningSPAU.MoveTransport(DirectionType.Right);
break;
}
Draw();

View File

@ -1,5 +1,7 @@
package SelfPropelledArtilleryUnit.MovementStrategy;
import SelfPropelledArtilleryUnit.DirectionType;
/**
* Класс-стратегия перемещения объекта
*/

View File

@ -4,7 +4,8 @@ import SelfPropelledArtilleryUnit.DirectionType;
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAU;
/**
* Класс-стратегия перемещения объекта, реализация интерфейса IMoveableObject для работы с объектом DrawningCar (паттерн Adapter)
* Класс-стратегия перемещения объекта, реализация интерфейса IMoveableObject
* для работы с объектом DrawningCar (паттерн Adapter)
*/
public class DrawningObjectSPAU implements IMoveableObject {
@ -13,33 +14,34 @@ public class DrawningObjectSPAU implements IMoveableObject {
public DrawningObjectSPAU(DrawningSPAU drawningCar) {
this.drawningCar = drawningCar;
}
@Override
public ObjectParameters getObjectPosition() {
if (drawningCar == null || drawningCar.getEntitySPAU() == null) {
return null;
}
return new ObjectParameters(drawningCar.getPosX(),
drawningCar.getPosY(),
drawningCar.getWidth(),
drawningCar.getHeight());
drawningCar.getPosY(),
drawningCar.getWidth(),
drawningCar.getHeight());
}
@Override
public int getStep() {
return drawningCar != null && drawningCar.getEntitySPAU() != null
? (int) drawningCar.getEntitySPAU().getStep() : 0;
return drawningCar != null && drawningCar.getEntitySPAU() != null
? (int) drawningCar.getEntitySPAU().getStep()
: 0;
}
@Override
public boolean checkCanMove(DirectionType direction) {
return drawningCar != null && drawningCar.canMove(direction);
return drawningCar != null && drawningCar.CanMove(direction);
}
@Override
public void moveObject(DirectionType direction) {
if (drawningCar != null) {
drawningCar.moveTransport(direction);
drawningCar.MoveTransport(direction);
}
}
}

View File

@ -0,0 +1,30 @@
package SelfPropelledArtilleryUnit.MovementStrategy;
import SelfPropelledArtilleryUnit.DirectionType;
public interface IMoveableObject {
/**
* Получение координаты X объекта
* @return координаты объекта
*/
ObjectParameters getObjectPosition();
/**
* Получение шага объекта
* @return шаг объекта
*/
int getStep();
/**
* Проверка, можно ли переместиться по нужному направлению
* @param direction направление
* @return true, если можно переместиться, иначе false
*/
boolean checkCanMove(DirectionType direction);
/**
* Изменение направления перемещения объекта
* @param direction направление
*/
void moveObject(DirectionType direction);
}

View File

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

View File

@ -0,0 +1,41 @@
package SelfPropelledArtilleryUnit.MovementStrategy;
public class MoveToCenter extends AbstractStrategy {
@Override
protected boolean isTargetDestination() {
ObjectParameters 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;
}
double diffX = objParams.getObjectMiddleHorizontal() - getFieldWidth() / 2;
if (Math.abs(diffX) > getStep()) {
if (diffX > 0) {
moveLeft();
} else {
moveRight();
}
}
double diffY = objParams.getObjectMiddleVertical() - getFieldHeight() / 2;
if (Math.abs(diffY) > getStep()) {
if (diffY > 0) {
moveUp();
} else {
moveDown();
}
}
}
}

View File

@ -0,0 +1,70 @@
package SelfPropelledArtilleryUnit.MovementStrategy;
public class ObjectParameters {
private final int x;
private final int y;
private final int width;
private final int height;
/**
* Левая граница
* @return координата X
*/
public int getLeftBorder() {
return x;
}
/**
* Верхняя граница
* @return координата Y
*/
public int getTopBorder() {
return y;
}
/**
* Правая граница
* @return координата X + ширина
*/
public int getRightBorder() {
return x + width;
}
/**
* Нижняя граница
* @return координата Y + высота
*/
public int getDownBorder() {
return y + height;
}
/**
* Середина объекта
* @return координата X + ширина / 2
*/
public int getObjectMiddleHorizontal() {
return x + width / 2;
}
/**
* Середина объекта
* @return координата Y + высота / 2
*/
public int getObjectMiddleVertical() {
return y + height / 2;
}
/**
* Конструктор
* @param x координата X
* @param y координата Y
* @param width ширина
* @param height высота
*/
public ObjectParameters(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}

View File

@ -0,0 +1,7 @@
package SelfPropelledArtilleryUnit.MovementStrategy;
public enum Status {
NotInit,
InProgress,
Finish
}