Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / entites / gameComponents / Connection.java
NoranekoFelician 9 hours ago 1 KB ・Runnable→voidへ変更
package gameEngine.entites.gameComponents;

import gameEngine.views.Sprite;
import org.joml.Vector3f;

public class Connection {
    private final PortView portA;
    private final PortView portB;
    private final Sprite lineSprite;

    public Connection(PortView inPort, PortView outPort) {
        this.portA = inPort;
        this.portB = outPort;
        this.lineSprite = new Sprite("GameEngine/resources/Line.png");
    }

    public void render() {
        Vector3f posA = portA.getPosition();
        Vector3f posB = portB.getPosition();

        Vector3f linePosition = calculatePosition(posA, posB);
        float lineLength = calculateDistance(posA, posB);
        float angle = calculateAngle(posA, posB);

        linePosition.x -= (float) (lineLength / 2 * Math.cos(Math.toRadians(angle)));
        linePosition.y -= (float) (lineLength / 2 * Math.sin(Math.toRadians(angle)));

        lineSprite.setPosition(linePosition.x+8, linePosition.y+8);
        lineSprite.setRotation(new Vector3f(0, 0, angle)); // z軸の回転を設定
        lineSprite.setScale(new Vector3f(lineLength, 3, 1)); // 長さをスケールで調整

        lineSprite.update();
    }

    private Vector3f calculatePosition(Vector3f posA, Vector3f posB) {
        return new Vector3f((posA.x + posB.x) / 2, (posA.y + posB.y) / 2, 0);
    }

    private float calculateDistance(Vector3f posA, Vector3f posB) {
        return (float) Math.sqrt(Math.pow(posB.x - posA.x, 2) + Math.pow(posB.y - posA.y, 2));
    }

    private float calculateAngle(Vector3f posA, Vector3f posB) {
        return (float) Math.toDegrees(Math.atan2(posB.y - posA.y, posB.x - posA.x));
    }

    public boolean isClicked(float mouseX, float mouseY) {
        return lineSprite.isMouseOver(mouseX, mouseY);
    }

    public PortView getPortA() {
        return portA;
    }

    public PortView getPortB() {
        return portB;
    }
}