Add splitter and fix a few bugs

This commit is contained in:
Stone_Red
2022-05-19 11:32:09 +02:00
parent 4716a087c1
commit 840da9a483
8 changed files with 129 additions and 17 deletions
@@ -8,11 +8,10 @@ public class ConveyorBehaviour extends Component {
private final String receiveDataKey; private final String receiveDataKey;
private final String offerDataKey; private final String offerDataKey;
private final int delay; private final int delay;
int count = 0; int delayCount = 0;
boolean isConveyed = false; boolean isConveyed = false;
public ConveyorBehaviour(Entity entity, BlockContainer blockContainer, String receiveDataKey, String offerDataKey, int delay) { public ConveyorBehaviour(Entity entity, BlockContainer blockContainer, int delay, String receiveDataKey, String offerDataKey) {
super(entity, blockContainer); super(entity, blockContainer);
this.receiveDataKey = receiveDataKey; this.receiveDataKey = receiveDataKey;
this.offerDataKey = offerDataKey; this.offerDataKey = offerDataKey;
@@ -26,15 +25,15 @@ public class ConveyorBehaviour extends Component {
@Override @Override
public void update() { public void update() {
if (count > delay) { if (delayCount > delay) {
count = 0; delayCount = 0;
if (parent.getData(offerDataKey) != null) return; if (parent.getData(offerDataKey) != null) return;
parent.setData(offerDataKey, parent.getData(receiveDataKey)); parent.setData(offerDataKey, parent.getData(receiveDataKey));
isConveyed = true; isConveyed = true;
} else if (parent.getData(receiveDataKey) != null && !isConveyed) { } else if (parent.getData(receiveDataKey) != null && !isConveyed) {
count++; delayCount++;
} }
if (isConveyed && parent.getData(offerDataKey) == null) { if (isConveyed && parent.getData(offerDataKey) == null) {
@@ -4,14 +4,19 @@ import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Component; import com.yes.yes.utils.Component;
import com.yes.yes.utils.Entity; import com.yes.yes.utils.Entity;
import com.yes.yes.utils.Item; import com.yes.yes.utils.Item;
import com.yes.yes.world.Chunk;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Scale; import javafx.scene.transform.Scale;
import javafx.scene.transform.Translate;
public class ItemBehaviour extends Component { public class ItemBehaviour extends Component {
private VBox itemGroup; private StackPane itemGroup;
private final String dataKey; private final String dataKey;
public ItemBehaviour(Entity entity, BlockContainer blockContainer, String dataKey) { public ItemBehaviour(Entity entity, BlockContainer blockContainer, String dataKey) {
@@ -25,6 +30,11 @@ public class ItemBehaviour extends Component {
this.itemGroup.getChildren().clear(); this.itemGroup.getChildren().clear();
if (item != null) if (item != null)
this.itemGroup.getChildren().add(item); this.itemGroup.getChildren().add(item);
else {
Rectangle alignmentRectangle = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
alignmentRectangle.setFill(Color.rgb(0,0,0,0));
this.itemGroup.getChildren().add(alignmentRectangle);
}
}); });
//System.out.println("Finished item update"); //System.out.println("Finished item update");
} }
@@ -33,14 +43,21 @@ public class ItemBehaviour extends Component {
public void initialize() { public void initialize() {
parent.addListener(dataKey + "Changed", this, this::itemChanged); parent.addListener(dataKey + "Changed", this, this::itemChanged);
itemGroup = new VBox(); itemGroup = new StackPane();
itemGroup.setAlignment(Pos.CENTER); itemGroup.setAlignment(Pos.CENTER);
//itemGroup.setRotate(-this.parent.getRotate()); itemGroup.setRotate(-this.parent.getRotate());
this.parent.getChildren().add(itemGroup); this.parent.getChildren().add(itemGroup);
Scale s = new Scale(0.7, 0.7); Scale s = new Scale(0.7, 0.7);
itemGroup.getTransforms().add(s); itemGroup.getTransforms().add(s);
Translate translate = new Translate(Chunk.ENTITY_SIZE/4,Chunk.ENTITY_SIZE/4);
itemGroup.getTransforms().add(translate);
Rectangle alignmentRectangle = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
alignmentRectangle.setFill(Color.rgb(0,0,0,0));
this.itemGroup.getChildren().add(alignmentRectangle);
} }
@Override @Override
@@ -1,6 +1,7 @@
package com.yes.yes.behaviours; package com.yes.yes.behaviours;
import com.yes.yes.utils.*; import com.yes.yes.utils.*;
import javafx.util.Pair;
public class OfferBehaviour extends Component { public class OfferBehaviour extends Component {
@@ -58,7 +59,7 @@ public class OfferBehaviour extends Component {
Item item = this.parent.getData(dataKey); Item item = this.parent.getData(dataKey);
if (item == null) return; if (item == null) return;
this.receiver.trigger("offerItem", item); this.receiver.trigger("offerItem", new Pair(parent, item));
} }
@Override @Override
@@ -1,6 +1,7 @@
package com.yes.yes.behaviours; package com.yes.yes.behaviours;
import com.yes.yes.utils.*; import com.yes.yes.utils.*;
import javafx.util.Pair;
public class ReceiveBehaviour extends Component { public class ReceiveBehaviour extends Component {
@@ -38,18 +39,17 @@ public class ReceiveBehaviour extends Component {
this.parent.addListener("placed", this, this::placed); this.parent.addListener("placed", this, this::placed);
} }
void receive(Item item) { void receive(Pair<Entity,Item> pair) {
if (offerer == null) return; if (offerer == null || offerer != pair.getKey()) return;
if (this.parent.getData(dataKey) == item) if (this.parent.getData(dataKey) == pair.getValue())
this.offerer.trigger("itemAccepted", this.parent); this.offerer.trigger("itemAccepted", this.parent);
if (this.parent.getData(dataKey) != null) return; if (this.parent.getData(dataKey) != null) return;
this.parent.setData(dataKey, item); this.parent.setData(dataKey, pair.getValue());
this.parent.trigger(dataKey + "Changed", item); this.parent.trigger(dataKey + "Changed", pair.getValue());
this.offerer.trigger("itemAccepted", this.parent); this.offerer.trigger("itemAccepted", this.parent);
} }
@Override @Override
@@ -0,0 +1,54 @@
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 SplitBehaviour extends Component {
private final int delay;
private final String receiveDataKey;
private final String[] offerDataKeys;
private int delayCount = 0;
private int offerDataKeyIndex = 0;
private String offerDataKey;
public SplitBehaviour(Entity entity, BlockContainer blockContainer, int delay, String receiveDataKey, String... offerDataKeys) {
super(entity, blockContainer);
this.delay = delay;
this.receiveDataKey = receiveDataKey;
this.offerDataKeys = offerDataKeys;
}
@Override
public void initialize() {
}
@Override
public void update() {
if (delayCount > delay) {
offerDataKeyIndex++;
if (offerDataKeyIndex >= offerDataKeys.length)
offerDataKeyIndex = 0;
offerDataKey = offerDataKeys[offerDataKeyIndex];
if (parent.getData(offerDataKey) != null) return;
parent.setData(offerDataKey, parent.getData(receiveDataKey));
parent.setData(receiveDataKey, null);
delayCount = 0;
} else if (parent.getData(receiveDataKey) != null) {
delayCount++;
}
}
@Override
public void destroy() {
}
}
@@ -29,6 +29,6 @@ public class ConveyorBelt extends Entity {
this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.LEFT, "ReceivedItem")); this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.LEFT, "ReceivedItem"));
this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.RIGHT, "ReceivedItem")); this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.RIGHT, "ReceivedItem"));
this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "OfferItem")); this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "OfferItem"));
this.addBehaviour(new ConveyorBehaviour(this, blockContainer, "ReceivedItem", "OfferItem", 1)); this.addBehaviour(new ConveyorBehaviour(this, blockContainer, 1, "ReceivedItem", "OfferItem"));
} }
} }
@@ -0,0 +1,40 @@
package com.yes.yes.entities.machines;
import com.yes.yes.behaviours.OfferBehaviour;
import com.yes.yes.behaviours.PlaceBehaviour;
import com.yes.yes.behaviours.ReceiveBehaviour;
import com.yes.yes.behaviours.SplitBehaviour;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Direction;
import com.yes.yes.utils.Entity;
import com.yes.yes.world.Chunk;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
public class Splitter extends Entity {
public Splitter() {
super();
Polygon p1 = new Polygon();
p1.getPoints().addAll(
0.0, Chunk.ENTITY_SIZE / 2d,
Chunk.ENTITY_SIZE / 2d, 0.0,
(double) Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE / 2d,
Chunk.ENTITY_SIZE / 4d * 3, (double) Chunk.ENTITY_SIZE,
Chunk.ENTITY_SIZE / 2d, Chunk.ENTITY_SIZE / 2d,
Chunk.ENTITY_SIZE / 4d, (double) Chunk.ENTITY_SIZE
);
p1.setFill(new Color(0, 0, 0, 1));
this.getChildren().addAll(p1);
}
public Splitter(BlockContainer blockContainer) {
this();
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new SplitBehaviour(this, blockContainer, 1, "ReceivedItem", "OfferItemLeft", "OfferItemRight", "OfferItemUp"));
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"));
this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "OfferItemUp"));
}
}
@@ -36,6 +36,7 @@ public class GameManager {
EntityRegistry.register(new RegistryEntry("conveyorBelt", "Conveyor Belt", ConveyorBelt.class)); EntityRegistry.register(new RegistryEntry("conveyorBelt", "Conveyor Belt", ConveyorBelt.class));
EntityRegistry.register(new RegistryEntry("hub", "Central Hub", HubDisplay.class)); EntityRegistry.register(new RegistryEntry("hub", "Central Hub", HubDisplay.class));
EntityRegistry.register(new RegistryEntry("hubAcceptor", "Central Hub Acceptor", HubAcceptor.class)); EntityRegistry.register(new RegistryEntry("hubAcceptor", "Central Hub Acceptor", HubAcceptor.class));
EntityRegistry.register(new RegistryEntry("splitter", "Splitter", Splitter.class));
} catch (AlreadyExistsException e) { } catch (AlreadyExistsException e) {
e.printStackTrace(); e.printStackTrace();
} }