More code cleanup and fix receive behavior

This commit is contained in:
Stone_Red
2022-05-13 11:26:12 +02:00
parent 40b58a42ef
commit e53db4256b
14 changed files with 68 additions and 29 deletions
@@ -1,2 +1,30 @@
package com.yes.yes.behaviours;public class DestroyBehaviour {
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 DestroyBehaviour extends Component {
private String dataKey;
public DestroyBehaviour(Entity entity, BlockContainer blockContainer, String dataKey) {
super(entity, blockContainer);
this.dataKey = dataKey;
}
@Override
public void initialize() {
}
@Override
public void update() {
this.parent.setData(dataKey, null);
this.parent.trigger(dataKey + "Changed", null);
}
@Override
public void destroy() {
}
}
@@ -11,8 +11,8 @@ import javafx.scene.transform.Scale;
public class ItemBehaviour extends Component {
private final String dataKey;
private VBox itemGroup;
private String dataKey;
public ItemBehaviour(Entity entity, BlockContainer blockContainer, String dataKey) {
super(entity, blockContainer);
@@ -35,6 +35,7 @@ public class ItemBehaviour extends Component {
itemGroup = new VBox();
itemGroup.setAlignment(Pos.CENTER);
//itemGroup.setRotate(-this.parent.getRotate());
this.parent.getChildren().add(itemGroup);
@@ -48,6 +48,7 @@ public class ReceiveBehaviour extends Component {
this.parent.setData(dataKey, item);
this.parent.trigger(dataKey + "Changed", item);
this.offerer.trigger("itemAccepted", this.parent);
}
@@ -4,6 +4,7 @@ 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.Polygon;
@@ -12,9 +13,9 @@ public class ConveyorBelt extends Entity {
super();
Polygon t = new Polygon();
t.getPoints().addAll(
25.0, 0.0,
0.0, 50.0,
50.0, 50.0);
Chunk.ENTITY_SIZE / 2d, 0.0,
0.0, (double) Chunk.ENTITY_SIZE,
(double) Chunk.ENTITY_SIZE, (double) Chunk.ENTITY_SIZE);
t.setFill(new Color(0, 0, 0, 1));
this.getChildren().addAll(t);
@@ -1,9 +1,6 @@
package com.yes.yes.entities.machines;
import com.yes.yes.behaviours.ItemBehaviour;
import com.yes.yes.behaviours.PlaceBehaviour;
import com.yes.yes.behaviours.ReceiveBehaviour;
import com.yes.yes.behaviours.TestBehaviour;
import com.yes.yes.behaviours.*;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Direction;
import com.yes.yes.utils.Entity;
@@ -11,9 +8,10 @@ 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 TestMachine extends Entity {
public TestMachine() {
public class DestroyMachine extends Entity {
public DestroyMachine() {
super();
Circle c = new Circle(Chunk.ENTITY_SIZE / 2);
c.setCenterX(Chunk.ENTITY_SIZE / 2);
@@ -29,11 +27,10 @@ public class TestMachine extends Entity {
this.getChildren().addAll(c, t);
}
public TestMachine(BlockContainer blockContainer) {
public DestroyMachine(BlockContainer blockContainer) {
this();
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new TestBehaviour(this, blockContainer));
this.addBehaviour(new ItemBehaviour(this, blockContainer, "Item"));
this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.DOWN, "Item"));
this.addBehaviour(new DestroyBehaviour(this, blockContainer, "Item"));
}
}
@@ -9,6 +9,7 @@ import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Direction;
import com.yes.yes.utils.Entity;
import com.yes.yes.utils.Item;
import com.yes.yes.world.Chunk;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
@@ -16,14 +17,14 @@ import javafx.scene.shape.Rectangle;
public class GeneratorMachine extends Entity {
public GeneratorMachine() {
super();
Rectangle r = new Rectangle(50, 50);
Rectangle r = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
r.setFill(Color.RED);
Polygon p = new Polygon();
p.getPoints().addAll(
25.0, 0.0,
0.0, 50.0,
50.0, 50.0);
Chunk.ENTITY_SIZE/2d, 0.0,
0.0, (double) Chunk.ENTITY_SIZE,
(double) Chunk.ENTITY_SIZE, (double) Chunk.ENTITY_SIZE);
p.setFill(new Color(0, 0, 0, 0.5));
this.getChildren().addAll(r, p);
@@ -5,12 +5,13 @@ 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.world.Chunk;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class HubAcceptor extends Entity {
public HubAcceptor() {
Rectangle r = new Rectangle(50, 50);
Rectangle r = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
r.setFill(Color.PINK);
this.getChildren().add(r);
@@ -4,6 +4,9 @@ import com.yes.yes.behaviours.ItemBehaviour;
import com.yes.yes.behaviours.PlaceBehaviour;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Entity;
import com.yes.yes.world.Chunk;
import javafx.geometry.Pos;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
@@ -11,12 +14,17 @@ import javafx.scene.text.Text;
public class HubDisplay extends Entity {
public HubDisplay() {
Rectangle r = new Rectangle(50, 50);
Rectangle r = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
r.setFill(Color.PINK);
Text text = new Text("yes");
Text text = new Text("yes\nthis is a test sentence");
text.setWrappingWidth(Chunk.ENTITY_SIZE);
this.getChildren().addAll(r, text);
StackPane stackPane = new StackPane();
stackPane.getChildren().addAll(r, text);
stackPane.setAlignment(Pos.CENTER);
this.getChildren().addAll(stackPane);
}
public HubDisplay(BlockContainer blockContainer) {
@@ -7,12 +7,13 @@ import com.yes.yes.behaviours.TestBehaviour;
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.shape.Rectangle;
public class TestMachine2 extends Entity {
public TestMachine2() {
super();
this.getChildren().add(new Rectangle(50, 50));
this.getChildren().add(new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE));
}
public TestMachine2(BlockContainer blockContainer) {
@@ -6,7 +6,7 @@ import javafx.scene.shape.Rectangle;
import java.util.Random;
public class Square extends com.yes.yes.utils.Part {
Random rand = new Random();
final Random rand = new Random();
public Square() {
int r = rand.nextInt(255);
@@ -31,7 +31,7 @@ public class GameManager {
world.toBack();
try {
EntityRegistry.register(new RegistryEntry("test", "test receive machine", TestMachine.class));
EntityRegistry.register(new RegistryEntry("test", "test receive machine", DestroyMachine.class));
EntityRegistry.register(new RegistryEntry("test2", "test offer machine", TestMachine2.class));
EntityRegistry.register(new RegistryEntry("generator", "Generator", GeneratorMachine.class));
EntityRegistry.register(new RegistryEntry("conveyorBelt", "Conveyor Belt", ConveyorBelt.class));
@@ -65,7 +65,7 @@ public class PlayerManager {
Chunk chunk = world.getChunk(chunkCoordinate);
Entity oldEntity = chunk.getEntity(blockCoordinate);
if (oldEntity != null && oldEntity.getClass().getSimpleName() == entity.getClass().getSimpleName()) {
if (oldEntity != null && oldEntity.getClass().getSimpleName().equals(entity.getClass().getSimpleName())) {
if (mouse.getButton() == MouseButton.PRIMARY)
currentRotation = oldEntity.getRotation() + 1;
else
@@ -38,7 +38,7 @@ public record BlockContainer(World world, Coordinate blockCoordinate, Coordinate
}
// Only works with a valid direction
public final Entity getBlockRelative(Coordinate direction, int rotation) throws IllegalAccessException {
public Entity getBlockRelative(Coordinate direction, int rotation) throws IllegalAccessException {
for (int i = 0; i < rotation % 4; i++) {
direction = Direction.rotateRight(direction);
}
@@ -11,7 +11,7 @@ public class Direction {
private Direction() {
}
public static final Coordinate rotateRight(Coordinate direction) {
public static Coordinate rotateRight(Coordinate direction) {
if (direction == Direction.LEFT) return Direction.UP;
if (direction == Direction.UP) return Direction.RIGHT;
if (direction == Direction.RIGHT) return Direction.DOWN;
@@ -20,7 +20,7 @@ public class Direction {
throw new InvalidParameterException("Coordinate is not a direction");
}
public static final Coordinate rotateLeft(Coordinate direction) {
public static Coordinate rotateLeft(Coordinate direction) {
if (direction == Direction.LEFT) return Direction.DOWN;
if (direction == Direction.DOWN) return Direction.RIGHT;
if (direction == Direction.RIGHT) return Direction.UP;