работа 1
This commit is contained in:
parent
9beb5a38be
commit
bf7d70d980
@ -5,120 +5,33 @@ import MovementStrategy.*;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningElectricLocomotive {
|
||||
public EntityElectricLocomotive _entityElectricLocomotive;
|
||||
private DrawningWheels drawningWheels;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
private final int _locomotiveWidth = 120;
|
||||
private final int _locomotiveHeight = 70;
|
||||
|
||||
public boolean Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, int countWheels, boolean horns, boolean battery, int width, int height) {
|
||||
if (width < _locomotiveWidth || height < _locomotiveHeight) {
|
||||
return false;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_entityElectricLocomotive = new EntityElectricLocomotive();
|
||||
_entityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor,
|
||||
horns, battery);
|
||||
drawningWheels = new DrawningWheels();
|
||||
drawningWheels.setCount(countWheels);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y) {
|
||||
x = Math.min(Math.max(0, x), _pictureWidth - _locomotiveWidth);
|
||||
y = Math.min(Math.max(0, y), _pictureHeight - _locomotiveHeight);
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public void MoveTransport(DirectionType direction) {
|
||||
if (_entityElectricLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
switch (direction) {
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - _entityElectricLocomotive.Step > 0) {
|
||||
_startPosX -= (int) _entityElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - _entityElectricLocomotive.Step > 0) {
|
||||
_startPosY -= (int) _entityElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + _locomotiveWidth + _entityElectricLocomotive.Step < _pictureWidth) {
|
||||
_startPosX += (int) _entityElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + _locomotiveHeight + _entityElectricLocomotive.Step < _pictureHeight) {
|
||||
_startPosY += (int) _entityElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
public class DrawningElectricLocomotive extends DrawningLocomotive {
|
||||
public DrawningElectricLocomotive(int speed, double weight,
|
||||
Color bodyColor, Color additionalColor,
|
||||
int countWheels,
|
||||
boolean horns, boolean battery,
|
||||
int width, int height){
|
||||
super(speed, weight, bodyColor, countWheels, width, height, 120, 70);
|
||||
if (entityLocomotive != null){
|
||||
entityLocomotive = new EntityElectricLocomotive(
|
||||
speed, weight, bodyColor, additionalColor, countWheels, horns, battery);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTransport(Graphics g) {
|
||||
if (_entityElectricLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
super.DrawTransport(g);
|
||||
EntityElectricLocomotive entityElectricLocomotiveLocomotive = (EntityElectricLocomotive)entityLocomotive;
|
||||
|
||||
g.clearRect(0, 0, _pictureWidth, _pictureHeight);
|
||||
// корпус электровоза
|
||||
g.setColor(_entityElectricLocomotive.BodyColor);
|
||||
|
||||
int[] posX = {
|
||||
_startPosX,
|
||||
_startPosX + 10,
|
||||
_startPosX + 120,
|
||||
_startPosX + 120,
|
||||
_startPosX
|
||||
};
|
||||
|
||||
int[] posY = {
|
||||
_startPosY + 40,
|
||||
_startPosY + 20,
|
||||
_startPosY + 20,
|
||||
_startPosY + 60,
|
||||
_startPosY + 60
|
||||
};
|
||||
|
||||
g.fillPolygon(posX, posY, 5);
|
||||
g.drawLine(_startPosX, _startPosY + 40,
|
||||
_startPosX + 120, _startPosY + 40);
|
||||
|
||||
// окна
|
||||
g.setColor(Color.blue);
|
||||
g.fillRect(_startPosX + 12, _startPosY + 24, 30, 11);
|
||||
g.fillRect(_startPosX + 55, _startPosY + 24, 11, 11);
|
||||
g.fillRect(_startPosX + 75, _startPosY + 24, 11, 11);
|
||||
g.fillRect(_startPosX + 95, _startPosY + 24, 11, 11);
|
||||
|
||||
// колёса
|
||||
drawningWheels.DrawWheels(g, _startPosX, _startPosY);
|
||||
|
||||
// рога
|
||||
if (_entityElectricLocomotive.Horns) {
|
||||
if (entityElectricLocomotiveLocomotive.Horns) {
|
||||
g.setColor(Color.black);
|
||||
posX = new int[]{
|
||||
int[] posX = new int[]{
|
||||
_startPosX + 50,
|
||||
_startPosX + 40,
|
||||
_startPosX + 50,
|
||||
_startPosX + 60
|
||||
};
|
||||
|
||||
posY = new int[]{
|
||||
int[] posY = new int[]{
|
||||
_startPosY + 20,
|
||||
_startPosY + 10,
|
||||
_startPosY,
|
||||
@ -127,14 +40,9 @@ public class DrawningElectricLocomotive {
|
||||
g.drawPolygon(posX, posY, 4);
|
||||
}
|
||||
// отсек для батарей
|
||||
if (_entityElectricLocomotive.Battery) {
|
||||
g.setColor(_entityElectricLocomotive.AdditionalColor);
|
||||
if (entityElectricLocomotiveLocomotive.Battery) {
|
||||
g.setColor(entityElectricLocomotiveLocomotive.AdditionalColor);
|
||||
g.fillRect(_startPosX + 80, _startPosY + 45, 35, 9);
|
||||
}
|
||||
}
|
||||
public void SetPictureSize(int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,150 @@
|
||||
package DrawningObjects;
|
||||
|
||||
import Entities.EntityLocomotive;
|
||||
import MovementStrategy.DirectionType;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningLocomotive {
|
||||
public EntityLocomotive entityLocomotive;
|
||||
protected int _pictureWidth;
|
||||
protected int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
protected int _locomotiveWidth = 120;
|
||||
protected int _locomotiveHeight = 70;
|
||||
|
||||
|
||||
public int GetPosX() {
|
||||
return _startPosX;
|
||||
}
|
||||
|
||||
public int GetPosY() {
|
||||
return _startPosY;
|
||||
}
|
||||
|
||||
public int GetWidth() {
|
||||
return _locomotiveWidth;
|
||||
}
|
||||
|
||||
public int GetHeight() {
|
||||
return _locomotiveHeight;
|
||||
}
|
||||
|
||||
public DrawningLocomotive(int speed, double weight,
|
||||
Color bodyColor, int countWheels,
|
||||
int width, int height) {
|
||||
if (width < _locomotiveWidth || height < _locomotiveHeight) {
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
entityLocomotive = new EntityLocomotive(speed, weight, bodyColor, countWheels);
|
||||
}
|
||||
|
||||
public DrawningLocomotive(int speed, double weight,
|
||||
Color bodyColor, int countWheels,
|
||||
int width, int height,
|
||||
int locomotiveWidth, int locomotiveHeight) {
|
||||
if (width < locomotiveWidth || height < locomotiveHeight) {
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_locomotiveWidth = locomotiveWidth;
|
||||
_locomotiveHeight = locomotiveHeight;
|
||||
entityLocomotive = new EntityLocomotive(speed, weight, bodyColor, countWheels);
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y) {
|
||||
x = Math.min(Math.max(0, x), _pictureWidth - _locomotiveWidth);
|
||||
y = Math.min(Math.max(0, y), _pictureHeight - _locomotiveHeight);
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics g) {
|
||||
if (entityLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
g.clearRect(0, 0, _pictureWidth, _pictureHeight);
|
||||
// корпус
|
||||
g.setColor(entityLocomotive.BodyColor);
|
||||
|
||||
int[] posX = {
|
||||
_startPosX,
|
||||
_startPosX + 10,
|
||||
_startPosX + 120,
|
||||
_startPosX + 120,
|
||||
_startPosX
|
||||
};
|
||||
|
||||
int[] posY = {
|
||||
_startPosY + 40,
|
||||
_startPosY + 20,
|
||||
_startPosY + 20,
|
||||
_startPosY + 60,
|
||||
_startPosY + 60
|
||||
};
|
||||
|
||||
g.fillPolygon(posX, posY, 5);
|
||||
g.drawLine(_startPosX, _startPosY + 40,
|
||||
_startPosX + 120, _startPosY + 40);
|
||||
|
||||
// окна
|
||||
g.setColor(Color.blue);
|
||||
g.fillRect(_startPosX + 12, _startPosY + 24, 30, 11);
|
||||
g.fillRect(_startPosX + 55, _startPosY + 24, 11, 11);
|
||||
g.fillRect(_startPosX + 75, _startPosY + 24, 11, 11);
|
||||
g.fillRect(_startPosX + 95, _startPosY + 24, 11, 11);
|
||||
|
||||
// колёса
|
||||
entityLocomotive.DrawningWheels.DrawWheels(g, _startPosX, _startPosY);
|
||||
}
|
||||
|
||||
public void SetPictureSize(int width, int height) {
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
}
|
||||
|
||||
public boolean CanMove(DirectionType direction) {
|
||||
if (entityLocomotive == null) {
|
||||
return false;
|
||||
}
|
||||
switch (direction) {
|
||||
case Down -> {
|
||||
return _startPosX + _locomotiveHeight + entityLocomotive.Step < _pictureHeight;
|
||||
}
|
||||
case Up -> {
|
||||
return _startPosY - entityLocomotive.Step > 0;
|
||||
}
|
||||
case Right -> {
|
||||
return _startPosX + _locomotiveWidth + entityLocomotive.Step < _pictureWidth;
|
||||
}
|
||||
case Left -> {
|
||||
return _startPosX - entityLocomotive.Step > 0;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public void MoveTransport(DirectionType direction) {
|
||||
if (!CanMove(direction) || entityLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
switch (direction) {
|
||||
case Left -> {
|
||||
_startPosX -= (int) entityLocomotive.Step;
|
||||
}
|
||||
case Up -> {
|
||||
_startPosY -= (int) entityLocomotive.Step;
|
||||
}
|
||||
case Right -> {
|
||||
_startPosX += (int) entityLocomotive.Step;
|
||||
}
|
||||
case Down -> {
|
||||
_startPosY += (int) entityLocomotive.Step;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -3,25 +3,16 @@ package Entities;
|
||||
import java.awt.*;
|
||||
import java.lang.FunctionalInterface;
|
||||
|
||||
public class EntityElectricLocomotive {
|
||||
public int Speed;
|
||||
public double Weight;
|
||||
public Color BodyColor;
|
||||
public class EntityElectricLocomotive extends EntityLocomotive {
|
||||
public Color AdditionalColor;
|
||||
public boolean Horns;
|
||||
public boolean Battery;
|
||||
public double Step;
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean horns, boolean battery)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
public EntityElectricLocomotive(int speed, double weight,
|
||||
Color bodyColor, Color additionalColor,
|
||||
int countWheels,
|
||||
boolean horns, boolean battery) {
|
||||
super(speed, weight, bodyColor, countWheels);
|
||||
Horns = horns;
|
||||
Battery = battery;
|
||||
Step = Speed * 100.0f / Weight;
|
||||
}
|
||||
|
||||
}
|
||||
|
22
ProjectElectricLocomotive/src/Entities/EntityLocomotive.java
Normal file
22
ProjectElectricLocomotive/src/Entities/EntityLocomotive.java
Normal file
@ -0,0 +1,22 @@
|
||||
package Entities;
|
||||
|
||||
import DrawningObjects.DrawningWheels;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityLocomotive {
|
||||
public int Speed;
|
||||
public double Weight;
|
||||
public Color BodyColor;
|
||||
public double Step;
|
||||
public DrawningWheels DrawningWheels;
|
||||
public EntityLocomotive(int speed, double weight, Color bodyColor, int countWheels)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
Step = Speed * 100.0f / Weight;
|
||||
DrawningWheels = new DrawningWheels();
|
||||
DrawningWheels.setCount(countWheels);
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package Forms;
|
||||
|
||||
import DrawningObjects.CountEnum;
|
||||
import DrawningObjects.DrawningElectricLocomotive;
|
||||
import DrawningObjects.*;
|
||||
import MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
@ -11,23 +10,33 @@ import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormElectricLocomotive extends JFrame {
|
||||
private DrawningElectricLocomotive _drawningElectricLocomotive;
|
||||
private DrawningLocomotive _drawningLocomotive;
|
||||
private Canvas canvas;
|
||||
private JButton buttonCreate;
|
||||
private JButton buttonCreateElectricLocomotive;
|
||||
private JButton buttonCreateLocomotive;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonLeft;
|
||||
private JTextField numberField;
|
||||
private JComboBox<String> comboBoxStrategy;
|
||||
public void Draw() {
|
||||
if (_drawningElectricLocomotive == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_drawningElectricLocomotive.DrawTransport(canvas.getGraphics());
|
||||
_drawningLocomotive.DrawTransport(canvas.getGraphics());
|
||||
}
|
||||
private void InitializeComponent(){
|
||||
buttonCreate = new JButton("Создать");
|
||||
buttonCreateElectricLocomotive = new JButton("Создать Электропоезд");
|
||||
buttonCreateElectricLocomotive.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
buttonCreateLocomotive = new JButton("Создать локомотив");
|
||||
buttonCreateLocomotive.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
comboBoxStrategy = new JComboBox<>();
|
||||
comboBoxStrategy.addItem("идти к центру экрана");
|
||||
comboBoxStrategy.addItem("идти к краю экрана");
|
||||
|
||||
buttonUp = new JButton();
|
||||
buttonUp.setBorderPainted(false);
|
||||
@ -68,25 +77,29 @@ public class FormElectricLocomotive extends JFrame {
|
||||
setLayout(null);
|
||||
|
||||
canvas = new Canvas();
|
||||
canvas.setBounds(0, 0, 1000, 600);
|
||||
buttonCreate.setBounds(10, 520, 120, 40);
|
||||
buttonUp.setBounds(50, 430, 40, 40);
|
||||
buttonDown.setBounds(50, 470, 40, 40);
|
||||
buttonRight.setBounds(90, 470, 40, 40);
|
||||
buttonLeft.setBounds(10, 470, 40, 40);
|
||||
numberField.setBounds(150, 520, 40, 40);
|
||||
canvas.setBounds(0, 0, 960, 560);
|
||||
buttonCreateElectricLocomotive.setBounds(10, 470, 200, 40);
|
||||
buttonCreateLocomotive.setBounds(10, 520, 200, 40);
|
||||
buttonUp.setBounds(50, 380, 40, 40);
|
||||
buttonDown.setBounds(50, 420, 40, 40);
|
||||
buttonRight.setBounds(90, 420, 40, 40);
|
||||
buttonLeft.setBounds(10, 420, 40, 40);
|
||||
numberField.setBounds(220, 520, 40, 40);
|
||||
comboBoxStrategy.setBounds(770, 10, 200, 30);
|
||||
|
||||
|
||||
add(buttonCreate);
|
||||
add(buttonCreateElectricLocomotive);
|
||||
add(buttonCreateLocomotive);
|
||||
add(buttonUp);
|
||||
add(buttonDown);
|
||||
add(buttonRight);
|
||||
add(buttonLeft);
|
||||
add(numberField);
|
||||
add(comboBoxStrategy);
|
||||
add(canvas);
|
||||
}
|
||||
private void InitializeLogic(){
|
||||
buttonCreate.addActionListener(
|
||||
buttonCreateElectricLocomotive.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
int countWheels;
|
||||
@ -98,14 +111,43 @@ public class FormElectricLocomotive extends JFrame {
|
||||
}
|
||||
System.out.println(e.getActionCommand());
|
||||
Random random = new Random();
|
||||
_drawningElectricLocomotive = new DrawningElectricLocomotive();
|
||||
_drawningElectricLocomotive.Init(random.nextInt(100, 300), random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
_drawningLocomotive = new DrawningElectricLocomotive(
|
||||
random.nextInt(100, 300), random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256),
|
||||
random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
new Color(random.nextInt(0, 256),
|
||||
random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
countWheels,
|
||||
random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,
|
||||
1000, 560);
|
||||
_drawningElectricLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
_drawningLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
buttonCreateLocomotive.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int countWheels;
|
||||
try {
|
||||
countWheels = Integer.parseInt(numberField.getText());
|
||||
}
|
||||
catch (Exception ex){
|
||||
countWheels = 0;
|
||||
}
|
||||
System.out.println(e.getActionCommand());
|
||||
Random random = new Random();
|
||||
_drawningLocomotive = new DrawningLocomotive(
|
||||
random.nextInt(100, 300), random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256),
|
||||
random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
countWheels,
|
||||
canvas.getWidth(), canvas.getHeight()
|
||||
);
|
||||
_drawningLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
@ -114,22 +156,22 @@ public class FormElectricLocomotive extends JFrame {
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
System.out.println(((JButton)(e.getSource())).getName());
|
||||
if (_drawningElectricLocomotive == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch(((JButton)(e.getSource())).getName()){
|
||||
case "up":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Up);
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "down":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Down);
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "left":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Left);
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "right":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Right);
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
|
@ -0,0 +1,93 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject _movableObject;
|
||||
private Status _state = Status.NotInit;
|
||||
private int _fieldWidth;
|
||||
|
||||
protected int FieldWidth() {
|
||||
return _fieldWidth;
|
||||
}
|
||||
|
||||
private int _fieldHeight;
|
||||
|
||||
protected int FieldHeight() {
|
||||
return _fieldHeight;
|
||||
}
|
||||
|
||||
public Status GetStatus() {
|
||||
return _state;
|
||||
}
|
||||
|
||||
public void SetData(IMoveableObject movableObject, int width, int height) {
|
||||
if (movableObject == null) {
|
||||
_state = Status.NotInit;
|
||||
return;
|
||||
}
|
||||
_state = Status.InProgress;
|
||||
_movableObject = movableObject;
|
||||
_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 ObjectParameters GetObjectParameters() {
|
||||
if (_movableObject != null) {
|
||||
return _movableObject.GetObjectPosition();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected int GetStep() {
|
||||
if (_state != Status.InProgress) {
|
||||
return 0;
|
||||
}
|
||||
return _movableObject.GetStep();
|
||||
}
|
||||
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
protected abstract boolean IsTargetDestination();
|
||||
|
||||
private boolean MoveTo(DirectionType directionType) {
|
||||
if (_state != Status.InProgress) {
|
||||
return false;
|
||||
}
|
||||
if (_movableObject != null && _movableObject.CheckCanMove(directionType)) {
|
||||
_movableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SetFieldSize(int width, int height) {
|
||||
_fieldWidth = width;
|
||||
_fieldHeight = height;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package MovementStrategy;
|
||||
|
||||
import DrawningObjects.DrawningLocomotive;
|
||||
|
||||
public class DrawningObjectLocomotive implements IMoveableObject {
|
||||
private DrawningLocomotive _drawningLocomotive = null;
|
||||
public DrawningObjectLocomotive(DrawningLocomotive drawningLocomotive)
|
||||
{
|
||||
_drawningLocomotive = drawningLocomotive;
|
||||
}
|
||||
@Override
|
||||
public ObjectParameters GetObjectPosition() {
|
||||
if (_drawningLocomotive == null || _drawningLocomotive.entityLocomotive == null){
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(
|
||||
_drawningLocomotive.GetPosX(), _drawningLocomotive.GetPosY(),
|
||||
_drawningLocomotive.GetWidth(), _drawningLocomotive.GetHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int GetStep() {
|
||||
if (_drawningLocomotive == null){
|
||||
return 0;
|
||||
}
|
||||
return (int)_drawningLocomotive.entityLocomotive.Step;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean CheckCanMove(DirectionType direction) {
|
||||
if (_drawningLocomotive == null){
|
||||
return false;
|
||||
}
|
||||
return _drawningLocomotive.CanMove(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void MoveObject(DirectionType direction) {
|
||||
if (_drawningLocomotive == null){
|
||||
return;
|
||||
}
|
||||
_drawningLocomotive.MoveTransport(direction);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public interface IMoveableObject {
|
||||
public ObjectParameters GetObjectPosition();
|
||||
int GetStep();
|
||||
boolean CheckCanMove(DirectionType direction);
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy{
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
MoveRight();
|
||||
MoveDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null){
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal() <= FieldHeight() &&
|
||||
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth() &&
|
||||
objParams.ObjectMiddleVertical() <= FieldHeight() &&
|
||||
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight();
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy{
|
||||
@Override
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null){
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal() <= FieldHeight() / 2 &&
|
||||
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleVertical() <= FieldHeight() / 2 &&
|
||||
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight() / 2;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user