Improve code formatting

This commit is contained in:
Stone_Red
2022-04-07 14:05:37 +02:00
parent c9d5b44f90
commit 2a107f029e
7 changed files with 39 additions and 47 deletions
@@ -30,7 +30,7 @@ public class PlaceBehaviour extends Component {
item.setPart(i, i1, new Square());
}
}
// parent.getChildren().add(item);
// parent.getChildren().add(item);
}
@Override
@@ -5,7 +5,7 @@ import javafx.scene.shape.Rectangle;
public class Square extends com.yes.yes.utils.Part {
public Square() {
var rect = new Rectangle(25,25);
var rect = new Rectangle(25, 25);
rect.setFill(Color.RED);
rect.setStroke(Color.BLACK);
rect.setStrokeWidth(1);
@@ -5,10 +5,7 @@ import com.yes.yes.utils.*;
import com.yes.yes.world.Chunk;
import com.yes.yes.world.World;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.*;
public class PlayerManager {
@@ -30,40 +27,39 @@ public class PlayerManager {
Scene scene = world.getScene();
scene.setOnKeyPressed(this::ProcessKeyPress);
scene.widthProperty().addListener((e)-> loadAllOnScreen());
scene.heightProperty().addListener((e)-> loadAllOnScreen());
world.setOnMouseClicked((e) ->
{
Coordinate mouseCoordinate = new Coordinate((int) e.getX(), (int) e.getY());
Coordinate chunkCoordinate = Coordinate.WorldToChunkCoordinate(mouseCoordinate);
Coordinate blockCoordinate = Coordinate.WorldToChunkBlock(mouseCoordinate);
try {
Class<?>[] types = new Class<?>[1];
types[0] = BlockContainer.class;
BlockContainer blockContainer = new BlockContainer(world, blockCoordinate, chunkCoordinate);
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) {
ex.printStackTrace();
}
}
);
scene.widthProperty().addListener((e) -> loadAllOnScreen());
scene.heightProperty().addListener((e) -> loadAllOnScreen());
world.setOnMouseClicked(this::ProcessClick);
world.setTranslateX(Integer.MAX_VALUE / -5000d);
world.setTranslateY(Integer.MAX_VALUE / -5000d);
loadAllOnScreen();
}
private void ProcessClick(MouseEvent mouse) {
Coordinate mouseCoordinate = new Coordinate((int) mouse.getX(), (int) mouse.getY());
Coordinate chunkCoordinate = Coordinate.WorldToChunkCoordinate(mouseCoordinate);
Coordinate blockCoordinate = Coordinate.WorldToChunkBlock(mouseCoordinate);
try {
Class<?>[] types = new Class<?>[1];
types[0] = BlockContainer.class;
BlockContainer blockContainer = new BlockContainer(world, blockCoordinate, chunkCoordinate);
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) {
ex.printStackTrace();
}
}
private void ProcessKeyPress(KeyEvent key) {
if (LEFT_KEY.match(key)) {
world.setTranslateX(world.getTranslateX() + 45);
@@ -113,8 +109,7 @@ public class PlayerManager {
chunkPos = new Coordinate(chunkPosition.x, chunkPosition.y);
}
private void loadAllOnScreen()
{
private void loadAllOnScreen() {
Size loadedChunks = getAmountOfLoadedChunks();
Coordinate chunkPosition = getCurrentChunkPosition();
@@ -125,18 +120,16 @@ public class PlayerManager {
}
}
private Coordinate getCurrentChunkPosition()
{
private Coordinate getCurrentChunkPosition() {
double offset = Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE;
int chunkX = -(int) Math.floor((world.getTranslateX() + offset) / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE);
int chunkY = -(int) Math.floor((world.getTranslateY() + offset) / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE);
return new Coordinate(chunkX,chunkY);
return new Coordinate(chunkX, chunkY);
}
private Size getAmountOfLoadedChunks()
{
private Size getAmountOfLoadedChunks() {
int loadedChunksX = (int) Math.ceil(world.getScene().getWidth() / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE) + 1;
int loadedChunksY = (int) Math.ceil(world.getScene().getHeight() / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE) + 1;
@@ -6,11 +6,11 @@ import java.util.ArrayList;
import java.util.HashMap;
public class EntityRegistry {
private static final HashMap<String, RegistryEntry> entities = new HashMap<>();
private EntityRegistry() {
}
private static final HashMap<String, RegistryEntry> entities = new HashMap<>();
public static void register(RegistryEntry entry) throws AlreadyExistsException {
if (entities.containsKey(entry.getName())) {
throw new AlreadyExistsException("Name already registered");
@@ -4,11 +4,11 @@ import java.util.function.Consumer;
import java.util.function.Function;
public class GlobalEventHandler {
private static final EventHandler handler = new EventHandler();
private GlobalEventHandler() {
}
private static final EventHandler handler = new EventHandler();
public static <T> void addListener(String eventName, Consumer<T> function) {
handler.addListener(eventName, function);
}
+1 -2
View File
@@ -8,12 +8,11 @@ public class Item extends javafx.scene.Group {
throw new IllegalArgumentException("Argument out of range");
}
if(parts[layer][section] != null) {
if (parts[layer][section] != null) {
this.getChildren().remove(parts[layer][section]);
}
parts[layer][section] = part;
this.getChildren().add(part);
}