Improve code formatting

This commit is contained in:
Stone_Red
2022-06-02 17:04:31 +02:00
parent 98fe8b2d1b
commit a20841491d
21 changed files with 68 additions and 83 deletions
@@ -20,7 +20,7 @@ public class YesApplication extends javafx.application.Application {
@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"));
Scene scene = new Scene(fxmlLoader.load(), 1280 , 720 ); Scene scene = new Scene(fxmlLoader.load(), 1280, 720);
stage.setTitle("Yes!"); stage.setTitle("Yes!");
stage.setScene(scene); stage.setScene(scene);
@@ -4,9 +4,9 @@ import com.yes.yes.utils.*;
public class DataStoreEvent extends Component { public class DataStoreEvent extends Component {
String dataKey; private final String dataKey;
String eventName; private final String eventName;
boolean global; private final boolean global;
public DataStoreEvent(Entity entity, BlockContainer blockContainer, String dataKey, String eventName, boolean global) { public DataStoreEvent(Entity entity, BlockContainer blockContainer, String dataKey, String eventName, boolean global) {
@@ -19,7 +19,7 @@ public class DestroyBehaviour extends Component {
@Override @Override
public void update() { public void update() {
if(this.parent.getData(dataKey) !=null) { if (this.parent.getData(dataKey) != null) {
this.parent.setData(dataKey, null); this.parent.setData(dataKey, null);
} }
} }
@@ -4,9 +4,9 @@ import com.yes.yes.utils.*;
public class EventDataStore extends Component { public class EventDataStore extends Component {
String eventName; private final String eventName;
String dataKey; private final String dataKey;
boolean global; private final boolean global;
public EventDataStore(Entity entity, BlockContainer blockContainer, String eventName, String dataKey, boolean global) { public EventDataStore(Entity entity, BlockContainer blockContainer, String eventName, String dataKey, boolean global) {
super(entity, blockContainer); super(entity, blockContainer);
@@ -8,8 +8,8 @@ import javafx.scene.text.Text;
public class HubDisplayBehaviour extends Component { public class HubDisplayBehaviour extends Component {
String eventName; private final String eventName;
Text text; private final Text text;
public HubDisplayBehaviour(Entity entity, BlockContainer blockContainer, String eventName, Text text) { public HubDisplayBehaviour(Entity entity, BlockContainer blockContainer, String eventName, Text text) {
super(entity, blockContainer); super(entity, blockContainer);
@@ -17,8 +17,7 @@ public class HubDisplayBehaviour extends Component {
this.text = text; this.text = text;
} }
private void updateText(String content) private void updateText(String content) {
{
this.text.setText(content); this.text.setText(content);
} }
@@ -2,9 +2,7 @@ package com.yes.yes.behaviours;
import com.yes.yes.utils.*; import com.yes.yes.utils.*;
import com.yes.yes.world.Chunk; import com.yes.yes.world.Chunk;
import javafx.application.Platform;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
@@ -13,8 +11,8 @@ import javafx.scene.transform.Translate;
public class ItemBehaviour extends Component { public class ItemBehaviour extends Component {
private StackPane itemGroup;
private final String dataKey; private final String dataKey;
private StackPane itemGroup;
public ItemBehaviour(Entity entity, BlockContainer blockContainer, String dataKey) { public ItemBehaviour(Entity entity, BlockContainer blockContainer, String dataKey) {
super(entity, blockContainer); super(entity, blockContainer);
@@ -22,21 +20,20 @@ public class ItemBehaviour extends Component {
} }
private void itemChanged(Item item) { private void itemChanged(Item item) {
//GlobalExecQueue.schedule(() -> { GlobalExecQueue.schedule(() -> {
Platform.runLater(() -> {
try { try {
this.parent.getChildren().clear(); this.itemGroup.getChildren().clear();
if (item != null) if (item != null)
this.itemGroup.getChildren().add(item); this.itemGroup.getChildren().add(item.clone());
else { else {
Rectangle alignmentRectangle = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE); Rectangle alignmentRectangle = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
alignmentRectangle.setFill(Color.rgb(0, 0, 0, 0)); alignmentRectangle.setFill(Color.rgb(0, 0, 0, 0));
this.itemGroup.getChildren().add(alignmentRectangle); this.itemGroup.getChildren().add(alignmentRectangle);
} }
} catch (IndexOutOfBoundsException ignore) {
} }
catch (IndexOutOfBoundsException ex){System.out.print(ex.getMessage() + " | " + this.parent.getClass().getSimpleName()); }
}); });
} }
@@ -53,11 +50,11 @@ public class ItemBehaviour extends Component {
Scale s = new Scale(0.7, 0.7); Scale s = new Scale(0.7, 0.7);
itemGroup.getTransforms().add(s); itemGroup.getTransforms().add(s);
Translate translate = new Translate(Chunk.ENTITY_SIZE/4,Chunk.ENTITY_SIZE/4); Translate translate = new Translate(Chunk.ENTITY_SIZE / 4d, Chunk.ENTITY_SIZE / 4d);
itemGroup.getTransforms().add(translate); itemGroup.getTransforms().add(translate);
Rectangle alignmentRectangle = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE); Rectangle alignmentRectangle = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
alignmentRectangle.setFill(Color.rgb(0,0,0,0)); alignmentRectangle.setFill(Color.rgb(0, 0, 0, 0));
this.itemGroup.getChildren().add(alignmentRectangle); this.itemGroup.getChildren().add(alignmentRectangle);
} }
@@ -20,7 +20,6 @@ public class OfferBehaviour extends Component {
} }
private void placed(Entity entity) { private void placed(Entity entity) {
//System.out.println("Entity placed in range");
try { try {
if (blockContainer.getBlockRelative(direction, parent.getRotation()) == entity) { if (blockContainer.getBlockRelative(direction, parent.getRotation()) == entity) {
this.receiver = entity; this.receiver = entity;
@@ -39,7 +39,7 @@ public class ReceiveBehaviour extends Component {
this.parent.addListener("placed", this, this::placed); this.parent.addListener("placed", this, this::placed);
} }
void receive(Pair<Entity,Item> pair) { void receive(Pair<Entity, Item> pair) {
if (offerer == null || offerer != pair.getKey()) return; if (offerer == null || offerer != pair.getKey()) return;
if (this.parent.getData(dataKey) == pair.getValue()) if (this.parent.getData(dataKey) == pair.getValue())
@@ -1,13 +1,12 @@
package com.yes.yes.behaviours; package com.yes.yes.behaviours;
import com.yes.yes.utils.*; import com.yes.yes.utils.*;
import javafx.application.Platform;
public class StackBehaviour extends Component { public class StackBehaviour extends Component {
private final int processingDuration; private final int processingDuration;
private String receiveDataKey1; private final String receiveDataKey1;
private String receiveDataKey2; private final String receiveDataKey2;
private String offerDataKey; private final String offerDataKey;
private int progress = 0; private int progress = 0;
@@ -26,8 +25,7 @@ public class StackBehaviour extends Component {
@Override @Override
public void update() { public void update() {
//GlobalExecQueue.schedule( () -> { GlobalExecQueue.schedule(() -> {
Platform.runLater(() -> {
if (progress > processingDuration) { if (progress > processingDuration) {
if (parent.getData(offerDataKey) != null) return; if (parent.getData(offerDataKey) != null) return;
@@ -17,10 +17,10 @@ public class Cutter extends Entity {
public Cutter() { public Cutter() {
super(); super();
Circle c = new Circle(Chunk.ENTITY_SIZE / 2); Circle c = new Circle(Chunk.ENTITY_SIZE / 2d);
c.setFill(Color.BLACK); c.setFill(Color.BLACK);
c.setCenterX(Chunk.ENTITY_SIZE / 2); c.setCenterX(Chunk.ENTITY_SIZE / 2d);
c.setCenterY(Chunk.ENTITY_SIZE / 2); c.setCenterY(Chunk.ENTITY_SIZE / 2d);
Polygon p = new Polygon(); Polygon p = new Polygon();
p.getPoints().addAll( p.getPoints().addAll(
@@ -8,14 +8,13 @@ import com.yes.yes.utils.Direction;
import com.yes.yes.utils.Entity; import com.yes.yes.utils.Entity;
import com.yes.yes.world.Chunk; import com.yes.yes.world.Chunk;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon; import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
public class DestroyMachine extends Entity { public class DestroyMachine extends Entity {
public DestroyMachine() { public DestroyMachine() {
super(); super();
Rectangle r = new Rectangle(Chunk.ENTITY_SIZE,Chunk.ENTITY_SIZE); Rectangle r = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
Polygon p = new Polygon(); Polygon p = new Polygon();
p.getPoints().addAll( p.getPoints().addAll(
@@ -1,7 +1,6 @@
package com.yes.yes.entities.machines; package com.yes.yes.entities.machines;
import com.yes.yes.behaviours.GeneratorBehaviour; import com.yes.yes.behaviours.GeneratorBehaviour;
import com.yes.yes.behaviours.ItemBehaviour;
import com.yes.yes.behaviours.OfferBehaviour; import com.yes.yes.behaviours.OfferBehaviour;
import com.yes.yes.behaviours.PlaceBehaviour; import com.yes.yes.behaviours.PlaceBehaviour;
import com.yes.yes.entities.parts.Square; import com.yes.yes.entities.parts.Square;
@@ -15,7 +14,7 @@ import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
public class GeneratorMachine extends Entity { public class GeneratorMachine extends Entity {
Rectangle rectangle; private final Rectangle rectangle;
public GeneratorMachine() { public GeneratorMachine() {
super(); super();
@@ -39,7 +38,6 @@ public class GeneratorMachine extends Entity {
rectangle.setFill(blockContainer.chunkColor()); rectangle.setFill(blockContainer.chunkColor());
this.addBehaviour(new PlaceBehaviour(this, blockContainer)); this.addBehaviour(new PlaceBehaviour(this, blockContainer));
//this.addBehaviour(new ItemBehaviour(this, blockContainer, "Item"));
this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "Item")); this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "Item"));
this.addBehaviour(new GeneratorBehaviour(this, blockContainer, generateItem(blockContainer.chunkColor()), "Item")); this.addBehaviour(new GeneratorBehaviour(this, blockContainer, generateItem(blockContainer.chunkColor()), "Item"));
} }
@@ -1,8 +1,6 @@
package com.yes.yes.entities.machines; package com.yes.yes.entities.machines;
import com.yes.yes.behaviours.EventDataStore;
import com.yes.yes.behaviours.HubDisplayBehaviour; import com.yes.yes.behaviours.HubDisplayBehaviour;
import com.yes.yes.behaviours.ItemBehaviour;
import com.yes.yes.behaviours.PlaceBehaviour; import com.yes.yes.behaviours.PlaceBehaviour;
import com.yes.yes.utils.BlockContainer; import com.yes.yes.utils.BlockContainer;
import com.yes.yes.utils.Entity; import com.yes.yes.utils.Entity;
@@ -16,7 +14,7 @@ import javafx.scene.text.Text;
public class HubDisplay extends Entity implements NotOverridable { public class HubDisplay extends Entity implements NotOverridable {
Text text; private final Text text;
public HubDisplay() { public HubDisplay() {
Rectangle r = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE); Rectangle r = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE);
@@ -36,6 +34,6 @@ public class HubDisplay extends Entity implements NotOverridable {
public HubDisplay(BlockContainer blockContainer) { public HubDisplay(BlockContainer blockContainer) {
this(); this();
this.addBehaviour(new PlaceBehaviour(this, blockContainer)); this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new HubDisplayBehaviour(this, blockContainer, "hub:statusText",text)); this.addBehaviour(new HubDisplayBehaviour(this, blockContainer, "hub:statusText", text));
} }
} }
@@ -28,9 +28,9 @@ public class Stacker extends Entity {
public Stacker(BlockContainer blockContainer) { public Stacker(BlockContainer blockContainer) {
this(); this();
this.addBehaviour(new PlaceBehaviour(this, blockContainer)); this.addBehaviour(new PlaceBehaviour(this, blockContainer));
this.addBehaviour(new StackBehaviour(this,blockContainer,"Input1","Input2","Output",2)); this.addBehaviour(new StackBehaviour(this, blockContainer, "Input1", "Input2", "Output", 2));
this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "Output")); this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.UP, "Output"));
this.addBehaviour(new ReceiveBehaviour(this,blockContainer,Direction.LEFT,"Input1")); this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.LEFT, "Input1"));
this.addBehaviour(new ReceiveBehaviour(this,blockContainer,Direction.RIGHT,"Input2")); this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.RIGHT, "Input2"));
} }
} }
@@ -3,17 +3,15 @@ package com.yes.yes.entities.parts;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
import java.util.Random;
public class Square extends com.yes.yes.utils.Part { public class Square extends com.yes.yes.utils.Part {
Rectangle rect = new Rectangle(23, 23); Rectangle rect;
public Square(Color color) public Square(Color color) {
{
this(); this();
setColor(color); setColor(color);
} }
public Square() { public Square() {
rect = new Rectangle(23, 23); rect = new Rectangle(23, 23);
rect.setStroke(Color.BLACK); rect.setStroke(Color.BLACK);
@@ -22,8 +20,7 @@ public class Square extends com.yes.yes.utils.Part {
} }
@Override @Override
protected void onColorChanged(Color color) protected void onColorChanged(Color color) {
{
rect.setFill(color); rect.setFill(color);
} }
} }
@@ -42,16 +42,16 @@ public class GameManager {
e.printStackTrace(); e.printStackTrace();
} }
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(() -> { executor.scheduleAtFixedRate(() -> {
try { try {
GlobalEventHandler.trigger("global:timerTick", null); GlobalEventHandler.trigger("global:timerTick", null);
//GlobalExecQueue.run(); GlobalExecQueue.run();
} catch (Exception ex) { } catch (Exception ex) {
//ex.printStackTrace(); ex.printStackTrace();
} }
//System.out.println("Finished tick!"); //System.out.println("Finished tick!");
}, 0, 10, TimeUnit.MILLISECONDS); }, 0, 100, TimeUnit.MILLISECONDS);
GlobalEventHandler.addListener("app:closing", this, (__) -> executor.shutdown()); GlobalEventHandler.addListener("app:closing", this, (__) -> executor.shutdown());
@@ -30,7 +30,6 @@ public class HubManager {
} }
private Item generateObjective() { private Item generateObjective() {
Item item = new Item(); Item item = new Item();
@@ -48,7 +47,7 @@ public class HubManager {
} }
private String getStatusText() { private String getStatusText() {
return String.format("%s\n%s/%s",level,objectiveCompletion,objectiveTotal); return String.format("%s\n%s/%s", level, objectiveCompletion, objectiveTotal);
} }
private void objectiveCompletion(Item item) { private void objectiveCompletion(Item item) {
@@ -60,7 +59,7 @@ public class HubManager {
newLevel(); newLevel();
} }
GlobalEventHandler.trigger("hub:statusText",getStatusText()); GlobalEventHandler.trigger("hub:statusText", getStatusText());
} }
} }
+1 -1
View File
@@ -49,7 +49,7 @@ public abstract class Entity extends javafx.scene.Group {
public final void setData(String key, Object value) { public final void setData(String key, Object value) {
data.put(key, value); data.put(key, value);
this.trigger(key+"Changed",value); this.trigger(key + "Changed", value);
} }
public final <T> T getData(String key) { public final <T> T getData(String key) {
@@ -2,11 +2,13 @@ package com.yes.yes.utils;
import javafx.application.Platform; import javafx.application.Platform;
import java.util.ArrayList; import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
public class GlobalExecQueue { public class GlobalExecQueue {
private final static ArrayList<Runnable> queue = new ArrayList<>(); private final static Queue<Runnable> queue = new ConcurrentLinkedQueue<>();
private GlobalExecQueue() { private GlobalExecQueue() {
} }
@@ -16,22 +18,21 @@ public class GlobalExecQueue {
} }
public static void run() { public static void run() {
//Semaphore s = new Semaphore(0); Semaphore s = new Semaphore(0);
Platform.runLater( () -> { Platform.runLater(() -> {
for (Runnable runnable : queue) { while (queue.size() > 0) {
synchronized (GlobalExecQueue.class) { synchronized (GlobalExecQueue.class) {
runnable.run(); Objects.requireNonNull(queue.poll()).run();
} }
} }
// s.release(); s.release();
}); });
/*try { try {
//s.acquire(); s.acquire();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
}*/ }
queue.clear(); queue.clear();
} }
} }
@@ -1,10 +1,10 @@
package com.yes.yes.utils; package com.yes.yes.utils;
public class NoiseGenerator { public class NoiseGenerator {
private NoiseGenerator(){} private NoiseGenerator() {
}
public static double calculate(int value) public static double calculate(int value) {
{ return Math.abs(Math.sin(Math.tan(value) + Math.pow(value, 2)) * Math.cos(Math.pow(value, 2)) * Integer.MAX_VALUE);
return Math.abs(Math.sin(Math.tan(value) + Math.pow(value,2)) * Math.cos(Math.pow(value,2)) * Integer.MAX_VALUE);
} }
} }
+4 -4
View File
@@ -15,15 +15,15 @@ public abstract class Part extends javafx.scene.Group {
protected abstract void onColorChanged(Color color); protected abstract void onColorChanged(Color color);
public Color getColor() {
return color;
}
public void setColor(Color color) { public void setColor(Color color) {
this.color = color; this.color = color;
onColorChanged(color); onColorChanged(color);
} }
public Color getColor() {
return color;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;