Compare commits
No commits in common. "0d75f131016ae7130cd63e2fe4253e84a40bdd24" and "9beb5a38bedf8f83b0ccf86a0ad3a11a301613cd" have entirely different histories.
0d75f13101
...
9beb5a38be
@ -5,33 +5,120 @@ import MovementStrategy.*;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void DrawTransport(Graphics g) {
|
||||
super.DrawTransport(g);
|
||||
EntityElectricLocomotive entityElectricLocomotiveLocomotive = (EntityElectricLocomotive)entityLocomotive;
|
||||
|
||||
if (entityElectricLocomotiveLocomotive.Horns) {
|
||||
public void DrawTransport(Graphics g) {
|
||||
if (_entityElectricLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
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) {
|
||||
g.setColor(Color.black);
|
||||
int[] posX = new int[]{
|
||||
posX = new int[]{
|
||||
_startPosX + 50,
|
||||
_startPosX + 40,
|
||||
_startPosX + 50,
|
||||
_startPosX + 60
|
||||
};
|
||||
|
||||
int[] posY = new int[]{
|
||||
posY = new int[]{
|
||||
_startPosY + 20,
|
||||
_startPosY + 10,
|
||||
_startPosY,
|
||||
@ -40,9 +127,14 @@ public class DrawningElectricLocomotive extends DrawningLocomotive {
|
||||
g.drawPolygon(posX, posY, 4);
|
||||
}
|
||||
// отсек для батарей
|
||||
if (entityElectricLocomotiveLocomotive.Battery) {
|
||||
g.setColor(entityElectricLocomotiveLocomotive.AdditionalColor);
|
||||
if (_entityElectricLocomotive.Battery) {
|
||||
g.setColor(_entityElectricLocomotive.AdditionalColor);
|
||||
g.fillRect(_startPosX + 80, _startPosY + 45, 35, 9);
|
||||
}
|
||||
}
|
||||
public void SetPictureSize(int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
}
|
||||
}
|
||||
|
@ -1,150 +0,0 @@
|
||||
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 _startPosY + _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,16 +3,25 @@ package Entities;
|
||||
import java.awt.*;
|
||||
import java.lang.FunctionalInterface;
|
||||
|
||||
public class EntityElectricLocomotive extends EntityLocomotive {
|
||||
public class EntityElectricLocomotive {
|
||||
public int Speed;
|
||||
public double Weight;
|
||||
public Color BodyColor;
|
||||
public Color AdditionalColor;
|
||||
public boolean Horns;
|
||||
public boolean Battery;
|
||||
public EntityElectricLocomotive(int speed, double weight,
|
||||
Color bodyColor, Color additionalColor,
|
||||
int countWheels,
|
||||
boolean horns, boolean battery) {
|
||||
super(speed, weight, bodyColor, countWheels);
|
||||
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;
|
||||
Horns = horns;
|
||||
Battery = battery;
|
||||
Step = Speed * 100.0f / Weight;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,22 +0,0 @@
|
||||
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,6 +1,7 @@
|
||||
package Forms;
|
||||
|
||||
import DrawningObjects.*;
|
||||
import DrawningObjects.CountEnum;
|
||||
import DrawningObjects.DrawningElectricLocomotive;
|
||||
import MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
@ -10,39 +11,23 @@ import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormElectricLocomotive extends JFrame {
|
||||
private DrawningLocomotive _drawningLocomotive;
|
||||
private AbstractStrategy _abstractStrategy;
|
||||
private DrawningElectricLocomotive _drawningElectricLocomotive;
|
||||
private Canvas canvas;
|
||||
private JButton buttonCreateElectricLocomotive;
|
||||
private JButton buttonCreateLocomotive;
|
||||
private JButton buttonCreate;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonStep;
|
||||
private JTextField numberField;
|
||||
private JComboBox<AbstractStrategy> comboBoxStrategy;
|
||||
public void Draw() {
|
||||
if (_drawningLocomotive == null)
|
||||
if (_drawningElectricLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_drawningLocomotive.DrawTransport(canvas.getGraphics());
|
||||
_drawningElectricLocomotive.DrawTransport(canvas.getGraphics());
|
||||
}
|
||||
private void InitializeComponent(){
|
||||
buttonCreateElectricLocomotive = new JButton("Создать Электропоезд");
|
||||
buttonCreateElectricLocomotive.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
buttonCreateLocomotive = new JButton("Создать локомотив");
|
||||
buttonCreateLocomotive.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
buttonStep = new JButton("шаг");
|
||||
buttonStep.setName("шаг");
|
||||
|
||||
comboBoxStrategy = new JComboBox<>();
|
||||
comboBoxStrategy.addItem(new MoveToCenter());
|
||||
|
||||
comboBoxStrategy.addItem(new MoveToBorder());
|
||||
buttonCreate = new JButton("Создать");
|
||||
|
||||
buttonUp = new JButton();
|
||||
buttonUp.setBorderPainted(false);
|
||||
@ -83,61 +68,27 @@ public class FormElectricLocomotive extends JFrame {
|
||||
setLayout(null);
|
||||
|
||||
canvas = new Canvas();
|
||||
canvas.setBounds(0, 0, 980, 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);
|
||||
buttonStep.setBounds(770, 50, 200, 30);
|
||||
comboBoxStrategy.setBounds(770, 10, 200, 30);
|
||||
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);
|
||||
|
||||
|
||||
add(buttonCreateElectricLocomotive);
|
||||
add(buttonCreateLocomotive);
|
||||
add(buttonCreate);
|
||||
add(buttonUp);
|
||||
add(buttonDown);
|
||||
add(buttonRight);
|
||||
add(buttonLeft);
|
||||
add(numberField);
|
||||
add(buttonStep);
|
||||
add(comboBoxStrategy);
|
||||
add(canvas);
|
||||
}
|
||||
private void InitializeLogic(){
|
||||
buttonCreateElectricLocomotive.addActionListener(
|
||||
buttonCreate.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 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,
|
||||
canvas.getWidth(), canvas.getHeight()
|
||||
);
|
||||
_drawningLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
buttonCreateLocomotive.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
int countWheels;
|
||||
try {
|
||||
countWheels = Integer.parseInt(numberField.getText());
|
||||
@ -147,68 +98,38 @@ public class FormElectricLocomotive extends JFrame {
|
||||
}
|
||||
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)),
|
||||
_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)),
|
||||
countWheels,
|
||||
canvas.getWidth(), canvas.getHeight()
|
||||
);
|
||||
_drawningLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,
|
||||
1000, 560);
|
||||
_drawningElectricLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
buttonStep.addActionListener(
|
||||
new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.out.println(((JButton)(e.getSource())).getName());
|
||||
if (_drawningLocomotive == null){
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.isEnabled()){
|
||||
_abstractStrategy = (AbstractStrategy) comboBoxStrategy.getSelectedItem();
|
||||
if (_abstractStrategy == null){
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new DrawningObjectLocomotive(_drawningLocomotive),
|
||||
canvas.getWidth(), canvas.getHeight());
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
}
|
||||
if (_abstractStrategy == null){
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish){
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
System.out.println(((JButton)(e.getSource())).getName());
|
||||
if (_drawningLocomotive == null)
|
||||
if (_drawningElectricLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch(((JButton)(e.getSource())).getName()){
|
||||
case "up":
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Up);
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "down":
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Down);
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "left":
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Left);
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "right":
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Right);
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
|
@ -1,93 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public interface IMoveableObject {
|
||||
public ObjectParameters GetObjectPosition();
|
||||
int GetStep();
|
||||
boolean CheckCanMove(DirectionType direction);
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
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.RightBorder() <= FieldWidth() &&
|
||||
objParams.RightBorder() + GetStep() >= FieldWidth() &&
|
||||
objParams.DownBorder() <= FieldHeight() &&
|
||||
objParams.DownBorder() + GetStep() >= FieldHeight();
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return "идти к краю экрана";
|
||||
}
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
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() <= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleVertical() <= FieldHeight() / 2 &&
|
||||
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight() / 2;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return "идти к центру экрана";
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user