diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/com/yes/yes/MainController.java b/src/main/java/com/yes/yes/MainController.java index 0ba49db..7d644df 100644 --- a/src/main/java/com/yes/yes/MainController.java +++ b/src/main/java/com/yes/yes/MainController.java @@ -1,6 +1,7 @@ package com.yes.yes; import com.yes.yes.managers.GameManager; +import com.yes.yes.managers.HubManager; import com.yes.yes.managers.PlayerManager; import com.yes.yes.managers.UiManager; import javafx.application.Platform; @@ -13,15 +14,18 @@ public class MainController { private VBox root; public void initialize() { - GameManager gameManager = new GameManager(root); - gameManager.initialize(); - Platform.runLater(() -> { + GameManager gameManager = new GameManager(root); + gameManager.initialize(); + UiManager uiManager = new UiManager(root); uiManager.initialize(); PlayerManager playerManager = new PlayerManager(gameManager.getWorld(), uiManager.getBuildBox()); playerManager.initialize(); + + HubManager hubManager = new HubManager(); + hubManager.initialize(); }); } diff --git a/src/main/java/com/yes/yes/behaviours/ConveyorBehaviour.java b/src/main/java/com/yes/yes/behaviours/ConveyorBehaviour.java index 50c49b2..eac4933 100644 --- a/src/main/java/com/yes/yes/behaviours/ConveyorBehaviour.java +++ b/src/main/java/com/yes/yes/behaviours/ConveyorBehaviour.java @@ -38,7 +38,6 @@ public class ConveyorBehaviour extends Component { if (isConveyed && parent.getData(offerDataKey) == null) { parent.setData(receiveDataKey, null); - parent.trigger(receiveDataKey + "Changed", null); isConveyed = false; } } diff --git a/src/main/java/com/yes/yes/behaviours/DataStoreEvent.java b/src/main/java/com/yes/yes/behaviours/DataStoreEvent.java new file mode 100644 index 0000000..a2a6e34 --- /dev/null +++ b/src/main/java/com/yes/yes/behaviours/DataStoreEvent.java @@ -0,0 +1,45 @@ +package com.yes.yes.behaviours; + +import com.yes.yes.utils.*; + +public class DataStoreEvent extends Component { + + String dataKey; + String eventName; + boolean global; + + + public DataStoreEvent(Entity entity, BlockContainer blockContainer, String dataKey, String eventName, boolean global) { + super(entity, blockContainer); + this.dataKey = dataKey; + this.eventName = eventName; + this.global = global; + } + + private void triggerEvent(Item item) { + if (global) { + GlobalEventHandler.trigger(eventName, item); + } else { + this.parent.trigger(eventName, item); + } + } + + @Override + public void initialize() { + this.parent.addListener(dataKey + "Changed", this, this::triggerEvent); + } + + @Override + public void update() { + + } + + @Override + public void destroy() { + if (global) { + GlobalEventHandler.removeListener(eventName, this); + } else { + this.parent.removeListener(dataKey + "Changed", this); + } + } +} diff --git a/src/main/java/com/yes/yes/behaviours/DestroyBehaviour.java b/src/main/java/com/yes/yes/behaviours/DestroyBehaviour.java index 6a105b2..bbc46a8 100644 --- a/src/main/java/com/yes/yes/behaviours/DestroyBehaviour.java +++ b/src/main/java/com/yes/yes/behaviours/DestroyBehaviour.java @@ -19,8 +19,9 @@ public class DestroyBehaviour extends Component { @Override public void update() { - this.parent.setData(dataKey, null); - this.parent.trigger(dataKey + "Changed", null); + if(this.parent.getData(dataKey) !=null) { + this.parent.setData(dataKey, null); + } } @Override diff --git a/src/main/java/com/yes/yes/behaviours/EventDataStore.java b/src/main/java/com/yes/yes/behaviours/EventDataStore.java new file mode 100644 index 0000000..398b8a7 --- /dev/null +++ b/src/main/java/com/yes/yes/behaviours/EventDataStore.java @@ -0,0 +1,44 @@ +package com.yes.yes.behaviours; + +import com.yes.yes.utils.*; + +public class EventDataStore extends Component { + + String eventName; + String dataKey; + boolean global; + + public EventDataStore(Entity entity, BlockContainer blockContainer, String eventName, String dataKey, boolean global) { + super(entity, blockContainer); + this.eventName = eventName; + this.dataKey = dataKey; + this.global = global; + } + + private void setData(Item item) { + this.parent.setData(dataKey, item); + } + + @Override + public void initialize() { + if (global) { + GlobalEventHandler.addListener(this.eventName, this, this::setData); + } else { + this.parent.addListener(this.eventName, this, this::setData); + } + } + + @Override + public void update() { + + } + + @Override + public void destroy() { + if (global) { + GlobalEventHandler.removeListener(this.eventName, this); + } else { + this.parent.removeListener(this.eventName, this); + } + } +} diff --git a/src/main/java/com/yes/yes/behaviours/HubDisplayBehaviour.java b/src/main/java/com/yes/yes/behaviours/HubDisplayBehaviour.java new file mode 100644 index 0000000..9cd684a --- /dev/null +++ b/src/main/java/com/yes/yes/behaviours/HubDisplayBehaviour.java @@ -0,0 +1,39 @@ +package com.yes.yes.behaviours; + +import com.yes.yes.utils.BlockContainer; +import com.yes.yes.utils.Component; +import com.yes.yes.utils.Entity; +import com.yes.yes.utils.GlobalEventHandler; +import javafx.scene.text.Text; + +public class HubDisplayBehaviour extends Component { + + String eventName; + Text text; + + public HubDisplayBehaviour(Entity entity, BlockContainer blockContainer, String eventName, Text text) { + super(entity, blockContainer); + this.eventName = eventName; + this.text = text; + } + + private void updateText(String content) + { + this.text.setText(content); + } + + @Override + public void initialize() { + GlobalEventHandler.addListener(eventName, this, this::updateText); + } + + @Override + public void update() { + + } + + @Override + public void destroy() { + GlobalEventHandler.removeListener(eventName, this); + } +} diff --git a/src/main/java/com/yes/yes/behaviours/ItemBehaviour.java b/src/main/java/com/yes/yes/behaviours/ItemBehaviour.java index 653746f..2ab704e 100644 --- a/src/main/java/com/yes/yes/behaviours/ItemBehaviour.java +++ b/src/main/java/com/yes/yes/behaviours/ItemBehaviour.java @@ -1,12 +1,10 @@ package com.yes.yes.behaviours; -import com.yes.yes.utils.BlockContainer; -import com.yes.yes.utils.Component; -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.application.Platform; import javafx.geometry.Pos; +import javafx.scene.Node; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; @@ -24,15 +22,21 @@ public class ItemBehaviour extends Component { } private void itemChanged(Item item) { + //GlobalExecQueue.schedule(() -> { Platform.runLater(() -> { - this.itemGroup.getChildren().clear(); - if (item != null) - this.itemGroup.getChildren().add(item); - else { - Rectangle alignmentRectangle = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE); - alignmentRectangle.setFill(Color.rgb(0,0,0,0)); - this.itemGroup.getChildren().add(alignmentRectangle); + try { + + this.parent.getChildren().clear(); + + if (item != null) + this.itemGroup.getChildren().add(item); + else { + Rectangle alignmentRectangle = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE); + alignmentRectangle.setFill(Color.rgb(0, 0, 0, 0)); + this.itemGroup.getChildren().add(alignmentRectangle); + } } + catch (IndexOutOfBoundsException ex){System.out.print(ex.getMessage() + " | " + this.parent.getClass().getSimpleName()); } }); } diff --git a/src/main/java/com/yes/yes/behaviours/ReceiveBehaviour.java b/src/main/java/com/yes/yes/behaviours/ReceiveBehaviour.java index 4d7f275..48fa024 100644 --- a/src/main/java/com/yes/yes/behaviours/ReceiveBehaviour.java +++ b/src/main/java/com/yes/yes/behaviours/ReceiveBehaviour.java @@ -48,7 +48,6 @@ public class ReceiveBehaviour extends Component { if (this.parent.getData(dataKey) != null) return; this.parent.setData(dataKey, pair.getValue()); - this.parent.trigger(dataKey + "Changed", pair.getValue()); this.offerer.trigger("itemAccepted", this.parent); } diff --git a/src/main/java/com/yes/yes/behaviours/RotateBehaviour.java b/src/main/java/com/yes/yes/behaviours/RotateBehaviour.java new file mode 100644 index 0000000..4fb0dc1 --- /dev/null +++ b/src/main/java/com/yes/yes/behaviours/RotateBehaviour.java @@ -0,0 +1,4 @@ +package com.yes.yes.behaviours; + +public class RotateBehaviour { +} diff --git a/src/main/java/com/yes/yes/behaviours/StackBehaviour.java b/src/main/java/com/yes/yes/behaviours/StackBehaviour.java new file mode 100644 index 0000000..f2b98da --- /dev/null +++ b/src/main/java/com/yes/yes/behaviours/StackBehaviour.java @@ -0,0 +1,62 @@ +package com.yes.yes.behaviours; + +import com.yes.yes.utils.*; +import javafx.application.Platform; + +public class StackBehaviour extends Component { + private final int processingDuration; + private String receiveDataKey1; + private String receiveDataKey2; + private String offerDataKey; + private int progress = 0; + + + public StackBehaviour(Entity entity, BlockContainer blockContainer, String receiveDataKey1, String receiveDataKey2, String offerDataKey, int processingDuration) { + super(entity, blockContainer); + this.receiveDataKey1 = receiveDataKey1; + this.receiveDataKey2 = receiveDataKey2; + this.offerDataKey = offerDataKey; + this.processingDuration = processingDuration; + } + + @Override + public void initialize() { + + } + + @Override + public void update() { + //GlobalExecQueue.schedule( () -> { + Platform.runLater(() -> { + if (progress > processingDuration) { + if (parent.getData(offerDataKey) != null) return; + + Item item1 = parent.getData(receiveDataKey1); + Item item2 = parent.getData(receiveDataKey2); + + for (int quarter = 0; quarter < 4; quarter++) { + int offset = 0; + for (int layer = 0; layer < 4; layer++) { + if (item1.getPart(layer, quarter) != null) { + offset++; + continue; + } + item1.setPart(layer, quarter, item2.getPart(layer - offset, quarter)); + } + } + + parent.setData(offerDataKey, item1); + parent.setData(receiveDataKey1, null); + parent.setData(receiveDataKey2, null); + + } else if (parent.getData(receiveDataKey1) != null && parent.getData(receiveDataKey2) != null) { + progress++; + } + }); + } + + @Override + public void destroy() { + + } +} diff --git a/src/main/java/com/yes/yes/entities/machines/Cutter.java b/src/main/java/com/yes/yes/entities/machines/Cutter.java index 7243bf4..b2617eb 100644 --- a/src/main/java/com/yes/yes/entities/machines/Cutter.java +++ b/src/main/java/com/yes/yes/entities/machines/Cutter.java @@ -37,7 +37,6 @@ public class Cutter extends Entity { public Cutter(BlockContainer blockContainer) { this(); this.addBehaviour(new PlaceBehaviour(this, blockContainer)); - this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.DOWN, "Item")); this.addBehaviour(new CutItemBehaviour(this, blockContainer, Orientation.Horizontal, "ReceivedItem", "OfferItemLeft", "OfferItemRight", 5)); this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.DOWN, "ReceivedItem")); this.addBehaviour(new OfferBehaviour(this, blockContainer, Direction.LEFT, "OfferItemLeft")); 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 0aa5c71..7d555c9 100644 --- a/src/main/java/com/yes/yes/entities/machines/GeneratorMachine.java +++ b/src/main/java/com/yes/yes/entities/machines/GeneratorMachine.java @@ -5,7 +5,10 @@ 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.*; +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.world.Chunk; import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; @@ -36,7 +39,7 @@ public class GeneratorMachine extends Entity { rectangle.setFill(blockContainer.chunkColor()); 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(blockContainer.chunkColor()), "Item")); } @@ -44,10 +47,8 @@ public class GeneratorMachine extends Entity { private Item generateItem(Color color) { Item item = new Item(); for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - Square square = new Square(color); - item.setPart(i, j, square); - } + Square square = new Square(color); + item.setPart(0, i, square); } return item; } diff --git a/src/main/java/com/yes/yes/entities/machines/HubAcceptor.java b/src/main/java/com/yes/yes/entities/machines/HubAcceptor.java index 7025c5d..7f8c94e 100644 --- a/src/main/java/com/yes/yes/entities/machines/HubAcceptor.java +++ b/src/main/java/com/yes/yes/entities/machines/HubAcceptor.java @@ -1,15 +1,15 @@ package com.yes.yes.entities.machines; -import com.yes.yes.behaviours.PlaceBehaviour; -import com.yes.yes.behaviours.ReceiveBehaviour; +import com.yes.yes.behaviours.*; import com.yes.yes.utils.BlockContainer; import com.yes.yes.utils.Direction; import com.yes.yes.utils.Entity; +import com.yes.yes.utils.NotOverridable; import com.yes.yes.world.Chunk; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; -public class HubAcceptor extends Entity { +public class HubAcceptor extends Entity implements NotOverridable { public HubAcceptor() { Rectangle r = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE); r.setFill(Color.PINK); @@ -22,5 +22,12 @@ public class HubAcceptor extends Entity { this(); this.addBehaviour(new PlaceBehaviour(this, blockContainer)); this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.LEFT, "Input")); + this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.RIGHT, "Input")); + this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.UP, "Input")); + this.addBehaviour(new ReceiveBehaviour(this, blockContainer, Direction.DOWN, "Input")); + this.addBehaviour(new DataStoreEvent(this, blockContainer, "Input", "hub:objectiveCompletion", true)); + this.addBehaviour(new DestroyBehaviour(this, blockContainer, "Input")); + this.addBehaviour(new ItemBehaviour(this, blockContainer, "Objective")); + this.addBehaviour(new EventDataStore(this, blockContainer, "hub:newObjective", "Objective", true)); } } diff --git a/src/main/java/com/yes/yes/entities/machines/HubDisplay.java b/src/main/java/com/yes/yes/entities/machines/HubDisplay.java index ab39bf9..8ad850b 100644 --- a/src/main/java/com/yes/yes/entities/machines/HubDisplay.java +++ b/src/main/java/com/yes/yes/entities/machines/HubDisplay.java @@ -1,9 +1,12 @@ package com.yes.yes.entities.machines; +import com.yes.yes.behaviours.EventDataStore; +import com.yes.yes.behaviours.HubDisplayBehaviour; import com.yes.yes.behaviours.ItemBehaviour; import com.yes.yes.behaviours.PlaceBehaviour; import com.yes.yes.utils.BlockContainer; import com.yes.yes.utils.Entity; +import com.yes.yes.utils.NotOverridable; import com.yes.yes.world.Chunk; import javafx.geometry.Pos; import javafx.scene.layout.StackPane; @@ -11,13 +14,15 @@ import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.text.Text; -public class HubDisplay extends Entity { +public class HubDisplay extends Entity implements NotOverridable { + + Text text; public HubDisplay() { Rectangle r = new Rectangle(Chunk.ENTITY_SIZE, Chunk.ENTITY_SIZE); r.setFill(Color.PINK); - Text text = new Text("yes\nthis is a test"); + text = new Text("yes\nthis is a test"); text.setWrappingWidth(Chunk.ENTITY_SIZE); StackPane stackPane = new StackPane(); @@ -31,6 +36,6 @@ public class HubDisplay extends Entity { public HubDisplay(BlockContainer blockContainer) { this(); this.addBehaviour(new PlaceBehaviour(this, blockContainer)); - this.addBehaviour(new ItemBehaviour(this, blockContainer, "Objective")); + this.addBehaviour(new HubDisplayBehaviour(this, blockContainer, "hub:statusText",text)); } } diff --git a/src/main/java/com/yes/yes/entities/machines/Stacker.java b/src/main/java/com/yes/yes/entities/machines/Stacker.java new file mode 100644 index 0000000..7545d3d --- /dev/null +++ b/src/main/java/com/yes/yes/entities/machines/Stacker.java @@ -0,0 +1,36 @@ +package com.yes.yes.entities.machines; + +import com.yes.yes.behaviours.OfferBehaviour; +import com.yes.yes.behaviours.PlaceBehaviour; +import com.yes.yes.behaviours.ReceiveBehaviour; +import com.yes.yes.behaviours.StackBehaviour; +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.Polygon; + +public class Stacker extends Entity { + + public Stacker() { + super(); + 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); + p.setFill(new Color(1, 0, 0, 1)); + + this.getChildren().addAll(p); + } + + public Stacker(BlockContainer blockContainer) { + this(); + this.addBehaviour(new PlaceBehaviour(this, blockContainer)); + this.addBehaviour(new StackBehaviour(this,blockContainer,"Input1","Input2","Output",2)); + 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.RIGHT,"Input2")); + } +} 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 663b1c8..ace9fa5 100644 --- a/src/main/java/com/yes/yes/entities/parts/Square.java +++ b/src/main/java/com/yes/yes/entities/parts/Square.java @@ -22,7 +22,7 @@ public class Square extends com.yes.yes.utils.Part { } @Override - public void onColorChanged(Color color) + protected 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 68f1dd1..b9549ab 100644 --- a/src/main/java/com/yes/yes/managers/GameManager.java +++ b/src/main/java/com/yes/yes/managers/GameManager.java @@ -1,10 +1,9 @@ package com.yes.yes.managers; import com.yes.yes.entities.machines.*; -import com.yes.yes.utils.EntityRegistry; -import com.yes.yes.utils.GlobalEventHandler; -import com.yes.yes.utils.RegistryEntry; +import com.yes.yes.utils.*; import com.yes.yes.utils.exceptions.AlreadyExistsException; +import com.yes.yes.world.Chunk; import com.yes.yes.world.World; import javafx.scene.layout.VBox; @@ -13,6 +12,8 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class GameManager { + public static final Coordinate START_POSITION = new Coordinate(Integer.MAX_VALUE / -5000, Integer.MAX_VALUE / -5000); + private final VBox root; private World world; @@ -34,28 +35,50 @@ public class GameManager { EntityRegistry.register(new RegistryEntry("destroyer", "Incinerator", DestroyMachine.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("splitter", "Splitter", Splitter.class)); - EntityRegistry.register(new RegistryEntry("cutter","Cutter", Cutter.class)); + EntityRegistry.register(new RegistryEntry("cutter", "Cutter", Cutter.class)); + EntityRegistry.register(new RegistryEntry("stacker", "Stacker", Stacker.class)); } catch (AlreadyExistsException e) { e.printStackTrace(); } - ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); executor.scheduleAtFixedRate(() -> { try { GlobalEventHandler.trigger("global:timerTick", null); + //GlobalExecQueue.run(); } catch (Exception ex) { - ex.printStackTrace(); + //ex.printStackTrace(); } //System.out.println("Finished tick!"); - }, 0, 100, TimeUnit.MILLISECONDS); + }, 0, 10, TimeUnit.MILLISECONDS); - GlobalEventHandler.addListener("global:timerTick", this, (__) -> { - //System.out.println("Tick!"); - }); GlobalEventHandler.addListener("app:closing", this, (__) -> executor.shutdown()); + + generateHub(); + } + + public void generateHub() { + Coordinate startChunk = Coordinate.sceneToChunkCoordinate(START_POSITION); + Coordinate centerPosition = new Coordinate((int) Math.ceil(Chunk.CHUNK_SIZE / 2f), (int) Math.ceil(Chunk.CHUNK_SIZE / 2f)); + + Chunk chunk = world.getChunk(startChunk); + + for (int x = centerPosition.x - 2; x <= centerPosition.x; x++) { + for (int y = centerPosition.y - 2; y <= centerPosition.y; y++) { + if (x == centerPosition.x - 1 && y == centerPosition.y - 1) { + BlockContainer blockContainer = new BlockContainer(world, chunk.getChunkColor(), new Coordinate(x, y), startChunk); + HubDisplay center = new HubDisplay(blockContainer); + chunk.setEntity(center, new Coordinate(x, y)); + center.initialize(); + continue; + } + BlockContainer blockContainer = new BlockContainer(world, chunk.getChunkColor(), new Coordinate(x, y), startChunk); + HubAcceptor hubAcceptor = new HubAcceptor(blockContainer); + chunk.setEntity(hubAcceptor, new Coordinate(x, y)); + hubAcceptor.initialize(); + } + } + } } diff --git a/src/main/java/com/yes/yes/managers/HubManager.java b/src/main/java/com/yes/yes/managers/HubManager.java new file mode 100644 index 0000000..73a8b62 --- /dev/null +++ b/src/main/java/com/yes/yes/managers/HubManager.java @@ -0,0 +1,66 @@ +package com.yes.yes.managers; + +import com.yes.yes.entities.parts.Square; +import com.yes.yes.utils.GlobalEventHandler; +import com.yes.yes.utils.Item; +import javafx.scene.paint.Color; + +public class HubManager { + + private int level = 0; + private int objectiveCompletion = 0; + private int objectiveTotal = 0; + private Item objective = null; + + public void initialize() { + GlobalEventHandler.addListener("hub:objectiveCompletion", this, this::objectiveCompletion); + newLevel(); + } + + private void newLevel() { + level++; + + objective = generateObjective(); + GlobalEventHandler.trigger("hub:newObjective", objective); + + objectiveCompletion = 0; + objectiveTotal = level * 10; + + System.out.println("New level: " + level); + } + + + + private Item generateObjective() { + Item item = new Item(); + + int layerCount = (level / 10) + 1; + int complexity = level % 10; //TODO: Make complexity make stuff more complex + + //for (int l = 0; l < layerCount && l < 4; l++) { + for (int i = 0; i < 4; i++) { + //item.setPart(l, i, new Square(Color.RED)); + item.setPart(0, i, new Square(Color.GREEN)); + } + + //} + return item; + } + + private String getStatusText() { + return String.format("%s\n%s/%s",level,objectiveCompletion,objectiveTotal); + } + + private void objectiveCompletion(Item item) { + if (item == null) return; + if (item.equals(objective)) { + objectiveCompletion++; + } + if (objectiveCompletion >= objectiveTotal) { + newLevel(); + } + + GlobalEventHandler.trigger("hub:statusText",getStatusText()); + + } +} diff --git a/src/main/java/com/yes/yes/managers/PlayerManager.java b/src/main/java/com/yes/yes/managers/PlayerManager.java index 8bf6203..1db96ba 100644 --- a/src/main/java/com/yes/yes/managers/PlayerManager.java +++ b/src/main/java/com/yes/yes/managers/PlayerManager.java @@ -15,7 +15,6 @@ public class PlayerManager { 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.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; @@ -37,8 +36,8 @@ public class PlayerManager { scene.heightProperty().addListener((e) -> redrawChunks()); world.setOnMouseClicked(this::processClick); - world.setTranslateX(START_POSITION.x); - world.setTranslateY(START_POSITION.y); + world.setTranslateX(GameManager.START_POSITION.x); + world.setTranslateY(GameManager.START_POSITION.y); world.setOnMouseMoved((mouse) -> currentMousePosition = new Coordinate((int) mouse.getX(), (int) mouse.getY())); redrawChunks(); @@ -46,8 +45,8 @@ public class PlayerManager { 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); + Coordinate chunkCoordinate = Coordinate.worldToChunkCoordinate(mouseCoordinate); + Coordinate blockCoordinate = Coordinate.worldToChunkBlock(mouseCoordinate); try { @@ -84,8 +83,8 @@ public class PlayerManager { private void processKeyRelease(KeyEvent key) { if (HOME_KEY.match(key)) { - world.setTranslateX(START_POSITION.x); - world.setTranslateY(START_POSITION.y); + world.setTranslateX(GameManager.START_POSITION.x); + world.setTranslateY(GameManager.START_POSITION.y); redrawChunks(); } } @@ -104,15 +103,15 @@ public class PlayerManager { world.setTranslateY(world.getTranslateY() - 45); } if (DELETE_KEY.match(key)) { - Coordinate chunkCoordinate = Coordinate.WorldToChunkCoordinate(currentMousePosition); - Coordinate blockCoordinate = Coordinate.WorldToChunkBlock(currentMousePosition); + Coordinate chunkCoordinate = Coordinate.worldToChunkCoordinate(currentMousePosition); + Coordinate blockCoordinate = Coordinate.worldToChunkBlock(currentMousePosition); Chunk chunk = world.getChunk(chunkCoordinate); chunk.removeEntity(blockCoordinate); } Size preferredAmountOfChunks = getPreferredAmountOfChunks(); - Coordinate chunkPosition = getCurrentChunkPosition(); + Coordinate chunkPosition = Coordinate.sceneToChunkCoordinate(new Coordinate(((int) world.getTranslateX()), ((int) world.getTranslateY()))); if (chunkPosition.x < chunkPos.x) { for (int i = 0; i < preferredAmountOfChunks.y; i++) { @@ -147,7 +146,7 @@ public class PlayerManager { private void redrawChunks() { Size preferredAmountOfChunks = getPreferredAmountOfChunks(); - Coordinate chunkPosition = getCurrentChunkPosition(); + Coordinate chunkPosition = Coordinate.sceneToChunkCoordinate(new Coordinate(((int) world.getTranslateX()), ((int) world.getTranslateY()))); world.unloadAllChunks(); @@ -158,15 +157,6 @@ public class PlayerManager { } } - 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); - } - private Size getPreferredAmountOfChunks() { 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/Coordinate.java b/src/main/java/com/yes/yes/utils/Coordinate.java index aaa75d3..64236d1 100644 --- a/src/main/java/com/yes/yes/utils/Coordinate.java +++ b/src/main/java/com/yes/yes/utils/Coordinate.java @@ -13,13 +13,13 @@ public class Coordinate { this.y = y; } - public static Coordinate WorldToChunkCoordinate(Coordinate worldPos) { + public static Coordinate worldToChunkCoordinate(Coordinate worldPos) { int chunkSize = Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE; return new Coordinate(worldPos.x / chunkSize, worldPos.y / chunkSize); } - public static Coordinate WorldToChunkBlock(Coordinate worldPos) { + public static Coordinate worldToChunkBlock(Coordinate worldPos) { int chunkSize = Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE; worldPos.x = worldPos.x % chunkSize; worldPos.y = worldPos.y % chunkSize; @@ -27,6 +27,15 @@ public class Coordinate { return new Coordinate(worldPos.x / Chunk.ENTITY_SIZE, worldPos.y / Chunk.ENTITY_SIZE); } + public static Coordinate sceneToChunkCoordinate(Coordinate coordinate) { + double offset = Chunk.CHUNK_SIZE * Chunk.ENTITY_SIZE; + + int chunkX = -(int) Math.floor((coordinate.x + offset) / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE); + int chunkY = -(int) Math.floor((coordinate.y + offset) / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE); + + return new Coordinate(chunkX, chunkY); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/com/yes/yes/utils/Entity.java b/src/main/java/com/yes/yes/utils/Entity.java index 74f587e..a45fce1 100644 --- a/src/main/java/com/yes/yes/utils/Entity.java +++ b/src/main/java/com/yes/yes/utils/Entity.java @@ -49,6 +49,7 @@ public abstract class Entity extends javafx.scene.Group { public final void setData(String key, Object value) { data.put(key, value); + this.trigger(key+"Changed",value); } public final T getData(String key) { diff --git a/src/main/java/com/yes/yes/utils/GlobalExecQueue.java b/src/main/java/com/yes/yes/utils/GlobalExecQueue.java new file mode 100644 index 0000000..47693cf --- /dev/null +++ b/src/main/java/com/yes/yes/utils/GlobalExecQueue.java @@ -0,0 +1,37 @@ +package com.yes.yes.utils; + +import javafx.application.Platform; + +import java.util.ArrayList; +import java.util.concurrent.Semaphore; + +public class GlobalExecQueue { + private final static ArrayList queue = new ArrayList<>(); + + private GlobalExecQueue() { + } + + public static void schedule(Runnable runnable) { + queue.add(runnable); + } + + + + public static void run() { + //Semaphore s = new Semaphore(0); + Platform.runLater( () -> { + for (Runnable runnable : queue) { + synchronized (GlobalExecQueue.class) { + runnable.run(); + } + } + // s.release(); + }); + /*try { + //s.acquire(); + } catch (InterruptedException e) { + e.printStackTrace(); + }*/ + queue.clear(); + } +} diff --git a/src/main/java/com/yes/yes/utils/Item.java b/src/main/java/com/yes/yes/utils/Item.java index 28100dc..ef3a57b 100644 --- a/src/main/java/com/yes/yes/utils/Item.java +++ b/src/main/java/com/yes/yes/utils/Item.java @@ -2,9 +2,12 @@ package com.yes.yes.utils; import javafx.scene.transform.Scale; +import java.util.Arrays; + public class Item extends javafx.scene.Group implements Cloneable { final Part[][] parts = new Part[4][4]; + public void setPart(int layer, int section, Part part) throws IllegalArgumentException { if (layer < 0 || layer > 4 && section < 0 || section > 4) { throw new IllegalArgumentException("Argument out of range"); @@ -65,7 +68,12 @@ public class Item extends javafx.scene.Group implements Cloneable { try { Part part = this.getPart(i, j); + + if (part == null) + continue; + Part clonePart = part.getClass().getConstructor(types).newInstance(); + clonePart.setColor(part.getColor()); clone.setPart(i, j, clonePart); } catch (Exception ex) { @@ -75,4 +83,11 @@ public class Item extends javafx.scene.Group implements Cloneable { } return clone; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Item item)) return false; + return Arrays.deepEquals(parts, item.parts); + } } diff --git a/src/main/java/com/yes/yes/utils/NotOverridable.java b/src/main/java/com/yes/yes/utils/NotOverridable.java new file mode 100644 index 0000000..7a3cce5 --- /dev/null +++ b/src/main/java/com/yes/yes/utils/NotOverridable.java @@ -0,0 +1,4 @@ +package com.yes.yes.utils; + +public interface NotOverridable { +} diff --git a/src/main/java/com/yes/yes/utils/Part.java b/src/main/java/com/yes/yes/utils/Part.java index 773872a..fbd6ca8 100644 --- a/src/main/java/com/yes/yes/utils/Part.java +++ b/src/main/java/com/yes/yes/utils/Part.java @@ -2,8 +2,10 @@ package com.yes.yes.utils; import javafx.scene.paint.Color; +import java.util.Objects; + public abstract class Part extends javafx.scene.Group { - Color color; + private Color color; public void rotate(int quarters) { @@ -21,4 +23,11 @@ public abstract class Part extends javafx.scene.Group { public Color getColor() { return color; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Part part)) return false; + return Objects.equals(color, part.color) && o.getClass().getSimpleName().equals(this.getClass().getSimpleName()); + } } diff --git a/src/main/java/com/yes/yes/world/Chunk.java b/src/main/java/com/yes/yes/world/Chunk.java index 18b211c..59dc9d8 100644 --- a/src/main/java/com/yes/yes/world/Chunk.java +++ b/src/main/java/com/yes/yes/world/Chunk.java @@ -3,6 +3,7 @@ package com.yes.yes.world; import com.yes.yes.utils.Coordinate; import com.yes.yes.utils.Entity; import com.yes.yes.utils.NoiseGenerator; +import com.yes.yes.utils.NotOverridable; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.geometry.VPos; @@ -28,7 +29,7 @@ public class Chunk extends GridPane { default -> throw new IllegalStateException(); }; - Color fillColor = new Color(chunkColor.getRed(),chunkColor.getGreen(), chunkColor.getBlue(), 0.1); + Color fillColor = new Color(chunkColor.getRed(), chunkColor.getGreen(), chunkColor.getBlue(), 0.1); this.setBackground(new Background(new BackgroundFill(fillColor, CornerRadii.EMPTY, Insets.EMPTY))); @@ -50,6 +51,9 @@ public class Chunk extends GridPane { } public void setEntity(Entity component, Coordinate pos) { + if (getEntity(pos) instanceof NotOverridable) + return; + removeEntity(pos); data[pos.x + pos.y * CHUNK_SIZE] = component; this.add(component, pos.x, pos.y); @@ -58,6 +62,9 @@ public class Chunk extends GridPane { } public void removeEntity(Coordinate pos) { + if (getEntity(pos) instanceof NotOverridable) + return; + Entity entity = getEntity(pos); if (entity != null) {