Add recieve and offer behaviours; + generate behaviour

This commit is contained in:
Stefan-5422
2022-05-06 11:26:56 +02:00
parent 0dd786f812
commit 16d100f9fb
16 changed files with 333 additions and 95 deletions
@@ -0,0 +1,28 @@
package com.yes.yes.behaviours;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Component;
import com.yes.yes.utils.Entity;
import com.yes.yes.utils.Item;
public class GeneratorBehaviour extends Component {
private Item item;
private String dataKey;
public GeneratorBehaviour(Entity entity, BlockContainer blockContainer, Item item, String dataKey) {
super(entity, blockContainer);
this.item = item;
this.dataKey = dataKey;
}
@Override
public void initialize() {
}
@Override
public void update() {
this.parent.setData(dataKey, item.clone());
}
}
@@ -4,6 +4,7 @@ import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Component;
import com.yes.yes.utils.Entity;
import com.yes.yes.utils.Item;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.layout.VBox;
import javafx.scene.transform.Scale;
@@ -11,24 +12,25 @@ import javafx.scene.transform.Scale;
public class ItemBehaviour extends Component {
private VBox itemGroup;
private Item item;
private String dataKey;
public ItemBehaviour(Entity entity, BlockContainer blockContainer) {
public ItemBehaviour(Entity entity, BlockContainer blockContainer, String dataKey) {
super(entity, blockContainer);
this.dataKey = dataKey;
}
private void itemChanged(Item item) {
if(this.item != null) {
System.out.println("Running item update");
Platform.runLater(() -> {
this.itemGroup.getChildren().clear();
}
this.item = item;
this.itemGroup.getChildren().add(item);
this.itemGroup.getChildren().add(this.parent.getData(dataKey));
});
System.out.println("Finished item update");
}
@Override
public void initialize() {
parent.addListener("itemChanged", this::itemChanged);
parent.addListener(dataKey + "Changed", this::itemChanged);
itemGroup = new VBox();
itemGroup.setAlignment(Pos.CENTER);
@@ -0,0 +1,63 @@
package com.yes.yes.behaviours;
import com.yes.yes.utils.*;
public class OfferBehaviour extends Component {
private Coordinate direction;
private String dataKey;
private Entity receiver;
public OfferBehaviour(Entity entity, BlockContainer blockContainer, Coordinate direction, String dataKey) {
super(entity, blockContainer);
this.direction = direction;
this.dataKey = dataKey;
}
private void placed(Entity entity) {
//System.out.println("Entity placed in range");
try {
if (blockContainer.getBlockRelative(direction, parent.getRotation()) == entity) {
//System.out.println("Success");
this.receiver = entity;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private void itemAccepted(Entity entity) {
try {
if (blockContainer.getBlockRelative(direction, parent.getRotation()) == entity) {
this.parent.setData(dataKey, null);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public void initialize() {
try {
this.receiver = blockContainer.getBlockRelative(direction, parent.getRotation());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
this.parent.addListener("itemAccepted", this::itemAccepted);
this.parent.addListener("placed", this::placed);
}
@Override
public void update() {
if (receiver == null) return;
Item item = this.parent.getData(dataKey);
if (item == null) return;
this.receiver.trigger("offerItem", item);
}
}
@@ -14,7 +14,7 @@ public class PlaceBehaviour extends Component {
for (int x = -1; x < 2; x++) {
for (int y = -1; y < 2; y++) {
try {
Entity entity = blockContainer.getBlock(new Coordinate(x, y));
Entity entity = blockContainer.getBlockAbsolute(new Coordinate(x, y));
if (entity != null) {
entity.trigger("placed", parent);
@@ -0,0 +1,58 @@
package com.yes.yes.behaviours;
import com.yes.yes.utils.*;
public class RecieveBehaviour extends Component {
private Coordinate direction;
private String dataKey;
private Entity offerer;
public RecieveBehaviour(Entity entity, BlockContainer blockContainer, Coordinate direction, String dataKey) {
super(entity, blockContainer);
this.direction = direction;
this.dataKey = dataKey;
}
private void placed(Entity entity) {
System.out.println("Entity placed in range");
try {
if (blockContainer.getBlockRelative(direction, parent.getRotation()) == entity) {
System.out.println("Success");
this.offerer = entity;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public void initialize() {
try {
this.offerer = blockContainer.getBlockRelative(direction, parent.getRotation());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
this.parent.addListener("offerItem", this::receive);
this.parent.addListener("placed", this::placed);
}
void receive(Item item) {
if (offerer == null)
return;
this.parent.setData(dataKey, item);
this.parent.trigger(dataKey + "Changed", item);
this.offerer.trigger("itemAccepted", this.parent);
System.out.println(this.parent.getClass().getSimpleName() + "> Received item");
}
@Override
public void update() {
}
}
@@ -1,42 +0,0 @@
package com.yes.yes.behaviours;
import com.yes.yes.utils.*;
public class TestOffererBehaviour extends Component {
private Entity receiver;
public TestOffererBehaviour(Entity entity, BlockContainer blockContainer) {
super(entity, blockContainer);
}
private void placed(Entity entity) {
try {
if (blockContainer.getBlock(new Coordinate(1, 0)) == entity) {
this.receiver = entity;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public void initialize() {
try {
this.receiver = blockContainer.getBlock(new Coordinate(1, 0));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
this.parent.addListener("placed", this::placed);
}
@Override
public void update() {
if(receiver == null) return;
Item someItem = new Item();
receiver.trigger("offerItem", someItem);
}
}
@@ -1,25 +0,0 @@
package com.yes.yes.behaviours;
import com.yes.yes.utils.*;
public class TestRecieveBeahaviour extends Component {
public TestRecieveBeahaviour(Entity entity, BlockContainer blockContainer) {
super(entity, blockContainer);
}
@Override
public void initialize() {
this.parent.addListener("offerItem", this::recieve);
}
void recieve(Item item) {
System.out.println("Received Item");
}
@Override
public void update() {
}
}
@@ -0,0 +1,50 @@
package com.yes.yes.entities.machines;
import com.yes.yes.behaviours.GeneratorBehaviour;
import com.yes.yes.behaviours.ItemBehaviour;
import com.yes.yes.behaviours.OfferBehaviour;
import com.yes.yes.behaviours.PlaceBehaviour;
import com.yes.yes.entities.parts.Square;
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 javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.TriangleMesh;
public class GeneratorMachine extends Entity {
public GeneratorMachine() {
super();
Rectangle c = new Rectangle(50, 50);
c.setFill(Color.RED);
Polygon t = new Polygon();
t.getPoints().addAll(
25.0,0.0,
0.0,50.0,
50.0,50.0);
t.setFill(new Color(0,0,0,0.5));
this.getChildren().addAll(c,t);
}
public GeneratorMachine(BlockContainer blockContainer) {
this();
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new ItemBehaviour(this, blockContainer,"Item"));
this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "Item"));
this.addBehaviour(new GeneratorBehaviour(this, blockContainer, generateItem(), "Item"));
}
private Item generateItem() {
Item it = new Item();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
it.setPart(i, j, new Square());
}
}
return it;
}
}
@@ -2,11 +2,14 @@ package com.yes.yes.entities.machines;
import com.yes.yes.behaviours.ItemBehaviour;
import com.yes.yes.behaviours.PlaceBehaviour;
import com.yes.yes.behaviours.RecieveBehaviour;
import com.yes.yes.behaviours.TestBehaviour;
import com.yes.yes.behaviours.TestRecieveBeahaviour;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Direction;
import com.yes.yes.utils.Entity;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
public class TestMachine extends Entity {
public TestMachine() {
@@ -14,14 +17,22 @@ public class TestMachine extends Entity {
Circle c = new Circle(25);
c.setCenterX(25);
c.setCenterY(25);
this.getChildren().add(c);
Polygon t = new Polygon();
t.getPoints().addAll(
25.0,0.0,
0.0,50.0,
50.0,50.0);
t.setFill(new Color(1,1,1,0.5));
this.getChildren().addAll(c,t);
}
public TestMachine(BlockContainer blockContainer) {
this();
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new TestBehaviour(this, blockContainer));
this.addBehaviour(new ItemBehaviour(this, blockContainer));
this.addBehaviour(new TestRecieveBeahaviour(this, blockContainer));
this.addBehaviour(new ItemBehaviour(this, blockContainer,"Item"));
this.addBehaviour(new RecieveBehaviour(this, blockContainer, Direction.DOWN, "Item"));
}
}
@@ -1,10 +1,11 @@
package com.yes.yes.entities.machines;
import com.yes.yes.behaviours.ItemBehaviour;
import com.yes.yes.behaviours.OfferBehaviour;
import com.yes.yes.behaviours.PlaceBehaviour;
import com.yes.yes.behaviours.TestBehaviour;
import com.yes.yes.behaviours.TestOffererBehaviour;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Direction;
import com.yes.yes.utils.Entity;
import javafx.scene.shape.Rectangle;
@@ -14,11 +15,11 @@ public class TestMachine2 extends Entity {
this.getChildren().add(new Rectangle(50, 50));
}
public TestMachine2(BlockContainer blockContainer) {
public TestMachine2(BlockContainer blockContainer) {
this();
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new TestBehaviour(this, blockContainer));
this.addBehaviour(new ItemBehaviour(this, blockContainer));
this.addBehaviour(new TestOffererBehaviour(this, blockContainer));
this.addBehaviour(new ItemBehaviour(this, blockContainer,"Item"));
this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.RIGHT, "Item"));
}
}
@@ -1,5 +1,6 @@
package com.yes.yes.managers;
import com.yes.yes.entities.machines.GeneratorMachine;
import com.yes.yes.entities.machines.TestMachine;
import com.yes.yes.entities.machines.TestMachine2;
import com.yes.yes.utils.EntityRegistry;
@@ -32,16 +33,29 @@ public class GameManager {
world.toBack();
try {
EntityRegistry.register(new RegistryEntry("test", "test machine", TestMachine.class));
EntityRegistry.register(new RegistryEntry("test2", "test machine2", TestMachine2.class));
EntityRegistry.register(new RegistryEntry("test", "test receive machine", TestMachine.class));
EntityRegistry.register(new RegistryEntry("test2", "test offer machine", TestMachine2.class));
EntityRegistry.register(new RegistryEntry("generator", "generator", GeneratorMachine.class));
} catch (AlreadyExistsException e) {
e.printStackTrace();
}
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(() -> GlobalEventHandler.trigger("TimerTick", null), 0, 3, TimeUnit.SECONDS);
executor.scheduleAtFixedRate(() -> {
try {
GlobalEventHandler.trigger("timerTick", null);
}
catch (Exception ex)
{
ex.printStackTrace();
}
System.out.println("Finished tick!");
}, 0, 1, TimeUnit.SECONDS);
GlobalEventHandler.addListener("closing",(__)->executor.shutdown());
GlobalEventHandler.addListener("timerTick", (__) -> {
System.out.println("Tick!");
});
GlobalEventHandler.addListener("closing", (__) -> executor.shutdown());
}
}
@@ -7,6 +7,8 @@ import com.yes.yes.world.World;
import javafx.scene.Scene;
import javafx.scene.input.*;
import java.util.Random;
public class PlayerManager {
private static final KeyCombination LEFT_KEY = new KeyCodeCombination(KeyCode.A);
@@ -37,6 +39,8 @@ public class PlayerManager {
redrawChunks();
}
Random random = new Random();
private void ProcessClick(MouseEvent mouse) {
Coordinate mouseCoordinate = new Coordinate((int) mouse.getX(), (int) mouse.getY());
Coordinate chunkCoordinate = Coordinate.WorldToChunkCoordinate(mouseCoordinate);
@@ -53,8 +57,9 @@ public class PlayerManager {
return;
Entity entity = (Entity) registryEntry.getEntity().getConstructor(types).newInstance(blockContainer);
world.getChunk(chunkCoordinate).setEntity(entity, blockCoordinate);
entity.setRotation(random.nextInt(0,4));
entity.initialize();
} catch (Exception ex) {
ex.printStackTrace();
}
@@ -6,12 +6,12 @@ import com.yes.yes.world.World;
public record BlockContainer(World world, Coordinate blockCoordinate, Coordinate chunkCoordinate) {
final static int MAX_RADIUS = 2;
public Entity getBlock(Coordinate offset) throws IllegalAccessException {
public Entity getBlockAbsolute(Coordinate offset) throws IllegalAccessException {
if (Math.abs(offset.x) > MAX_RADIUS || Math.abs(offset.y) > MAX_RADIUS)
throw new IllegalAccessException();
Coordinate localChunkCoordinate = new Coordinate(chunkCoordinate.x, chunkCoordinate.y);
Coordinate localBlockCoordinate = new Coordinate(blockCoordinate.x - offset.x, blockCoordinate.y + offset.y);
Coordinate localBlockCoordinate = new Coordinate(blockCoordinate.x + offset.x, blockCoordinate.y + offset.y);
while (localBlockCoordinate.x < 0) {
localBlockCoordinate.x += Chunk.CHUNK_SIZE;
@@ -36,4 +36,12 @@ public record BlockContainer(World world, Coordinate blockCoordinate, Coordinate
Chunk chunk = world.getChunk(localChunkCoordinate);
return chunk.getEntity(localBlockCoordinate);
}
// Only works with a valid direction
public final Entity getBlockRelative(Coordinate direction, int rotation) throws IllegalAccessException {
for (int i = 0; i < rotation % 4; i++) {
direction = Direction.rotateRight(direction);
}
return getBlockAbsolute(direction);
}
}
@@ -0,0 +1,30 @@
package com.yes.yes.utils;
import java.security.InvalidParameterException;
public class Direction {
private Direction() {}
public static final Coordinate UP = new Coordinate(0, -1);
public static final Coordinate RIGHT = new Coordinate(1, 0);
public static final Coordinate DOWN = new Coordinate(0, 1);
public static final Coordinate LEFT = new Coordinate(-1, 0);
public static final 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;
if (direction == Direction.DOWN) return Direction.LEFT;
throw new InvalidParameterException("Coordinate is not a direction");
}
public static final 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;
if (direction == Direction.UP) return Direction.LEFT;
throw new InvalidParameterException("Coordinate is not a direction");
}
}
+18 -2
View File
@@ -12,12 +12,11 @@ public abstract class Entity extends javafx.scene.Group {
private int rotation;
public Entity() {
GlobalEventHandler.addListener("TimerTick",(__)->update());
GlobalEventHandler.addListener("timerTick", (__) -> update());
}
public final void addBehaviour(Component behaviour) {
behaviours.add(behaviour);
behaviour.initialize();
}
public final void removeBehaviour(Component behaviour) {
@@ -31,12 +30,17 @@ public abstract class Entity extends javafx.scene.Group {
}
public final void update() {
//System.out.println("Updating: " + getClass().getSimpleName());
for (Component behaviour : behaviours) {
//System.out.println("Updating " + behaviour.getClass().getSimpleName());
behaviour.update();
}
}
public final void initialize() {
behaviours.forEach(Component::initialize);
}
public final void setData(String key, Object value) {
data.put(key, value);
}
@@ -56,4 +60,16 @@ public abstract class Entity extends javafx.scene.Group {
public final void trigger(String eventName, Object parameter) {
handler.trigger(eventName, parameter);
}
public int getRotation() {
return rotation;
}
public void setRotation(int rotation) {
rotation %= 4;
if (rotation != this.rotation) this.trigger("rotate", rotation);
this.rotation = rotation;
setRotate(rotation * 90);
}
}
+21 -2
View File
@@ -2,7 +2,7 @@ package com.yes.yes.utils;
import javafx.scene.transform.Scale;
public class Item extends javafx.scene.Group {
public class Item extends javafx.scene.Group implements Cloneable {
final Part[][] parts = new Part[4][4];
public void setPart(int layer, int section, Part part) throws IllegalArgumentException {
@@ -16,7 +16,7 @@ public class Item extends javafx.scene.Group {
part.rotate(section);
part.getTransforms().add(new Scale(-layer*0.25f+1f, -layer*0.25f+1f));
part.getTransforms().add(new Scale(-layer * 0.25f + 1f, -layer * 0.25f + 1f));
parts[layer][section] = part;
this.getChildren().add(part);
@@ -50,4 +50,23 @@ public class Item extends javafx.scene.Group {
}
}
}
@Override
public Item clone() {
Item clone = new Item();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
Class<?>[] types = new Class<?>[0];
try {
clone.setPart(i, j, this.getPart(i, j).getClass().getConstructor(types).newInstance());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return clone;
}
}