Fix Conveyor belts

This commit is contained in:
Stone_Red
2022-05-06 20:13:50 +02:00
parent 3d7b86d859
commit a604ce10fe
5 changed files with 60 additions and 5 deletions
@@ -0,0 +1,46 @@
package com.yes.yes.behaviours;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Component;
import com.yes.yes.utils.Entity;
public class ConveyorBehaviour extends Component {
private final String receiveDataKey;
private final String offerDataKey;
private final int delay;
int count = 0;
boolean isConveyed = false;
public ConveyorBehaviour(Entity entity, BlockContainer blockContainer, String receiveDataKey, String offerDataKey, int delay) {
super(entity, blockContainer);
this.receiveDataKey = receiveDataKey;
this.offerDataKey = offerDataKey;
this.delay = delay;
}
@Override
public void initialize() {
}
@Override
public void update() {
if (count >= delay) {
if (parent.getData(offerDataKey) != null) return;
if (parent.getData(receiveDataKey) == null) return;
parent.setData(offerDataKey, parent.getData(receiveDataKey));
count = 0;
isConveyed = true;
} else if (parent.getData(receiveDataKey) != null && !isConveyed) {
count++;
}
if(isConveyed && parent.getData(offerDataKey) == null)
{
parent.setData(receiveDataKey, null);
isConveyed = false;
}
}
}
@@ -24,5 +24,6 @@ public class GeneratorBehaviour extends Component {
@Override @Override
public void update() { public void update() {
this.parent.setData(dataKey, item.clone()); this.parent.setData(dataKey, item.clone());
System.out.println("generate");
} }
} }
@@ -47,7 +47,6 @@ public class RecieveBehaviour extends Component {
this.parent.setData(dataKey, item); this.parent.setData(dataKey, item);
this.parent.trigger(dataKey + "Changed", item); this.parent.trigger(dataKey + "Changed", item);
this.offerer.trigger("itemAccepted", this.parent); this.offerer.trigger("itemAccepted", this.parent);
System.out.println(this.parent.getClass().getSimpleName() + "> Received item");
} }
@Override @Override
@@ -24,8 +24,9 @@ public class ConveyorBelt extends Entity {
public ConveyorBelt(BlockContainer blockContainer) { public ConveyorBelt(BlockContainer blockContainer) {
this(); this();
this.addBehaviour(new PlaceBehaviour(this, blockContainer)); this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new ItemBehaviour(this, blockContainer,"Item")); this.addBehaviour(new ItemBehaviour(this, blockContainer,"ReceivedItem"));
this.addBehaviour(new RecieveBehaviour(this, blockContainer, Direction.DOWN, "Item")); this.addBehaviour(new RecieveBehaviour(this, blockContainer, Direction.DOWN, "ReceivedItem"));
this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "Item")); this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "OfferItem"));
this.addBehaviour(new ConveyorBehaviour(this,blockContainer, "ReceivedItem","OfferItem", 1));
} }
} }
@@ -3,10 +3,18 @@ package com.yes.yes.entities.parts;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
import java.util.Random;
public class Square extends com.yes.yes.utils.Part { public class Square extends com.yes.yes.utils.Part {
Random rand = new Random();
public Square() { public Square() {
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);
var rect = new Rectangle(23, 23); var rect = new Rectangle(23, 23);
rect.setFill(Color.RED); rect.setFill(Color.rgb(r,g,b));
rect.setStroke(Color.BLUE); rect.setStroke(Color.BLUE);
rect.setStrokeWidth(1); rect.setStrokeWidth(1);
this.getChildren().add(rect); this.getChildren().add(rect);