From 84a904dcac38a6cead5d07bc1183be47a56bf911 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Fri, 4 Mar 2022 11:31:01 +0100 Subject: [PATCH] Add class to get entities around other entities - Add `BlockContainer` class - Add more error logging - Refactor code to work with the new system --- .idea/misc.xml | 2 +- .../com/yes/yes/behaviours/TestBehaviour.java | 21 ++++++-- .../yes/entities/machines/TestMachine.java | 12 ++--- .../yes/entities/machines/TestMachine2.java | 10 ++-- .../com/yes/yes/managers/PlayerManager.java | 29 ++++++----- src/main/java/com/yes/yes/ui/BuildItem.java | 17 ++++--- .../com/yes/yes/utils/BlockContainer.java | 48 +++++++++++++++++++ .../java/com/yes/yes/utils/Component.java | 5 +- .../compile/default-compile/.gitignore | 1 + .../compile/default-compile/createdFiles.lst | 2 + 10 files changed, 113 insertions(+), 34 deletions(-) create mode 100644 src/main/java/com/yes/yes/utils/BlockContainer.java create mode 100644 target/maven-status/maven-compiler-plugin/compile/default-compile/.gitignore create mode 100644 target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst diff --git a/.idea/misc.xml b/.idea/misc.xml index e8d124d..144a7be 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,7 +7,7 @@ - + \ No newline at end of file diff --git a/src/main/java/com/yes/yes/behaviours/TestBehaviour.java b/src/main/java/com/yes/yes/behaviours/TestBehaviour.java index f3c6276..9686512 100644 --- a/src/main/java/com/yes/yes/behaviours/TestBehaviour.java +++ b/src/main/java/com/yes/yes/behaviours/TestBehaviour.java @@ -1,13 +1,14 @@ package com.yes.yes.behaviours; +import com.yes.yes.utils.BlockContainer; import com.yes.yes.utils.Component; +import com.yes.yes.utils.Coordinate; import com.yes.yes.utils.Entity; -import com.yes.yes.utils.EntityRegistry; public class TestBehaviour extends Component { - public TestBehaviour(Entity entity) { - super(entity); + public TestBehaviour(Entity entity, BlockContainer blockContainer) { + super(entity, blockContainer); } @Override @@ -16,6 +17,18 @@ public class TestBehaviour extends Component { @Override public void update() { - + System.out.println("Updating " + parent.getClass().getSimpleName()); + for (int x = -1; x < 2; x++) { + for (int y = -1; y < 2; y++) { + try { + System.out.println("Offset: X:" + x + " Y:" + y); + Entity entity = blockContainer.getBlock(new Coordinate(x, y)); + if (entity != null) + System.out.println(entity.getClass().getSimpleName()); + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + } + } + } } } diff --git a/src/main/java/com/yes/yes/entities/machines/TestMachine.java b/src/main/java/com/yes/yes/entities/machines/TestMachine.java index 3723605..3ed846d 100644 --- a/src/main/java/com/yes/yes/entities/machines/TestMachine.java +++ b/src/main/java/com/yes/yes/entities/machines/TestMachine.java @@ -1,19 +1,19 @@ package com.yes.yes.entities.machines; import com.yes.yes.behaviours.TestBehaviour; -import com.yes.yes.utils.Coordinate; -import com.yes.yes.utils.Entity; -import com.yes.yes.utils.EntityRegistry; -import com.yes.yes.utils.RegistryEntry; +import com.yes.yes.utils.*; import com.yes.yes.utils.exceptions.AlreadyExistsException; import javafx.scene.shape.Circle; public class TestMachine extends Entity { - public TestMachine() { super(); this.getChildren().add(new Circle(25)); + } - this.addBehaviour(new TestBehaviour(this)); + public TestMachine(BlockContainer blockContainer) { + this(); + this.addBehaviour(new TestBehaviour(this, blockContainer)); + update(); } } diff --git a/src/main/java/com/yes/yes/entities/machines/TestMachine2.java b/src/main/java/com/yes/yes/entities/machines/TestMachine2.java index ac9d328..f830de0 100644 --- a/src/main/java/com/yes/yes/entities/machines/TestMachine2.java +++ b/src/main/java/com/yes/yes/entities/machines/TestMachine2.java @@ -1,16 +1,20 @@ package com.yes.yes.entities.machines; import com.yes.yes.behaviours.TestBehaviour; +import com.yes.yes.utils.BlockContainer; import com.yes.yes.utils.Entity; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; public class TestMachine2 extends Entity { - public TestMachine2() { super(); - this.getChildren().add(new Rectangle(25,25)); + this.getChildren().add(new Rectangle(25, 25)); + } - this.addBehaviour(new TestBehaviour(this)); + public TestMachine2(BlockContainer blockContainer) { + this(); + this.addBehaviour(new TestBehaviour(this, blockContainer)); + update(); } } \ No newline at end of file diff --git a/src/main/java/com/yes/yes/managers/PlayerManager.java b/src/main/java/com/yes/yes/managers/PlayerManager.java index 793cc44..f31ccc4 100644 --- a/src/main/java/com/yes/yes/managers/PlayerManager.java +++ b/src/main/java/com/yes/yes/managers/PlayerManager.java @@ -1,6 +1,8 @@ package com.yes.yes.managers; +import com.yes.yes.entities.machines.TestMachine; import com.yes.yes.ui.BuildBox; +import com.yes.yes.utils.BlockContainer; import com.yes.yes.utils.Coordinate; import com.yes.yes.utils.Entity; import com.yes.yes.utils.EntityRegistry; @@ -35,18 +37,23 @@ public class PlayerManager { scene.setOnKeyPressed(this::ProcessKeyPress); world.setOnMouseClicked((e) -> - { - System.out.println("X:" + e.getX() + " Y:" + e.getY()); - Coordinate mouseCoordinate = new Coordinate((int)e.getX(), (int)e.getY()); - Coordinate chunkCoordinate = Coordinate.WorldToChunkCoordinate(mouseCoordinate); - Coordinate blockCoordinate = Coordinate.WorldToChunkBlock(mouseCoordinate); - System.out.println("Chunk: X:" + chunkCoordinate.x + " Y:" + chunkCoordinate.y); - System.out.println("Block: X:" + blockCoordinate.x + " Y:" + blockCoordinate.y); + { + Coordinate mouseCoordinate = new Coordinate((int) e.getX(), (int) e.getY()); + Coordinate chunkCoordinate = Coordinate.WorldToChunkCoordinate(mouseCoordinate); + Coordinate blockCoordinate = Coordinate.WorldToChunkBlock(mouseCoordinate); - try { - world.getChunk(chunkCoordinate).setEntity((Entity) EntityRegistry.getEntity(buildBox.getSelectedEntity()).getEntity().getConstructor().newInstance(),blockCoordinate); - } catch (Exception ignored) {} - } + try { + Class[] types = new Class[1]; + types[0] = BlockContainer.class; + + BlockContainer blockContainer = new BlockContainer(world, blockCoordinate, chunkCoordinate); + Entity entity = (Entity) EntityRegistry.getEntity(buildBox.getSelectedEntity()).getEntity().getConstructor(types).newInstance(blockContainer); + + world.getChunk(chunkCoordinate).setEntity(entity, blockCoordinate); + } catch (Exception ex) { + ex.printStackTrace(); + } + } ); world.setTranslateX(Integer.MAX_VALUE / -5000); diff --git a/src/main/java/com/yes/yes/ui/BuildItem.java b/src/main/java/com/yes/yes/ui/BuildItem.java index cac851b..b621933 100644 --- a/src/main/java/com/yes/yes/ui/BuildItem.java +++ b/src/main/java/com/yes/yes/ui/BuildItem.java @@ -1,5 +1,6 @@ package com.yes.yes.ui; +import com.yes.yes.utils.BlockContainer; import com.yes.yes.utils.EntityRegistry; import com.yes.yes.world.Chunk; import javafx.scene.Node; @@ -12,7 +13,7 @@ public class BuildItem extends javafx.scene.layout.StackPane { String name; Class entity; - public BuildItem(String name, Function onClick) { + public BuildItem(String name, Function onClick) { super(); this.display_name = EntityRegistry.getEntity(name).getDisplayName(); @@ -20,18 +21,20 @@ public class BuildItem extends javafx.scene.layout.StackPane { this.name = name; this.prefWidth(Chunk.ENTITY_SIZE); - this.prefHeight(Chunk.ENTITY_SIZE+10); + this.prefHeight(Chunk.ENTITY_SIZE + 10); try { - this.getChildren().add((Node)entity.getConstructor().newInstance()); - } catch (Exception ignored){} + this.getChildren().add((Node) entity.getConstructor().newInstance()); + } catch (Exception ex) { + ex.printStackTrace(); + } Button button = new Button(); button.setStyle("-fx-background-color: null; -fx-border-color: null"); - button.setPrefSize(Chunk.ENTITY_SIZE,Chunk.ENTITY_SIZE); - button.setMinSize(Chunk.ENTITY_SIZE,Chunk.ENTITY_SIZE); - button.setOnAction((e)-> onClick.apply(this.name)); + button.setPrefSize(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE); + button.setMinSize(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE); + button.setOnAction((e) -> onClick.apply(this.name)); this.getChildren().add(button); } diff --git a/src/main/java/com/yes/yes/utils/BlockContainer.java b/src/main/java/com/yes/yes/utils/BlockContainer.java new file mode 100644 index 0000000..b625cdd --- /dev/null +++ b/src/main/java/com/yes/yes/utils/BlockContainer.java @@ -0,0 +1,48 @@ +package com.yes.yes.utils; + +import com.yes.yes.world.Chunk; +import com.yes.yes.world.World; + +public class BlockContainer { + final World world; + final Coordinate blockCoordinate; + final Coordinate chunkCoordinate; + final static int MAX_RADIUS = 2; + + public BlockContainer(World world, Coordinate blockCoordinate, Coordinate chunkCoordinate) { + this.world = world; + this.blockCoordinate = blockCoordinate; + this.chunkCoordinate = chunkCoordinate; + } + + public Entity getBlock(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); + + while (localBlockCoordinate.x < 0) { + localBlockCoordinate.x += Chunk.CHUNK_SIZE; + localChunkCoordinate.x--; + } + + while (localBlockCoordinate.y < 0) { + localBlockCoordinate.y += Chunk.CHUNK_SIZE; + localChunkCoordinate.y--; + } + + while (localBlockCoordinate.x >= Chunk.CHUNK_SIZE) { + localBlockCoordinate.x -= Chunk.CHUNK_SIZE; + localChunkCoordinate.x++; + } + + while (localBlockCoordinate.y >= Chunk.CHUNK_SIZE) { + localBlockCoordinate.y -= Chunk.CHUNK_SIZE; + localChunkCoordinate.y++; + } + + Chunk chunk = world.getChunk(localChunkCoordinate); + return chunk.getEntity(localBlockCoordinate); + } +} diff --git a/src/main/java/com/yes/yes/utils/Component.java b/src/main/java/com/yes/yes/utils/Component.java index fa7570d..678c441 100644 --- a/src/main/java/com/yes/yes/utils/Component.java +++ b/src/main/java/com/yes/yes/utils/Component.java @@ -2,10 +2,11 @@ package com.yes.yes.utils; public abstract class Component { protected Entity parent; + protected final BlockContainer blockContainer; - - public Component(Entity entity) { + public Component(Entity entity, BlockContainer blockContainer) { this.parent = entity; + this.blockContainer = blockContainer; } public abstract void initialize(); diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/.gitignore b/target/maven-status/maven-compiler-plugin/compile/default-compile/.gitignore new file mode 100644 index 0000000..b305646 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/.gitignore @@ -0,0 +1 @@ +/inputFiles.lst diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..d121145 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1,2 @@ +com\yes\yes\ui\BuildItem.class +com\yes\yes\entities\machines\TestMachine2.class