mirror of
https://github.com/Stefan-5422/School-Programming-Projekt-022022.git
synced 2026-09-04 00:46:01 +02:00
Add class to get entities around other entities
- Add `BlockContainer` class - Add more error logging - Refactor code to work with the new system
This commit is contained in:
Generated
+1
-1
@@ -7,7 +7,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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<String,Void> onClick) {
|
||||
public BuildItem(String name, Function<String, Void> 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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
/inputFiles.lst
|
||||
@@ -0,0 +1,2 @@
|
||||
com\yes\yes\ui\BuildItem.class
|
||||
com\yes\yes\entities\machines\TestMachine2.class
|
||||
Reference in New Issue
Block a user