From a8eb398b0a01fb6996d3a53292f184888510cd3f Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Fri, 20 May 2022 15:44:26 +0200 Subject: [PATCH] Implement horizontal cutter --- .../yes/yes/behaviours/CutItemBehaviour.java | 65 +++++++++++++++++++ .../com/yes/yes/entities/machines/Cutter.java | 19 +++--- src/main/java/com/yes/yes/utils/Item.java | 7 +- .../java/com/yes/yes/utils/Orientation.java | 6 ++ 4 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/yes/yes/behaviours/CutItemBehaviour.java create mode 100644 src/main/java/com/yes/yes/utils/Orientation.java diff --git a/src/main/java/com/yes/yes/behaviours/CutItemBehaviour.java b/src/main/java/com/yes/yes/behaviours/CutItemBehaviour.java new file mode 100644 index 0000000..0cc9762 --- /dev/null +++ b/src/main/java/com/yes/yes/behaviours/CutItemBehaviour.java @@ -0,0 +1,65 @@ +package com.yes.yes.behaviours; + +import com.yes.yes.utils.*; + +public class CutItemBehaviour extends Component { + private final Orientation orientation; + private final String receiveDataKey; + private final String offerDataKey1; + private final String offerDataKey2; + private final int processingDuration; + private int progress = 0; + + public CutItemBehaviour(Entity entity, BlockContainer blockContainer, Orientation orientation, String receiveDataKey, String offerDataKey1, String offerDataKey2, int processingDuration) { + super(entity, blockContainer); + this.orientation = orientation; + this.receiveDataKey = receiveDataKey; + this.offerDataKey1 = offerDataKey1; + this.offerDataKey2 = offerDataKey2; + this.processingDuration = processingDuration; + } + + @Override + public void initialize() { + + } + + @Override + public void update() { + if (progress > processingDuration) { + if (parent.getData(offerDataKey1) != null || parent.getData(offerDataKey2) != null) return; + + + Item half1 = new Item(); + Item half2 = new Item(); + + Item item = parent.getData(receiveDataKey); + for (int i = 0; i < 4; i++) { + if (orientation == Orientation.Horizontal) { + half1.setPart(i, 0, item.getPart(i, 0)); + half1.setPart(i, 1, item.getPart(i, 1)); + half2.setPart(i, 2, item.getPart(i, 2)); + half2.setPart(i, 3, item.getPart(i, 3)); + } else { + half1.setPart(i, 0, item.getPart(i, 0)); + half1.setPart(i, 2, item.getPart(i, 2)); + half2.setPart(i, 1, item.getPart(i, 1)); + half2.setPart(i, 3, item.getPart(i, 3)); + } + } + + parent.setData(offerDataKey1, half1); + parent.setData(offerDataKey2, half2); + parent.setData(receiveDataKey, null); + + progress = 0; + } else if (parent.getData(receiveDataKey) != null) { + progress++; + } + } + + @Override + public void destroy() { + + } +} diff --git a/src/main/java/com/yes/yes/entities/machines/Cutter.java b/src/main/java/com/yes/yes/entities/machines/Cutter.java index 58f3f5f..7243bf4 100644 --- a/src/main/java/com/yes/yes/entities/machines/Cutter.java +++ b/src/main/java/com/yes/yes/entities/machines/Cutter.java @@ -1,22 +1,23 @@ package com.yes.yes.entities.machines; -import com.yes.yes.behaviours.DestroyBehaviour; +import com.yes.yes.behaviours.CutItemBehaviour; +import com.yes.yes.behaviours.OfferBehaviour; import com.yes.yes.behaviours.PlaceBehaviour; import com.yes.yes.behaviours.ReceiveBehaviour; import com.yes.yes.utils.BlockContainer; import com.yes.yes.utils.Direction; import com.yes.yes.utils.Entity; +import com.yes.yes.utils.Orientation; import com.yes.yes.world.Chunk; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Polygon; -import javafx.scene.shape.Rectangle; public class Cutter extends Entity { public Cutter() { super(); - Circle c = new Circle(Chunk.ENTITY_SIZE/2); + Circle c = new Circle(Chunk.ENTITY_SIZE / 2); c.setFill(Color.BLACK); c.setCenterX(Chunk.ENTITY_SIZE / 2); c.setCenterY(Chunk.ENTITY_SIZE / 2); @@ -24,13 +25,12 @@ public class Cutter extends Entity { Polygon p = new Polygon(); p.getPoints().addAll( Chunk.ENTITY_SIZE / 2d, 0.0, - Chunk.ENTITY_SIZE/4d, (double) Chunk.ENTITY_SIZE, - (double) Chunk.ENTITY_SIZE/4d*3, (double) Chunk.ENTITY_SIZE); + Chunk.ENTITY_SIZE / 4d, (double) Chunk.ENTITY_SIZE, + (double) Chunk.ENTITY_SIZE / 4d * 3, (double) Chunk.ENTITY_SIZE); p.setFill(new Color(1, 1, 1, 0.5)); - - this.getChildren().addAll(c,p); + this.getChildren().addAll(c, p); } @SuppressWarnings("unused") // Called dynamically @@ -38,6 +38,9 @@ public class Cutter extends Entity { this(); this.addBehaviour(new PlaceBehaviour(this, blockContainer)); this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.DOWN, "Item")); - //TODO: Add split/modify behaviour + this.addBehaviour(new CutItemBehaviour(this, blockContainer, Orientation.Horizontal, "ReceivedItem", "OfferItemLeft", "OfferItemRight", 5)); + this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.DOWN, "ReceivedItem")); + this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.LEFT, "OfferItemLeft")); + this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.RIGHT, "OfferItemRight")); } } diff --git a/src/main/java/com/yes/yes/utils/Item.java b/src/main/java/com/yes/yes/utils/Item.java index ff93540..28100dc 100644 --- a/src/main/java/com/yes/yes/utils/Item.java +++ b/src/main/java/com/yes/yes/utils/Item.java @@ -14,11 +14,14 @@ public class Item extends javafx.scene.Group implements Cloneable { this.getChildren().remove(parts[layer][section]); } - part.rotate(section); + parts[layer][section] = part; + if (part == null) return; + + part.rotate(section); part.getTransforms().add(new Scale(-layer * 0.25f + 1f, -layer * 0.25f + 1f)); - parts[layer][section] = part; + this.getChildren().add(part); } diff --git a/src/main/java/com/yes/yes/utils/Orientation.java b/src/main/java/com/yes/yes/utils/Orientation.java new file mode 100644 index 0000000..2d3132a --- /dev/null +++ b/src/main/java/com/yes/yes/utils/Orientation.java @@ -0,0 +1,6 @@ +package com.yes.yes.utils; + +public enum Orientation { + Horizontal, + Vertical +}