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:
Stone_Red
2022-03-18 18:10:24 +01:00
parent cde4b91ec5
commit ce2d26b762
25 changed files with 113 additions and 125 deletions
+14
View File
@@ -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;
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.fxml.FXML;
import javafx.scene.layout.VBox;
@@ -8,7 +10,7 @@ import javafx.scene.layout.VBox;
public class MainController {
@FXML
VBox root;
private VBox root;
public void initialize() {
GameManager gameManager = new GameManager(root);
@@ -2,12 +2,15 @@ package com.yes.yes;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
import java.io.IOException;
public class YesApplication extends javafx.application.Application {
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage stage) throws IOException {
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.show();
}
public static void main(String[] args) {
launch();
}
}
@@ -12,8 +12,7 @@ public class PlaceBehaviour extends Component {
}
@Override
public void initialize()
{
public void initialize() {
for (int x = -1; x < 2; x++) {
for (int y = -1; y < 2; y++) {
try {
@@ -21,7 +20,6 @@ public class PlaceBehaviour extends Component {
if (entity != null) {
entity.trigger("placed", parent);
System.out.println(entity.getClass().getSimpleName());
}
} catch (IllegalAccessException ex) {
ex.printStackTrace();
@@ -11,9 +11,13 @@ public class TestBehaviour extends Component {
super(entity, blockContainer);
}
private void onNeighborPlaced(Entity entity) {
System.out.println(parent.getClass().getSimpleName() + " > \"" + entity.getClass().getSimpleName() + "\" got placed in the radius.");
}
@Override
public void initialize() {
parent.addListener("placed", this::onNeighborPlaced);
}
@Override
@@ -1,7 +1,9 @@
package com.yes.yes.entities.machines;
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;
public class TestMachine extends Entity {
@@ -13,5 +15,6 @@ public class TestMachine extends Entity {
public TestMachine(BlockContainer blockContainer) {
this();
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new TestBehaviour(this, blockContainer));
}
}
@@ -1,6 +1,7 @@
package com.yes.yes.entities.machines;
import com.yes.yes.behaviours.PlaceBehaviour;
import com.yes.yes.behaviours.TestBehaviour;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Entity;
import javafx.scene.shape.Rectangle;
@@ -14,5 +15,6 @@ public class TestMachine2 extends Entity {
public TestMachine2(BlockContainer blockContainer) {
this();
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.TestMachine2;
import com.yes.yes.utils.Coordinate;
import com.yes.yes.utils.EntityRegistry;
import com.yes.yes.utils.RegistryEntry;
import com.yes.yes.utils.exceptions.AlreadyExistsException;
@@ -10,17 +9,17 @@ import com.yes.yes.world.World;
import javafx.scene.layout.VBox;
public class GameManager {
VBox root;
World world;
public World getWorld() {
return world;
}
private final VBox root;
private World world;
public GameManager(VBox root) {
this.root = root;
}
public World getWorld() {
return world;
}
public void initialize() {
world = new World();
@@ -1,10 +1,7 @@
package com.yes.yes.managers;
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;
import com.yes.yes.utils.*;
import com.yes.yes.world.Chunk;
import com.yes.yes.world.World;
import javafx.scene.Scene;
@@ -15,14 +12,14 @@ import javafx.scene.input.KeyEvent;
public class PlayerManager {
static final KeyCombination LEFT_KEY = new KeyCodeCombination(KeyCode.A);
static final KeyCombination RIGHT_KEY = new KeyCodeCombination(KeyCode.D);
static final KeyCombination UP_KEY = new KeyCodeCombination(KeyCode.W);
static final KeyCombination DOWN_KEY = new KeyCodeCombination(KeyCode.S);
private static final KeyCombination LEFT_KEY = new KeyCodeCombination(KeyCode.A);
private static final KeyCombination RIGHT_KEY = new KeyCodeCombination(KeyCode.D);
private static final KeyCombination UP_KEY = new KeyCodeCombination(KeyCode.W);
private static final KeyCombination DOWN_KEY = new KeyCodeCombination(KeyCode.S);
World world;
BuildBox buildBox;
Coordinate chunkPos = new Coordinate(0, 0);
private final World world;
private final BuildBox buildBox;
private Coordinate chunkPos = new Coordinate(0, 0);
public PlayerManager(World world, BuildBox buildBox) {
this.world = world;
@@ -40,11 +37,16 @@ public class PlayerManager {
Coordinate blockCoordinate = Coordinate.WorldToChunkBlock(mouseCoordinate);
try {
Class[] types = new Class[1];
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);
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);
} catch (Exception ex) {
@@ -53,11 +55,11 @@ public class PlayerManager {
}
);
world.setTranslateX(Integer.MAX_VALUE / -5000);
world.setTranslateY(Integer.MAX_VALUE / -5000);
world.setTranslateX(Integer.MAX_VALUE / -5000d);
world.setTranslateY(Integer.MAX_VALUE / -5000d);
}
void ProcessKeyPress(KeyEvent key) {
private void ProcessKeyPress(KeyEvent key) {
if (LEFT_KEY.match(key)) {
world.setTranslateX(world.getTranslateX() + 45);
}
@@ -4,8 +4,8 @@ import com.yes.yes.ui.BuildBox;
import javafx.scene.layout.VBox;
public class UiManager {
VBox root;
BuildBox buildBox;
private final VBox root;
private BuildBox buildBox;
public UiManager(VBox root) {
this.root = root;
+6 -9
View File
@@ -5,18 +5,10 @@ import javafx.application.Platform;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import java.util.function.Function;
public class BuildBox extends javafx.scene.Group {
String selectedEntity;
Function<String, Void> changeItem = i -> {
System.out.println(i);
this.selectedEntity = i;
return null;
};
public BuildBox() {
ScrollPane pane = new ScrollPane();
HBox hbox = new HBox();
@@ -32,7 +24,12 @@ public class BuildBox extends javafx.scene.Group {
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() {
+7 -8
View File
@@ -5,18 +5,17 @@ import com.yes.yes.world.Chunk;
import javafx.scene.Node;
import javafx.scene.control.Button;
import java.util.function.Function;
import java.util.function.Consumer;
public class BuildItem extends javafx.scene.layout.StackPane {
String display_name;
String name;
Class<?> entity;
private final String display_name;
private final String name;
public BuildItem(String name, Function<String, Void> onClick) {
public BuildItem(String name, Consumer<String> onClick) {
super();
this.display_name = EntityRegistry.getEntity(name).getDisplayName();
this.entity = EntityRegistry.getEntity(name).getEntity();
this.display_name = EntityRegistry.getEntry(name).getDisplayName();
Class<?> entity = EntityRegistry.getEntry(name).getEntity();
this.name = name;
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.setPrefSize(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);
}
@@ -3,18 +3,9 @@ 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;
public record BlockContainer(World world, Coordinate blockCoordinate, 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();
@@ -1,7 +1,7 @@
package com.yes.yes.utils;
public abstract class Component {
protected Entity parent;
protected final Entity parent;
protected final BlockContainer blockContainer;
public Component(Entity entity, BlockContainer blockContainer) {
@@ -10,5 +10,6 @@ public abstract class Component {
}
public abstract void initialize();
public abstract void update();
}
+12 -17
View File
@@ -13,23 +13,6 @@ public class Coordinate {
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) {
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);
}
@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);
}
}
+4 -3
View File
@@ -2,12 +2,13 @@ package com.yes.yes.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Consumer;
import java.util.function.Function;
public abstract class Entity extends javafx.scene.Group {
private ArrayList<Component> behaviours = new ArrayList<>();
private final HashMap<String, Object> data = new HashMap<String, Object>();
private final HashMap<String, Object> data = new HashMap<>();
private final EventHandler handler = new EventHandler();
private ArrayList<Component> behaviours = new ArrayList<>();
private int rotation;
public final void addBehaviour(Component behaviour) {
@@ -40,7 +41,7 @@ public abstract class Entity extends javafx.scene.Group {
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);
}
@@ -6,7 +6,7 @@ import java.util.ArrayList;
import java.util.HashMap;
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 {
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);
}
@@ -2,18 +2,19 @@ package com.yes.yes.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Consumer;
import java.util.function.Function;
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 void addListener(String eventName, Function<Object, Void> function) {
public <T> void addListener(String eventName, Consumer<T> function) {
if (!events.containsKey(eventName)) {
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 {
@@ -21,13 +22,13 @@ public class EventHandler {
if (!events.containsKey(eventName)) {
throw new IllegalArgumentException("Event " + eventName + " does not exist!");
}
//noinspection SuspiciousMethodCalls
events.get(eventName).remove(function);
}
public void trigger(String eventName, Object parameter) {
//TODO: Check if this needs null checking
public <T> void trigger(String eventName, T parameter) {
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;
import java.util.function.Consumer;
import java.util.function.Function;
public abstract class GlobalEventHandler {
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);
}
@@ -1,15 +1,6 @@
package com.yes.yes.utils;
public class RegistryEntry {
String name;
String displayName;
Class<?> entity;
public RegistryEntry(String name, String displayName, Class<?> entity) {
this.name = name;
this.displayName = displayName;
this.entity = entity;
}
public record RegistryEntry(String name, String displayName, Class<?> entity) {
public String getName() {
return name;
+2 -5
View File
@@ -3,17 +3,14 @@ package com.yes.yes.utils;
import java.util.Objects;
public class Size {
public int x;
public int y;
public final int x;
public final int y;
public Size(int x, int y) {
this.x = x;
this.y = y;
}
public Size() {
}
@Override
public boolean equals(Object o) {
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.Entity;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
@@ -1,10 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<VBox spacing="20.0" xmlns:fx="http://javafx.com/fxml"
fx:controller="com.yes.yes.MainController" fx:id="root">
</VBox>
@@ -1,10 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<VBox spacing="20.0" xmlns:fx="http://javafx.com/fxml"
fx:controller="com.yes.yes.MainController" fx:id="root">
</VBox>