From fa22fa23ff56a8faf55bd05c5a9002f58bee7dbe Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Thu, 19 May 2022 20:20:54 +0200 Subject: [PATCH] - Add cutter machine base - Add color system - Improve some variable names - --- src/main/java/com/yes/yes/YesApplication.java | 5 +-- .../com/yes/yes/behaviours/ItemBehaviour.java | 2 - .../yes/entities/machines/ConveyorBelt.java | 8 ++-- .../com/yes/yes/entities/machines/Cutter.java | 43 +++++++++++++++++++ .../yes/entities/machines/DestroyMachine.java | 13 +++--- .../entities/machines/GeneratorMachine.java | 29 +++++++------ .../yes/yes/entities/machines/Splitter.java | 8 ++-- .../com/yes/yes/entities/parts/Square.java | 23 ++++++---- .../com/yes/yes/managers/GameManager.java | 1 + .../com/yes/yes/managers/PlayerManager.java | 8 ++-- .../com/yes/yes/utils/BlockContainer.java | 3 +- src/main/java/com/yes/yes/utils/Entity.java | 2 - src/main/java/com/yes/yes/utils/Item.java | 5 ++- .../com/yes/yes/utils/NoiseGenerator.java | 10 +++++ src/main/java/com/yes/yes/utils/Part.java | 20 +++++---- src/main/java/com/yes/yes/world/Chunk.java | 20 ++++++++- src/main/java/com/yes/yes/world/World.java | 4 +- .../main/resources/com/yes/yes/main-view.fxml | 6 --- 18 files changed, 144 insertions(+), 66 deletions(-) create mode 100644 src/main/java/com/yes/yes/entities/machines/Cutter.java create mode 100644 src/main/java/com/yes/yes/utils/NoiseGenerator.java delete mode 100644 target/classes/main/resources/com/yes/yes/main-view.fxml diff --git a/src/main/java/com/yes/yes/YesApplication.java b/src/main/java/com/yes/yes/YesApplication.java index 00f1731..e1d3777 100644 --- a/src/main/java/com/yes/yes/YesApplication.java +++ b/src/main/java/com/yes/yes/YesApplication.java @@ -14,15 +14,14 @@ public class YesApplication extends javafx.application.Application { @Override public void stop() { - System.out.println("Closing!"); GlobalEventHandler.trigger("app:closing", null); } @Override public void start(Stage stage) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(YesApplication.class.getResource("main-view.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 320, 240); - stage.setTitle("Hello!"); + Scene scene = new Scene(fxmlLoader.load(), 1280 , 720 ); + stage.setTitle("Yes!"); stage.setScene(scene); stage.setMaximized(true); diff --git a/src/main/java/com/yes/yes/behaviours/ItemBehaviour.java b/src/main/java/com/yes/yes/behaviours/ItemBehaviour.java index 7c2dd6c..653746f 100644 --- a/src/main/java/com/yes/yes/behaviours/ItemBehaviour.java +++ b/src/main/java/com/yes/yes/behaviours/ItemBehaviour.java @@ -24,7 +24,6 @@ public class ItemBehaviour extends Component { } private void itemChanged(Item item) { - //System.out.println("Running item update"); Platform.runLater(() -> { this.itemGroup.getChildren().clear(); if (item != null) @@ -35,7 +34,6 @@ public class ItemBehaviour extends Component { this.itemGroup.getChildren().add(alignmentRectangle); } }); - //System.out.println("Finished item update"); } @Override diff --git a/src/main/java/com/yes/yes/entities/machines/ConveyorBelt.java b/src/main/java/com/yes/yes/entities/machines/ConveyorBelt.java index 50efe55..ca63aa9 100644 --- a/src/main/java/com/yes/yes/entities/machines/ConveyorBelt.java +++ b/src/main/java/com/yes/yes/entities/machines/ConveyorBelt.java @@ -11,14 +11,14 @@ import javafx.scene.shape.Polygon; public class ConveyorBelt extends Entity { public ConveyorBelt() { super(); - Polygon t = new Polygon(); - t.getPoints().addAll( + Polygon p = new Polygon(); + p.getPoints().addAll( Chunk.ENTITY_SIZE / 2d, 0.0, 0.0, (double) Chunk.ENTITY_SIZE, (double) Chunk.ENTITY_SIZE, (double) Chunk.ENTITY_SIZE); - t.setFill(new Color(0, 0, 0, 1)); + p.setFill(new Color(0, 0, 0, 1)); - this.getChildren().addAll(t); + this.getChildren().addAll(p); } @SuppressWarnings("unused") // Called dynamically diff --git a/src/main/java/com/yes/yes/entities/machines/Cutter.java b/src/main/java/com/yes/yes/entities/machines/Cutter.java new file mode 100644 index 0000000..58f3f5f --- /dev/null +++ b/src/main/java/com/yes/yes/entities/machines/Cutter.java @@ -0,0 +1,43 @@ +package com.yes.yes.entities.machines; + +import com.yes.yes.behaviours.DestroyBehaviour; +import com.yes.yes.behaviours.PlaceBehaviour; +import com.yes.yes.behaviours.ReceiveBehaviour; +import com.yes.yes.utils.BlockContainer; +import com.yes.yes.utils.Direction; +import com.yes.yes.utils.Entity; +import com.yes.yes.world.Chunk; +import javafx.scene.paint.Color; +import javafx.scene.shape.Circle; +import javafx.scene.shape.Polygon; +import javafx.scene.shape.Rectangle; + +public class Cutter extends Entity { + public Cutter() { + super(); + + Circle c = new Circle(Chunk.ENTITY_SIZE/2); + c.setFill(Color.BLACK); + c.setCenterX(Chunk.ENTITY_SIZE / 2); + c.setCenterY(Chunk.ENTITY_SIZE / 2); + + Polygon p = new Polygon(); + p.getPoints().addAll( + Chunk.ENTITY_SIZE / 2d, 0.0, + Chunk.ENTITY_SIZE/4d, (double) Chunk.ENTITY_SIZE, + (double) Chunk.ENTITY_SIZE/4d*3, (double) Chunk.ENTITY_SIZE); + p.setFill(new Color(1, 1, 1, 0.5)); + + + + this.getChildren().addAll(c,p); + } + + @SuppressWarnings("unused") // Called dynamically + public Cutter(BlockContainer blockContainer) { + this(); + this.addBehaviour(new PlaceBehaviour(this, blockContainer)); + this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.DOWN, "Item")); + //TODO: Add split/modify behaviour + } +} diff --git a/src/main/java/com/yes/yes/entities/machines/DestroyMachine.java b/src/main/java/com/yes/yes/entities/machines/DestroyMachine.java index 450e676..4ba73c7 100644 --- a/src/main/java/com/yes/yes/entities/machines/DestroyMachine.java +++ b/src/main/java/com/yes/yes/entities/machines/DestroyMachine.java @@ -10,22 +10,21 @@ import com.yes.yes.world.Chunk; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Polygon; +import javafx.scene.shape.Rectangle; public class DestroyMachine extends Entity { public DestroyMachine() { super(); - Circle c = new Circle(Chunk.ENTITY_SIZE / 2); - c.setCenterX(Chunk.ENTITY_SIZE / 2); - c.setCenterY(Chunk.ENTITY_SIZE / 2); + Rectangle r = new Rectangle(Chunk.ENTITY_SIZE,Chunk.ENTITY_SIZE); - Polygon t = new Polygon(); - t.getPoints().addAll( + Polygon p = new Polygon(); + p.getPoints().addAll( Chunk.ENTITY_SIZE / 2d, 0.0, 0.0, (double) Chunk.ENTITY_SIZE, (double) Chunk.ENTITY_SIZE, (double) Chunk.ENTITY_SIZE); - t.setFill(new Color(1, 1, 1, 0.5)); + p.setFill(new Color(1, 1, 1, 0.5)); - this.getChildren().addAll(c, t); + this.getChildren().addAll(r, p); } @SuppressWarnings("unused") // Called dynamically diff --git a/src/main/java/com/yes/yes/entities/machines/GeneratorMachine.java b/src/main/java/com/yes/yes/entities/machines/GeneratorMachine.java index 81e8136..0aa5c71 100644 --- a/src/main/java/com/yes/yes/entities/machines/GeneratorMachine.java +++ b/src/main/java/com/yes/yes/entities/machines/GeneratorMachine.java @@ -5,47 +5,50 @@ import com.yes.yes.behaviours.ItemBehaviour; import com.yes.yes.behaviours.OfferBehaviour; import com.yes.yes.behaviours.PlaceBehaviour; import com.yes.yes.entities.parts.Square; -import com.yes.yes.utils.BlockContainer; -import com.yes.yes.utils.Direction; -import com.yes.yes.utils.Entity; -import com.yes.yes.utils.Item; +import com.yes.yes.utils.*; import com.yes.yes.world.Chunk; import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; import javafx.scene.shape.Rectangle; public class GeneratorMachine extends Entity { + Rectangle rectangle; + public GeneratorMachine() { super(); - Rectangle r = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE); - r.setFill(Color.RED); + rectangle = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE); + rectangle.setFill(Color.RED); Polygon p = new Polygon(); p.getPoints().addAll( - Chunk.ENTITY_SIZE/2d, 0.0, + Chunk.ENTITY_SIZE / 2d, 0.0, 0.0, (double) Chunk.ENTITY_SIZE, (double) Chunk.ENTITY_SIZE, (double) Chunk.ENTITY_SIZE); p.setFill(new Color(0, 0, 0, 0.5)); - this.getChildren().addAll(r, p); + this.getChildren().addAll(rectangle, p); } @SuppressWarnings("unused") // Called dynamically public GeneratorMachine(BlockContainer blockContainer) { this(); + + rectangle.setFill(blockContainer.chunkColor()); + 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 GeneratorBehaviour(this, blockContainer, generateItem(), "Item")); + this.addBehaviour(new GeneratorBehaviour(this, blockContainer, generateItem(blockContainer.chunkColor()), "Item")); } - private Item generateItem() { - Item it = new Item(); + private Item generateItem(Color color) { + Item item = new Item(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { - it.setPart(i, j, new Square()); + Square square = new Square(color); + item.setPart(i, j, square); } } - return it; + return item; } } diff --git a/src/main/java/com/yes/yes/entities/machines/Splitter.java b/src/main/java/com/yes/yes/entities/machines/Splitter.java index 17fb3fa..48bf966 100644 --- a/src/main/java/com/yes/yes/entities/machines/Splitter.java +++ b/src/main/java/com/yes/yes/entities/machines/Splitter.java @@ -14,8 +14,8 @@ import javafx.scene.shape.Polygon; public class Splitter extends Entity { public Splitter() { super(); - Polygon p1 = new Polygon(); - p1.getPoints().addAll( + Polygon p = new Polygon(); + p.getPoints().addAll( 0.0, Chunk.ENTITY_SIZE / 2d, Chunk.ENTITY_SIZE / 2d, 0.0, (double) Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE / 2d, @@ -23,9 +23,9 @@ public class Splitter extends Entity { Chunk.ENTITY_SIZE / 2d, Chunk.ENTITY_SIZE / 2d, Chunk.ENTITY_SIZE / 4d, (double) Chunk.ENTITY_SIZE ); - p1.setFill(new Color(0, 0, 0, 1)); + p.setFill(new Color(0, 0, 0, 1)); - this.getChildren().addAll(p1); + this.getChildren().addAll(p); } @SuppressWarnings("unused") // Called dynamically 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 834ecdd..663b1c8 100644 --- a/src/main/java/com/yes/yes/entities/parts/Square.java +++ b/src/main/java/com/yes/yes/entities/parts/Square.java @@ -6,17 +6,24 @@ import javafx.scene.shape.Rectangle; import java.util.Random; public class Square extends com.yes.yes.utils.Part { - final Random rand = new Random(); + Rectangle rect = new Rectangle(23, 23); + + public Square(Color color) + { + this(); + setColor(color); + } public Square() { - int r = rand.nextInt(255); - int g = rand.nextInt(255); - int b = rand.nextInt(255); - - var rect = new Rectangle(23, 23); - rect.setFill(Color.rgb(r, g, b)); - rect.setStroke(Color.BLUE); + rect = new Rectangle(23, 23); + rect.setStroke(Color.BLACK); rect.setStrokeWidth(1); this.getChildren().add(rect); } + + @Override + public void onColorChanged(Color color) + { + rect.setFill(color); + } } diff --git a/src/main/java/com/yes/yes/managers/GameManager.java b/src/main/java/com/yes/yes/managers/GameManager.java index 5ceb181..68f1dd1 100644 --- a/src/main/java/com/yes/yes/managers/GameManager.java +++ b/src/main/java/com/yes/yes/managers/GameManager.java @@ -37,6 +37,7 @@ public class GameManager { EntityRegistry.register(new RegistryEntry("hub", "Central Hub", HubDisplay.class)); EntityRegistry.register(new RegistryEntry("hubAcceptor", "Central Hub Acceptor", HubAcceptor.class)); EntityRegistry.register(new RegistryEntry("splitter", "Splitter", Splitter.class)); + EntityRegistry.register(new RegistryEntry("cutter","Cutter", Cutter.class)); } catch (AlreadyExistsException e) { e.printStackTrace(); } diff --git a/src/main/java/com/yes/yes/managers/PlayerManager.java b/src/main/java/com/yes/yes/managers/PlayerManager.java index 07c3561..8bf6203 100644 --- a/src/main/java/com/yes/yes/managers/PlayerManager.java +++ b/src/main/java/com/yes/yes/managers/PlayerManager.java @@ -54,7 +54,10 @@ public class PlayerManager { Class[] types = new Class[1]; types[0] = BlockContainer.class; - BlockContainer blockContainer = new BlockContainer(world, blockCoordinate, chunkCoordinate); + Chunk chunk = world.getChunk(chunkCoordinate); + Entity oldEntity = chunk.getEntity(blockCoordinate); + + BlockContainer blockContainer = new BlockContainer(world, chunk.getChunkColor(), blockCoordinate, chunkCoordinate); RegistryEntry registryEntry = EntityRegistry.getEntry(buildBox.getSelectedEntity()); if (registryEntry == null) @@ -62,9 +65,6 @@ public class PlayerManager { Entity entity = (Entity) registryEntry.getEntity().getConstructor(types).newInstance(blockContainer); - Chunk chunk = world.getChunk(chunkCoordinate); - Entity oldEntity = chunk.getEntity(blockCoordinate); - if (oldEntity != null && oldEntity.getClass().getSimpleName().equals(entity.getClass().getSimpleName())) { if (mouse.getButton() == MouseButton.PRIMARY) currentRotation = oldEntity.getRotation() + 1; diff --git a/src/main/java/com/yes/yes/utils/BlockContainer.java b/src/main/java/com/yes/yes/utils/BlockContainer.java index 5eb1a7e..e503e4d 100644 --- a/src/main/java/com/yes/yes/utils/BlockContainer.java +++ b/src/main/java/com/yes/yes/utils/BlockContainer.java @@ -2,8 +2,9 @@ package com.yes.yes.utils; import com.yes.yes.world.Chunk; import com.yes.yes.world.World; +import javafx.scene.paint.Color; -public record BlockContainer(World world, Coordinate blockCoordinate, Coordinate chunkCoordinate) { +public record BlockContainer(World world, Color chunkColor, Coordinate blockCoordinate, Coordinate chunkCoordinate) { final static int MAX_RADIUS = 2; public Entity getBlockAbsolute(Coordinate offset) throws IllegalArgumentException { diff --git a/src/main/java/com/yes/yes/utils/Entity.java b/src/main/java/com/yes/yes/utils/Entity.java index 1ff2f9e..74f587e 100644 --- a/src/main/java/com/yes/yes/utils/Entity.java +++ b/src/main/java/com/yes/yes/utils/Entity.java @@ -31,9 +31,7 @@ public abstract class Entity extends javafx.scene.Group { } public final void update() { - //System.out.println("Updating: " + getClass().getSimpleName()); for (Component behaviour : behaviours) { - //System.out.println("Updating " + behaviour.getClass().getSimpleName()); behaviour.update(); } } diff --git a/src/main/java/com/yes/yes/utils/Item.java b/src/main/java/com/yes/yes/utils/Item.java index 38e2645..ff93540 100644 --- a/src/main/java/com/yes/yes/utils/Item.java +++ b/src/main/java/com/yes/yes/utils/Item.java @@ -61,7 +61,10 @@ public class Item extends javafx.scene.Group implements Cloneable { Class[] types = new Class[0]; try { - clone.setPart(i, j, this.getPart(i, j).getClass().getConstructor(types).newInstance()); + Part part = this.getPart(i, j); + Part clonePart = part.getClass().getConstructor(types).newInstance(); + clonePart.setColor(part.getColor()); + clone.setPart(i, j, clonePart); } catch (Exception ex) { ex.printStackTrace(); } diff --git a/src/main/java/com/yes/yes/utils/NoiseGenerator.java b/src/main/java/com/yes/yes/utils/NoiseGenerator.java new file mode 100644 index 0000000..7925d8f --- /dev/null +++ b/src/main/java/com/yes/yes/utils/NoiseGenerator.java @@ -0,0 +1,10 @@ +package com.yes.yes.utils; + +public class NoiseGenerator { + private NoiseGenerator(){} + + 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); + } +} diff --git a/src/main/java/com/yes/yes/utils/Part.java b/src/main/java/com/yes/yes/utils/Part.java index 4fe40e1..773872a 100644 --- a/src/main/java/com/yes/yes/utils/Part.java +++ b/src/main/java/com/yes/yes/utils/Part.java @@ -1,15 +1,9 @@ package com.yes.yes.utils; -import javafx.scene.Node; +import javafx.scene.paint.Color; public abstract class Part extends javafx.scene.Group { - public void addChild(Node element) { - this.getChildren().add(element); - } - - public void removeChild(Node element) { - this.getChildren().remove(element); - } + Color color; public void rotate(int quarters) { @@ -17,4 +11,14 @@ public abstract class Part extends javafx.scene.Group { this.getTransforms().add(new javafx.scene.transform.Rotate(quarters % 4 * 90, 0, 0)); } + protected abstract void onColorChanged(Color color); + + public void setColor(Color color) { + this.color = color; + onColorChanged(color); + } + + public Color getColor() { + return color; + } } diff --git a/src/main/java/com/yes/yes/world/Chunk.java b/src/main/java/com/yes/yes/world/Chunk.java index 174b0b8..18b211c 100644 --- a/src/main/java/com/yes/yes/world/Chunk.java +++ b/src/main/java/com/yes/yes/world/Chunk.java @@ -2,7 +2,9 @@ package com.yes.yes.world; import com.yes.yes.utils.Coordinate; import com.yes.yes.utils.Entity; +import com.yes.yes.utils.NoiseGenerator; import javafx.geometry.HPos; +import javafx.geometry.Insets; import javafx.geometry.VPos; import javafx.scene.layout.*; import javafx.scene.paint.Color; @@ -12,12 +14,24 @@ public class Chunk extends GridPane { public static final int CHUNK_SIZE = 8; public static final int ENTITY_SIZE = 50; private final Entity[] data = new Entity[CHUNK_SIZE * CHUNK_SIZE]; + private final Color chunkColor; - public Chunk() { + public Chunk(Coordinate pos) { super(); this.setMaxSize(CHUNK_SIZE * ENTITY_SIZE, CHUNK_SIZE * ENTITY_SIZE); this.setMinSize(CHUNK_SIZE * ENTITY_SIZE, CHUNK_SIZE * ENTITY_SIZE); + chunkColor = switch (((int) NoiseGenerator.calculate(pos.x * pos.y)) % 3) { + case 0 -> Color.RED; + case 1 -> Color.GREEN; + case 2 -> Color.BLUE; + default -> throw new IllegalStateException(); + }; + + Color fillColor = new Color(chunkColor.getRed(),chunkColor.getGreen(), chunkColor.getBlue(), 0.1); + + this.setBackground(new Background(new BackgroundFill(fillColor, CornerRadii.EMPTY, Insets.EMPTY))); + for (int i = 0; i < CHUNK_SIZE; i++) { for (int j = 0; j < CHUNK_SIZE; j++) { this.getColumnConstraints().add(new ColumnConstraints(ENTITY_SIZE)); @@ -53,6 +67,10 @@ public class Chunk extends GridPane { data[pos.x + pos.y * CHUNK_SIZE] = null; } + public Color getChunkColor() { + return chunkColor; + } + public Entity getEntity(Coordinate pos) { return data[pos.x + pos.y * CHUNK_SIZE]; } diff --git a/src/main/java/com/yes/yes/world/World.java b/src/main/java/com/yes/yes/world/World.java index 0bbad72..a6a08d7 100644 --- a/src/main/java/com/yes/yes/world/World.java +++ b/src/main/java/com/yes/yes/world/World.java @@ -38,7 +38,7 @@ public class World extends GridPane { public void addChunk(Coordinate pos) throws AlreadyExistsException { if (chunks.get(pos) == null) { - chunks.put(pos, new Chunk()); + chunks.put(pos, new Chunk(pos)); } else { throw new AlreadyExistsException("Chunk already exists"); } @@ -47,7 +47,7 @@ public class World extends GridPane { public Chunk getChunk(Coordinate pos) { Chunk c = chunks.get(pos); if (c == null) { - c = new Chunk(); + c = new Chunk(pos); chunks.put(pos, c); } return c; diff --git a/target/classes/main/resources/com/yes/yes/main-view.fxml b/target/classes/main/resources/com/yes/yes/main-view.fxml deleted file mode 100644 index 7624817..0000000 --- a/target/classes/main/resources/com/yes/yes/main-view.fxml +++ /dev/null @@ -1,6 +0,0 @@ - - - - -