mirror of
https://github.com/Stefan-5422/School-Programming-Projekt-022022.git
synced 2026-09-04 00:46:01 +02:00
Code cleanup and change reset position key to the home key
This commit is contained in:
@@ -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;
|
||||
@@ -17,7 +15,7 @@ public class YesApplication extends javafx.application.Application {
|
||||
@Override
|
||||
public void stop() {
|
||||
System.out.println("Closing!");
|
||||
GlobalEventHandler.trigger("closing",null);
|
||||
GlobalEventHandler.trigger("closing", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -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;
|
||||
@@ -48,7 +48,7 @@ public class OfferBehaviour extends Component {
|
||||
}
|
||||
|
||||
this.parent.addListener("itemAccepted", this, this::itemAccepted);
|
||||
this.parent.addListener("placed", this,this::placed);
|
||||
this.parent.addListener("placed", this, this::placed);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -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,17 +6,16 @@ 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() {
|
||||
super();
|
||||
Polygon t = new Polygon();
|
||||
t.getPoints().addAll(
|
||||
25.0,0.0,
|
||||
0.0,50.0,
|
||||
50.0,50.0);
|
||||
t.setFill(new Color(0,0,0,1));
|
||||
25.0, 0.0,
|
||||
0.0, 50.0,
|
||||
50.0, 50.0);
|
||||
t.setFill(new Color(0, 0, 0, 1));
|
||||
|
||||
this.getChildren().addAll(t);
|
||||
}
|
||||
@@ -24,11 +23,11 @@ public class ConveyorBelt extends Entity {
|
||||
public ConveyorBelt(BlockContainer blockContainer) {
|
||||
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 ItemBehaviour(this, blockContainer, "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));
|
||||
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() {
|
||||
@@ -22,18 +21,18 @@ public class GeneratorMachine extends Entity {
|
||||
|
||||
Polygon p = new Polygon();
|
||||
p.getPoints().addAll(
|
||||
25.0,0.0,
|
||||
0.0,50.0,
|
||||
50.0,50.0);
|
||||
p.setFill(new Color(0,0,0,0.5));
|
||||
25.0, 0.0,
|
||||
0.0, 50.0,
|
||||
50.0, 50.0);
|
||||
p.setFill(new Color(0, 0, 0, 0.5));
|
||||
|
||||
this.getChildren().addAll(r,p);
|
||||
this.getChildren().addAll(r, p);
|
||||
}
|
||||
|
||||
public GeneratorMachine(BlockContainer blockContainer) {
|
||||
this();
|
||||
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
|
||||
this.addBehaviour(new ItemBehaviour(this, blockContainer,"Item"));
|
||||
this.addBehaviour(new ItemBehaviour(this, blockContainer, "Item"));
|
||||
this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "Item"));
|
||||
this.addBehaviour(new GeneratorBehaviour(this, blockContainer, generateItem(), "Item"));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -20,19 +20,19 @@ public class TestMachine extends Entity {
|
||||
|
||||
Polygon t = new Polygon();
|
||||
t.getPoints().addAll(
|
||||
25.0,0.0,
|
||||
0.0,50.0,
|
||||
50.0,50.0);
|
||||
t.setFill(new Color(1,1,1,0.5));
|
||||
25.0, 0.0,
|
||||
0.0, 50.0,
|
||||
50.0, 50.0);
|
||||
t.setFill(new Color(1, 1, 1, 0.5));
|
||||
|
||||
this.getChildren().addAll(c,t);
|
||||
this.getChildren().addAll(c, t);
|
||||
}
|
||||
|
||||
public TestMachine(BlockContainer blockContainer) {
|
||||
this();
|
||||
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 ItemBehaviour(this, blockContainer, "Item"));
|
||||
this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.DOWN, "Item"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public class TestMachine2 extends Entity {
|
||||
this();
|
||||
this.addBehaviour(new PlaceBehaviour(this, blockContainer));
|
||||
this.addBehaviour(new TestBehaviour(this, blockContainer));
|
||||
this.addBehaviour(new ItemBehaviour(this, blockContainer,"Item"));
|
||||
this.addBehaviour(new ItemBehaviour(this, blockContainer, "Item"));
|
||||
this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.RIGHT, "Item"));
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ public class Square extends com.yes.yes.utils.Part {
|
||||
int b = rand.nextInt(255);
|
||||
|
||||
var rect = new Rectangle(23, 23);
|
||||
rect.setFill(Color.rgb(r,g,b));
|
||||
rect.setFill(Color.rgb(r, g, b));
|
||||
rect.setStroke(Color.BLUE);
|
||||
rect.setStrokeWidth(1);
|
||||
this.getChildren().add(rect);
|
||||
|
||||
@@ -35,8 +35,8 @@ public class GameManager {
|
||||
EntityRegistry.register(new RegistryEntry("test2", "test offer machine", TestMachine2.class));
|
||||
EntityRegistry.register(new RegistryEntry("generator", "Generator", GeneratorMachine.class));
|
||||
EntityRegistry.register(new RegistryEntry("conveyorBelt", "Conveyor Belt", ConveyorBelt.class));
|
||||
EntityRegistry.register(new RegistryEntry("hub","Central Hub", HubDisplay.class));
|
||||
EntityRegistry.register(new RegistryEntry("hubAcceptor","Central Hub Acceptor", HubAcceptor.class));
|
||||
EntityRegistry.register(new RegistryEntry("hub", "Central Hub", HubDisplay.class));
|
||||
EntityRegistry.register(new RegistryEntry("hubAcceptor", "Central Hub Acceptor", HubAcceptor.class));
|
||||
} catch (AlreadyExistsException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -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());
|
||||
@@ -41,7 +39,7 @@ public class PlayerManager {
|
||||
world.setOnMouseClicked(this::processClick);
|
||||
world.setTranslateX(START_POSITION.x);
|
||||
world.setTranslateY(START_POSITION.y);
|
||||
world.setOnMouseMoved((mouse)-> currentMousePosition = new Coordinate((int) mouse.getX(), (int) mouse.getY()));
|
||||
world.setOnMouseMoved((mouse) -> currentMousePosition = new Coordinate((int) mouse.getX(), (int) mouse.getY()));
|
||||
|
||||
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;
|
||||
|
||||
@@ -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,9 +42,8 @@ public abstract class Entity extends javafx.scene.Group {
|
||||
behaviours.forEach(Component::initialize);
|
||||
}
|
||||
|
||||
public final void destroy()
|
||||
{
|
||||
GlobalEventHandler.removeListener("timerTick",this, (__) -> update());
|
||||
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();
|
||||
@@ -10,7 +9,7 @@ public class GlobalEventHandler {
|
||||
}
|
||||
|
||||
public static <T> void addListener(String eventName, Object object, Consumer<T> function) {
|
||||
handler.addListener(eventName,object, function);
|
||||
handler.addListener(eventName, object, function);
|
||||
}
|
||||
|
||||
public static <T> void removeListener(String eventName, Object object, Consumer<T> function) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user