diff --git a/DoubleDeckerBus/pom.xml b/DoubleDeckerBus/pom.xml index effcc0d..62d9a1d 100644 --- a/DoubleDeckerBus/pom.xml +++ b/DoubleDeckerBus/pom.xml @@ -69,7 +69,7 @@ default-cli - com.example.doubledeckerbus/com.example.doubledeckerbus.FormBus + com.example.doubledeckerbus/com.example.doubledeckerbus.Form app app diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/AbstractMap.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/AbstractMap.java new file mode 100644 index 0000000..0c8d9fb --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/AbstractMap.java @@ -0,0 +1,119 @@ +package com.example.doubledeckerbus; + +import javafx.scene.canvas.GraphicsContext; + +import java.util.Random; + +public abstract class AbstractMap { + private IDrawingObject _drawingObject = null; + protected int[][] _map = null; + protected int _width; + protected int _height; + protected float _size_x; + protected float _size_y; + protected final Random _random = new Random(); + protected final int _freeRoad = 0; + protected final int _barrier = 1; + + protected GraphicsContext gc; + + public void CreateMap(int width, int height, IDrawingObject drawingObject, GraphicsContext gc) + { + _width = width; + _height = height; + _drawingObject = drawingObject; + this.gc = gc; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + DrawMapWithObject(); + } + + private boolean SetObjectOnMap() + { + if (_drawingObject == null || _map == null) + { + return false; + } + int x = _random.nextInt(0, 10); + int y = _random.nextInt(0, 10); + _drawingObject.SetObject(x, y, _width, _height); + return CheckCollision(); + } + + private void DrawMapWithObject() + { + if (_drawingObject == null || _map == null) + { + return; + } + + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + if (_map[i][j] == _freeRoad) + { + DrawRoadPart(i, j); + } + else if (_map[i][j] == _barrier) + { + DrawBarrierPart(i, j); + } + } + } + _drawingObject.DrawingObject(gc); + } + + public void MoveObject(Direction direction) + { + _drawingObject.MoveObject(direction); + if (CheckCollision()) { + switch (direction) { + case Left -> _drawingObject.MoveObject(Direction.Right); + case Right -> _drawingObject.MoveObject(Direction.Left); + case Up -> _drawingObject.MoveObject(Direction.Down); + case Down -> _drawingObject.MoveObject(Direction.Up); + } + + } + DrawMapWithObject(); + } + + private boolean CheckCollision() { + var pos = _drawingObject.GetCurrentPosition(); + int startX = (int)((pos.Left) / _size_x); + int endX = (int)((pos.Right) / _size_x); + int startY = (int)((pos.Top) / _size_y); + int endY = (int)((pos.Bottom) / _size_y); + + if (startX < 0 || startY < 0 || endX > _map[0].length || endY > _map.length) { + return false; + } + + for (int y = startY; y <= endY; y++) + { + for (int x = startX; x <= endX; x++) + { + try { + if (_map[x][y] == _barrier) + { + return true; + } + } + catch (ArrayIndexOutOfBoundsException e) { + continue; + } + } + } + + return false; + } + + + protected abstract void GenerateMap(); + protected abstract void DrawRoadPart(int i, int j); + protected abstract void DrawBarrierPart(int i, int j); +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java index ada3280..47f98da 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java @@ -17,40 +17,40 @@ import java.util.Random; public class ControllerBus { - private DrawingBus _bus; - ObservableList countOfDoors = FXCollections.observableArrayList(3, 4, 5); + protected DrawingBus _bus; + protected ObservableList countOfDoors = FXCollections.observableArrayList(3, 4, 5); @FXML - private Button buttonCreate; + protected Button buttonCreate; @FXML - private Button buttonDown; + protected Button buttonDown; @FXML - private Button buttonLeft; + protected Button buttonLeft; @FXML - private Button buttonRight; + protected Button buttonRight; @FXML - private Button buttonUp; + protected Button buttonUp; @FXML - private Canvas canvasBus; + protected Canvas canvasBus; @FXML - private AnchorPane pictureBoxBus; + protected AnchorPane pictureBoxBus; @FXML - private Label statusColor; + protected Label statusColor; @FXML - private Label statusSpeed; + protected Label statusSpeed; @FXML - private Label statusWeight; + protected Label statusWeight; @FXML - private ChoiceBox choiceDoors; + protected ChoiceBox choiceDoors; @FXML void ButtonCreate_Click(ActionEvent event) { @@ -58,14 +58,18 @@ public class ControllerBus { pictureBoxBus.heightProperty().addListener(listener); Random rnd = new Random(); - _bus = new DrawingBus(); - _bus.Init(rnd.nextInt(100, 300), rnd.nextFloat(1000, 2000), - Color.rgb(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), choiceDoors.getValue()); + _bus = new DrawingBus(rnd.nextInt(100, 300), rnd.nextFloat(1000, 2000), + Color.rgb(rnd.nextInt(256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), choiceDoors.getValue()); + _bus.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), (int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight()); + BorderChanged(); + } + + void SetData() { + Random rnd = new Random(); _bus.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), (int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight()); statusSpeed.setText("Скорость: %s".formatted(_bus.Bus.Speed)); statusWeight.setText("Вес: %s".formatted(_bus.Bus.Weight)); statusColor.setText("Цвет: %s".formatted(_bus.Bus.BodyColor)); - BorderChanged(); } @FXML @@ -102,4 +106,17 @@ public class ControllerBus { Draw(); } + @FXML + private void ButtonCreateExtra_Click(ActionEvent event) { + pictureBoxBus.widthProperty().addListener(listener); + pictureBoxBus.heightProperty().addListener(listener); + Random rnd = new Random(); + _bus = new DrawingDDB(rnd.nextInt(300), rnd.nextInt(2000), + Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + choiceDoors.getValue(), + Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), rnd.nextBoolean()); + SetData(); + BorderChanged(); + } } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMap.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMap.java new file mode 100644 index 0000000..7a2ea31 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMap.java @@ -0,0 +1,118 @@ +package com.example.doubledeckerbus; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.control.Button; +import javafx.scene.control.ChoiceBox; +import javafx.scene.control.Label; +import javafx.scene.layout.AnchorPane; +import javafx.scene.paint.Color; + +import java.util.Random; + +public class ControllerMap{ + private AbstractMap _abstractMap; + protected ObservableList countOfDoors = FXCollections.observableArrayList(3, 4, 5); + protected ObservableList countOfMap = FXCollections.observableArrayList("Простая карта", "Водная карта"); + + @FXML + private ChoiceBox choiceMap; + + @FXML + protected Button buttonCreate; + + @FXML + protected Button buttonDown; + + @FXML + protected Button buttonLeft; + + @FXML + protected Button buttonRight; + + @FXML + protected Button buttonUp; + + @FXML + protected Canvas canvasBus; + + @FXML + protected AnchorPane pictureBoxBus; + + @FXML + protected Label statusColor; + + @FXML + protected Label statusSpeed; + + @FXML + protected Label statusWeight; + + @FXML + protected ChoiceBox choiceDoors; + + @FXML + void ButtonCreate_Click(ActionEvent event) { + BorderChanged(); + Random rnd = new Random(); + var bus = new DrawingBus(rnd.nextInt(100, 300), rnd.nextFloat(1000, 2000), + Color.rgb(rnd.nextInt(256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + choiceDoors.getValue()); + SetData(bus); + } + + @FXML + public void initialize() { + choiceDoors.setItems(countOfDoors); + choiceDoors.setValue(countOfDoors.get(0)); + choiceMap.setItems(countOfMap); + choiceMap.setValue(countOfMap.get(0)); + + _abstractMap = new SimpleMap(); + } + + @FXML + void ButtonCreateExtra_Click() { + canvasBus.setWidth(pictureBoxBus.getWidth()); + canvasBus.setHeight(pictureBoxBus.getHeight()); + Random rnd = new Random(); + var bus = new DrawingDDB(rnd.nextInt(300), rnd.nextInt(2000), + Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + choiceDoors.getValue(), + Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), rnd.nextBoolean()); + SetData(bus); + } + + @FXML + void ButtonMove_Click(ActionEvent event) { + String name = ((Button) event.getSource()).getId(); + Direction dir = Direction.None; + switch (name) { + case "buttonUp" -> dir = Direction.Up; + case "buttonDown" -> dir = Direction.Down; + case "buttonLeft" -> dir = Direction.Left; + case "buttonRight" -> dir = Direction.Right; + } + _abstractMap.MoveObject(dir); + } + + private void BorderChanged() { + canvasBus.setWidth(pictureBoxBus.getWidth()); + canvasBus.setHeight(pictureBoxBus.getHeight()); + } + + private void SetData(DrawingBus bus) + { + statusSpeed.setText("Скорость: %s".formatted(bus.Bus.Speed)); + statusWeight.setText("Вес: %s".formatted(bus.Bus.Weight)); + statusColor.setText("Цвет: %s".formatted(bus.Bus.BodyColor)); + GraphicsContext gc = canvasBus.getGraphicsContext2D(); + _abstractMap.CreateMap((int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight(), + new DrawingObjectBus(bus), gc); + } +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Direction.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Direction.java index 66ea455..965f271 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Direction.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Direction.java @@ -1,6 +1,7 @@ package com.example.doubledeckerbus; public enum Direction { + None(0), Up(1), Down(2), Left(3), diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java index 29f30a7..a15beae 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java @@ -13,26 +13,26 @@ public class DrawingBus { } private static final int _null = -1000; - private float _startPosX; - private float _startPosY; + protected float _startPosX; + protected float _startPosY; private int _pictureWidth; private int _pictureHeight; private static final int _busWidth = 100; private static final int _busHeight = 50; - public void Init(int speed, float weight, Color bodyColor, int countOfDoors) { - Bus = new EntityBus(); - Bus.Init(speed, weight, bodyColor); + public DrawingBus(int speed, float weight, Color bodyColor, int countOfDoors) { + Bus = new EntityBus(speed, weight, bodyColor); Doors = new DrawingDoors(); Doors.setCountOfDoors(countOfDoors); } public void SetPosition(int x, int y, int width, int height) { - if (_pictureWidth <= _busWidth || _pictureHeight <= _busHeight) { + if (width <= _busWidth || height <= _busHeight) { _pictureWidth = _null; _pictureHeight = _null; return; } + _startPosX = x; _startPosY = y; _pictureWidth = width; @@ -81,7 +81,7 @@ public class DrawingBus { return; } - gc.clearRect(0, 0, _pictureWidth, _pictureHeight); + gc.setFill(Bus.BodyColor); gc.fillRect(_startPosX, _startPosY + 10, 100, 30); @@ -119,4 +119,8 @@ public class DrawingBus { _startPosY = _pictureHeight - _busHeight; } } + + public Position GetCurrentPosition() { + return new Position(_startPosX, _startPosX + _busWidth, _startPosY, _startPosY + _busHeight); + } } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java new file mode 100644 index 0000000..8b9c17f --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java @@ -0,0 +1,46 @@ +package com.example.doubledeckerbus; + +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class DrawingDDB extends DrawingBus{ + public DrawingDDB(int speed, float weight, Color bodyColor, int countOfDoors, Color extraColor, boolean ladder, boolean secondStage) { + super(speed, weight, bodyColor, countOfDoors); + Bus = new EntityDDB(speed, weight, bodyColor, extraColor, ladder, secondStage); + } + + @Override + public void DrawTransport(GraphicsContext gc) + { + if (Bus == null) return; + + _startPosY += 30; + super.DrawTransport(gc); + _startPosY -= 30; + EntityDDB ddb = (EntityDDB) Bus; + + gc.setFill(ddb.ExtraColor); + if (ddb.SecondStage) { + gc.fillRect(_startPosX, _startPosY + 10, 100, 30); + gc.setFill(Color.BLACK); + gc.fillRect(_startPosX + 30, _startPosY + 20, 10, 20); + + gc.setFill(Color.BLUE); + gc.fillOval(_startPosX + 10, _startPosY + 15, 10, 15); + gc.fillOval(_startPosX + 50, _startPosY + 15, 10, 15); + gc.fillOval(_startPosX + 70, _startPosY + 15, 10, 15); + gc.fillOval(_startPosX + 90, _startPosY + 15, 10, 15); + } + + if (ddb.Ladder) { + gc.strokeLine(_startPosX, _startPosY + 70, _startPosX, _startPosY + 10); + gc.strokeLine(_startPosX + 10, _startPosY + 70, _startPosX + 10, _startPosY + 10); + + gc.strokeLine(_startPosX, _startPosY + 20, _startPosX + 10, _startPosY + 20); + gc.strokeLine(_startPosX, _startPosY + 30, _startPosX + 10, _startPosY + 30); + gc.strokeLine(_startPosX, _startPosY + 40, _startPosX + 10, _startPosY + 40); + gc.strokeLine(_startPosX, _startPosY + 50, _startPosX + 10, _startPosY + 50); + gc.strokeLine(_startPosX, _startPosY + 60, _startPosX + 10, _startPosY + 60); + } + } +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java new file mode 100644 index 0000000..dc37f2d --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java @@ -0,0 +1,42 @@ +package com.example.doubledeckerbus; + +import javafx.scene.canvas.GraphicsContext; + +public class DrawingObjectBus implements IDrawingObject { + private DrawingBus _bus = null; + + public DrawingObjectBus(DrawingBus bus) + { + _bus = bus; + } + + public float Step() { + if (_bus != null && _bus.Bus != null) { + return _bus.Bus.Step; + } + return 0; + } + + public Position GetCurrentPosition() + { + if (_bus != null) { + return _bus.GetCurrentPosition(); + } + return null; + } + + public void MoveObject(Direction direction) + { + _bus.MoveTransport(direction); + } + + public void SetObject(int x, int y, int width, int height) + { + _bus.SetPosition(x, y, width, height); + } + + public void DrawingObject(GraphicsContext gc) + { + _bus.DrawTransport(gc); + } +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/EntityBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/EntityBus.java index 832cff3..6e5643b 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/EntityBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/EntityBus.java @@ -10,7 +10,7 @@ public class EntityBus { public Color BodyColor; public float Step; - public void Init(int speed, float weight, Color bodyColor) + public EntityBus(int speed, float weight, Color bodyColor) { Random rnd = new Random(); Speed = (speed <= 0) ? rnd.nextInt(50, 150): speed; diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/EntityDDB.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/EntityDDB.java new file mode 100644 index 0000000..f42986d --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/EntityDDB.java @@ -0,0 +1,33 @@ +package com.example.doubledeckerbus; + +import javafx.scene.paint.Color; + +public class EntityDDB extends EntityBus { + + public Color ExtraColor; + + public boolean Ladder; + + public boolean SecondStage; + + + private void setExtraColor(Color extraColor) { + ExtraColor = extraColor; + } + + private void setLadder(boolean ladder) { + Ladder = ladder; + } + + private void setSecondStage(boolean secondStage) { + SecondStage = secondStage; + } + + public EntityDDB(int speed, float height, Color bodyColor, Color extraColor, boolean ladder, boolean secondStage) + { + super(speed, height, bodyColor); + ExtraColor = extraColor; + Ladder = ladder; + SecondStage = secondStage; + } +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/FormBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java similarity index 77% rename from DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/FormBus.java rename to DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java index 56cb9d8..83c9192 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/FormBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java @@ -7,10 +7,10 @@ import javafx.stage.Stage; import java.io.IOException; -public class FormBus extends Application { +public class Form extends Application { @Override public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(FormBus.class.getResource("hello-view.fxml")); + FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormMap.fxml")); Scene scene = new Scene(fxmlLoader.load()); stage.setTitle("DoubleDeckerBus"); stage.setScene(scene); diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/IDrawingObject.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/IDrawingObject.java new file mode 100644 index 0000000..b7e76f9 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/IDrawingObject.java @@ -0,0 +1,15 @@ +package com.example.doubledeckerbus; + +import javafx.scene.canvas.GraphicsContext; + +public interface IDrawingObject { + float Step = -1; + + void SetObject(int x, int y, int width, int height); + + void MoveObject(Direction direction); + + void DrawingObject(GraphicsContext g); + + Position GetCurrentPosition(); +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Position.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Position.java new file mode 100644 index 0000000..74c8da3 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Position.java @@ -0,0 +1,15 @@ +package com.example.doubledeckerbus; + +public class Position { + public float Left; + public float Right; + public float Top; + public float Bottom; + + public Position (float left, float right, float top, float bottom) { + Left = left; + Right = right; + Top = top; + Bottom = bottom; + } +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SimpleMap.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SimpleMap.java new file mode 100644 index 0000000..4d3305c --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SimpleMap.java @@ -0,0 +1,44 @@ +package com.example.doubledeckerbus; + +import javafx.scene.paint.Color; + +public class SimpleMap extends AbstractMap{ + @Override + protected void GenerateMap() { + _map = new int[100][100]; + _size_x = (float)_width / _map.length; + _size_y = (float)_height / _map[0].length; + + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + + int counter = 0; + while (counter < 50) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + counter++; + } + } + } + + @Override + protected void DrawRoadPart(int i, int j) { + gc.setFill(Color.GRAY); + gc.fillRect(i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + } + + @Override + protected void DrawBarrierPart(int i, int j) { + gc.setFill(Color.BLACK); + gc.fillRect(i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + } +} diff --git a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/hello-view.fxml b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBus.fxml similarity index 93% rename from DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/hello-view.fxml rename to DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBus.fxml index cb431c8..5bbdc23 100644 --- a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/hello-view.fxml +++ b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBus.fxml @@ -27,8 +27,8 @@