Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / ConnectionManager.java
NoranekoFelician 3 hours ago 3 KB ・Runnable→voidへ変更
package gameEngine;

import java.util.ArrayList;
import java.util.List;

import gameEngine.entites.GameObject;
import gameEngine.entites.gameComponents.*;
import gameEngine.input.Input;
import gameEngine.input.MouseInput;

public class ConnectionManager {
    private final List<Connection> connections = new ArrayList<>();
    private PortView pressedPort;

    public void handlePortPress(PortView port) {
        pressedPort = port;
    }

    public void handlePortRelease(PortView port) {

        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;
    }

    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 (toRemove != null) {
            connections.remove(toRemove);
            portA.removeConnectedPort(portB);
            portB.removeConnectedPort(portA);

            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);
            }
        }
    }
}