lab2 v 0.2
This commit is contained in:
parent
fd6752a9b4
commit
f40d570442
@ -1,12 +1,15 @@
|
|||||||
package com.example.aircraftcarrier;
|
package com.example.aircraftcarrier;
|
||||||
|
|
||||||
|
import com.example.aircraftcarrier.Logic.Drawing.DrawingSimpleAircraftCarrier;
|
||||||
import com.example.aircraftcarrier.Logic.Enum.DirectionType;
|
import com.example.aircraftcarrier.Logic.Enum.DirectionType;
|
||||||
import com.example.aircraftcarrier.Logic.Drawing.DrawingAircraftCarrier;
|
import com.example.aircraftcarrier.Logic.Drawing.DrawingAircraftCarrier;
|
||||||
|
import com.example.aircraftcarrier.Logic.MovementStategy.*;
|
||||||
import javafx.event.Event;
|
import javafx.event.Event;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.fxml.Initializable;
|
import javafx.fxml.Initializable;
|
||||||
import javafx.scene.canvas.Canvas;
|
import javafx.scene.canvas.Canvas;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
|
import javafx.scene.control.ChoiceBox;
|
||||||
import javafx.scene.control.TextField;
|
import javafx.scene.control.TextField;
|
||||||
import javafx.scene.input.KeyCode;
|
import javafx.scene.input.KeyCode;
|
||||||
import javafx.scene.input.KeyEvent;
|
import javafx.scene.input.KeyEvent;
|
||||||
@ -20,7 +23,8 @@ import java.util.ResourceBundle;
|
|||||||
|
|
||||||
public class AircraftCarrierController implements Initializable {
|
public class AircraftCarrierController implements Initializable {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
DrawingAircraftCarrier drawingAircraftCarrier;
|
DrawingSimpleAircraftCarrier drawingAircraftCarrier;
|
||||||
|
AbstractStrategy strategy;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private AnchorPane Root;
|
private AnchorPane Root;
|
||||||
@ -29,17 +33,62 @@ public class AircraftCarrierController implements Initializable {
|
|||||||
@FXML
|
@FXML
|
||||||
private TextField inputCountBlocks;
|
private TextField inputCountBlocks;
|
||||||
@FXML
|
@FXML
|
||||||
protected void buttonCreate_Click() {
|
private ChoiceBox<String> choiceBoxStrategy;
|
||||||
drawingAircraftCarrier = new DrawingAircraftCarrier();
|
private String[] strategyText = new String[]{"к центру", "к углу"};
|
||||||
int inputCountBlock;
|
|
||||||
|
|
||||||
try {
|
@FXML
|
||||||
inputCountBlock = Integer.parseInt(inputCountBlocks.getCharacters().toString());
|
protected void ButtonStrategy_Click() {
|
||||||
} catch (NumberFormatException ignored) {
|
if (drawingAircraftCarrier == null) return;
|
||||||
inputCountBlock = 0;
|
if (!choiceBoxStrategy.isDisable()) {
|
||||||
|
strategy = switch (choiceBoxStrategy.getValue()) {
|
||||||
|
case "к центру" -> new MoveToCenter();
|
||||||
|
case "к углу" -> new MoveToCorner();
|
||||||
|
default -> null;
|
||||||
|
};
|
||||||
|
if (strategy == null) return;
|
||||||
|
strategy.SetDate(new MoveableCarrier(drawingAircraftCarrier),
|
||||||
|
(int)Root.getWidth(),
|
||||||
|
(int)Root.getHeight()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
if (strategy==null) return;
|
||||||
|
|
||||||
drawingAircraftCarrier.Init(
|
choiceBoxStrategy.setDisable(true);
|
||||||
|
strategy.MakeStep();
|
||||||
|
drawingAircraftCarrier.Draw(canvas.getGraphicsContext2D());
|
||||||
|
if (strategy.GetStatus() == StrategyStatus.Finish) {
|
||||||
|
choiceBoxStrategy.setDisable(false);
|
||||||
|
strategy = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
protected void ButtonCreateSimple_Click() {
|
||||||
|
drawingAircraftCarrier = new DrawingSimpleAircraftCarrier(
|
||||||
|
random.nextInt(100) + 100,
|
||||||
|
random.nextInt(1000) + 1000,
|
||||||
|
Color.rgb(
|
||||||
|
random.nextInt(256),
|
||||||
|
random.nextInt(256),
|
||||||
|
random.nextInt(256)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
drawingAircraftCarrier.setCanvasWidth((int) canvas.getWidth());
|
||||||
|
drawingAircraftCarrier.setCanvasHeight((int) canvas.getHeight());
|
||||||
|
if (
|
||||||
|
drawingAircraftCarrier.setPosition(
|
||||||
|
random.nextInt(50) + 10,
|
||||||
|
random.nextInt(50) + 10
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
drawingAircraftCarrier.Draw(canvas.getGraphicsContext2D());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
protected void ButtonCreateAdvanced_Click() {
|
||||||
|
drawingAircraftCarrier = new DrawingAircraftCarrier(
|
||||||
random.nextInt(100) + 100,
|
random.nextInt(100) + 100,
|
||||||
random.nextInt(1000) + 1000,
|
random.nextInt(1000) + 1000,
|
||||||
Color.rgb(
|
Color.rgb(
|
||||||
@ -53,10 +102,14 @@ public class AircraftCarrierController implements Initializable {
|
|||||||
random.nextInt(256)
|
random.nextInt(256)
|
||||||
),
|
),
|
||||||
random.nextBoolean(),
|
random.nextBoolean(),
|
||||||
random.nextBoolean(),
|
random.nextBoolean()
|
||||||
inputCountBlock
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
DrawingAircraftCarrier a = (DrawingAircraftCarrier) drawingAircraftCarrier;
|
||||||
|
a.setDrawingBlock(Integer.parseInt(inputCountBlocks.getCharacters().toString()));
|
||||||
|
} catch (NumberFormatException ignored) {}
|
||||||
|
|
||||||
drawingAircraftCarrier.setCanvasWidth((int) canvas.getWidth());
|
drawingAircraftCarrier.setCanvasWidth((int) canvas.getWidth());
|
||||||
drawingAircraftCarrier.setCanvasHeight((int) canvas.getHeight());
|
drawingAircraftCarrier.setCanvasHeight((int) canvas.getHeight());
|
||||||
if (
|
if (
|
||||||
@ -105,5 +158,6 @@ public class AircraftCarrierController implements Initializable {
|
|||||||
System.out.println("start");
|
System.out.println("start");
|
||||||
canvas.widthProperty().bind(Root.widthProperty());
|
canvas.widthProperty().bind(Root.widthProperty());
|
||||||
canvas.heightProperty().bind(Root.heightProperty());
|
canvas.heightProperty().bind(Root.heightProperty());
|
||||||
|
choiceBoxStrategy.getItems().addAll(strategyText);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,107 +1,52 @@
|
|||||||
package com.example.aircraftcarrier.Logic.Drawing;
|
package com.example.aircraftcarrier.Logic.Drawing;
|
||||||
|
|
||||||
import com.example.aircraftcarrier.Logic.Enum.BlockCount;
|
|
||||||
import com.example.aircraftcarrier.Logic.Enum.DirectionType;
|
|
||||||
import com.example.aircraftcarrier.Logic.Entity.EntityAircraftCarrier;
|
import com.example.aircraftcarrier.Logic.Entity.EntityAircraftCarrier;
|
||||||
import javafx.scene.canvas.GraphicsContext;
|
import javafx.scene.canvas.GraphicsContext;
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
|
|
||||||
public class DrawingAircraftCarrier {
|
public class DrawingAircraftCarrier extends DrawingSimpleAircraftCarrier {
|
||||||
public EntityAircraftCarrier aircraftCarrier;
|
|
||||||
public DrawingBlock drawingBlock;
|
public DrawingBlock drawingBlock;
|
||||||
private int x;
|
public DrawingAircraftCarrier(
|
||||||
private int y;
|
|
||||||
private int widthCanvas;
|
|
||||||
private int heightCanvas;
|
|
||||||
private static final int widthPicture = 250;
|
|
||||||
private static final int heightPicture = 100;
|
|
||||||
|
|
||||||
public void Init(
|
|
||||||
int speed,
|
int speed,
|
||||||
int weight,
|
int weight,
|
||||||
Color primaryColor,
|
Color primaryColor,
|
||||||
Color secondaryColor,
|
Color secondaryColor,
|
||||||
boolean hasDeck,
|
boolean hasDeck,
|
||||||
boolean hasСabin,
|
boolean hasСabin
|
||||||
int countBlock
|
|
||||||
) {
|
) {
|
||||||
aircraftCarrier = new EntityAircraftCarrier();
|
super();
|
||||||
aircraftCarrier.Init(speed,
|
aircraftCarrier = new EntityAircraftCarrier(speed,
|
||||||
weight,
|
weight,
|
||||||
primaryColor,
|
primaryColor,
|
||||||
secondaryColor,
|
secondaryColor,
|
||||||
hasDeck,
|
hasDeck,
|
||||||
hasСabin
|
hasСabin);
|
||||||
);
|
|
||||||
drawingBlock = new DrawingBlock();
|
|
||||||
drawingBlock.setBlockCount(countBlock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDrawingBlock(int count) {
|
public void setDrawingBlock(int count) {
|
||||||
|
drawingBlock = new DrawingBlock();
|
||||||
drawingBlock.setBlockCount(count);
|
drawingBlock.setBlockCount(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCanvasWidth(int width) {
|
@Override
|
||||||
if (x+widthPicture > width) return;
|
|
||||||
widthCanvas = width;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCanvasHeight(int height) {
|
|
||||||
if (y+heightPicture > height) return;
|
|
||||||
heightCanvas = height;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean setPosition(int newX, int newY) {
|
|
||||||
if (newX < 0 || newY < 0 || newX + widthPicture > widthCanvas || newY + heightPicture > heightCanvas) return false;
|
|
||||||
x = newX;
|
|
||||||
y = newY;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Draw(GraphicsContext gc) {
|
public void Draw(GraphicsContext gc) {
|
||||||
gc.clearRect(0,0,widthCanvas,heightCanvas);
|
if (aircraftCarrier == null) return;
|
||||||
|
|
||||||
double[] X = new double[]{x,x+200,x+250,x+200,x};
|
if (aircraftCarrier instanceof EntityAircraftCarrier entityAircraftCarrier) {
|
||||||
double[] Y = new double[]{y,y,y+50,y+100,y+100};
|
|
||||||
gc.strokePolygon(X, Y,5);
|
|
||||||
|
|
||||||
gc.strokeOval(x+175,y+35,30,30);
|
super.Draw(gc);
|
||||||
|
|
||||||
if (aircraftCarrier.isHasDeck()) {
|
if (entityAircraftCarrier.isHasDeck()) {
|
||||||
gc.setFill(aircraftCarrier.getPrimaryColor());
|
gc.setFill(aircraftCarrier.getPrimaryColor());
|
||||||
gc.fillRect(x + 50, y + 40, 55, 20);
|
gc.fillRect(x + 50, y + 40, 55, 20);
|
||||||
}
|
}
|
||||||
if (aircraftCarrier.isHasСabin()) {
|
if (entityAircraftCarrier.isHasСabin()) {
|
||||||
gc.setFill(aircraftCarrier.getSecondaryColor());
|
gc.setFill(entityAircraftCarrier.getSecondaryColor());
|
||||||
gc.fillRect(x + 105, y + 25, 30, 50);
|
gc.fillRect(x + 105, y + 25, 30, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (drawingBlock == null) return;
|
if (drawingBlock == null) return;
|
||||||
drawingBlock.Draw(gc,x,y,aircraftCarrier.getSecondaryColor());
|
drawingBlock.Draw(gc, x, y, entityAircraftCarrier.getSecondaryColor());
|
||||||
}
|
|
||||||
|
|
||||||
public void Move(DirectionType direction) {
|
|
||||||
switch (direction) {
|
|
||||||
case Up:
|
|
||||||
if (y - aircraftCarrier.Step > 0) {
|
|
||||||
y -= aircraftCarrier.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Down:
|
|
||||||
if (y + aircraftCarrier.Step + heightPicture < heightCanvas) {
|
|
||||||
y += aircraftCarrier.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Right:
|
|
||||||
if (x + aircraftCarrier.Step + widthPicture < widthCanvas) {
|
|
||||||
x += aircraftCarrier.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Left:
|
|
||||||
if (x - aircraftCarrier.Step > 0) {
|
|
||||||
x -= aircraftCarrier.Step;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,104 @@
|
|||||||
|
package com.example.aircraftcarrier.Logic.Drawing;
|
||||||
|
|
||||||
|
import com.example.aircraftcarrier.Logic.Entity.EntityAircraftCarrier;
|
||||||
|
import com.example.aircraftcarrier.Logic.Entity.EntitySimpleAircraftCarrier;
|
||||||
|
import com.example.aircraftcarrier.Logic.Enum.DirectionType;
|
||||||
|
import javafx.scene.canvas.GraphicsContext;
|
||||||
|
import javafx.scene.paint.Color;
|
||||||
|
|
||||||
|
public class DrawingSimpleAircraftCarrier {
|
||||||
|
public EntitySimpleAircraftCarrier aircraftCarrier;
|
||||||
|
protected int x;
|
||||||
|
protected int y;
|
||||||
|
private int widthCanvas;
|
||||||
|
private int heightCanvas;
|
||||||
|
private static final int widthPicture = 250;
|
||||||
|
private static final int heightPicture = 100;
|
||||||
|
|
||||||
|
public DrawingSimpleAircraftCarrier() {}
|
||||||
|
|
||||||
|
public DrawingSimpleAircraftCarrier(
|
||||||
|
int speed,
|
||||||
|
int weight,
|
||||||
|
Color primaryColor
|
||||||
|
) {
|
||||||
|
aircraftCarrier = new EntitySimpleAircraftCarrier(
|
||||||
|
speed,
|
||||||
|
weight,
|
||||||
|
primaryColor
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCanvasWidth(int width) {
|
||||||
|
if (x+widthPicture > width);
|
||||||
|
widthCanvas = width;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCanvasHeight(int height) {
|
||||||
|
if (y+heightPicture > height) return;
|
||||||
|
heightCanvas = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean setPosition(int newX, int newY) {
|
||||||
|
if (newX < 0 || newY < 0 || newX + widthPicture > widthCanvas || newY + heightPicture > heightCanvas) return false;
|
||||||
|
x = newX;
|
||||||
|
y = newY;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWidthPicture() {
|
||||||
|
return widthPicture;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeightPicture() {
|
||||||
|
return heightPicture;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Draw(GraphicsContext gc) {
|
||||||
|
gc.clearRect(0,0,widthCanvas,heightCanvas);
|
||||||
|
|
||||||
|
double[] X = new double[]{x,x+200,x+250,x+200,x};
|
||||||
|
double[] Y = new double[]{y,y,y+50,y+100,y+100};
|
||||||
|
gc.strokePolygon(X, Y,5);
|
||||||
|
|
||||||
|
gc.strokeOval(x+175,y+35,30,30);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean Move(DirectionType direction) {
|
||||||
|
switch (direction) {
|
||||||
|
case Up:
|
||||||
|
if (y - aircraftCarrier.getStep() > 0) {
|
||||||
|
y -= aircraftCarrier.getStep();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
case Down:
|
||||||
|
if (y + aircraftCarrier.getStep() + heightPicture < heightCanvas) {
|
||||||
|
y += aircraftCarrier.getStep();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
case Right:
|
||||||
|
if (x + aircraftCarrier.getStep() + widthPicture < widthCanvas) {
|
||||||
|
x += aircraftCarrier.getStep();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
case Left:
|
||||||
|
if (x - aircraftCarrier.getStep() > 0) {
|
||||||
|
x -= aircraftCarrier.getStep();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
default: return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,43 +2,24 @@ package com.example.aircraftcarrier.Logic.Entity;
|
|||||||
|
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
|
|
||||||
public class EntityAircraftCarrier {
|
public class EntityAircraftCarrier extends EntitySimpleAircraftCarrier {
|
||||||
private int Speed;
|
|
||||||
private double Weight;
|
|
||||||
public int Step;
|
|
||||||
private Color PrimaryColor;
|
|
||||||
private Color SecondaryColor;
|
private Color SecondaryColor;
|
||||||
private boolean HasDeck;
|
private boolean HasDeck;
|
||||||
private boolean HasСabin;
|
private boolean HasСabin;
|
||||||
|
|
||||||
|
|
||||||
public void Init(
|
public EntityAircraftCarrier(
|
||||||
int speed,
|
int speed,
|
||||||
double weight,
|
int weight,
|
||||||
Color primaryColor,
|
Color primaryColor,
|
||||||
Color secondaryColor,
|
Color secondaryColor,
|
||||||
boolean hasDeck,
|
boolean hasDeck,
|
||||||
boolean hasСabin
|
boolean hasСabin
|
||||||
) {
|
){
|
||||||
this.Speed = speed;
|
super(speed,weight,primaryColor);
|
||||||
this.Weight = weight;
|
|
||||||
this.PrimaryColor = primaryColor;
|
|
||||||
this.SecondaryColor = secondaryColor;
|
this.SecondaryColor = secondaryColor;
|
||||||
this.HasDeck = hasDeck;
|
this.HasDeck = hasDeck;
|
||||||
this.HasСabin = hasСabin;
|
this.HasСabin = hasСabin;
|
||||||
this.Step = (int) (Speed * 100 / Weight);
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSpeed() {
|
|
||||||
return Speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double getWeight() {
|
|
||||||
return Weight;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color getPrimaryColor() {
|
|
||||||
return PrimaryColor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Color getSecondaryColor() {
|
public Color getSecondaryColor() {
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.example.aircraftcarrier.Logic.Entity;
|
||||||
|
|
||||||
|
import javafx.scene.paint.Color;
|
||||||
|
|
||||||
|
public class EntitySimpleAircraftCarrier {
|
||||||
|
private int Speed;
|
||||||
|
private double Weight;
|
||||||
|
private Color PrimaryColor;
|
||||||
|
|
||||||
|
public EntitySimpleAircraftCarrier(
|
||||||
|
int speed,
|
||||||
|
double weight,
|
||||||
|
Color primaryColor
|
||||||
|
) {
|
||||||
|
this.Speed = speed;
|
||||||
|
this.Weight = weight;
|
||||||
|
this.PrimaryColor = primaryColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSpeed() {
|
||||||
|
return Speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getWeight() {
|
||||||
|
return Weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color getPrimaryColor() {
|
||||||
|
return PrimaryColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getStep() {
|
||||||
|
return (int) (Speed * 100 / Weight);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package com.example.aircraftcarrier.Logic.MovementStategy;
|
||||||
|
|
||||||
|
public abstract class AbstractStrategy {
|
||||||
|
private IMoveableObject moveableObject;
|
||||||
|
private StrategyStatus status = StrategyStatus.NotInit;
|
||||||
|
protected int FieldWidth;
|
||||||
|
protected int FieldHeight;
|
||||||
|
|
||||||
|
public int getFieldWidth() {
|
||||||
|
return FieldWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getFieldHeight() {
|
||||||
|
return FieldHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StrategyStatus GetStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetDate(IMoveableObject moveableObject,
|
||||||
|
int width,
|
||||||
|
int height) {
|
||||||
|
if (moveableObject == null) {
|
||||||
|
status = StrategyStatus.NotInit;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
status = StrategyStatus.InProgress;
|
||||||
|
this.moveableObject = moveableObject;
|
||||||
|
this.FieldWidth = width;
|
||||||
|
this.FieldHeight = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void MakeStep() {
|
||||||
|
if(status != StrategyStatus.InProgress) return;
|
||||||
|
if (IsTargetDestination()) {
|
||||||
|
status = StrategyStatus.Finish;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MoveToTarget();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void MoveToTarget();
|
||||||
|
protected abstract boolean IsTargetDestination();
|
||||||
|
|
||||||
|
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 Integer GetStep() {
|
||||||
|
if (status != StrategyStatus.InProgress) return null;
|
||||||
|
return moveableObject.GetStep();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean MoveTo(MovementDirection direction) {
|
||||||
|
if (moveableObject==null || status != StrategyStatus.InProgress) return false;
|
||||||
|
return moveableObject.TryMoveObject(direction);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.aircraftcarrier.Logic.MovementStategy;
|
||||||
|
|
||||||
|
public interface IMoveableObject {
|
||||||
|
ObjectParameters GetObjectPosition();
|
||||||
|
int GetStep();
|
||||||
|
boolean TryMoveObject(MovementDirection direction);
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.example.aircraftcarrier.Logic.MovementStategy;
|
||||||
|
|
||||||
|
public class MoveToCenter extends AbstractStrategy {
|
||||||
|
@Override
|
||||||
|
protected void MoveToTarget() {
|
||||||
|
ObjectParameters objectParameters = GetObjectParameters();
|
||||||
|
if (objectParameters == null) return;
|
||||||
|
int diffX = objectParameters.CenterX() - getFieldWidth()/2;
|
||||||
|
if (Math.abs(diffX) > GetStep()) {
|
||||||
|
if (diffX > 0) {
|
||||||
|
MoveLeft();
|
||||||
|
} else { MoveRight(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
int diffY = objectParameters.CenterY() - getFieldHeight()/2;
|
||||||
|
if (Math.abs(diffY) > GetStep()) {
|
||||||
|
if (diffY > 0) {
|
||||||
|
MoveUp();
|
||||||
|
} else { MoveDown(); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean IsTargetDestination() {
|
||||||
|
ObjectParameters objectParameters = GetObjectParameters();
|
||||||
|
if (objectParameters == null) return false;
|
||||||
|
return objectParameters.CenterX() - GetStep() <= getFieldWidth()/2 &&
|
||||||
|
objectParameters.CenterX() + GetStep() >= getFieldWidth()/2 &&
|
||||||
|
objectParameters.CenterY() - GetStep() <= getFieldHeight()/2 &&
|
||||||
|
objectParameters.CenterY() + GetStep() >= getFieldHeight()/2;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.example.aircraftcarrier.Logic.MovementStategy;
|
||||||
|
|
||||||
|
public class MoveToCorner extends AbstractStrategy {
|
||||||
|
@Override
|
||||||
|
protected void MoveToTarget() {
|
||||||
|
ObjectParameters objectParameters = GetObjectParameters();
|
||||||
|
if (objectParameters == null) return;
|
||||||
|
int diffX = objectParameters.RightBorder() - getFieldWidth();
|
||||||
|
if (Math.abs(diffX) > GetStep()) {
|
||||||
|
MoveRight();
|
||||||
|
}
|
||||||
|
|
||||||
|
int diffY = objectParameters.BottomBorder() - getFieldHeight();
|
||||||
|
if (Math.abs(diffY) > GetStep()) {
|
||||||
|
MoveDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected boolean IsTargetDestination() {
|
||||||
|
ObjectParameters objectParameters = GetObjectParameters();
|
||||||
|
if (objectParameters == null) return false;
|
||||||
|
return objectParameters.RightBorder() + GetStep() >= getFieldWidth() &&
|
||||||
|
objectParameters.RightBorder() + GetStep() >= getFieldHeight();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.example.aircraftcarrier.Logic.MovementStategy;
|
||||||
|
|
||||||
|
import com.example.aircraftcarrier.Logic.Drawing.DrawingSimpleAircraftCarrier;
|
||||||
|
import com.example.aircraftcarrier.Logic.Enum.DirectionType;
|
||||||
|
|
||||||
|
public class MoveableCarrier implements IMoveableObject {
|
||||||
|
private final DrawingSimpleAircraftCarrier drawingSimpleAircraftCarrier;
|
||||||
|
|
||||||
|
public MoveableCarrier(DrawingSimpleAircraftCarrier drawingSimpleAircraftCarrier) {
|
||||||
|
this.drawingSimpleAircraftCarrier = drawingSimpleAircraftCarrier;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ObjectParameters GetObjectPosition() {
|
||||||
|
if (drawingSimpleAircraftCarrier == null || drawingSimpleAircraftCarrier.aircraftCarrier == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new ObjectParameters(drawingSimpleAircraftCarrier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int GetStep() {
|
||||||
|
return drawingSimpleAircraftCarrier.aircraftCarrier.getStep();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean TryMoveObject(MovementDirection direction) {
|
||||||
|
if (drawingSimpleAircraftCarrier == null || drawingSimpleAircraftCarrier.aircraftCarrier == null)
|
||||||
|
return false;
|
||||||
|
return drawingSimpleAircraftCarrier.Move(GetDirectionType(direction));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static DirectionType GetDirectionType(MovementDirection direction) {
|
||||||
|
return switch (direction) {
|
||||||
|
case Up -> DirectionType.Up;
|
||||||
|
case Down -> DirectionType.Down;
|
||||||
|
case Left -> DirectionType.Left;
|
||||||
|
case Right -> DirectionType.Right;
|
||||||
|
default -> DirectionType.Unknown;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.example.aircraftcarrier.Logic.MovementStategy;
|
||||||
|
|
||||||
|
public enum MovementDirection {
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package com.example.aircraftcarrier.Logic.MovementStategy;
|
||||||
|
|
||||||
|
import com.example.aircraftcarrier.Logic.Drawing.DrawingSimpleAircraftCarrier;
|
||||||
|
|
||||||
|
public class ObjectParameters {
|
||||||
|
private final int x;
|
||||||
|
private final int y;
|
||||||
|
private final int width;
|
||||||
|
private final int height;
|
||||||
|
|
||||||
|
public ObjectParameters(int x, int y, int width, int height) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectParameters(DrawingSimpleAircraftCarrier drawingSimpleAircraftCarrier) {
|
||||||
|
this.x = drawingSimpleAircraftCarrier.getX();
|
||||||
|
this.y = drawingSimpleAircraftCarrier.getY();
|
||||||
|
this.width = drawingSimpleAircraftCarrier.getWidthPicture();
|
||||||
|
this.height = drawingSimpleAircraftCarrier.getHeightPicture();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int LeftBorder() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
public int RightBorder() {
|
||||||
|
return x+width;
|
||||||
|
}
|
||||||
|
public int TopBorder() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
public int BottomBorder() {
|
||||||
|
return y+height;
|
||||||
|
}
|
||||||
|
public int CenterX() {
|
||||||
|
return x+width / 2;
|
||||||
|
}
|
||||||
|
public int CenterY() {
|
||||||
|
return y+height/2;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.aircraftcarrier.Logic.MovementStategy;
|
||||||
|
|
||||||
|
public enum StrategyStatus {
|
||||||
|
NotInit,
|
||||||
|
InProgress,
|
||||||
|
Finish
|
||||||
|
}
|
@ -7,11 +7,14 @@
|
|||||||
<AnchorPane fx:id="Root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#MoveEvent" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.aircraftcarrier.AircraftCarrierController">
|
<AnchorPane fx:id="Root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#MoveEvent" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.aircraftcarrier.AircraftCarrierController">
|
||||||
<children>
|
<children>
|
||||||
<Canvas fx:id="canvas" height="400.0" width="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
|
<Canvas fx:id="canvas" height="400.0" width="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
|
||||||
<Button layoutX="14.0" layoutY="361.0" mnemonicParsing="false" onAction="#buttonCreate_Click" text="Создать" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" />
|
<Button layoutX="14.0" layoutY="361.0" mnemonicParsing="false" onAction="#ButtonCreateSimple_Click" text="Создать простую" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" />
|
||||||
|
<Button layoutX="130.0" layoutY="354.0" mnemonicParsing="false" onAction="#ButtonCreateAdvanced_Click" text="Создать сложную" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="135.0" />
|
||||||
<Button fx:id="up" layoutX="524.4" layoutY="324.4" minHeight="25.0" minWidth="25.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url("up-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="50.0" AnchorPane.rightAnchor="50.0" />
|
<Button fx:id="up" layoutX="524.4" layoutY="324.4" minHeight="25.0" minWidth="25.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url("up-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="50.0" AnchorPane.rightAnchor="50.0" />
|
||||||
<Button fx:id="right" layoutX="554.4" layoutY="354.4" minHeight="25.0" minWidth="25.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url("right-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="20.0" />
|
<Button fx:id="right" layoutX="554.4" layoutY="354.4" minHeight="25.0" minWidth="25.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url("right-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="20.0" />
|
||||||
<Button fx:id="down" layoutX="524.4" layoutY="354.4" minHeight="25.0" minWidth="25.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url("down-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="50.0" />
|
<Button fx:id="down" layoutX="524.4" layoutY="354.4" minHeight="25.0" minWidth="25.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url("down-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="50.0" />
|
||||||
<Button fx:id="left" layoutX="494.4" layoutY="354.4" minHeight="25.0" minWidth="25.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url("left-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="80.0" />
|
<Button fx:id="left" layoutX="494.4" layoutY="354.4" minHeight="25.0" minWidth="25.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url("left-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="80.0" />
|
||||||
<TextField fx:id="inputCountBlocks" layoutX="446.0" layoutY="14.0" promptText="количество блоков" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0" />
|
<TextField fx:id="inputCountBlocks" layoutX="446.0" layoutY="14.0" promptText="количество блоков" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0" />
|
||||||
|
<ChoiceBox fx:id="choiceBoxStrategy" layoutX="430.0" layoutY="46.0" prefHeight="26.0" prefWidth="110.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="50.0" />
|
||||||
|
<Button layoutX="524.0" layoutY="83.0" mnemonicParsing="false" onAction="#ButtonStrategy_Click" text="шаг" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="80.0" />
|
||||||
</children>
|
</children>
|
||||||
</AnchorPane>
|
</AnchorPane>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user