mirror of
https://github.com/Stefan-5422/School-Programming-Projekt-022022.git
synced 2026-09-04 00:46:01 +02:00
Improve code readability and structure
- Fix exception when trying to place an item without one selected - Add access modifiers to all class members - Add `final` to all variables if applicable - Add `TestBehaviour` to the machines to test the event system - Convert classes to records where appropriate - Change `Function<T, T>` to `Consumer<T>` - Change non generic methods in the `EventHandler` class to generic methods - Remove unused imports - Other minor improvements
This commit is contained in:
+14
@@ -0,0 +1,14 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="MavenPackageUpdate" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
|
<option name="excludeList">
|
||||||
|
<list>
|
||||||
|
<option value="org.openjfx:javafx-fxml" />
|
||||||
|
<option value="org.junit.jupiter:junit-jupiter-api" />
|
||||||
|
<option value="org.junit.jupiter:junit-jupiter-engine" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</inspection_tool>
|
||||||
|
</profile>
|
||||||
|
</component>
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.yes.yes;
|
package com.yes.yes;
|
||||||
|
|
||||||
import com.yes.yes.managers.*;
|
import com.yes.yes.managers.GameManager;
|
||||||
|
import com.yes.yes.managers.PlayerManager;
|
||||||
|
import com.yes.yes.managers.UiManager;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
@@ -8,7 +10,7 @@ import javafx.scene.layout.VBox;
|
|||||||
|
|
||||||
public class MainController {
|
public class MainController {
|
||||||
@FXML
|
@FXML
|
||||||
VBox root;
|
private VBox root;
|
||||||
|
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
GameManager gameManager = new GameManager(root);
|
GameManager gameManager = new GameManager(root);
|
||||||
|
|||||||
@@ -2,12 +2,15 @@ package com.yes.yes;
|
|||||||
|
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
import javafx.scene.transform.Scale;
|
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class YesApplication extends javafx.application.Application {
|
public class YesApplication extends javafx.application.Application {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
launch();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage stage) throws IOException {
|
public void start(Stage stage) throws IOException {
|
||||||
FXMLLoader fxmlLoader = new FXMLLoader(YesApplication.class.getResource("main-view.fxml"));
|
FXMLLoader fxmlLoader = new FXMLLoader(YesApplication.class.getResource("main-view.fxml"));
|
||||||
@@ -21,8 +24,4 @@ public class YesApplication extends javafx.application.Application {
|
|||||||
stage.setMaximized(true);
|
stage.setMaximized(true);
|
||||||
stage.show();
|
stage.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
launch();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -12,8 +12,7 @@ public class PlaceBehaviour extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize()
|
public void initialize() {
|
||||||
{
|
|
||||||
for (int x = -1; x < 2; x++) {
|
for (int x = -1; x < 2; x++) {
|
||||||
for (int y = -1; y < 2; y++) {
|
for (int y = -1; y < 2; y++) {
|
||||||
try {
|
try {
|
||||||
@@ -21,7 +20,6 @@ public class PlaceBehaviour extends Component {
|
|||||||
|
|
||||||
if (entity != null) {
|
if (entity != null) {
|
||||||
entity.trigger("placed", parent);
|
entity.trigger("placed", parent);
|
||||||
System.out.println(entity.getClass().getSimpleName());
|
|
||||||
}
|
}
|
||||||
} catch (IllegalAccessException ex) {
|
} catch (IllegalAccessException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
|
|||||||
@@ -11,9 +11,13 @@ public class TestBehaviour extends Component {
|
|||||||
super(entity, blockContainer);
|
super(entity, blockContainer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void onNeighborPlaced(Entity entity) {
|
||||||
|
System.out.println(parent.getClass().getSimpleName() + " > \"" + entity.getClass().getSimpleName() + "\" got placed in the radius.");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
|
parent.addListener("placed", this::onNeighborPlaced);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package com.yes.yes.entities.machines;
|
package com.yes.yes.entities.machines;
|
||||||
|
|
||||||
import com.yes.yes.behaviours.PlaceBehaviour;
|
import com.yes.yes.behaviours.PlaceBehaviour;
|
||||||
import com.yes.yes.utils.*;
|
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.Circle;
|
||||||
|
|
||||||
public class TestMachine extends Entity {
|
public class TestMachine extends Entity {
|
||||||
@@ -13,5 +15,6 @@ public class TestMachine extends Entity {
|
|||||||
public TestMachine(BlockContainer blockContainer) {
|
public TestMachine(BlockContainer blockContainer) {
|
||||||
this();
|
this();
|
||||||
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
|
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
|
||||||
|
this.addBehaviour(new TestBehaviour(this, blockContainer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.yes.yes.entities.machines;
|
package com.yes.yes.entities.machines;
|
||||||
|
|
||||||
import com.yes.yes.behaviours.PlaceBehaviour;
|
import com.yes.yes.behaviours.PlaceBehaviour;
|
||||||
|
import com.yes.yes.behaviours.TestBehaviour;
|
||||||
import com.yes.yes.utils.BlockContainer;
|
import com.yes.yes.utils.BlockContainer;
|
||||||
import com.yes.yes.utils.Entity;
|
import com.yes.yes.utils.Entity;
|
||||||
import javafx.scene.shape.Rectangle;
|
import javafx.scene.shape.Rectangle;
|
||||||
@@ -14,5 +15,6 @@ public class TestMachine2 extends Entity {
|
|||||||
public TestMachine2(BlockContainer blockContainer) {
|
public TestMachine2(BlockContainer blockContainer) {
|
||||||
this();
|
this();
|
||||||
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
|
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
|
||||||
|
this.addBehaviour(new TestBehaviour(this, blockContainer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,6 @@ package com.yes.yes.managers;
|
|||||||
|
|
||||||
import com.yes.yes.entities.machines.TestMachine;
|
import com.yes.yes.entities.machines.TestMachine;
|
||||||
import com.yes.yes.entities.machines.TestMachine2;
|
import com.yes.yes.entities.machines.TestMachine2;
|
||||||
import com.yes.yes.utils.Coordinate;
|
|
||||||
import com.yes.yes.utils.EntityRegistry;
|
import com.yes.yes.utils.EntityRegistry;
|
||||||
import com.yes.yes.utils.RegistryEntry;
|
import com.yes.yes.utils.RegistryEntry;
|
||||||
import com.yes.yes.utils.exceptions.AlreadyExistsException;
|
import com.yes.yes.utils.exceptions.AlreadyExistsException;
|
||||||
@@ -10,17 +9,17 @@ import com.yes.yes.world.World;
|
|||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
|
|
||||||
public class GameManager {
|
public class GameManager {
|
||||||
VBox root;
|
private final VBox root;
|
||||||
World world;
|
private World world;
|
||||||
|
|
||||||
public World getWorld() {
|
|
||||||
return world;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GameManager(VBox root) {
|
public GameManager(VBox root) {
|
||||||
this.root = root;
|
this.root = root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public World getWorld() {
|
||||||
|
return world;
|
||||||
|
}
|
||||||
|
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
world = new World();
|
world = new World();
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
package com.yes.yes.managers;
|
package com.yes.yes.managers;
|
||||||
|
|
||||||
import com.yes.yes.ui.BuildBox;
|
import com.yes.yes.ui.BuildBox;
|
||||||
import com.yes.yes.utils.BlockContainer;
|
import com.yes.yes.utils.*;
|
||||||
import com.yes.yes.utils.Coordinate;
|
|
||||||
import com.yes.yes.utils.Entity;
|
|
||||||
import com.yes.yes.utils.EntityRegistry;
|
|
||||||
import com.yes.yes.world.Chunk;
|
import com.yes.yes.world.Chunk;
|
||||||
import com.yes.yes.world.World;
|
import com.yes.yes.world.World;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
@@ -15,14 +12,14 @@ import javafx.scene.input.KeyEvent;
|
|||||||
|
|
||||||
public class PlayerManager {
|
public class PlayerManager {
|
||||||
|
|
||||||
static final KeyCombination LEFT_KEY = new KeyCodeCombination(KeyCode.A);
|
private static final KeyCombination LEFT_KEY = new KeyCodeCombination(KeyCode.A);
|
||||||
static final KeyCombination RIGHT_KEY = new KeyCodeCombination(KeyCode.D);
|
private static final KeyCombination RIGHT_KEY = new KeyCodeCombination(KeyCode.D);
|
||||||
static final KeyCombination UP_KEY = new KeyCodeCombination(KeyCode.W);
|
private static final KeyCombination UP_KEY = new KeyCodeCombination(KeyCode.W);
|
||||||
static final KeyCombination DOWN_KEY = new KeyCodeCombination(KeyCode.S);
|
private static final KeyCombination DOWN_KEY = new KeyCodeCombination(KeyCode.S);
|
||||||
|
|
||||||
World world;
|
private final World world;
|
||||||
BuildBox buildBox;
|
private final BuildBox buildBox;
|
||||||
Coordinate chunkPos = new Coordinate(0, 0);
|
private Coordinate chunkPos = new Coordinate(0, 0);
|
||||||
|
|
||||||
public PlayerManager(World world, BuildBox buildBox) {
|
public PlayerManager(World world, BuildBox buildBox) {
|
||||||
this.world = world;
|
this.world = world;
|
||||||
@@ -40,11 +37,16 @@ public class PlayerManager {
|
|||||||
Coordinate blockCoordinate = Coordinate.WorldToChunkBlock(mouseCoordinate);
|
Coordinate blockCoordinate = Coordinate.WorldToChunkBlock(mouseCoordinate);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Class[] types = new Class[1];
|
Class<?>[] types = new Class<?>[1];
|
||||||
types[0] = BlockContainer.class;
|
types[0] = BlockContainer.class;
|
||||||
|
|
||||||
BlockContainer blockContainer = new BlockContainer(world, blockCoordinate, chunkCoordinate);
|
BlockContainer blockContainer = new BlockContainer(world, blockCoordinate, chunkCoordinate);
|
||||||
Entity entity = (Entity) EntityRegistry.getEntity(buildBox.getSelectedEntity()).getEntity().getConstructor(types).newInstance(blockContainer);
|
RegistryEntry registryEntry = EntityRegistry.getEntry(buildBox.getSelectedEntity());
|
||||||
|
|
||||||
|
if (registryEntry == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Entity entity = (Entity) registryEntry.getEntity().getConstructor(types).newInstance(blockContainer);
|
||||||
|
|
||||||
world.getChunk(chunkCoordinate).setEntity(entity, blockCoordinate);
|
world.getChunk(chunkCoordinate).setEntity(entity, blockCoordinate);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
@@ -53,11 +55,11 @@ public class PlayerManager {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
world.setTranslateX(Integer.MAX_VALUE / -5000);
|
world.setTranslateX(Integer.MAX_VALUE / -5000d);
|
||||||
world.setTranslateY(Integer.MAX_VALUE / -5000);
|
world.setTranslateY(Integer.MAX_VALUE / -5000d);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProcessKeyPress(KeyEvent key) {
|
private void ProcessKeyPress(KeyEvent key) {
|
||||||
if (LEFT_KEY.match(key)) {
|
if (LEFT_KEY.match(key)) {
|
||||||
world.setTranslateX(world.getTranslateX() + 45);
|
world.setTranslateX(world.getTranslateX() + 45);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import com.yes.yes.ui.BuildBox;
|
|||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
|
|
||||||
public class UiManager {
|
public class UiManager {
|
||||||
VBox root;
|
private final VBox root;
|
||||||
BuildBox buildBox;
|
private BuildBox buildBox;
|
||||||
|
|
||||||
public UiManager(VBox root) {
|
public UiManager(VBox root) {
|
||||||
this.root = root;
|
this.root = root;
|
||||||
|
|||||||
@@ -5,18 +5,10 @@ import javafx.application.Platform;
|
|||||||
import javafx.scene.control.ScrollPane;
|
import javafx.scene.control.ScrollPane;
|
||||||
import javafx.scene.layout.HBox;
|
import javafx.scene.layout.HBox;
|
||||||
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
public class BuildBox extends javafx.scene.Group {
|
public class BuildBox extends javafx.scene.Group {
|
||||||
String selectedEntity;
|
String selectedEntity;
|
||||||
|
|
||||||
|
|
||||||
Function<String, Void> changeItem = i -> {
|
|
||||||
System.out.println(i);
|
|
||||||
this.selectedEntity = i;
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
public BuildBox() {
|
public BuildBox() {
|
||||||
ScrollPane pane = new ScrollPane();
|
ScrollPane pane = new ScrollPane();
|
||||||
HBox hbox = new HBox();
|
HBox hbox = new HBox();
|
||||||
@@ -32,7 +24,12 @@ public class BuildBox extends javafx.scene.Group {
|
|||||||
|
|
||||||
Platform.runLater(() -> pane.prefWidthProperty().bind(this.getScene().getWindow().widthProperty()));
|
Platform.runLater(() -> pane.prefWidthProperty().bind(this.getScene().getWindow().widthProperty()));
|
||||||
|
|
||||||
EntityRegistry.getKeys().forEach(e -> hbox.getChildren().add(new BuildItem(e, changeItem)));
|
EntityRegistry.getKeys().forEach(e -> hbox.getChildren().add(new BuildItem(e, this::changeItem)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void changeItem(String name) {
|
||||||
|
System.out.println(name);
|
||||||
|
this.selectedEntity = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSelectedEntity() {
|
public String getSelectedEntity() {
|
||||||
|
|||||||
@@ -5,18 +5,17 @@ import com.yes.yes.world.Chunk;
|
|||||||
import javafx.scene.Node;
|
import javafx.scene.Node;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class BuildItem extends javafx.scene.layout.StackPane {
|
public class BuildItem extends javafx.scene.layout.StackPane {
|
||||||
String display_name;
|
private final String display_name;
|
||||||
String name;
|
private final String name;
|
||||||
Class<?> entity;
|
|
||||||
|
|
||||||
public BuildItem(String name, Function<String, Void> onClick) {
|
public BuildItem(String name, Consumer<String> onClick) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.display_name = EntityRegistry.getEntity(name).getDisplayName();
|
this.display_name = EntityRegistry.getEntry(name).getDisplayName();
|
||||||
this.entity = EntityRegistry.getEntity(name).getEntity();
|
Class<?> entity = EntityRegistry.getEntry(name).getEntity();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
|
||||||
this.prefWidth(Chunk.ENTITY_SIZE);
|
this.prefWidth(Chunk.ENTITY_SIZE);
|
||||||
@@ -33,7 +32,7 @@ public class BuildItem extends javafx.scene.layout.StackPane {
|
|||||||
button.setStyle("-fx-background-color: null; -fx-border-color: null");
|
button.setStyle("-fx-background-color: null; -fx-border-color: null");
|
||||||
button.setPrefSize(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
|
button.setPrefSize(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
|
||||||
button.setMinSize(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
|
button.setMinSize(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
|
||||||
button.setOnAction((e) -> onClick.apply(this.name));
|
button.setOnAction((e) -> onClick.accept(this.name));
|
||||||
|
|
||||||
this.getChildren().add(button);
|
this.getChildren().add(button);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,18 +3,9 @@ package com.yes.yes.utils;
|
|||||||
import com.yes.yes.world.Chunk;
|
import com.yes.yes.world.Chunk;
|
||||||
import com.yes.yes.world.World;
|
import com.yes.yes.world.World;
|
||||||
|
|
||||||
public class BlockContainer {
|
public record BlockContainer(World world, Coordinate blockCoordinate, Coordinate chunkCoordinate) {
|
||||||
final World world;
|
|
||||||
final Coordinate blockCoordinate;
|
|
||||||
final Coordinate chunkCoordinate;
|
|
||||||
final static int MAX_RADIUS = 2;
|
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 {
|
public Entity getBlock(Coordinate offset) throws IllegalAccessException {
|
||||||
if (Math.abs(offset.x) > MAX_RADIUS || Math.abs(offset.y) > MAX_RADIUS)
|
if (Math.abs(offset.x) > MAX_RADIUS || Math.abs(offset.y) > MAX_RADIUS)
|
||||||
throw new IllegalAccessException();
|
throw new IllegalAccessException();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.yes.yes.utils;
|
package com.yes.yes.utils;
|
||||||
|
|
||||||
public abstract class Component {
|
public abstract class Component {
|
||||||
protected Entity parent;
|
protected final Entity parent;
|
||||||
protected final BlockContainer blockContainer;
|
protected final BlockContainer blockContainer;
|
||||||
|
|
||||||
public Component(Entity entity, BlockContainer blockContainer) {
|
public Component(Entity entity, BlockContainer blockContainer) {
|
||||||
@@ -10,5 +10,6 @@ public abstract class Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public abstract void initialize();
|
public abstract void initialize();
|
||||||
|
|
||||||
public abstract void update();
|
public abstract void update();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,23 +13,6 @@ public class Coordinate {
|
|||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinate() {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o) return true;
|
|
||||||
if (!(o instanceof Coordinate)) return false;
|
|
||||||
Coordinate that = (Coordinate) o;
|
|
||||||
return x == that.x && y == that.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Coordinate WorldToChunkCoordinate(Coordinate worldPos) {
|
public static Coordinate WorldToChunkCoordinate(Coordinate worldPos) {
|
||||||
int chunkSize = Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE;
|
int chunkSize = Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE;
|
||||||
|
|
||||||
@@ -43,4 +26,16 @@ public class Coordinate {
|
|||||||
|
|
||||||
return new Coordinate(worldPos.x / Chunk.ENTITY_SIZE, worldPos.y / Chunk.ENTITY_SIZE);
|
return new Coordinate(worldPos.x / Chunk.ENTITY_SIZE, worldPos.y / Chunk.ENTITY_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (!(o instanceof Coordinate that)) return false;
|
||||||
|
return x == that.x && y == that.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(x, y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,12 +2,13 @@ package com.yes.yes.utils;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public abstract class Entity extends javafx.scene.Group {
|
public abstract class Entity extends javafx.scene.Group {
|
||||||
private ArrayList<Component> behaviours = new ArrayList<>();
|
private final HashMap<String, Object> data = new HashMap<>();
|
||||||
private final HashMap<String, Object> data = new HashMap<String, Object>();
|
|
||||||
private final EventHandler handler = new EventHandler();
|
private final EventHandler handler = new EventHandler();
|
||||||
|
private ArrayList<Component> behaviours = new ArrayList<>();
|
||||||
private int rotation;
|
private int rotation;
|
||||||
|
|
||||||
public final void addBehaviour(Component behaviour) {
|
public final void addBehaviour(Component behaviour) {
|
||||||
@@ -40,7 +41,7 @@ public abstract class Entity extends javafx.scene.Group {
|
|||||||
return (T) data.get(key);
|
return (T) data.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void addListener(String eventName, Function<Object, Void> function) {
|
public final <T> void addListener(String eventName, Consumer<T> function) {
|
||||||
handler.addListener(eventName, function);
|
handler.addListener(eventName, function);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public abstract class EntityRegistry {
|
public abstract class EntityRegistry {
|
||||||
static HashMap<String, RegistryEntry> entities = new HashMap<>();
|
private static final HashMap<String, RegistryEntry> entities = new HashMap<>();
|
||||||
|
|
||||||
public static void register(RegistryEntry entry) throws AlreadyExistsException {
|
public static void register(RegistryEntry entry) throws AlreadyExistsException {
|
||||||
if (entities.containsKey(entry.getName())) {
|
if (entities.containsKey(entry.getName())) {
|
||||||
@@ -16,7 +16,7 @@ public abstract class EntityRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RegistryEntry getEntity(String name) {
|
public static RegistryEntry getEntry(String name) {
|
||||||
return entities.get(name);
|
return entities.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,18 +2,19 @@ package com.yes.yes.utils;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class EventHandler {
|
public class EventHandler {
|
||||||
private final HashMap<String, ArrayList<Function<Object, Void>>> events = new HashMap<>();
|
private final HashMap<String, ArrayList<Consumer<Object>>> events = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
public <T> void addListener(String eventName, Consumer<T> function) {
|
||||||
public void addListener(String eventName, Function<Object, Void> function) {
|
|
||||||
if (!events.containsKey(eventName)) {
|
if (!events.containsKey(eventName)) {
|
||||||
events.put(eventName, new ArrayList<>());
|
events.put(eventName, new ArrayList<>());
|
||||||
}
|
}
|
||||||
events.get(eventName).add(function);
|
|
||||||
|
events.get(eventName).add((e) -> function.accept((T) e));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeListener(String eventName, Function<Object, Void> function) throws IllegalArgumentException {
|
public void removeListener(String eventName, Function<Object, Void> function) throws IllegalArgumentException {
|
||||||
@@ -21,13 +22,13 @@ public class EventHandler {
|
|||||||
if (!events.containsKey(eventName)) {
|
if (!events.containsKey(eventName)) {
|
||||||
throw new IllegalArgumentException("Event " + eventName + " does not exist!");
|
throw new IllegalArgumentException("Event " + eventName + " does not exist!");
|
||||||
}
|
}
|
||||||
|
//noinspection SuspiciousMethodCalls
|
||||||
events.get(eventName).remove(function);
|
events.get(eventName).remove(function);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void trigger(String eventName, Object parameter) {
|
public <T> void trigger(String eventName, T parameter) {
|
||||||
//TODO: Check if this needs null checking
|
|
||||||
if (events.containsKey(eventName)) {
|
if (events.containsKey(eventName)) {
|
||||||
events.get(eventName).forEach(e -> e.apply(parameter));
|
events.get(eventName).forEach(e -> e.accept(parameter));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
package com.yes.yes.utils;
|
package com.yes.yes.utils;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public abstract class GlobalEventHandler {
|
public abstract class GlobalEventHandler {
|
||||||
private static final EventHandler handler = new EventHandler();
|
private static final EventHandler handler = new EventHandler();
|
||||||
|
|
||||||
public static void addListener(String eventName, Function<Object, Void> function) {
|
public static <T> void addListener(String eventName, Consumer<T> function) {
|
||||||
handler.addListener(eventName, function);
|
handler.addListener(eventName, function);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,6 @@
|
|||||||
package com.yes.yes.utils;
|
package com.yes.yes.utils;
|
||||||
|
|
||||||
public class RegistryEntry {
|
public record RegistryEntry(String name, String displayName, Class<?> entity) {
|
||||||
String name;
|
|
||||||
String displayName;
|
|
||||||
Class<?> entity;
|
|
||||||
|
|
||||||
public RegistryEntry(String name, String displayName, Class<?> entity) {
|
|
||||||
this.name = name;
|
|
||||||
this.displayName = displayName;
|
|
||||||
this.entity = entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
|
|||||||
@@ -3,17 +3,14 @@ package com.yes.yes.utils;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class Size {
|
public class Size {
|
||||||
public int x;
|
public final int x;
|
||||||
public int y;
|
public final int y;
|
||||||
|
|
||||||
public Size(int x, int y) {
|
public Size(int x, int y) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Size() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package com.yes.yes.world;
|
|||||||
import com.yes.yes.utils.Coordinate;
|
import com.yes.yes.utils.Coordinate;
|
||||||
import com.yes.yes.utils.Entity;
|
import com.yes.yes.utils.Entity;
|
||||||
import javafx.geometry.HPos;
|
import javafx.geometry.HPos;
|
||||||
import javafx.geometry.Pos;
|
|
||||||
import javafx.geometry.VPos;
|
import javafx.geometry.VPos;
|
||||||
import javafx.scene.layout.*;
|
import javafx.scene.layout.*;
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<?import javafx.geometry.Insets?>
|
|
||||||
<?import javafx.scene.control.Label?>
|
|
||||||
<?import javafx.scene.layout.VBox?>
|
<?import javafx.scene.layout.VBox?>
|
||||||
|
|
||||||
<?import javafx.scene.control.Button?>
|
|
||||||
<VBox spacing="20.0" xmlns:fx="http://javafx.com/fxml"
|
<VBox spacing="20.0" xmlns:fx="http://javafx.com/fxml"
|
||||||
fx:controller="com.yes.yes.MainController" fx:id="root">
|
fx:controller="com.yes.yes.MainController" fx:id="root">
|
||||||
</VBox>
|
</VBox>
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<?import javafx.geometry.Insets?>
|
|
||||||
<?import javafx.scene.control.Label?>
|
|
||||||
<?import javafx.scene.layout.VBox?>
|
<?import javafx.scene.layout.VBox?>
|
||||||
|
|
||||||
<?import javafx.scene.control.Button?>
|
|
||||||
<VBox spacing="20.0" xmlns:fx="http://javafx.com/fxml"
|
<VBox spacing="20.0" xmlns:fx="http://javafx.com/fxml"
|
||||||
fx:controller="com.yes.yes.MainController" fx:id="root">
|
fx:controller="com.yes.yes.MainController" fx:id="root">
|
||||||
</VBox>
|
</VBox>
|
||||||
|
|||||||
Reference in New Issue
Block a user