Code cleanup and change reset position key to the home key

This commit is contained in:
Stone_Red
2022-05-13 10:25:24 +02:00
parent b7caf1ce35
commit e14e973295
20 changed files with 69 additions and 83 deletions
@@ -3,9 +3,7 @@ package com.yes.yes;
import com.yes.yes.managers.GameManager;
import com.yes.yes.managers.PlayerManager;
import com.yes.yes.managers.UiManager;
import com.yes.yes.utils.GlobalEventHandler;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.layout.VBox;
@@ -1,8 +1,6 @@
package com.yes.yes;
import com.yes.yes.utils.GlobalEventHandler;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
@@ -7,8 +7,8 @@ import com.yes.yes.utils.Item;
public class GeneratorBehaviour extends Component {
private Item item;
private String dataKey;
private final Item item;
private final String dataKey;
public GeneratorBehaviour(Entity entity, BlockContainer blockContainer, Item item, String dataKey) {
super(entity, blockContainer);
@@ -11,8 +11,8 @@ import javafx.scene.transform.Scale;
public class ItemBehaviour extends Component {
private final String dataKey;
private VBox itemGroup;
private String dataKey;
public ItemBehaviour(Entity entity, BlockContainer blockContainer, String dataKey) {
super(entity, blockContainer);
@@ -4,8 +4,8 @@ import com.yes.yes.utils.*;
public class OfferBehaviour extends Component {
private Coordinate direction;
private String dataKey;
private final Coordinate direction;
private final String dataKey;
private Entity receiver;
@@ -1,7 +1,9 @@
package com.yes.yes.behaviours;
import com.yes.yes.entities.parts.Square;
import com.yes.yes.utils.*;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Component;
import com.yes.yes.utils.Coordinate;
import com.yes.yes.utils.Entity;
public class PlaceBehaviour extends Component {
@@ -6,7 +6,6 @@ import com.yes.yes.utils.Direction;
import com.yes.yes.utils.Entity;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
public class ConveyorBelt extends Entity {
public ConveyorBelt() {
@@ -25,9 +24,9 @@ public class ConveyorBelt extends Entity {
this();
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new ItemBehaviour(this, blockContainer, "ReceivedItem"));
this.addBehaviour(new RecieveBehaviour(this, blockContainer, Direction.DOWN, "ReceivedItem"));
this.addBehaviour(new RecieveBehaviour(this, blockContainer, Direction.LEFT, "ReceivedItem"));
this.addBehaviour(new RecieveBehaviour(this, blockContainer, Direction.RIGHT, "ReceivedItem"));
this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.DOWN, "ReceivedItem"));
this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.LEFT, "ReceivedItem"));
this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.RIGHT, "ReceivedItem"));
this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "OfferItem"));
this.addBehaviour(new ConveyorBehaviour(this, blockContainer, "ReceivedItem", "OfferItem", 1));
}
@@ -12,7 +12,6 @@ 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() {
@@ -1,8 +1,7 @@
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.ReceiveBehaviour;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Direction;
import com.yes.yes.utils.Entity;
@@ -20,6 +19,6 @@ public class HubAcceptor extends Entity {
public HubAcceptor(BlockContainer blockContainer) {
this();
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new RecieveBehaviour(this, blockContainer, Direction.LEFT, "Input"));
this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.LEFT, "Input"));
}
}
@@ -2,9 +2,7 @@ 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.utils.BlockContainer;
import com.yes.yes.utils.Direction;
import com.yes.yes.utils.Entity;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
@@ -2,7 +2,7 @@ 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.ReceiveBehaviour;
import com.yes.yes.behaviours.TestBehaviour;
import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Direction;
@@ -33,6 +33,6 @@ public class TestMachine extends Entity {
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new TestBehaviour(this, blockContainer));
this.addBehaviour(new ItemBehaviour(this, blockContainer, "Item"));
this.addBehaviour(new RecieveBehaviour(this, blockContainer, Direction.DOWN, "Item"));
this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.DOWN, "Item"));
}
}
@@ -46,9 +46,7 @@ public class GameManager {
executor.scheduleAtFixedRate(() -> {
try {
GlobalEventHandler.trigger("timerTick", null);
}
catch (Exception ex)
{
} catch (Exception ex) {
ex.printStackTrace();
}
//System.out.println("Finished tick!");
@@ -7,8 +7,6 @@ import com.yes.yes.world.World;
import javafx.scene.Scene;
import javafx.scene.input.*;
import java.security.Key;
public class PlayerManager {
private static final KeyCombination LEFT_KEY = new KeyCodeCombination(KeyCode.A);
@@ -16,12 +14,11 @@ public class PlayerManager {
private static final KeyCombination UP_KEY = new KeyCodeCombination(KeyCode.W);
private static final KeyCombination DOWN_KEY = new KeyCodeCombination(KeyCode.S);
private static final KeyCombination DELETE_KEY = new KeyCodeCombination(KeyCode.X);
private static final KeyCombination HOME_KEY = new KeyCodeCombination(KeyCode.INSERT);
private static final KeyCombination HOME_KEY = new KeyCodeCombination(KeyCode.HOME);
private static final Coordinate START_POSITION = new Coordinate(Integer.MAX_VALUE / -5000, Integer.MAX_VALUE / -5000);
private final World world;
private final BuildBox buildBox;
private static final Coordinate START_POSITION = new Coordinate(Integer.MAX_VALUE / -5000,Integer.MAX_VALUE / -5000);
private Coordinate chunkPos = new Coordinate(0, 0);
private int currentRotation = 0;
private Coordinate currentMousePosition;
@@ -34,6 +31,7 @@ public class PlayerManager {
public void initialize() {
Scene scene = world.getScene();
scene.setOnKeyPressed(this::processKeyPress);
scene.setOnKeyReleased(this::processKeyRelease);
scene.widthProperty().addListener((e) -> redrawChunks());
scene.heightProperty().addListener((e) -> redrawChunks());
@@ -84,6 +82,21 @@ public class PlayerManager {
}
}
private void processKeyRelease(KeyEvent key) {
if (DELETE_KEY.match(key)) {
Coordinate chunkCoordinate = Coordinate.WorldToChunkCoordinate(currentMousePosition);
Coordinate blockCoordinate = Coordinate.WorldToChunkBlock(currentMousePosition);
Chunk chunk = world.getChunk(chunkCoordinate);
chunk.removeEntity(blockCoordinate);
}
if (HOME_KEY.match(key)) {
world.setTranslateX(START_POSITION.x);
world.setTranslateY(START_POSITION.y);
redrawChunks();
}
}
private void processKeyPress(KeyEvent key) {
if (LEFT_KEY.match(key)) {
world.setTranslateX(world.getTranslateX() + 45);
@@ -97,20 +110,6 @@ public class PlayerManager {
if (DOWN_KEY.match(key)) {
world.setTranslateY(world.getTranslateY() - 45);
}
if(DELETE_KEY.match(key)) {
Coordinate chunkCoordinate = Coordinate.WorldToChunkCoordinate(currentMousePosition);
Coordinate blockCoordinate = Coordinate.WorldToChunkBlock(currentMousePosition);
Chunk chunk = world.getChunk(chunkCoordinate);
chunk.removeEntity(blockCoordinate);
}
if(HOME_KEY.match(key))
{
world.setTranslateX(START_POSITION.x);
world.setTranslateY(START_POSITION.y);
redrawChunks();
}
Size preferredAmountOfChunks = getPreferredAmountOfChunks();
Coordinate chunkPosition = getCurrentChunkPosition();
@@ -12,5 +12,6 @@ public abstract class Component {
public abstract void initialize();
public abstract void update();
public abstract void destroy();
}
@@ -3,13 +3,14 @@ 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);
private Direction() {
}
public static final Coordinate rotateRight(Coordinate direction) {
if (direction == Direction.LEFT) return Direction.UP;
if (direction == Direction.UP) return Direction.RIGHT;
+2 -6
View File
@@ -3,7 +3,6 @@ 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 final HashMap<String, Object> data = new HashMap<>();
@@ -24,9 +23,7 @@ public abstract class Entity extends javafx.scene.Group {
for (Component component : behaviours) {
if (!component.getClass().equals(behaviour.getClass())) {
components.add(component);
}
else
{
} else {
component.destroy();
}
}
@@ -45,8 +42,7 @@ public abstract class Entity extends javafx.scene.Group {
behaviours.forEach(Component::initialize);
}
public final void destroy()
{
public final void destroy() {
GlobalEventHandler.removeListener("timerTick", this, (__) -> update());
for (Component component : behaviours) {
component.destroy();
@@ -1,7 +1,6 @@
package com.yes.yes.utils;
import java.util.function.Consumer;
import java.util.function.Function;
public class GlobalEventHandler {
private static final EventHandler handler = new EventHandler();
@@ -1,6 +1,5 @@
package com.yes.yes.utils;
import com.yes.yes.world.Chunk;
import javafx.scene.Node;
public abstract class Part extends javafx.scene.Group {