Доделал 2 часть с стратегиями и усложненное задание

This commit is contained in:
pyzhov.egor 2024-03-11 18:49:30 +04:00
parent f1f7561301
commit f6ba6fe601
20 changed files with 531 additions and 126 deletions

View File

@ -1,6 +0,0 @@
public enum DirectionType {
Up,
Down,
Left,
Right
}

View File

@ -1,10 +1,12 @@
import Entities.*;
package Drawnings;
import Entities.*;
import MovementStrategy.*;
import java.awt.*;
import java.util.Random;
public class DrawningBoat {
private EntityBoat entityBoat;
private final EntityBoat entityBoat;
public EntityBoat getEntityBoat() {
return entityBoat;
}
@ -13,25 +15,38 @@ public class DrawningBoat {
protected Integer _startPosX;
protected Integer _startPosY;
private int _drawingBoatWidth = 107;
private int _drawingBoatHeight = 55;
public DrawningCatamaranPaddle _drawingCatamaranPaddle;
private int _drawingBoatHeight = 68;
public int GetPosX(){return _startPosX;}
public int GetPosY(){return _startPosY;}
public int GetWidth(){return _drawingBoatWidth;}
public int GetHeight(){return _drawingBoatHeight;}
private IDrawPaddles drawPaddles;
public DrawningBoat(int speed, float weight, Color bodyColor) {
public DrawningBoat(int speed, float weight, Color bodyColor, int paddlesType) {
entityBoat = new EntityBoat(speed, weight, bodyColor);
_startPosY = null;
_startPosX = null;
_pictureWidth = null;
_pictureHeight = null;
_drawingCatamaranPaddle = new DrawningCatamaranPaddle();
switch (paddlesType) {
case 0:
drawPaddles = new DrawningPaddles();
break;
case 1:
drawPaddles = new DrawningOvalPaddles();
break;
case 2:
drawPaddles = new DrawningRectanglePaddles();
break;
}
Random random = new Random();
int paddlesCount = random.nextInt(1,4);
_drawingCatamaranPaddle.setEnumNumber(paddlesCount);
drawPaddles.setNumber(paddlesCount);
}
protected DrawningBoat(int speed, float weight, Color bodyColor, int boatWidth, int boatHeight) {
this(speed, weight, bodyColor);
protected DrawningBoat(int speed, float weight, Color bodyColor, int paddlesType, int boatWidth, int boatHeight) {
this(speed, weight, bodyColor, paddlesType);
_drawingBoatHeight = boatHeight;
_drawingBoatWidth = boatWidth;
@ -69,25 +84,25 @@ public class DrawningBoat {
}
public boolean moveTransport(DirectionType direction) {
public boolean moveTransport(MovementDirection direction) {
if (entityBoat == null || _pictureWidth == null || _pictureHeight == null)
return false;
switch (direction) {
case Left:
if (_startPosX - entityBoat.Step() > 0)
_startPosX -= (int) entityBoat.Step();
case MovementDirection.Left:
if (_startPosX - entityBoat.getStep() > 0)
_startPosX -= (int) entityBoat.getStep();
return true;
case Up:
if (_startPosY - entityBoat.Step() > 0)
_startPosY -= (int) entityBoat.Step();
case MovementDirection.Up:
if (_startPosY - entityBoat.getStep() > 0)
_startPosY -= (int) entityBoat.getStep();
return true;
case Right:
if (_startPosX + entityBoat.Step() < _pictureWidth - _drawingBoatWidth)
_startPosX += (int) entityBoat.Step();
case MovementDirection.Right:
if (_startPosX + entityBoat.getStep() < _pictureWidth - _drawingBoatWidth)
_startPosX += (int) entityBoat.getStep();
return true;
case Down:
if (_startPosY + entityBoat.Step() < _pictureHeight - _drawingBoatHeight)
_startPosY += (int) entityBoat.Step();
case MovementDirection.Down:
if (_startPosY + entityBoat.getStep() < _pictureHeight - _drawingBoatHeight)
_startPosY += (int) entityBoat.getStep();
return true;
default:
return false;
@ -127,6 +142,6 @@ public class DrawningBoat {
g2d.setColor(Color.WHITE);
g2d.fillOval(ellipseX, ellipseY, ellipseWidth, ellipseHeight);
_drawingCatamaranPaddle.drawCatamaranPaddles(g2d, entityBoat.getBodyColor(), _startPosX, _startPosY);
drawPaddles.drawPaddles(g2d, entityBoat.getBodyColor(), _startPosX, _startPosY);
}
}

View File

@ -1,11 +1,14 @@
package Drawnings;
import Drawnings.DrawningBoat;
import Entities.*;
import java.awt.*;
public class DrawningCatamaran extends DrawningBoat {
EntityCatamaran EntityBoat;
public DrawningCatamaran(int speed, float weight, Color bodyColor, Color additionalColor, boolean sail, boolean floaters) {
super(speed, weight, bodyColor, 110, 58);
public DrawningCatamaran(int speed, float weight, Color bodyColor, int paddlesType, Color additionalColor, boolean sail, boolean floaters) {
super(speed, weight, bodyColor, paddlesType, 110, 68);
EntityBoat = new EntityCatamaran(speed, weight, bodyColor, additionalColor, floaters, sail);
}
@ -15,9 +18,10 @@ public class DrawningCatamaran extends DrawningBoat {
private void drawFloater(int y0, Color additionalColor, Graphics g2d)
private void drawFloater(int y0, Color additionalColor, Graphics2D g2d)
{
g2d.setColor(additionalColor);
g2d.setStroke(new BasicStroke(2));
Point[] floater = new Point[]
{
new Point(_startPosX + 10, _startPosY + y0 + 6),

View File

@ -0,0 +1,38 @@
package Drawnings;
import java.awt.*;
public class DrawningOvalPaddles implements IDrawPaddles {
private PaddlesCount _paddlesCount;
@Override
public void setNumber(int paddlesCount) {
for (PaddlesCount value : PaddlesCount.values()) {
if (value.getEnumNumber() == paddlesCount) {
_paddlesCount = value;
return;
}
}
}
@Override
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));
int distanceBetweenPaddles = 27;
for (int i = 0; i < _paddlesCount.getEnumNumber(); i++) {
int posX = (int)(_startX + i * distanceBetweenPaddles); // Позиция X для текущей пары весел
drawPaddlePair(g2d, posX, (int)_startY + 5);
}
}
private void drawPaddlePair(Graphics2D g2d, int posX, int posY) {
g2d.drawLine(posX + 20, posY + 15, posX + 9, posY - 3); // Рисуем левое весло
g2d.drawLine(posX + 20, posY + 50, posX + 9, posY + 68); // Рисуем правое весло
// Добавляем овалы на концы весел
int ovalSize = 10; // Размер овала
int halfStrokeWidth = 2; // Половина толщины линии
g2d.fillOval(posX + 9 - halfStrokeWidth - ovalSize / 2, posY + 68 - ovalSize / 2, ovalSize, ovalSize);
g2d.fillOval(posX + 9 - halfStrokeWidth - ovalSize / 2, posY - 3 - ovalSize / 2, ovalSize, ovalSize);
}
}

View File

@ -1,34 +1,35 @@
package Drawnings;
import java.awt.*;
public class DrawningCatamaranPaddle {
public class DrawningPaddles implements IDrawPaddles {
private PaddlesCount _paddlesCount;
public void setEnumNumber(int paddlesCount) {
@Override
public void setNumber(int paddlesCount) {
for (PaddlesCount value : PaddlesCount.values()) {
if (value.getEnumNumber() == paddlesCount) {
_paddlesCount = value;
return;
}
}
}
public void drawCatamaranPaddles(Graphics g, Color color, float startPosX, float startPosY) {
Graphics2D g2d = (Graphics2D) g;
@Override
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));
int distanceBetweenPaddles = 27;
for (int i = 0; i < _paddlesCount.getEnumNumber(); i++) {
int posX = (int)(startPosX + i * distanceBetweenPaddles); // Позиция X для текущей пары весел
drawPaddlePair(g2d, posX, (int)startPosY + 5);
int posX = (int)(_startX + i * distanceBetweenPaddles); // Позиция X для текущей пары весел
drawPaddlePair(g2d, posX, (int)_startY + 5);
}
}
private void drawPaddlePair(Graphics2D g2d, int posX, int posY) {
g2d.drawLine(posX + 20, posY + 15, posX + 5, posY); // Рисуем левое весло
g2d.drawLine(posX + 20, posY + 50, posX + 5, posY + 65); // Рисуем правое весло
g2d.drawLine(posX + 20, posY + 15, posX + 9, posY - 3); // Рисуем левое весло
g2d.drawLine(posX + 20, posY + 50, posX + 9, posY + 68); // Рисуем правое весло
}
}
}

View File

@ -0,0 +1,39 @@
package Drawnings;
import java.awt.*;
public class DrawningRectanglePaddles implements IDrawPaddles {
private PaddlesCount _paddlesCount;
@Override
public void setNumber(int paddlesCount) {
for (PaddlesCount value : PaddlesCount.values()) {
if (value.getEnumNumber() == paddlesCount) {
_paddlesCount = value;
return;
}
}
}
@Override
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));
int distanceBetweenPaddles = 27;
for (int i = 0; i < _paddlesCount.getEnumNumber(); i++) {
int posX = (int)(_startX + i * distanceBetweenPaddles); // Позиция X для текущей пары весел
drawPaddlePair(g2d, posX, (int)_startY + 5);
}
}
private void drawPaddlePair(Graphics2D g2d, int posX, int posY) {
g2d.drawLine(posX + 20, posY + 15, posX + 9, posY - 3); // Рисуем левое весло
g2d.drawLine(posX + 20, posY + 50, posX + 9, posY + 68); // Рисуем правое весло
int rectangleSize = 8; // Размер прямоугольника
int halfStrokeWidth = 2; // Половина толщины линии
// Добавляем прямоугольники на концы весел
g2d.fillRect(posX + 9 - halfStrokeWidth - rectangleSize / 2, posY + 68 - rectangleSize / 2, rectangleSize, rectangleSize);
g2d.fillRect(posX + 9 - halfStrokeWidth - rectangleSize / 2, posY - 3 - rectangleSize / 2, rectangleSize, rectangleSize);
}
}

View File

@ -0,0 +1,8 @@
package Drawnings;
import java.awt.*;
public interface IDrawPaddles {
void setNumber(int x);
void drawPaddles(Graphics2D graphics2D, Color color, int _startX, int _startY);
}

View File

@ -1,3 +1,5 @@
package Drawnings;
public enum PaddlesCount {
One(1),
Two(2),

View File

@ -15,7 +15,8 @@ public class EntityBoat {
public Color getBodyColor() {
return BodyColor;
}
public double Step() {
private double Step;
public double getStep() {
return Speed*100/Weight;
}

View File

@ -8,17 +8,89 @@
<properties/>
<border type="none"/>
<children>
<grid id="1ff9a" binding="PictureBox" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="1ff9a" binding="PictureBox" layout-manager="GridLayoutManager" row-count="6" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<properties>
<toolTipText value=""/>
</properties>
<border type="none"/>
<children>
<vspacer id="7b40c">
<constraints>
<grid row="2" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
<preferred-size width="88" height="14"/>
</grid>
</constraints>
</vspacer>
<hspacer id="e0b21">
<constraints>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="a8c2" class="javax.swing.JButton" binding="buttonCreateCatamaran">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="150" height="30"/>
<preferred-size width="150" height="30"/>
<maximum-size width="150" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать катамаран"/>
</properties>
</component>
<component id="79b3e" class="javax.swing.JButton" binding="buttonCreateBoat">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="150" height="30"/>
<preferred-size width="150" height="30"/>
<maximum-size width="150" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать лодку"/>
</properties>
</component>
<component id="fe25b" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="5" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="30px_arrow_left.png"/>
<text value=""/>
</properties>
</component>
<component id="3eabd" class="javax.swing.JButton" binding="buttonStrategyStep">
<constraints>
<grid row="1" column="3" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Шаг"/>
</properties>
</component>
<component id="86d84" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="5" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="30px_arrow_right.png"/>
<text value=""/>
</properties>
</component>
<component id="3dc0" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="5" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -31,7 +103,7 @@
</component>
<component id="ccb2a" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="4" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -42,66 +114,14 @@
<text value=""/>
</properties>
</component>
<component id="fe25b" class="javax.swing.JButton" binding="buttonLeft">
<component id="988c8" class="javax.swing.JComboBox" binding="comboBoxStrategy">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="47" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
<grid row="0" column="2" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="9" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="30px_arrow_left.png"/>
<text value=""/>
</properties>
</component>
<component id="86d84" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="3" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="30px_arrow_right.png"/>
<text value=""/>
</properties>
</component>
<vspacer id="7b40c">
<constraints>
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
<preferred-size width="88" height="14"/>
</grid>
</constraints>
</vspacer>
<hspacer id="e0b21">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="a8c2" class="javax.swing.JButton" binding="buttonCreateCatamaran">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="150" height="30"/>
<preferred-size width="150" height="30"/>
<maximum-size width="150" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать катамаран"/>
</properties>
</component>
<component id="79b3e" class="javax.swing.JButton" binding="buttonCreateBoat">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="150" height="30"/>
<preferred-size width="150" height="30"/>
<maximum-size width="150" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать лодку"/>
<actionCommand value=""/>
<doubleBuffered value="false"/>
<toolTipText value=""/>
</properties>
</component>
</children>

View File

@ -1,3 +1,5 @@
import Drawnings.*;
import MovementStrategy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
@ -16,17 +18,21 @@ public class FormCatamaran extends JFrame {
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonCreateBoat;
private JComboBox comboBoxStrategy;
private JButton buttonStrategyStep;
private AbstractStrategy _strategy;
private List<JComponent> controls;
private void createObject(String type) {
Random random = new Random();
switch (type) {
case "DrawningBoat":
_drawningBoat = new DrawningBoat(random.nextInt(100 - 30) + 30, random.nextInt(500 - 100) + 100, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
case "Drawnings.DrawningBoat":
_drawningBoat = new DrawningBoat(random.nextInt(100 - 30) + 30, random.nextInt(500 - 100) + 100,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3));
break;
case "DrawningCatamaran":
case "Drawnings.DrawningCatamaran":
_drawningBoat = new DrawningCatamaran(random.nextInt(100 - 30) + 30, random.nextInt(500 - 100) + 100,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean());
break;
@ -36,6 +42,9 @@ public class FormCatamaran extends JFrame {
_drawningBoat.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
_drawningBoat.setPosition(random.nextInt(25, 100),
random.nextInt(25, 100));
_strategy = null;
comboBoxStrategy.setEnabled(true);
Draw();
}
@ -47,20 +56,17 @@ public class FormCatamaran extends JFrame {
InitializeControlsRepaintList();
buttonCreateCatamaran.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createObject("DrawningCatamaran");
createObject("Drawnings.DrawningCatamaran");
}
});
buttonCreateBoat.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createObject("DrawningBoat");
createObject("Drawnings.DrawningBoat");
}
});
ActionListener buttonMoveClickedListener = new ActionListener() {
@ -71,19 +77,19 @@ public class FormCatamaran extends JFrame {
switch (buttonName) {
case "buttonUp": {
result = _drawningBoat.moveTransport(DirectionType.Up);
result = _drawningBoat.moveTransport(MovementDirection.Up);
}
break;
case "buttonDown": {
result = _drawningBoat.moveTransport(DirectionType.Down);
result = _drawningBoat.moveTransport(MovementDirection.Down);
}
break;
case "buttonLeft": {
result = _drawningBoat.moveTransport(DirectionType.Left);
result = _drawningBoat.moveTransport(MovementDirection.Left);
}
break;
case "buttonRight": {
result = _drawningBoat.moveTransport(DirectionType.Right);
result = _drawningBoat.moveTransport(MovementDirection.Right);
}
break;
@ -98,8 +104,53 @@ public class FormCatamaran extends JFrame {
buttonLeft.addActionListener(buttonMoveClickedListener);
buttonUp.addActionListener(buttonMoveClickedListener);
String[] itemsComboBox = {
"К центру",
"К краю"
};
for (String item: itemsComboBox) {
comboBoxStrategy.addItem(item);
}
buttonStrategyStep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_drawningBoat == null)
return;
if (comboBoxStrategy.isEnabled()) {
switch (comboBoxStrategy.getSelectedIndex()) {
case 0:
_strategy = new MoveToCenter();
break;
case 1:
_strategy = new MoveToBorder();
break;
default:
_strategy = null;
break;
}
if (_strategy == null) {
return;
}
_strategy.SetData(new MoveableBoat(_drawningBoat), PictureBox.getWidth(), PictureBox.getHeight());
}
if (_strategy == null) {
return;
}
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish) {
comboBoxStrategy.setEnabled(true);
_strategy = null;
}
}
});
}
private void Draw() {
if (_drawningBoat.getEntityBoat() == null)
return;
@ -120,7 +171,6 @@ public class FormCatamaran extends JFrame {
}
}
private void InitializeControlsRepaintList() {
controls = new LinkedList<>();
controls.add(buttonCreateCatamaran);
@ -129,6 +179,8 @@ public class FormCatamaran extends JFrame {
controls.add(buttonDown);
controls.add(buttonLeft);
controls.add(buttonRight);
controls.add(comboBoxStrategy);
controls.add(buttonStrategyStep);
}

View File

@ -1,7 +1,5 @@
import javax.swing.*;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(false);
@ -12,8 +10,5 @@ public class Main {
frame.pack();
frame.setSize(700, 500);
frame.setVisible(true);
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
}
}

View File

@ -0,0 +1,76 @@
package MovementStrategy;
public abstract class AbstractStrategy {
private IMoveableObject _moveableObject;
private StrategyStatus _state = StrategyStatus.NotInit;
protected int FieldWidth;
protected int FieldHeight;
public StrategyStatus GetStatus() { return _state; }
// Изменить статус, установить поля
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
// сделать шаг
public void MakeStep()
{
if (_state != StrategyStatus.InProgress)
{
return;
}
if (IsTargetDestination())
{
_state = StrategyStatus.Finish;
return;
}
MoveToTarget();
}
// перемещения
protected boolean MoveLeft() { return MoveTo(MovementDirection.Left); }
protected boolean MoveRight() { return MoveTo(MovementDirection.Right); }
protected boolean MoveUp() { return MoveTo(MovementDirection.Up); }
protected boolean MoveDown() { return MoveTo(MovementDirection.Down); }
// параметры
protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); }
// шаг
protected int GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return 0;
}
return _moveableObject.GetStep();
}
// перемещение
protected abstract void MoveToTarget();
// достигнута ли цель
protected abstract boolean IsTargetDestination();
// попытка перемещения по направлению
private boolean MoveTo(MovementDirection directionType)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
if (_moveableObject.TryMoveObject(directionType))
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}

View File

@ -0,0 +1,8 @@
package MovementStrategy;
public interface IMoveableObject {
ObjectParameters GetObjectPosition();
int GetStep();
boolean TryMoveObject(MovementDirection direction);
void MoveObject(MovementDirection direction);
}

View File

@ -0,0 +1,37 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy {
protected boolean IsTargetDestination()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return objParams.RightBorder() <= FieldWidth &&
objParams.RightBorder() + GetStep() >= FieldWidth &&
objParams.DownBorder() <= FieldHeight &&
objParams.DownBorder() + GetStep() >= FieldHeight;
}
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = FieldWidth - objParams.ObjectMiddleHorizontal();
if (Math.abs(diffX) > GetStep()) {
MoveRight();
}
var diffY = FieldHeight - objParams.ObjectMiddleVertical();
if (Math.abs(diffY) > GetStep()) {
MoveDown();
}
}
}

View File

@ -0,0 +1,47 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy {
protected boolean IsTargetDestination()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return (objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical() <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2);
}
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2;
if (Math.abs(diffX) > GetStep()) {
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2;
if (Math.abs(diffY) > GetStep()) {
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,28 @@
package MovementStrategy;
import Drawnings.*;
import Entities.EntityBoat;
public class MoveableBoat implements IMoveableObject {
private DrawningBoat _boat = null;
public MoveableBoat(DrawningBoat drawningBoat)
{
_boat = drawningBoat;
}
public ObjectParameters GetObjectPosition()
{
if (_boat == null || _boat.getEntityBoat() == null)
{
return null;
}
return new ObjectParameters(_boat.GetPosX(), _boat.GetPosY(), _boat.GetWidth(), _boat.GetHeight());
}
public int GetStep() { return (int) _boat.getEntityBoat().getStep(); }
public boolean TryMoveObject(MovementDirection direction) { return _boat.moveTransport(direction); }
public void MoveObject(MovementDirection direction) { _boat.moveTransport(direction); }
}

View File

@ -0,0 +1,8 @@
package MovementStrategy;
public enum MovementDirection {
Up,
Down,
Left,
Right;
}

View File

@ -0,0 +1,25 @@
package MovementStrategy;
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private int _height;
public int LeftBorder() { return _x; }
public int TopBorder() { return _y; }
public int RightBorder() { return _x + _width; }
public int DownBorder() { return _y + _height; }
public int ObjectMiddleHorizontal () { return _x + _width / 2; }
public int ObjectMiddleVertical () { return _y + _height / 2; }
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}

View File

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