diff --git a/src/main/java/Controllers/ConnectionController.java b/src/main/java/Controllers/ConnectionController.java index f5b92c69..3e976063 100644 --- a/src/main/java/Controllers/ConnectionController.java +++ b/src/main/java/Controllers/ConnectionController.java @@ -11,22 +11,22 @@ public class ConnectionController { private final GUItoSystemInterface converter; - private final PABlockHandler handler; + private final PABlockHandler blockHandler; - public ConnectionController(GUItoSystemInterface converter, PABlockHandler handler) { + public ConnectionController(GUItoSystemInterface converter, PABlockHandler blockHandler) { this.converter = converter; - this.handler = handler; + this.blockHandler = blockHandler; } public void connectBlocks(GUIBlock withMain, GUIBlock withSub, String connectionID) { Block mainBlock = converter.getBlockFromGUIBlock(withMain); SubConnector subConnector = converter.getSubConnectorFromGUIBlockWithID(withSub, connectionID); - handler.connectToExistingBlock(mainBlock, subConnector); + blockHandler.connectToExistingBlock(mainBlock, subConnector); } public void disconnectBlock(GUIBlock withMain) { Block mainBlock = converter.getBlockFromGUIBlock(withMain); - handler.disconnectInPA(mainBlock); + blockHandler.disconnectInPA(mainBlock); } public boolean isValidConnection(GUIBlock withMain, GUIBlock withSub, String connectionID) { diff --git a/src/main/java/Controllers/GUItoSystemInterface.java b/src/main/java/Controllers/GUItoSystemInterface.java index 994c49c6..6368ae2d 100644 --- a/src/main/java/Controllers/GUItoSystemInterface.java +++ b/src/main/java/Controllers/GUItoSystemInterface.java @@ -39,7 +39,12 @@ public GUIBlock createNewGUIBlock(String id, int x, int y) { } } - public Block getBlockFromGUIBlock(GUIBlock block) { + public Block getBlockFromGUIBlock(GUIBlock block) throws IllegalArgumentException { + + if (!conversionTable.containsKey(block)) { + throw new IllegalArgumentException("The given GUI block is not present in the conversion table!"); + } + return conversionTable.get(block); } diff --git a/src/main/java/Controllers/LevelDataLoader.java b/src/main/java/Controllers/LevelDataLoader.java index 2e156ec8..8f8bce56 100644 --- a/src/main/java/Controllers/LevelDataLoader.java +++ b/src/main/java/Controllers/LevelDataLoader.java @@ -7,24 +7,51 @@ import java.awt.*; +/** + * A class used as a static level data loader. + * Is only used as a first loader to load the level data the first time. + * + * @author Alpha-team + */ public class LevelDataLoader { + /** + * Return the current robot position + * @return the current robot position + */ public Position getRobotPosition() { return new Position(GameState.getCurrentLevel().getRobot().getX(), GameState.getCurrentLevel().getRobot().getY()); } + /** + * Return the current robot direction + * @return the current robot direction + */ public Direction getRobotDirection() { return GameState.getCurrentLevel().getRobot().getDirection(); } + /** + * Return the current grid size + * @return the current grid size + */ public Position getGridSize() { return new Position(GameState.getCurrentLevel().getGrid().getWidth(), GameState.getCurrentLevel().getGrid().getHeight()); } + /** + * Return the current array of grid cells + * @return the current array of grid cells + */ public Cell[][] getGridCells() { return GameState.getCurrentLevel().getGrid().getCells(); } + /** + * Subscribe the given robot listener to the current robot observer. + * + * @param listener the given robot listener. + */ public void subscribe(RobotListener listener) { GameState.getCurrentLevel().getRobot().subscribe(listener); } diff --git a/src/main/java/Controllers/ProgramController.java b/src/main/java/Controllers/ProgramController.java index f840fea0..c7185efd 100644 --- a/src/main/java/Controllers/ProgramController.java +++ b/src/main/java/Controllers/ProgramController.java @@ -10,10 +10,14 @@ public class ProgramController { - private final PABlockHandler blockHandler = new PABlockHandler(); private final LevelLoader loader = new LevelLoader(); - private final GUItoSystemInterface converter = new GUItoSystemInterface(blockHandler); - private final ConnectionController controller = new ConnectionController(converter, blockHandler); + private final GUItoSystemInterface converter; + private final PABlockHandler blockHandler; + + public ProgramController(GUItoSystemInterface converter, PABlockHandler blockHandler) { + this.converter = converter; + this.blockHandler = blockHandler; + } public void addBlockToPA(GUIBlock block) { Block toAdd = converter.getBlockFromGUIBlock(block); @@ -29,8 +33,7 @@ public void deleteFromPA(GUIBlock block) { } public void deleteAsProgram(GUIBlock block) { - Block toDelete = converter.getBlockFromGUIBlock(block); - blockHandler.deleteProgram(toDelete); + blockHandler.deleteProgram(converter.getBlockFromGUIBlock(block)); } public boolean reachedMaxBlocks() { @@ -47,10 +50,7 @@ public GUIBlock getHightlightedBlock() { } public GUIBlock getBlock(String ID, int x, int y) { - if (!reachedMaxBlocks()) { - return converter.createNewGUIBlock(ID, x, y); - } - return null; + return converter.createNewGUIBlock(ID, x, y); } public void resetProgram() { @@ -69,8 +69,4 @@ public void subscribeListener(ProgramListener listener) { public void unsubscribeListener(ProgramListener listener) { blockHandler.getPA().unsubscribe(listener); } - - public ConnectionController getController() { - return controller; - } } diff --git a/src/main/java/GUI/BlockrCanvas.java b/src/main/java/GUI/BlockrCanvas.java index ec18d243..d4dd7229 100644 --- a/src/main/java/GUI/BlockrCanvas.java +++ b/src/main/java/GUI/BlockrCanvas.java @@ -1,5 +1,7 @@ package GUI; +import Controllers.ConnectionController; +import Controllers.GUItoSystemInterface; import Controllers.ProgramController; import GUI.Blocks.GUIBlock; import GUI.Components.GUIBlockHandler; @@ -8,6 +10,7 @@ import GUI.Panel.GameWorldPanel; import GUI.Panel.PalettePanel; import GUI.Panel.ProgramAreaPanel; +import System.Logic.ProgramArea.PABlockHandler; import java.awt.*; import java.awt.event.KeyEvent; @@ -25,7 +28,8 @@ public class BlockrCanvas extends CanvasWindow { private GUIBlockHandler blockHandler; private GUIBlock previousBlock; - private ProgramController programController = new ProgramController(); + private ProgramController programController; + private ConnectionController connectionController; /** * Initializes a CanvasWindow object. @@ -40,7 +44,9 @@ protected BlockrCanvas(String title, int width, int height, String imagePackName this.height = height; GamePanel.setImageLibrary(ImagePreLoader.createImageLibrary(imagePackName)); + setControllers(); setPanels(); + blockHandler = new GUIBlockHandler(palettePanel, programAreaPanel); } @@ -61,13 +67,13 @@ protected void paint(Graphics g) { @Override protected void handleMouseEvent(int id, int x, int y, int clickCount) { super.handleMouseEvent(id, x, y, clickCount); - blockHandler.handleMouseEvent(id, x, y, programController); + blockHandler.handleMouseEvent(id, x, y); repaint(); } private void setPanels() { palettePanel = new PalettePanel(0, 0, (int)(width * PALETTE_WIDTH_RATIO), height, programController); - programAreaPanel = new ProgramAreaPanel((int)(width * PALETTE_WIDTH_RATIO),0, (int)(width * PROGRAMAREA_WIDTH_RATIO), height, programController); + programAreaPanel = new ProgramAreaPanel((int)(width * PALETTE_WIDTH_RATIO),0, (int)(width * PROGRAMAREA_WIDTH_RATIO), height, programController, connectionController); gameWorldPanel = new GameWorldPanel((int)(width * PALETTE_WIDTH_RATIO) + (int)(width * PROGRAMAREA_WIDTH_RATIO),0, (int)(width * GAMEWORLD_WIDTH_RATIO), height, programController); } @@ -93,4 +99,11 @@ protected void handleKeyEvent(int id, int keyCode, char keyChar) { repaint(); } + + private void setControllers() { + PABlockHandler blockHandler = new PABlockHandler(); + GUItoSystemInterface converter = new GUItoSystemInterface(blockHandler); + connectionController = new ConnectionController(converter, blockHandler); + programController = new ProgramController(converter, blockHandler); + } } diff --git a/src/main/java/GUI/Blocks/GUIBlock.java b/src/main/java/GUI/Blocks/GUIBlock.java index 482cd036..0e0f9e52 100644 --- a/src/main/java/GUI/Blocks/GUIBlock.java +++ b/src/main/java/GUI/Blocks/GUIBlock.java @@ -235,25 +235,44 @@ public boolean intersectsWithConnector(GUIBlock other) { return findCollidingConnector(subConnectors, other.mainConnector) != null || findCollidingConnector(other.subConnectors, mainConnector) != null; } - // TODO doc - public void connectWithStaticBlock(GUIBlock other, ProgramController Pcontroller) { + /** + * The current dragged block or set of blocks this block is in is being connected with a given + * static block if possible, while using the given program controller and connection controller to handle + * program and connection actions. + * + * @param other The given static block + * @param programController The given program controller + * @param connectionController The given connection controller + * + * @post The height of this block and its connected set of blocks is changed accordingly. + * @post The position of this block set is changed accordingly to the type of completed connection. + * + * @effect The main connector of this block is connected to a sub connector of the given static block, if possible. + * @effect A sub connector of this block is connected to the main connector of the given static block, if possible. + * @effect The connection controller adds a logical system connection depending on the type of connection. + * @effect The program controller adds the highest block from the set this block is in to the logical program area. + * @effect The height of the given static block and its connected set of blocks is changed accordingly. + * @effect The position of the static block set is changed accordingly to the type of completed connection. + * + * @throws IllegalArgumentException When the given block does not have a colliding connector + */ + public void connectWithStaticBlock(GUIBlock other, ProgramController programController, ConnectionController connectionController) throws IllegalArgumentException{ GUIConnector intersectingConnectorSub; - ConnectionController controller = Pcontroller.getController(); Position staticBlockConnectorPosition, draggedBlockConnectorPosition; GUIBlock main, sub; if ((intersectingConnectorSub = findCollidingConnector(other.subConnectors, mainConnector)) != null) { main = this; sub = other; - Pcontroller.deleteAsProgram(this); + programController.deleteAsProgram(this); draggedBlockConnectorPosition = mainConnector.getCollisionCircle().getPosition(); staticBlockConnectorPosition = intersectingConnectorSub.getCollisionCircle().getPosition(); } else if ((intersectingConnectorSub = findCollidingConnector(subConnectors, other.mainConnector)) != null) { sub = this; main = other; - Pcontroller.deleteAsProgram(other); + programController.deleteAsProgram(other); staticBlockConnectorPosition = other.mainConnector.getCollisionCircle().getPosition(); draggedBlockConnectorPosition = intersectingConnectorSub.getCollisionCircle().getPosition(); } @@ -261,23 +280,22 @@ else if ((intersectingConnectorSub = findCollidingConnector(subConnectors, other throw new IllegalArgumentException("Given block does not have a colliding connector!"); } - if (controller.isValidConnection(main, sub, intersectingConnectorSub.getId())) { - - + if (connectionController.isValidConnection(main, sub, intersectingConnectorSub.getId())) { if (!mainConnector.isConnected()) { setPosition(staticBlockConnectorPosition.getX() + (getX() - draggedBlockConnectorPosition.getX()), staticBlockConnectorPosition.getY() + (getY() - draggedBlockConnectorPosition.getY())); main.mainConnector.connect(intersectingConnectorSub); - controller.connectBlocks(main, sub, intersectingConnectorSub.getId()); + connectionController.connectBlocks(main, sub, intersectingConnectorSub.getId()); changeHeight(main.getHeight(), main); } else { other.setPosition(draggedBlockConnectorPosition.getX() + (other.getX() - staticBlockConnectorPosition.getX()), draggedBlockConnectorPosition.getY() + (other.getY() - staticBlockConnectorPosition.getY())); main.mainConnector.connect(intersectingConnectorSub); - controller.connectBlocks(main, sub, intersectingConnectorSub.getId()); + connectionController.connectBlocks(main, sub, intersectingConnectorSub.getId()); other.changeHeight(main.getHeight(), main); } } - Pcontroller.addBlockToPA(getHighest()); + + programController.addBlockToPA(getHighest()); } /** @@ -295,12 +313,18 @@ public GUIBlock getHighest() { } } - // TODO doc + /** + * Reset the height of the block structure connected to this block's main connector + * as if the current block and its connected sub-blocks where added to the block structure. + */ public void resetHeight() { changeHeight(getHeight(), this); } - // TODO doc + /** + * Decrease the height of the block structure connected to this block's main connector + * as if the current block and its connected sub-blocks where removed from the block structure. + */ public void disconnectHeight() { changeHeight(-getHeight(), this); } diff --git a/src/main/java/GUI/Components/GUIBlockHandler.java b/src/main/java/GUI/Components/GUIBlockHandler.java index eef7751f..b76926f6 100644 --- a/src/main/java/GUI/Components/GUIBlockHandler.java +++ b/src/main/java/GUI/Components/GUIBlockHandler.java @@ -1,6 +1,5 @@ package GUI.Components; -import Controllers.ProgramController; import GUI.Blocks.GUIBlock; import GUI.CollisionShapes.CollisionRectangle; import GUI.Panel.GamePanel; @@ -13,6 +12,11 @@ import java.util.List; import java.util.Optional; +/** + * A class used to handle GUI blocks. + * + * @author Alpha-team + */ public class GUIBlockHandler { private final PalettePanel palette; @@ -27,19 +31,32 @@ public GUIBlockHandler(PalettePanel palette, ProgramAreaPanel programArea) { this.programArea = programArea; } - public void handleMouseEvent(int id, int x, int y, ProgramController programController) { + /** + * Handle the current mouse event with the given id, x and y coordinate. + * @param id the given mouse event id + * @param x the given mouse x-coordinate + * @param y the given mouse y-coordinate + * + * @effect The right mouse event is handled accordingly. + */ + public void handleMouseEvent(int id, int x, int y) { if (id == MouseEvent.MOUSE_PRESSED) { handleMousePressed(x, y); } else if (id == MouseEvent.MOUSE_RELEASED) { - handleMouseReleased(programController); + handleMouseReleased(); } else if (id == MouseEvent.MOUSE_DRAGGED) { handleMouseDragged(x, y); } } + /** + * todo + * @param x + * @param y + */ private void handleMousePressed(int x, int y) { boolean paletteBlockContainsMouse = AnyContains(palette.getBlocks(), x, y); @@ -64,15 +81,18 @@ private void handleMousePressed(int x, int y) { } } - private void handleMouseReleased(ProgramController programController) { + /** + * todo + */ + private void handleMouseReleased() { if (draggedBlock != null) { if (isInPanel(programArea.getPanelRectangle(), draggedBlocks)) { if (blockSourcePanel == palette) { - handleBlockFromPaletteToProgramArea(programController); + handleBlockFromPaletteToProgramArea(); } else if (blockSourcePanel == programArea) { - handleBlockFromProgramAreaToProgramArea(programController); + handleBlockFromProgramAreaToProgramArea(); } } else if (isInPanelAny(palette.getPanelRectangle(), draggedBlocks)) { @@ -89,13 +109,24 @@ else if (isInPanelAny(palette.getPanelRectangle(), draggedBlocks)) { } } + /** + * Handle the event where the mouse is dragged with the given x and y coordinates of the mouse. + * + * @param x the given x-coordinate of the mouse + * @param y the given y-coordinate of the mouse + * + * @effect The position of the dragged block is updated accordingly. + */ private void handleMouseDragged(int x, int y) { if (draggedBlock != null) { draggedBlock.setPosition(x + dragDelta.getX(), y + dragDelta.getY()); } } - private void handleBlockFromPaletteToProgramArea(ProgramController programController) { + /** + * todo + */ + private void handleBlockFromPaletteToProgramArea() { GUIBlock newBlock = palette.getNewBlock(draggedBlock.getId(), draggedBlock.getX(), draggedBlock.getY()); draggedBlock.setPosition(lastValidPosition.getX(), lastValidPosition.getY()); @@ -108,30 +139,40 @@ private void handleBlockFromPaletteToProgramArea(ProgramController programContro programArea.addBlockToProgramAreaControllerCall(draggedBlock); } else { - draggedBlock.connectWithStaticBlock(connectedBlock.get(), programController); + draggedBlock.connectWithStaticBlock(connectedBlock.get(), programArea.getProgramController(), programArea.getConnectionController()); } } - private void handleBlockFromProgramAreaToProgramArea(ProgramController programController) { + /** + * todo + */ + private void handleBlockFromProgramAreaToProgramArea() { + boolean connectionFound = false; programArea.disconnectInProgramArea(draggedBlock); draggedBlock.disconnectMainConnector(); - for (GUIBlock block : draggedBlocks) { Optional connectedBlock = programArea.getBlocks().stream().filter(b -> b.intersectsWithConnector(block)).findAny(); if (connectedBlock.isPresent()) { - block.connectWithStaticBlock(connectedBlock.get(), programController); + block.connectWithStaticBlock(connectedBlock.get(), programArea.getProgramController(), programArea.getConnectionController()); connectionFound = true; break; } } if (!connectionFound) { - programController.addBlockToPA(draggedBlocks.get(0)); + programArea.addBlockToProgramAreaControllerCall(draggedBlocks.get(0)); } } + /** + * Handle the event where the dragged gui bock goes from a certain position to the palette + * + * @effect Disconnect the dragged block from its previous set of blocks. + * @effect Reset the position of the original palette block if the dragged block came from the palette. + * @effect Delete the block from the program area if the dragged block came from the program area. + */ private void handleBlockToPalette() { programArea.disconnectInProgramArea(draggedBlock); @@ -145,14 +186,40 @@ else if (blockSourcePanel == programArea) { } } + /** + * Return whether any block in the given list of gui blocks contains the point defined + * by the given x and y coordinate. + * + * @param blocks the given list of gui blocks + * @param x the given x-coordinate + * @param y the given y-coordinate + * + * @return whether any block in the given list of gui blocks contains the point. + */ private boolean AnyContains(List blocks, int x, int y) { return blocks.stream().anyMatch(b -> b.contains(x, y)); } + /** + * Checks if a given gui block is in a given collision rectangle panel. + * + * @param panel the given panel + * @param blocks the given gui block + * + * @return if the given gui block is in the given panel. + */ private boolean isInPanel(CollisionRectangle panel, List blocks) { return blocks.stream().allMatch(b -> b.isInside(panel)); } + /** + * Checks if a given list of guiblocks is in a given collision rectangle panel. + * + * @param panel the given panel + * @param blocks the given list of gui blocks + * + * @return if the given list of gui blocks is in the given panel. + */ private boolean isInPanelAny(CollisionRectangle panel, List blocks) { return blocks.stream().anyMatch(b -> b.isInside(panel)); } diff --git a/src/main/java/GUI/Components/GUIConnector.java b/src/main/java/GUI/Components/GUIConnector.java index b488977c..b2348d9f 100644 --- a/src/main/java/GUI/Components/GUIConnector.java +++ b/src/main/java/GUI/Components/GUIConnector.java @@ -4,7 +4,11 @@ import GUI.Blocks.GUIBlock; import java.awt.*; - +/** + * A class used as a GUI connector to connect GUI Blocks visually. + * + * @author Alpha-team + */ public class GUIConnector { /** diff --git a/src/main/java/GUI/Panel/ProgramAreaPanel.java b/src/main/java/GUI/Panel/ProgramAreaPanel.java index 993574a1..77ae4ca8 100644 --- a/src/main/java/GUI/Panel/ProgramAreaPanel.java +++ b/src/main/java/GUI/Panel/ProgramAreaPanel.java @@ -1,5 +1,6 @@ package GUI.Panel; +import Controllers.ConnectionController; import Controllers.ProgramController; import Controllers.ProgramListener; import GUI.Blocks.GUIBlock; @@ -20,10 +21,8 @@ public class ProgramAreaPanel extends GamePanel implements ProgramListener { * Variable referring to the blocks in the program area panel. */ private List blocks = new ArrayList<>(); - /** - * Variable referring to the controller for the program. - */ - private ProgramController controller; + private ProgramController programController; + private ConnectionController connectionController; /** * Initialise a new program area panel with given corner, dimensions and controller. @@ -32,17 +31,29 @@ public class ProgramAreaPanel extends GamePanel implements ProgramListener { * @param cornerY The y coordinate for the corner of this panel. * @param width The width of this panel. * @param height The height of this panel. - * @param controller The controller to control this panel. + * @param programController The program controller used to handle program actions. + * @param connectionController The connection controller used to handle connections. * * @effect Calls super constructor with given coordinates and dimensions. * @effect Subscribes this panel as a listener to the panel. * - * @post The controller of this panel is set to the given controller. + * @post The program controller of this panel is set to the given program controller. + * @post The connection controller of this panel is set to the given connection controller. */ - public ProgramAreaPanel(int cornerX, int cornerY, int width, int height, ProgramController controller) { + public ProgramAreaPanel(int cornerX, int cornerY, int width, int height, ProgramController programController, + ConnectionController connectionController) { super(cornerX, cornerY, width, height); - this.controller = controller; - controller.subscribeListener(this); + this.programController = programController; + this.connectionController = connectionController; + programController.subscribeListener(this); + } + + public ProgramController getProgramController() { + return programController; + } + + public ConnectionController getConnectionController() { + return connectionController; } /** @@ -62,7 +73,7 @@ public void addBlockToProgramArea(GUIBlock block) { * @param block The block to add to the program area. */ public void addBlockToProgramAreaControllerCall(GUIBlock block) { - controller.addBlockToPA(block); + programController.addBlockToPA(block); } /** @@ -74,8 +85,8 @@ public void addBlockToProgramAreaControllerCall(GUIBlock block) { * @effect The program is reset. */ public void disconnectInProgramArea(GUIBlock GUIBlock) { - controller.getController().disconnectBlock(GUIBlock); - controller.resetProgram(); + connectionController.disconnectBlock(GUIBlock); + programController.resetProgram(); } /** @@ -88,7 +99,7 @@ public void disconnectInProgramArea(GUIBlock GUIBlock) { public void deleteBlockFromProgramArea(List GUIBlocks) { blocks.removeAll(GUIBlocks); for (GUIBlock block:GUIBlocks) { - controller.deleteFromPA(block); + programController.deleteFromPA(block); } } diff --git a/src/main/java/GUI/ProgramGUI.java b/src/main/java/GUI/ProgramGUI.java index 4e80d735..78ff6228 100644 --- a/src/main/java/GUI/ProgramGUI.java +++ b/src/main/java/GUI/ProgramGUI.java @@ -1,7 +1,11 @@ package GUI; +import Controllers.ConnectionController; +import Controllers.GUItoSystemInterface; +import Controllers.ProgramController; import System.GameWorld.Level.LevelLoader; +import System.Logic.ProgramArea.PABlockHandler; import java.awt.*; @@ -9,7 +13,6 @@ public class ProgramGUI { public static void main(String args[]) throws Exception { LevelLoader loader = new LevelLoader(); loader.loadLevel(); - Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); BlockrCanvas canvas = new BlockrCanvas("Blockr", screenSize.width, screenSize.height); java.awt.EventQueue.invokeLater(canvas::show); diff --git a/src/main/java/System/BlockStructure/Blocks/CavityBlock.java b/src/main/java/System/BlockStructure/Blocks/CavityBlock.java index a65dab13..e5e8baf2 100644 --- a/src/main/java/System/BlockStructure/Blocks/CavityBlock.java +++ b/src/main/java/System/BlockStructure/Blocks/CavityBlock.java @@ -91,28 +91,6 @@ public boolean hasNext() { } } - /** - * Get the next block to execute, this depends on the evaluation of the condition of this block. - * If the condition is true, the next block is the first in the cavity, otherwise, the next block - * is the first block under the while. - * - * @return The next block to execute - */ - @Override - public Block getNext() { - if (getFunctionality().getEvaluation()) { - setAlreadyRan(false); - return cavitySubConnector.getConnectedBlock(); - } - else { - setAlreadyRan(true); - if (cavitySubConnector.isConnected()) { - cavitySubConnector.getConnectedBlock().reset(); - } - return getSubConnectors().get(0).getConnectedBlock(); - } - } - /** * Checks whether or not this cavity block has proper connections. * diff --git a/src/main/java/System/BlockStructure/Blocks/IfBlock.java b/src/main/java/System/BlockStructure/Blocks/IfBlock.java index 8384691d..221cedeb 100644 --- a/src/main/java/System/BlockStructure/Blocks/IfBlock.java +++ b/src/main/java/System/BlockStructure/Blocks/IfBlock.java @@ -34,6 +34,26 @@ else if (getCavitySubConnector().isConnected()) { return super.getNext(); } + /** + * Get the next block to execute, this depends on the evaluation of the condition of this block. + * If the condition is true, the next block is the first in the cavity, otherwise, the next block + * is the first block under the while. + * + * @return The next block to execute + */ + @Override + public Block getNext() { + if (getFunctionality().getEvaluation()) { + setAlreadyRan(true); + return getCavitySubConnector().getConnectedBlock(); + } + else if (getCavitySubConnector().isConnected()) { + getCavitySubConnector().getConnectedBlock().reset(); + } + setAlreadyRan(true); + return super.getNext(); + } + /** * Returns the next block if any other block didn't have a next block to run. * diff --git a/src/main/java/System/BlockStructure/Blocks/WhileBlock.java b/src/main/java/System/BlockStructure/Blocks/WhileBlock.java index 41fc35c5..1dbc2026 100644 --- a/src/main/java/System/BlockStructure/Blocks/WhileBlock.java +++ b/src/main/java/System/BlockStructure/Blocks/WhileBlock.java @@ -21,6 +21,26 @@ public WhileBlock(CavityFunctionality functionality) { super(functionality); } + /** + * Get the next block to execute, this depends on the evaluation of the condition of this block. + * If the condition is true, the next block is the first in the cavity, otherwise, the next block + * is the first block under the while. + * + * @return The next block to execute + */ + @Override + public Block getNext() { + if (getFunctionality().getEvaluation()) { + setAlreadyRan(false); + return getCavitySubConnector().getConnectedBlock(); + } + else if(getCavitySubConnector().isConnected()) { + getCavitySubConnector().getConnectedBlock().reset(); + } + setAlreadyRan(true); + return super.getNext(); + } + /** * Returns the next block if any other block didn't have a next block to run. *