From 06e6c72dc3c11c9e258bdba052a528ba967a816b Mon Sep 17 00:00:00 2001 From: Stefan-5422 <32109571+Stefan-5422@users.noreply.github.com> Date: Thu, 24 Mar 2022 12:09:12 +0100 Subject: [PATCH] Add items; + Minor changes --- .idea/runConfigurations.xml | 10 ++++ .../yes/yes/behaviours/PlaceBehaviour.java | 17 +++++-- .../yes/entities/machines/TestMachine2.java | 2 +- .../com/yes/yes/entities/parts/Square.java | 14 ++++++ src/main/java/com/yes/yes/utils/Item.java | 49 +++++++++++++++++++ src/main/java/com/yes/yes/utils/Part.java | 19 +++++++ 6 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 .idea/runConfigurations.xml create mode 100644 src/main/java/com/yes/yes/entities/parts/Square.java create mode 100644 src/main/java/com/yes/yes/utils/Item.java create mode 100644 src/main/java/com/yes/yes/utils/Part.java diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/com/yes/yes/behaviours/PlaceBehaviour.java b/src/main/java/com/yes/yes/behaviours/PlaceBehaviour.java index 80fefe4..335d538 100644 --- a/src/main/java/com/yes/yes/behaviours/PlaceBehaviour.java +++ b/src/main/java/com/yes/yes/behaviours/PlaceBehaviour.java @@ -1,12 +1,14 @@ package com.yes.yes.behaviours; -import com.yes.yes.utils.BlockContainer; -import com.yes.yes.utils.Component; -import com.yes.yes.utils.Coordinate; -import com.yes.yes.utils.Entity; +import com.yes.yes.entities.parts.Square; +import com.yes.yes.utils.*; public class PlaceBehaviour extends Component { + /** + * @param entity + * @param blockContainer + */ public PlaceBehaviour(Entity entity, BlockContainer blockContainer) { super(entity, blockContainer); } @@ -26,6 +28,13 @@ public class PlaceBehaviour extends Component { } } } + Item item = new Item(); + for (int i = 0; i < 4; i++) { + for (int i1 = 0; i1 < 4; i1++) { + item.setPart(i, i1, new Square()); + } + } + parent.getChildren().add(item); } @Override diff --git a/src/main/java/com/yes/yes/entities/machines/TestMachine2.java b/src/main/java/com/yes/yes/entities/machines/TestMachine2.java index 8516c65..5147af6 100644 --- a/src/main/java/com/yes/yes/entities/machines/TestMachine2.java +++ b/src/main/java/com/yes/yes/entities/machines/TestMachine2.java @@ -9,7 +9,7 @@ import javafx.scene.shape.Rectangle; public class TestMachine2 extends Entity { public TestMachine2() { super(); - this.getChildren().add(new Rectangle(25, 25)); + this.getChildren().add(new Rectangle(50, 50)); } public TestMachine2(BlockContainer blockContainer) { diff --git a/src/main/java/com/yes/yes/entities/parts/Square.java b/src/main/java/com/yes/yes/entities/parts/Square.java new file mode 100644 index 0000000..8358f8a --- /dev/null +++ b/src/main/java/com/yes/yes/entities/parts/Square.java @@ -0,0 +1,14 @@ +package com.yes.yes.entities.parts; + +import javafx.scene.paint.Color; +import javafx.scene.shape.Rectangle; + +public class Square extends com.yes.yes.utils.Part { + public Square() { + var rect = new Rectangle(25,25); + rect.setFill(Color.RED); + rect.setStroke(Color.BLACK); + rect.setStrokeWidth(1); + this.getChildren().add(rect); + } +} diff --git a/src/main/java/com/yes/yes/utils/Item.java b/src/main/java/com/yes/yes/utils/Item.java new file mode 100644 index 0000000..8223c7e --- /dev/null +++ b/src/main/java/com/yes/yes/utils/Item.java @@ -0,0 +1,49 @@ +package com.yes.yes.utils; + +public class Item extends javafx.scene.Group { + final Part[][] parts = new Part[4][4]; + + public void setPart(int layer, int section, Part part) throws IllegalArgumentException { + if (layer < 0 || layer > 4 && section < 0 || section > 4) { + throw new IllegalArgumentException("Argument out of range"); + } + + if(parts[layer][section] != null) { + this.getChildren().remove(parts[layer][section]); + } + + + + parts[layer][section] = part; + this.getChildren().add(part); + } + + public Part getPart(int layer, int section) throws IllegalArgumentException { + if (layer < 0 || layer > 4 && section < 0 || section > 4) { + throw new IllegalArgumentException("Argument out of range"); + } + return parts[layer][section]; + } + + public void rotate(int quarters) { + quarters %= 4; + + for (int i = 0; i < quarters; i++) { + for (int l = 0; l < 4; l++) { + Part[] rot = new Part[4]; + + rot[0] = parts[l][2]; + rot[1] = parts[l][0]; + rot[2] = parts[l][3]; + rot[3] = parts[l][1]; + + parts[l] = rot; + } + } + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + parts[i][j].rotate(quarters); + } + } + } +} diff --git a/src/main/java/com/yes/yes/utils/Part.java b/src/main/java/com/yes/yes/utils/Part.java new file mode 100644 index 0000000..55a8bca --- /dev/null +++ b/src/main/java/com/yes/yes/utils/Part.java @@ -0,0 +1,19 @@ +package com.yes.yes.utils; + +import javafx.scene.Node; + +public abstract class Part extends javafx.scene.Group { + public void addChild(Node element) { + this.getChildren().add(element); + } + + public void removeChild(Node element) { + this.getChildren().remove(element); + } + + public void rotate(int quarters) { + + this.getTransforms().clear(); + this.getTransforms().add(new javafx.scene.transform.Rotate(quarters % 4 * 90, 1, 1)); + } +}