diff --git a/GameEngine/src/main/java/gameEngine/ConnectionManager.java b/GameEngine/src/main/java/gameEngine/ConnectionManager.java index 9d5874d..18afb37 100644 --- a/GameEngine/src/main/java/gameEngine/ConnectionManager.java +++ b/GameEngine/src/main/java/gameEngine/ConnectionManager.java @@ -9,7 +9,7 @@ import gameEngine.input.MouseInput; public class ConnectionManager { - private List connections = new ArrayList<>(); + private final List connections = new ArrayList<>(); private PortView pressedPort; public void handlePortPress(PortView port) { @@ -17,17 +17,24 @@ } public void handlePortRelease(PortView port) { - if (pressedPort != null && pressedPort != port && pressedPort.getPortType() != port.getPortType()) { - System.out.println("Connect!"); - Connection newConnection = new Connection(pressedPort, port); - connections.add(newConnection); - GameObject entityA = (GameObject) pressedPort.getParent(); - GameObject entityB = (GameObject) port.getParent(); - if(entityA.getComponent(EntityView.class) != null){ - entityA.ComponentConnections.add(entityB.getComponent(ComponentView.class).connectionType); - }else { - entityB.ComponentConnections.add(entityA.getComponent(ComponentView.class).connectionType); + if (pressedPort != null && pressedPort != port && pressedPort.getPortType() != port.getPortType()) { + if (pressedPort.isConnectedTo(port)) { + System.out.println("already connected"); + } else { + System.out.println("Connect!"); + Connection newConnection = new Connection(pressedPort, port); + connections.add(newConnection); + pressedPort.addConnectedPort(port); + port.addConnectedPort(pressedPort); + + GameObject portParentA = pressedPort.getParent(); + GameObject portParentB = port.getParent(); + if (portParentA.getComponent(EntityView.class) != null) { + portParentA.ComponentConnections.add(portParentB.getComponent(ComponentView.class).connectionType); + } else { + portParentB.ComponentConnections.add(portParentA.getComponent(ComponentView.class).connectionType); + } } } pressedPort = null; @@ -36,15 +43,42 @@ public void update() { for (Connection connection : connections) { connection.render(); + + if (Input.GetMouseButtonDown(0)) { + float mouseX = MouseInput.getX(); + float mouseY = MouseInput.getY(); + + if (connection.isClicked(mouseX, mouseY)) { + System.out.println("Connection removed"); + removeConnection(connection.getPortA(), connection.getPortB()); + break; + } + } + } + } + + public void removeConnection(PortView portA, PortView portB) { + Connection toRemove = null; + for (Connection connection : connections) { + if ((connection.getPortA() == portA && connection.getPortB() == portB) || + (connection.getPortA() == portB && connection.getPortB() == portA)) { + toRemove = connection; + break; + } } - if (Input.GetMouseButtonDown(0)) { - float mouseX = MouseInput.getX(); - float mouseY = MouseInput.getY(); + if (toRemove != null) { + connections.remove(toRemove); + portA.removeConnectedPort(portB); + portB.removeConnectedPort(portA); - for (Connection connection : connections) { - if(connection.isClicked(mouseX, mouseY)) System.out.println("Clicked"); - else System.out.println("o"); + GameObject portParentA = portA.getParent(); + GameObject portParentB = portB.getParent(); + + if (portParentA.getComponent(EntityView.class) != null) { + portParentA.ComponentConnections.remove(portParentB.getComponent(ComponentView.class).connectionType); + } else { + portParentB.ComponentConnections.remove(portParentA.getComponent(ComponentView.class).connectionType); } } }