diff --git a/src/main/java/com/yes/yes/behaviours/PlaceBehaviour.java b/src/main/java/com/yes/yes/behaviours/PlaceBehaviour.java index 345d43c..c645b4b 100644 --- a/src/main/java/com/yes/yes/behaviours/PlaceBehaviour.java +++ b/src/main/java/com/yes/yes/behaviours/PlaceBehaviour.java @@ -30,7 +30,7 @@ public class PlaceBehaviour extends Component { item.setPart(i, i1, new Square()); } } - // parent.getChildren().add(item); + // parent.getChildren().add(item); } @Override diff --git a/src/main/java/com/yes/yes/entities/parts/Square.java b/src/main/java/com/yes/yes/entities/parts/Square.java index 8358f8a..7ebb2ad 100644 --- a/src/main/java/com/yes/yes/entities/parts/Square.java +++ b/src/main/java/com/yes/yes/entities/parts/Square.java @@ -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); diff --git a/src/main/java/com/yes/yes/managers/PlayerManager.java b/src/main/java/com/yes/yes/managers/PlayerManager.java index a9e9fe8..7760b7a 100644 --- a/src/main/java/com/yes/yes/managers/PlayerManager.java +++ b/src/main/java/com/yes/yes/managers/PlayerManager.java @@ -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; diff --git a/src/main/java/com/yes/yes/utils/EntityRegistry.java b/src/main/java/com/yes/yes/utils/EntityRegistry.java index ca7baee..21a41dd 100644 --- a/src/main/java/com/yes/yes/utils/EntityRegistry.java +++ b/src/main/java/com/yes/yes/utils/EntityRegistry.java @@ -6,11 +6,11 @@ import java.util.ArrayList; import java.util.HashMap; public class EntityRegistry { + private static final HashMap entities = new HashMap<>(); + private EntityRegistry() { } - private static final HashMap entities = new HashMap<>(); - public static void register(RegistryEntry entry) throws AlreadyExistsException { if (entities.containsKey(entry.getName())) { throw new AlreadyExistsException("Name already registered"); diff --git a/src/main/java/com/yes/yes/utils/GlobalEventHandler.java b/src/main/java/com/yes/yes/utils/GlobalEventHandler.java index acfc55e..0f5e558 100644 --- a/src/main/java/com/yes/yes/utils/GlobalEventHandler.java +++ b/src/main/java/com/yes/yes/utils/GlobalEventHandler.java @@ -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 void addListener(String eventName, Consumer function) { handler.addListener(eventName, function); } diff --git a/src/main/java/com/yes/yes/utils/Item.java b/src/main/java/com/yes/yes/utils/Item.java index 8223c7e..7873875 100644 --- a/src/main/java/com/yes/yes/utils/Item.java +++ b/src/main/java/com/yes/yes/utils/Item.java @@ -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); } diff --git a/src/main/resources/com/yes/yes/main-view.fxml b/src/main/resources/com/yes/yes/main-view.fxml index 7624817..1fc6386 100644 --- a/src/main/resources/com/yes/yes/main-view.fxml +++ b/src/main/resources/com/yes/yes/main-view.fxml @@ -1,6 +1,6 @@ -