- Add rotator

- Fix `Entity.rotate` method
This commit is contained in:
Stone_Red
2022-06-02 22:46:36 +02:00
parent da9e37b092
commit fc1779896a
7 changed files with 127 additions and 10 deletions
@@ -1,5 +1,7 @@
package com.yes.yes.behaviours;
import com.yes.yes.entities.parts.EmptyPart;
import com.yes.yes.entities.parts.Square;
import com.yes.yes.utils.*;
import com.yes.yes.world.Chunk;
import javafx.geometry.Pos;
@@ -25,8 +27,20 @@ public class ItemBehaviour extends Component {
this.itemGroup.getChildren().clear();
if (item != null)
this.itemGroup.getChildren().add(item.clone());
if (item != null) {
Item displayItem = item.clone();
for (int layer = 0; layer < 4; layer++) {
for (int quarter = 0; quarter < 4; quarter++) {
if(displayItem.getPart(layer,quarter) == null)
{
displayItem.setPart(layer, quarter, new EmptyPart());
}
}
}
this.itemGroup.getChildren().add(displayItem);
}
else {
Rectangle alignmentRectangle = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
alignmentRectangle.setFill(Color.rgb(0, 0, 0, 0));
@@ -1,4 +1,50 @@
package com.yes.yes.behaviours;
public class RotateBehaviour {
import com.yes.yes.utils.*;
public class RotateBehaviour extends Component {
private final Coordinate rotationDirection;
private final String receiveDataKey;
private final String offerDataKey;
private final int processingDuration;
private int progress = 0;
public RotateBehaviour(Entity entity, BlockContainer blockContainer, Coordinate rotationDirection, String receiveDataKey, String offerDataKey, int processingDuration) {
super(entity, blockContainer);
this.rotationDirection = rotationDirection;
this.receiveDataKey = receiveDataKey;
this.offerDataKey = offerDataKey;
this.processingDuration = processingDuration;
}
@Override
public void initialize() {
}
@Override
public void update() {
if (progress > processingDuration) {
if (parent.getData(offerDataKey) != null) return;
Item item = parent.getData(receiveDataKey);
if (rotationDirection.equals(Direction.RIGHT)) {
item.rotate(1);
} else if (rotationDirection.equals(Direction.LEFT)) {
item.rotate(3);
}
parent.setData(offerDataKey, item);
parent.setData(receiveDataKey, null);
progress = 0;
} else if (parent.getData(receiveDataKey) != null) {
progress++;
}
}
@Override
public void destroy() {
}
}
@@ -27,8 +27,6 @@ public class SplitBehaviour extends Component {
@Override
public void update() {
if (delayCount > delay) {
offerDataKeyIndex++;
if (offerDataKeyIndex >= offerDataKeys.length)
offerDataKeyIndex = 0;
@@ -0,0 +1,38 @@
package com.yes.yes.entities.machines;
import com.yes.yes.behaviours.*;
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.Circle;
import javafx.scene.shape.Polygon;
public class Rotator extends Entity {
public Rotator() {
super();
Circle c = new Circle(Chunk.ENTITY_SIZE / 2d);
c.setFill(Color.BLACK);
c.setCenterX(Chunk.ENTITY_SIZE / 2d);
c.setCenterY(Chunk.ENTITY_SIZE / 2d);
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);
p.setFill(new Color(1, 1, 0, 0.5));
this.getChildren().addAll(p);
}
@SuppressWarnings("unused") // Called dynamically
public Rotator(BlockContainer blockContainer) {
this();
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new RotateBehaviour(this, blockContainer, Direction.RIGHT, "ReceivedItem", "OfferItem", 1));
this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.DOWN, "ReceivedItem"));
this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "OfferItem"));
}
}
@@ -0,0 +1,17 @@
package com.yes.yes.entities.parts;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class EmptyPart extends com.yes.yes.utils.Part {
public EmptyPart() {
Rectangle rect = new Rectangle(23, 23);
rect.setFill(new Color(0,0,0,0));
this.getChildren().add(rect);
}
@Override
protected void onColorChanged(Color color) {
}
}
@@ -38,6 +38,7 @@ public class GameManager {
EntityRegistry.register(new RegistryEntry("splitter", "Splitter", Splitter.class));
EntityRegistry.register(new RegistryEntry("cutter", "Cutter", Cutter.class));
EntityRegistry.register(new RegistryEntry("stacker", "Stacker", Stacker.class));
EntityRegistry.register(new RegistryEntry("rotator", "Rotator", Rotator.class));
} catch (AlreadyExistsException e) {
e.printStackTrace();
}
+8 -5
View File
@@ -36,23 +36,26 @@ public class Item extends javafx.scene.Group implements Cloneable {
}
public void rotate(int quarters) {
quarters %= 4;
quarters = (quarters + 2) % 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[0] = parts[l][1];
rot[1] = parts[l][2];
rot[2] = parts[l][3];
rot[3] = parts[l][1];
rot[3] = parts[l][0];
parts[l] = rot;
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
parts[i][j].rotate(quarters);
Part part = parts[i][j];
if (part != null) {
part.rotate(quarters);
}
}
}
}